|
| 1 | +import { asyncPool } from '@seedcompany/common'; |
| 2 | +import { node, relation } from 'cypher-query-builder'; |
| 3 | +import { IEventBus } from '~/core'; |
| 4 | +import { BaseMigration, Migration } from '~/core/database'; |
| 5 | +import { ACTIVE, matchProps, merge } from '~/core/database/query'; |
| 6 | +import { FileService } from '../../file'; |
| 7 | +import { FileVersion } from '../../file/dto'; |
| 8 | +import { PeriodicReportUploadedEvent } from '../../periodic-report/events'; |
| 9 | +import { ProgressReport } from '../dto'; |
| 10 | + |
| 11 | +@Migration('2024-11-11T16:00:04') |
| 12 | +export class ReextractPnpProgressReportsMigration extends BaseMigration { |
| 13 | + constructor( |
| 14 | + private readonly eventBus: IEventBus, |
| 15 | + private readonly files: FileService, |
| 16 | + ) { |
| 17 | + super(); |
| 18 | + } |
| 19 | + |
| 20 | + async up() { |
| 21 | + const session = this.fakeAdminSession; |
| 22 | + const pnps = createPaginator((page) => |
| 23 | + this.grabSomePnpsToReextract(page, 100), |
| 24 | + ); |
| 25 | + await asyncPool(2, pnps, async ({ dto: report, fv }) => { |
| 26 | + try { |
| 27 | + const pnp = this.files.asDownloadable(fv); |
| 28 | + const event = new PeriodicReportUploadedEvent(report, pnp, session); |
| 29 | + await this.eventBus.publish(event); |
| 30 | + } catch (e) { |
| 31 | + this.logger.error('Failed to re-extract PnP', { |
| 32 | + report: report.id, |
| 33 | + fvId: fv.id, |
| 34 | + exception: e, |
| 35 | + }); |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + private async grabSomePnpsToReextract(page: number, size: number) { |
| 41 | + this.logger.info(`Grabbing page of progress reports ${page}`); |
| 42 | + |
| 43 | + const currentPage = await this.db |
| 44 | + .query() |
| 45 | + // eslint-disable-next-line no-loop-func |
| 46 | + .subQuery((s) => |
| 47 | + s |
| 48 | + .match(node('report', 'ProgressReport')) |
| 49 | + // Grab latest pnp file version, ignore reports without |
| 50 | + .subQuery('report', (sub) => |
| 51 | + sub |
| 52 | + .match([ |
| 53 | + node('report'), |
| 54 | + relation('out', '', 'reportFileNode', ACTIVE), |
| 55 | + node('', 'File'), |
| 56 | + relation('in', '', 'parent', ACTIVE), |
| 57 | + node('version', 'FileVersion'), |
| 58 | + relation('out', '', 'name', ACTIVE), |
| 59 | + node('name', 'Property'), |
| 60 | + ]) |
| 61 | + .return(merge('version', { name: 'name.value' }).as('fv')) |
| 62 | + .orderBy('fv.createdAt', 'DESC') |
| 63 | + .raw('LIMIT 1'), |
| 64 | + ) |
| 65 | + .return(['report, fv']) |
| 66 | + .orderBy('fv.createdAt') |
| 67 | + .skip(page * size) |
| 68 | + .limit(size), |
| 69 | + ) |
| 70 | + .match([ |
| 71 | + node('parent', 'BaseNode'), |
| 72 | + relation('out', '', 'report', ACTIVE), |
| 73 | + node('report'), |
| 74 | + ]) |
| 75 | + .apply(matchProps({ nodeName: 'report' })) |
| 76 | + .return<{ |
| 77 | + // These are close enough lol |
| 78 | + dto: ProgressReport; |
| 79 | + fv: FileVersion; |
| 80 | + }>([merge('props', { parent: 'parent' }).as('dto'), 'fv']) |
| 81 | + .run(); |
| 82 | + return currentPage; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +async function* createPaginator<T>( |
| 87 | + getPage: (page: number) => Promise<readonly T[]>, |
| 88 | +) { |
| 89 | + let page = 0; |
| 90 | + do { |
| 91 | + const currentPage = await getPage(page); |
| 92 | + if (currentPage.length === 0) { |
| 93 | + return; |
| 94 | + } |
| 95 | + yield* currentPage; |
| 96 | + page++; |
| 97 | + // eslint-disable-next-line no-constant-condition |
| 98 | + } while (true); |
| 99 | +} |
0 commit comments