|
| 1 | +import { FormMetricType, FormStatus, getErrorMessage } from '@defra/forms-model' |
| 2 | + |
| 3 | +import { createLogger } from '~/src/helpers/logging/logger.js' |
| 4 | +import { getSubmissionRecordsForDate } from '~/src/repositories/submission-repository.js' |
| 5 | + |
| 6 | +const logger = createLogger() |
| 7 | + |
| 8 | +/** |
| 9 | + * @param {Map<string, number>} map |
| 10 | + * @param {string} formId |
| 11 | + */ |
| 12 | +function incrementFormCount(map, formId) { |
| 13 | + const current = map.get(formId) ?? 0 |
| 14 | + map.set(formId, current + 1) |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * @param {any[]} timelineMetrics |
| 19 | + * @param {string} formId |
| 20 | + * @param {FormStatus} formStatus |
| 21 | + * @param {number} count |
| 22 | + * @param {Date} date |
| 23 | + */ |
| 24 | +function pushTimelineMetric(timelineMetrics, formId, formStatus, count, date) { |
| 25 | + timelineMetrics.push( |
| 26 | + /** @type {FormTimelineMetric} */ ({ |
| 27 | + type: FormMetricType.TimelineMetric, |
| 28 | + formId, |
| 29 | + formStatus, |
| 30 | + metricName: 'Submissions', |
| 31 | + metricValue: count, |
| 32 | + createdAt: date |
| 33 | + }) |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Generates a set of timeline metrics |
| 39 | + * @param {Date} date - date on which to gather the metrics for |
| 40 | + */ |
| 41 | +export async function generateReportTimeline(date) { |
| 42 | + logger.info( |
| 43 | + `[report] Generating timeline report for date ${date.toUTCString()}` |
| 44 | + ) |
| 45 | + |
| 46 | + try { |
| 47 | + const submissionsCursor = getSubmissionRecordsForDate(date) |
| 48 | + |
| 49 | + const timelineMapDraft = new Map() |
| 50 | + const timelineMapLive = new Map() |
| 51 | + |
| 52 | + for await (const submission of submissionsCursor) { |
| 53 | + const status = submission.meta.status |
| 54 | + if (status === FormStatus.Draft) { |
| 55 | + incrementFormCount(timelineMapDraft, submission.meta.formId) |
| 56 | + } else { |
| 57 | + incrementFormCount(timelineMapLive, submission.meta.formId) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const timelineMetrics = /** @type {FormTimelineMetric[]} */ ([]) |
| 62 | + |
| 63 | + if (timelineMapDraft.size) { |
| 64 | + for (const [formId, count] of timelineMapDraft) { |
| 65 | + pushTimelineMetric( |
| 66 | + timelineMetrics, |
| 67 | + formId, |
| 68 | + FormStatus.Draft, |
| 69 | + count, |
| 70 | + date |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (timelineMapLive.size) { |
| 76 | + for (const [formId, count] of timelineMapLive) { |
| 77 | + pushTimelineMetric( |
| 78 | + timelineMetrics, |
| 79 | + formId, |
| 80 | + FormStatus.Live, |
| 81 | + count, |
| 82 | + date |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + logger.info( |
| 88 | + `[report] Generated timeline report for date ${date.toString()}` |
| 89 | + ) |
| 90 | + |
| 91 | + return { |
| 92 | + timeline: timelineMetrics |
| 93 | + } |
| 94 | + } catch (err) { |
| 95 | + logger.error( |
| 96 | + err, |
| 97 | + `[report] Failed to generate timeline report for date ${date.toString()} - ${getErrorMessage(err)}` |
| 98 | + ) |
| 99 | + |
| 100 | + throw err |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * @import { FormTimelineMetric } from '@defra/forms-model' |
| 106 | + */ |
0 commit comments