gitlab_monitor/utils/robotTools.ts
zhaoyingbo 18a95387ee
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 33s
CI Monitor CI/CD / deploy (push) Successful in 37s
chore: 更新lint-staged和commitlint配置
2024-07-25 01:09:24 +00:00

62 lines
1.1 KiB
TypeScript

/**
* 计算百分比变化
* @param a
* @param b
*/
export const calculatePercentageChange = (cur: number, prev: number) => {
// 计算差值
const diff = cur - prev
if (diff === 0)
return {
diff,
percentage: "0",
}
// 计算百分比
const percentage = Math.abs((diff / prev) * 100).toFixed(1)
return {
diff,
percentage,
}
}
/**
* 计算周同比
* @param cur
* @param prev
* @param needCN
*/
export const calculateWeeklyRate = (
cur: string | number,
prev: string | number,
needCN = true
) => {
const { diff, percentage } = calculatePercentageChange(
Number(cur),
Number(prev)
)
if (diff > 0)
return {
text: `<font color='red'>${
needCN ? "较上周 " : ""
}${percentage}%</font>`,
diff,
percentage,
}
if (diff < 0)
return {
text: `<font color='green'>${
needCN ? "较上周 " : ""
}${percentage}%</font>`,
diff,
percentage,
}
return {
text: `<font color='gray'>${needCN ? "较上周 " : ""}0%</font>`,
diff,
percentage,
}
}