From 01c54b2d651045ed2a1b560a728cec2b6e070e8e Mon Sep 17 00:00:00 2001 From: Pavel Gnedov Date: Thu, 12 Dec 2024 02:27:55 +0700 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BE=D1=82=D1=87=D1=91=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Добавлен метод для сохранения отчёта saveReport * Доработан метод createJobHandler - загрузка данных и сохранение с помощью saveReport --- src/reports/daily-eccm-v2.report.service.ts | 97 +++++++++++++++++++-- 1 file changed, 91 insertions(+), 6 deletions(-) diff --git a/src/reports/daily-eccm-v2.report.service.ts b/src/reports/daily-eccm-v2.report.service.ts index 75c2cc0..f077a25 100644 --- a/src/reports/daily-eccm-v2.report.service.ts +++ b/src/reports/daily-eccm-v2.report.service.ts @@ -1,8 +1,13 @@ 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 { Injectable, Logger } from '@nestjs/common'; import { SchedulerRegistry } from '@nestjs/schedule'; 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 = { query: any; // TODO: add type @@ -39,7 +44,10 @@ export class DailyEccmV2ReportService { private cronJobs: Record = {}; - constructor(private schedulerRegistry: SchedulerRegistry) {} + constructor( + private schedulerRegistry: SchedulerRegistry, + private issuesService: IssuesService, + ) {} /** * Auto scan jobs every UPDATE_RATE seconds. @@ -92,7 +100,7 @@ export class DailyEccmV2ReportService { this.schedulerRegistry.deleteCronJob(jobId); } const job = new CronJob( - widget.dataLoaderParams?.schedule || '* * * * *', + widget.dataLoaderParams?.schedule || '0 0 * * *', this.createJobHandler(jobId, dashboard, widget), ); this.cronJobs[jobId] = job; @@ -116,14 +124,91 @@ export class DailyEccmV2ReportService { jobId: string, dashboard: Dashboard, widget: Widget, - ): () => void { - return async () => { + ): () => Promise { + this.logger.debug( + `Create job handler for cron job ${jobId}, ` + + `dashboard ${dashboard.id}, ` + + `widget ${widget.id}`, + ); + return async (): Promise => { this.logger.debug( `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 { + 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}`, + ); + } }