Skip to content

Commit 3c230ae

Browse files
authored
Add migration to re-extract all pnp progress & problems (#3329)
1 parent c1af809 commit 3c230ae

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

src/components/progress-report/progress-report.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ProgressReportHighlightsResolver } from './highlights/progress-report-h
99
import { ProgressReportHighlightsService } from './highlights/progress-report-highlights.service';
1010
import { ProgressReportMediaModule } from './media/progress-report-media.module';
1111
import { DropInternshipProgressReportsMigration } from './migrations/drop-internship-progress-reports.migration';
12+
import { ReextractPnpProgressReportsMigration } from './migrations/reextract-all-progress-reports.migration';
1213
import { ProgressReportExtraForPeriodicInterfaceRepository } from './progress-report-extra-for-periodic-interface.repository';
1314
import { ProgressReportRepository } from './progress-report.repository';
1415
import { ProgressReportService } from './progress-report.service';
@@ -48,6 +49,7 @@ import { ProgressReportWorkflowModule } from './workflow/progress-report-workflo
4849
ProgressReportRepository,
4950
ProgressReportExtraForPeriodicInterfaceRepository,
5051
DropInternshipProgressReportsMigration,
52+
ReextractPnpProgressReportsMigration,
5153
],
5254
exports: [ProgressReportExtraForPeriodicInterfaceRepository],
5355
})

0 commit comments

Comments
 (0)