143 lines
3.5 KiB
TypeScript
143 lines
3.5 KiB
TypeScript
import db from "../../db";
|
|
import service from "../../service";
|
|
import { calculateWeeklyRate } from "../../utils/robotTools";
|
|
import {
|
|
getPrevWeekWithYear,
|
|
getWeekTimeWithYear,
|
|
} from "../../utils/timeTools";
|
|
|
|
const getNewCicdStatus = async () => {
|
|
const fullProjList = await db.project.getFullList();
|
|
const has_new_cicd_count = String(
|
|
fullProjList.filter((item) => {
|
|
return item.has_new_cicd === true;
|
|
}).length
|
|
);
|
|
const without_new_cicd_count = String(
|
|
fullProjList.filter((item) => {
|
|
return item.has_new_cicd === false;
|
|
}).length
|
|
);
|
|
return {
|
|
has_new_cicd_count,
|
|
without_new_cicd_count,
|
|
};
|
|
};
|
|
|
|
const getStatisticsInfo = async () => {
|
|
const curWeekInfo = await db.view.getFullStatisticsByWeek(
|
|
getWeekTimeWithYear()
|
|
);
|
|
const prevWeekInfo = await db.view.getFullStatisticsByWeek(
|
|
getPrevWeekWithYear()
|
|
);
|
|
if (!curWeekInfo || !prevWeekInfo) return {};
|
|
return {
|
|
total_count: String(curWeekInfo?.total_count ?? 0),
|
|
weekly_count_rate: calculateWeeklyRate(
|
|
curWeekInfo?.total_count ?? 0,
|
|
prevWeekInfo?.total_count ?? 0
|
|
).text,
|
|
duration: String(curWeekInfo?.duration ?? 0),
|
|
weekly_duration_rate: calculateWeeklyRate(
|
|
curWeekInfo?.duration ?? 0,
|
|
prevWeekInfo?.duration ?? 0
|
|
).text,
|
|
success_rate: String(curWeekInfo?.success_rate),
|
|
weekly_success_rate: calculateWeeklyRate(
|
|
curWeekInfo?.success_rate,
|
|
prevWeekInfo?.success_rate
|
|
).text,
|
|
};
|
|
};
|
|
|
|
const getProjDiffInfo = async () => {
|
|
const curWeekInfo =
|
|
(await db.view.getProjStatisticsByWeek(getWeekTimeWithYear())) || [];
|
|
const prevWeekInfo =
|
|
(await db.view.getProjStatisticsByWeek(getPrevWeekWithYear())) || [];
|
|
|
|
const group: {
|
|
project_name: string;
|
|
project_ref: string;
|
|
project_duration: string;
|
|
project_duration_rate: string;
|
|
percentage: string;
|
|
}[] = [];
|
|
|
|
curWeekInfo.forEach((curWeekProjInfo) => {
|
|
const prevWeekProjInfo = prevWeekInfo.find(
|
|
(info) =>
|
|
info.ref === curWeekProjInfo.ref && info.name === curWeekProjInfo.name
|
|
);
|
|
if (!prevWeekProjInfo) return;
|
|
|
|
const { text: project_duration_rate, percentage } = calculateWeeklyRate(
|
|
curWeekProjInfo.duration,
|
|
prevWeekProjInfo.duration,
|
|
false
|
|
);
|
|
|
|
if (percentage === "0") return;
|
|
|
|
group.push({
|
|
project_name: curWeekProjInfo.name,
|
|
project_ref: curWeekProjInfo.ref,
|
|
project_duration: String(curWeekProjInfo.duration),
|
|
project_duration_rate,
|
|
percentage,
|
|
});
|
|
});
|
|
|
|
// 排序
|
|
group.sort((a, b) => Number(b.percentage) - Number(a.percentage));
|
|
|
|
// 取前五个
|
|
return group.slice(0, 5);
|
|
};
|
|
|
|
/**
|
|
* 获取机器人消息
|
|
* @returns
|
|
*/
|
|
const getRobotMsg = async () =>
|
|
JSON.stringify({
|
|
type: "template",
|
|
data: {
|
|
config: {
|
|
update_multi: true,
|
|
},
|
|
template_id: "ctp_AAyVLS6Q37cL",
|
|
template_variable: {
|
|
...(await getNewCicdStatus()),
|
|
...(await getStatisticsInfo()),
|
|
group_table: await getProjDiffInfo(),
|
|
},
|
|
},
|
|
});
|
|
|
|
/**
|
|
* 通过ChatID发送CI报告
|
|
* @param chat_id
|
|
*/
|
|
const sendCIReportByChatId = async (chat_id: string) => {
|
|
await service.egg.sendMessage.byChatId(chat_id, await getRobotMsg());
|
|
};
|
|
|
|
/**
|
|
* 通过定时任务发送CI报告
|
|
* @returns
|
|
*/
|
|
const sendCIReportByCron = async () =>
|
|
await service.egg.sendMessage.byGroupId(
|
|
"52usf3w8l6z4vs1",
|
|
await getRobotMsg()
|
|
);
|
|
|
|
const manageRobot = {
|
|
sendCIReportByChatId,
|
|
sendCIReportByCron,
|
|
};
|
|
|
|
export default manageRobot;
|