mirror of
https://github.com/whyour/qinglong.git
synced 2026-08-05 08:14:32 +08:00
Add statistics panel: backend models, services, APIs and frontend page
Agent-Logs-Url: https://github.com/whyour/qinglong/sessions/3db54913-03d2-4721-b720-8ccbf8d0f00e Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
0995808309
commit
34bc18cb25
@@ -3,6 +3,7 @@ import { Container } from 'typedi';
|
||||
import { Logger } from 'winston';
|
||||
import CronService from '../services/cron';
|
||||
import CronViewService from '../services/cronView';
|
||||
import CronStatsService from '../services/cronStats';
|
||||
import { celebrate, Joi } from 'celebrate';
|
||||
import { commonCronSchema } from '../validation/schedule';
|
||||
|
||||
@@ -141,6 +142,58 @@ export default (app: Router) => {
|
||||
},
|
||||
);
|
||||
|
||||
route.get(
|
||||
'/stats',
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const cronStatsService = Container.get(CronStatsService);
|
||||
const data = await cronStatsService.stats();
|
||||
return res.send({ code: 200, data });
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
route.get(
|
||||
'/stats/trend',
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const cronStatsService = Container.get(CronStatsService);
|
||||
const data = await cronStatsService.trend();
|
||||
return res.send({ code: 200, data });
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
route.get(
|
||||
'/stats/top-duration',
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const cronStatsService = Container.get(CronStatsService);
|
||||
const data = await cronStatsService.topDuration();
|
||||
return res.send({ code: 200, data });
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
route.get(
|
||||
'/stats/top-count',
|
||||
async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const cronStatsService = Container.get(CronStatsService);
|
||||
const data = await cronStatsService.topCount();
|
||||
return res.send({ code: 200, data });
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
route.get('/', async (req: Request, res: Response, next: NextFunction) => {
|
||||
const logger: Logger = Container.get('logger');
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { sequelize } from '.';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
|
||||
export class CronLog {
|
||||
id?: number;
|
||||
cron_id: number;
|
||||
cron_name: string;
|
||||
start_time: number;
|
||||
duration: number;
|
||||
|
||||
constructor(options: CronLog) {
|
||||
this.cron_id = options.cron_id;
|
||||
this.cron_name = options.cron_name;
|
||||
this.start_time = options.start_time;
|
||||
this.duration = options.duration;
|
||||
}
|
||||
}
|
||||
|
||||
export interface CronLogInstance extends Model<CronLog, CronLog>, CronLog {}
|
||||
export const CronLogModel = sequelize.define<CronLogInstance>(
|
||||
'CronLog',
|
||||
{
|
||||
cron_id: DataTypes.NUMBER,
|
||||
cron_name: DataTypes.STRING,
|
||||
start_time: DataTypes.NUMBER,
|
||||
duration: DataTypes.NUMBER,
|
||||
},
|
||||
{
|
||||
indexes: [{ fields: ['cron_id'] }, { fields: ['start_time'] }],
|
||||
},
|
||||
);
|
||||
@@ -6,6 +6,7 @@ import { AppModel } from '../data/open';
|
||||
import { SystemModel } from '../data/system';
|
||||
import { SubscriptionModel } from '../data/subscription';
|
||||
import { CrontabViewModel } from '../data/cronView';
|
||||
import { CronLogModel } from '../data/cronLog';
|
||||
import { sequelize } from '../data';
|
||||
|
||||
export default async () => {
|
||||
@@ -17,6 +18,7 @@ export default async () => {
|
||||
await EnvModel.sync();
|
||||
await SubscriptionModel.sync();
|
||||
await CrontabViewModel.sync();
|
||||
await CronLogModel.sync();
|
||||
|
||||
// 初始化新增字段
|
||||
const migrations = [
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Service, Inject } from 'typedi';
|
||||
import winston from 'winston';
|
||||
import config from '../config';
|
||||
import { Crontab, CrontabModel, CrontabStatus } from '../data/cron';
|
||||
import { CronLog, CronLogModel } from '../data/cronLog';
|
||||
import { exec, execSync } from 'child_process';
|
||||
import fs from 'fs/promises';
|
||||
import CronExpressionParser from 'cron-parser';
|
||||
@@ -176,6 +177,17 @@ export default class CronService {
|
||||
{ ...pickBy(options, (v) => v === 0 || !!v) },
|
||||
{ where: { id } },
|
||||
);
|
||||
|
||||
if (status === CrontabStatus.idle && last_running_time > 0) {
|
||||
await CronLogModel.create(
|
||||
new CronLog({
|
||||
cron_id: id,
|
||||
cron_name: cron.name || cron.command || '',
|
||||
start_time: last_execution_time,
|
||||
duration: last_running_time,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import winston from 'winston';
|
||||
import { CrontabModel, CrontabStatus } from '../data/cron';
|
||||
import { CronLogModel } from '../data/cronLog';
|
||||
import { Op } from 'sequelize';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
@Service()
|
||||
export default class CronStatsService {
|
||||
constructor(@Inject('logger') private logger: winston.Logger) {}
|
||||
|
||||
public async stats() {
|
||||
const todayStart = dayjs().startOf('day').unix();
|
||||
const todayEnd = dayjs().endOf('day').unix();
|
||||
|
||||
const [allCrons, todayLogs] = await Promise.all([
|
||||
CrontabModel.findAll({ where: {} }),
|
||||
CronLogModel.findAll({
|
||||
where: {
|
||||
start_time: { [Op.between]: [todayStart, todayEnd] },
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
const total = allCrons.length;
|
||||
const enabled = allCrons.filter((c) => c.isDisabled !== 1).length;
|
||||
const disabled = allCrons.filter((c) => c.isDisabled === 1).length;
|
||||
|
||||
const todayCount = todayLogs.length;
|
||||
const todayTotalDuration = todayLogs.reduce(
|
||||
(sum, l) => sum + (l.duration || 0),
|
||||
0,
|
||||
);
|
||||
const todayAvgDuration =
|
||||
todayCount > 0 ? Math.round(todayTotalDuration / todayCount) : 0;
|
||||
|
||||
return {
|
||||
total,
|
||||
enabled,
|
||||
disabled,
|
||||
today: {
|
||||
count: todayCount,
|
||||
avgDuration: todayAvgDuration,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public async trend() {
|
||||
const days = 7;
|
||||
const result: Array<{
|
||||
date: string;
|
||||
count: number;
|
||||
}> = [];
|
||||
|
||||
for (let i = days - 1; i >= 0; i--) {
|
||||
const dayStart = dayjs().subtract(i, 'day').startOf('day').unix();
|
||||
const dayEnd = dayjs().subtract(i, 'day').endOf('day').unix();
|
||||
const date = dayjs().subtract(i, 'day').format('MM-DD');
|
||||
|
||||
const logs = await CronLogModel.findAll({
|
||||
where: {
|
||||
start_time: { [Op.between]: [dayStart, dayEnd] },
|
||||
},
|
||||
});
|
||||
|
||||
result.push({
|
||||
date,
|
||||
count: logs.length,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async topDuration(limit = 5) {
|
||||
const todayStart = dayjs().startOf('day').unix();
|
||||
const todayEnd = dayjs().endOf('day').unix();
|
||||
|
||||
const logs = await CronLogModel.findAll({
|
||||
where: {
|
||||
start_time: { [Op.between]: [todayStart, todayEnd] },
|
||||
},
|
||||
});
|
||||
|
||||
const grouped: Record<
|
||||
number,
|
||||
{ cron_id: number; cron_name: string; durations: number[] }
|
||||
> = {};
|
||||
|
||||
for (const log of logs) {
|
||||
if (!grouped[log.cron_id]) {
|
||||
grouped[log.cron_id] = {
|
||||
cron_id: log.cron_id,
|
||||
cron_name: log.cron_name,
|
||||
durations: [],
|
||||
};
|
||||
}
|
||||
grouped[log.cron_id].durations.push(log.duration);
|
||||
}
|
||||
|
||||
const result = Object.values(grouped)
|
||||
.map((g) => {
|
||||
const avgDuration = Math.round(
|
||||
g.durations.reduce((a, b) => a + b, 0) / g.durations.length,
|
||||
);
|
||||
const maxDuration = Math.max(...g.durations);
|
||||
return {
|
||||
cron_id: g.cron_id,
|
||||
cron_name: g.cron_name,
|
||||
count: g.durations.length,
|
||||
avgDuration,
|
||||
maxDuration,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => b.avgDuration - a.avgDuration)
|
||||
.slice(0, limit);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async topCount(limit = 5) {
|
||||
const todayStart = dayjs().startOf('day').unix();
|
||||
const todayEnd = dayjs().endOf('day').unix();
|
||||
|
||||
const logs = await CronLogModel.findAll({
|
||||
where: {
|
||||
start_time: { [Op.between]: [todayStart, todayEnd] },
|
||||
},
|
||||
});
|
||||
|
||||
const grouped: Record<
|
||||
number,
|
||||
{ cron_id: number; cron_name: string; durations: number[] }
|
||||
> = {};
|
||||
|
||||
for (const log of logs) {
|
||||
if (!grouped[log.cron_id]) {
|
||||
grouped[log.cron_id] = {
|
||||
cron_id: log.cron_id,
|
||||
cron_name: log.cron_name,
|
||||
durations: [],
|
||||
};
|
||||
}
|
||||
grouped[log.cron_id].durations.push(log.duration);
|
||||
}
|
||||
|
||||
const result = Object.values(grouped)
|
||||
.map((g) => {
|
||||
const avgDuration = Math.round(
|
||||
g.durations.reduce((a, b) => a + b, 0) / g.durations.length,
|
||||
);
|
||||
return {
|
||||
cron_id: g.cron_id,
|
||||
cron_name: g.cron_name,
|
||||
count: g.durations.length,
|
||||
avgDuration,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => b.count - a.count)
|
||||
.slice(0, limit);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user