zhaoyingbo f66efef4ba
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 21s
CI Monitor CI/CD / deploy (push) Successful in 25s
feat: 消息改为所有人可见
2024-03-09 09:04:33 +00:00

124 lines
3.0 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: {
config: {
update_multi: true,
},
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;