Генерация отчёта
* Добавлен метод для сохранения отчёта saveReport * Доработан метод createJobHandler - загрузка данных и сохранение с помощью saveReport
This commit is contained in:
parent
a2131e9b03
commit
01c54b2d65
1 changed files with 91 additions and 6 deletions
|
|
@ -1,8 +1,13 @@
|
||||||
import { DashboardsService } from '@app/event-emitter/dashboards/dashboards.service';
|
import { DashboardsService } from '@app/event-emitter/dashboards/dashboards.service';
|
||||||
|
import { IssuesService } from '@app/event-emitter/issues/issues.service';
|
||||||
import { Dashboard, Widget } from '@app/event-emitter/models/dashboard';
|
import { Dashboard, Widget } from '@app/event-emitter/models/dashboard';
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { SchedulerRegistry } from '@nestjs/schedule';
|
import { SchedulerRegistry } from '@nestjs/schedule';
|
||||||
import { CronJob } from 'cron';
|
import { CronJob } from 'cron';
|
||||||
|
import { DailyEccmReportsV2Datasource } from 'src/couchdb-datasources/daily-eccm-reports-v2.datasource';
|
||||||
|
import { randomUUID } from 'crypto';
|
||||||
|
import { RedmineTypes } from '@app/event-emitter/models/redmine-types';
|
||||||
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
export type Params = {
|
export type Params = {
|
||||||
query: any; // TODO: add type
|
query: any; // TODO: add type
|
||||||
|
|
@ -39,7 +44,10 @@ export class DailyEccmV2ReportService {
|
||||||
|
|
||||||
private cronJobs: Record<string, CronJob> = {};
|
private cronJobs: Record<string, CronJob> = {};
|
||||||
|
|
||||||
constructor(private schedulerRegistry: SchedulerRegistry) {}
|
constructor(
|
||||||
|
private schedulerRegistry: SchedulerRegistry,
|
||||||
|
private issuesService: IssuesService,
|
||||||
|
) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Auto scan jobs every UPDATE_RATE seconds.
|
* Auto scan jobs every UPDATE_RATE seconds.
|
||||||
|
|
@ -92,7 +100,7 @@ export class DailyEccmV2ReportService {
|
||||||
this.schedulerRegistry.deleteCronJob(jobId);
|
this.schedulerRegistry.deleteCronJob(jobId);
|
||||||
}
|
}
|
||||||
const job = new CronJob(
|
const job = new CronJob(
|
||||||
widget.dataLoaderParams?.schedule || '* * * * *',
|
widget.dataLoaderParams?.schedule || '0 0 * * *',
|
||||||
this.createJobHandler(jobId, dashboard, widget),
|
this.createJobHandler(jobId, dashboard, widget),
|
||||||
);
|
);
|
||||||
this.cronJobs[jobId] = job;
|
this.cronJobs[jobId] = job;
|
||||||
|
|
@ -116,14 +124,91 @@ export class DailyEccmV2ReportService {
|
||||||
jobId: string,
|
jobId: string,
|
||||||
dashboard: Dashboard,
|
dashboard: Dashboard,
|
||||||
widget: Widget,
|
widget: Widget,
|
||||||
): () => void {
|
): () => Promise<void> {
|
||||||
return async () => {
|
this.logger.debug(
|
||||||
|
`Create job handler for cron job ${jobId}, ` +
|
||||||
|
`dashboard ${dashboard.id}, ` +
|
||||||
|
`widget ${widget.id}`,
|
||||||
|
);
|
||||||
|
return async (): Promise<void> => {
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
`Cron job ${jobId} started - dashboard ${dashboard.id}, widget ${widget.id}`,
|
`Cron job ${jobId} started - dashboard ${dashboard.id}, widget ${widget.id}`,
|
||||||
);
|
);
|
||||||
this.logger.debug(
|
|
||||||
`Cron job ${jobId} finished - dashboard ${dashboard.id}, widget ${widget.id}`,
|
const query = widget.dataLoaderParams?.query;
|
||||||
|
if (!query) {
|
||||||
|
this.logger.log(
|
||||||
|
`Cron job ${jobId} finished - dashboard ${dashboard.id}, widget ${widget.id}`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let issues: RedmineTypes.Issue[] = [];
|
||||||
|
try {
|
||||||
|
issues = await this.issuesService.mergedTreesAndFind(query);
|
||||||
|
await this.saveReport(dashboard, widget, issues);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.log(
|
||||||
|
`Cron job ${jobId} finished - ` +
|
||||||
|
`dashboard ${dashboard.id}, ` +
|
||||||
|
`widget ${widget.id}, ` +
|
||||||
|
`issues count ${issues.length}`,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async saveReport(
|
||||||
|
dashboard: Dashboard,
|
||||||
|
widget: Widget,
|
||||||
|
issues: RedmineTypes.Issue[],
|
||||||
|
): Promise<void> {
|
||||||
|
const datasource = await DailyEccmReportsV2Datasource.getDatasource();
|
||||||
|
const id = randomUUID();
|
||||||
|
const dashboardId = dashboard.id;
|
||||||
|
const widgetId = widget.id;
|
||||||
|
const now = DateTime.now();
|
||||||
|
const datetime = now.toMillis();
|
||||||
|
const datetimeFormatted = now.toISO();
|
||||||
|
const reportIssues = issues.map((issue: any) => {
|
||||||
|
return {
|
||||||
|
id: issue.id,
|
||||||
|
subject: issue.subject,
|
||||||
|
created_on: issue.created_on,
|
||||||
|
updated_on: issue.updated_on,
|
||||||
|
closed_on: issue.closed_on,
|
||||||
|
status: issue.status,
|
||||||
|
priority: issue.priority,
|
||||||
|
author: issue.author,
|
||||||
|
assigned_to: issue.assigned_to,
|
||||||
|
dev: issue.dev,
|
||||||
|
qa: issue.qa,
|
||||||
|
cr: issue.cr,
|
||||||
|
current_user: issue.current_user,
|
||||||
|
tracker: issue.tracker,
|
||||||
|
start_date: issue.start_date,
|
||||||
|
due_date: issue.due_date,
|
||||||
|
done_ratio: issue.done_ratio,
|
||||||
|
estimated_hours: issue.estimated_hours,
|
||||||
|
total_spent_hours: issue.total_spent_hours,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
const item: any = {
|
||||||
|
_id: id,
|
||||||
|
id: id,
|
||||||
|
dashboardId,
|
||||||
|
widgetId,
|
||||||
|
datetime,
|
||||||
|
datetimeFormatted,
|
||||||
|
reportIssues,
|
||||||
|
};
|
||||||
|
await datasource.insert(item);
|
||||||
|
this.logger.debug(
|
||||||
|
`Report saved to db - id ${id}, ` +
|
||||||
|
`issues count ${reportIssues.length}, ` +
|
||||||
|
`datetime ${datetimeFormatted}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue