Compare commits
No commits in common. "f284d70d6b9a9d5bf6518c20d6edb162c1db1834" and "40313185a5ee3935d12fdf924505bd00d30b2e6e" have entirely different histories.
f284d70d6b
...
40313185a5
2 changed files with 4 additions and 97 deletions
|
|
@ -86,21 +86,4 @@ export class DashboardsService {
|
||||||
if (!data.docs) throw createAppError('DASHBOARDS_NOT_FOUND');
|
if (!data.docs) throw createAppError('DASHBOARDS_NOT_FOUND');
|
||||||
return data.docs.map((d) => ({ id: d.id, title: d.data.title }));
|
return data.docs.map((d) => ({ id: d.id, title: d.data.title }));
|
||||||
}
|
}
|
||||||
|
|
||||||
async findDashboardsByWidgetType(
|
|
||||||
widgetType: string,
|
|
||||||
): Promise<DashboardModel.Dashboard[]> {
|
|
||||||
const ds = await this.db.getDatasource();
|
|
||||||
const data = await ds.find({
|
|
||||||
selector: {
|
|
||||||
'data.widgets': {
|
|
||||||
$elemMatch: {
|
|
||||||
type: widgetType,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (!data.docs) throw createAppError('DASHBOARDS_NOT_FOUND');
|
|
||||||
return data.docs;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,6 @@
|
||||||
import { DashboardsService } from '@app/event-emitter/dashboards/dashboards.service';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { Dashboard, Widget } from '@app/event-emitter/models/dashboard';
|
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
|
||||||
import { Cron, SchedulerRegistry } from '@nestjs/schedule';
|
|
||||||
import { CronJob } from 'cron';
|
|
||||||
|
|
||||||
export type Params = {
|
export type Params = any; // TODO
|
||||||
query: any; // TODO: add type
|
|
||||||
schedule: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type Report = {
|
export type Report = {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
@ -18,78 +11,9 @@ export type Report = {
|
||||||
data: any;
|
data: any;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Job = {
|
|
||||||
id: string;
|
|
||||||
params: Params;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const WIDGET_TYPE = 'daily-eccm-v2';
|
|
||||||
|
|
||||||
export const JOB_PREFIX = 'daily_eccm_v2';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DailyEccmV2ReportService {
|
export class DailyEccmV2ReportService {
|
||||||
private logger = new Logger(DailyEccmV2ReportService.name);
|
constructor(params: Params) {
|
||||||
|
|
||||||
private dashboardsService: DashboardsService;
|
|
||||||
|
|
||||||
constructor(private schedulerRegistry: SchedulerRegistry) {}
|
|
||||||
|
|
||||||
@Cron('* * * * *')
|
|
||||||
async autoScanJobs() {
|
|
||||||
this.logger.debug('Auto scan jobs started');
|
|
||||||
const dbs = this.getDashboardsService();
|
|
||||||
if (!dbs) {
|
|
||||||
this.logger.warn('Dashboards service not initialized');
|
|
||||||
this.logger.debug('Auto scan jobs finished');
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const dashboards = await dbs.findDashboardsByWidgetType(WIDGET_TYPE);
|
|
||||||
for (let i = 0; i < dashboards.length; i++) {
|
|
||||||
const dashboard: Dashboard = dashboards[i];
|
|
||||||
for (let j = 0; j < dashboard.data.widgets.length; j++) {
|
|
||||||
const widget = dashboard.data.widgets[j];
|
|
||||||
if (widget.type === WIDGET_TYPE) {
|
|
||||||
const jobId = `${JOB_PREFIX}_${dashboard.id}_${widget.id}`;
|
|
||||||
let cronJob = this.schedulerRegistry.getCronJob(jobId);
|
|
||||||
if (!cronJob) {
|
|
||||||
cronJob = new CronJob(
|
|
||||||
widget.dataLoaderParams.schedule,
|
|
||||||
async () => {
|
|
||||||
this.logger.debug(`Cron job ${jobId} started`);
|
|
||||||
// const report = await dbs.getReport(dashboard.id, widget.id);
|
|
||||||
this.logger.debug(`Cron job ${jobId} finished`);
|
|
||||||
// return report;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
this.schedulerRegistry.addCronJob(jobId, cronJob);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.logger.debug('Auto scan jobs finished');
|
|
||||||
}
|
|
||||||
|
|
||||||
getDashboardsService(): DashboardsService | null {
|
|
||||||
if (!this.dashboardsService) {
|
|
||||||
this.logger.warn('Dashboards service not initialized');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return this.dashboardsService;
|
|
||||||
}
|
|
||||||
|
|
||||||
setDashboardsService(dashboardsService: DashboardsService) {
|
|
||||||
this.dashboardsService = dashboardsService;
|
|
||||||
}
|
|
||||||
|
|
||||||
private createJobHandler(
|
|
||||||
jobId: string,
|
|
||||||
dashboard: Dashboard,
|
|
||||||
widget: Widget,
|
|
||||||
): () => void {
|
|
||||||
return async () => {
|
|
||||||
this.logger.debug(`Cron job ${jobId} started`);
|
|
||||||
this.logger.debug(`Cron job ${jobId} finished`);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue