zhaoyingbo f387806924
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 22s
CI Monitor CI/CD / deploy (push) Successful in 24s
feat: 接口改为返回JSON
2024-03-08 19:15:14 +08:00

120 lines
2.9 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()
);
return {
total_count: String(curWeekInfo.total_count),
weekly_count_rate: calculateWeeklyRate(
curWeekInfo.total_count,
prevWeekInfo.total_count
).text,
duration: String(curWeekInfo.duration),
weekly_duration_rate: calculateWeeklyRate(
curWeekInfo.duration,
prevWeekInfo.duration
).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);
};
const getRobotMsg = async () => JSON.stringify({
type: "template",
data: {
template_id: "ctp_AAyVLS6Q37cL",
template_variable: {
...(await getNewCicdStatus()),
...(await getStatisticsInfo()),
group_table: await getProjDiffInfo(),
},
},
})
const sendRobotMsg = async () => await service.sendMessage(await getRobotMsg())
const manageRobot = {
getRobotMsg,
sendRobotMsg,
};
export default manageRobot;