@stamhoofd/backend-statistics-syncer 2.153.0 → 2.161.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE.md +7 -7
  2. package/README.md +1 -1
  3. package/dist/index.js +5 -8
  4. package/dist/index.js.map +1 -0
  5. package/dist/migrations.js +19 -0
  6. package/dist/migrations.js.map +1 -0
  7. package/dist/src/boot.js +75 -0
  8. package/dist/src/boot.js.map +1 -0
  9. package/dist/src/crons.js +39 -0
  10. package/dist/src/crons.js.map +1 -0
  11. package/dist/src/database.js +88 -0
  12. package/dist/src/database.js.map +1 -0
  13. package/dist/src/migrate.js +38 -0
  14. package/dist/src/migrate.js.map +1 -0
  15. package/dist/src/migrations/1785958573-platform-statistics-schema.sql +382 -0
  16. package/dist/src/migrations/1786203367-postal-code-coordinates.sql +1203 -0
  17. package/dist/src/periods.js +86 -0
  18. package/dist/src/periods.js.map +1 -0
  19. package/dist/src/rows.js +209 -0
  20. package/dist/src/rows.js.map +1 -0
  21. package/dist/src/schema.js +28 -0
  22. package/dist/src/schema.js.map +1 -0
  23. package/dist/src/sources.js +10 -0
  24. package/dist/src/sources.js.map +1 -0
  25. package/dist/src/sync-state.js +36 -0
  26. package/dist/src/sync-state.js.map +1 -0
  27. package/dist/src/sync.js +608 -0
  28. package/dist/src/sync.js.map +1 -0
  29. package/dist/src/writer.js +63 -0
  30. package/dist/src/writer.js.map +1 -0
  31. package/dist-types/index.d.ts +2 -0
  32. package/dist-types/index.d.ts.map +1 -0
  33. package/dist-types/migrations.d.ts +2 -0
  34. package/dist-types/migrations.d.ts.map +1 -0
  35. package/dist-types/src/boot.d.ts +8 -0
  36. package/dist-types/src/boot.d.ts.map +1 -0
  37. package/dist-types/src/crons.d.ts +21 -0
  38. package/dist-types/src/crons.d.ts.map +1 -0
  39. package/dist-types/src/database.d.ts +28 -0
  40. package/dist-types/src/database.d.ts.map +1 -0
  41. package/dist-types/src/migrate.d.ts +2 -0
  42. package/dist-types/src/migrate.d.ts.map +1 -0
  43. package/dist-types/src/periods.d.ts +51 -0
  44. package/dist-types/src/periods.d.ts.map +1 -0
  45. package/dist-types/src/rows.d.ts +212 -0
  46. package/dist-types/src/rows.d.ts.map +1 -0
  47. package/dist-types/src/schema.d.ts +9 -0
  48. package/dist-types/src/schema.d.ts.map +1 -0
  49. package/dist-types/src/sources.d.ts +10 -0
  50. package/dist-types/src/sources.d.ts.map +1 -0
  51. package/dist-types/src/sync-state.d.ts +20 -0
  52. package/dist-types/src/sync-state.d.ts.map +1 -0
  53. package/dist-types/src/sync.d.ts +37 -0
  54. package/dist-types/src/sync.d.ts.map +1 -0
  55. package/dist-types/src/writer.d.ts +24 -0
  56. package/dist-types/src/writer.d.ts.map +1 -0
  57. package/index.ts +5 -8
  58. package/migrations.ts +16 -11
  59. package/package.json +37 -23
  60. package/src/boot.ts +7 -3
  61. package/src/crons.test.ts +33 -8
  62. package/src/crons.ts +7 -1
  63. package/src/database.test.ts +48 -11
  64. package/src/database.ts +30 -7
  65. package/src/migrate.ts +1 -1
  66. package/src/periods.ts +5 -2
  67. package/src/rows.test.ts +96 -16
  68. package/src/rows.ts +28 -6
  69. package/src/schema.test.ts +48 -19
  70. package/src/schema.ts +14 -4
  71. package/src/sync-state.test.ts +3 -1
  72. package/src/sync-state.ts +13 -4
  73. package/src/sync.test.ts +145 -32
  74. package/src/sync.ts +422 -91
  75. package/src/writer.ts +32 -10
  76. package/tsconfig.build.json +4 -11
  77. package/tsconfig.json +1 -1
  78. package/tsconfig.test.json +1 -3
  79. package/tsconfig.types.json +7 -0
  80. package/.gitignore +0 -221
  81. package/tests/vitest.global.setup.ts +0 -29
  82. package/tests/vitest.setup.ts +0 -24
  83. package/vitest.config.js +0 -18
@@ -0,0 +1,63 @@
1
+ import { getStatisticsConnection } from './database.js';
2
+ /**
3
+ * The number of statistics rows written per statement, and the number of source ids checked per
4
+ * query while reconciling deletes.
5
+ */
6
+ export const batchSize = 500;
7
+ /** What identifies a row when no other key is given. `members` is keyed per period as well. */
8
+ export const defaultKeyColumns = ['id'];
9
+ export function buildUpsertQuery(table, columns, escapeId, keyColumns = defaultKeyColumns) {
10
+ const assignments = columns
11
+ .filter((column) => !keyColumns.includes(column))
12
+ .map((column) => `${escapeId(column)} = new.${escapeId(column)}`);
13
+ // The row alias is the MySQL 8 replacement for VALUES(col), which is deprecated.
14
+ return (`INSERT INTO ${escapeId(table)} (${columns.map(escapeId).join(', ')}) VALUES ? AS new` +
15
+ (assignments.length > 0 ? ` ON DUPLICATE KEY UPDATE ${assignments.join(', ')}` : ''));
16
+ }
17
+ /**
18
+ * Write rows, replacing the ones that are already there. Running the same batch twice is a no-op, so
19
+ * a run that crashed halfway can simply be repeated.
20
+ */
21
+ export async function upsertRows(table, columns, rows, keyColumns = defaultKeyColumns) {
22
+ if (rows.length === 0) {
23
+ return 0;
24
+ }
25
+ const connection = getStatisticsConnection();
26
+ const query = buildUpsertQuery(table, columns, (value) => connection.escapeId(value), keyColumns);
27
+ for (let index = 0; index < rows.length; index += batchSize) {
28
+ const batch = rows
29
+ .slice(index, index + batchSize)
30
+ .map((row) => columns.map((column) => row[column] ?? null));
31
+ await connection.insert(query, [batch]);
32
+ }
33
+ return rows.length;
34
+ }
35
+ export async function deleteRows(table, ids) {
36
+ if (ids.length === 0) {
37
+ return 0;
38
+ }
39
+ const connection = getStatisticsConnection();
40
+ const [result] = await connection.delete(`DELETE FROM ${connection.escapeId(table)} WHERE \`id\` IN (?)`, [ids]);
41
+ return result.affectedRows;
42
+ }
43
+ /**
44
+ * The ids a source still holds in a statistics table, in pages, so reconciling never keeps the whole
45
+ * table in memory.
46
+ *
47
+ * Scoped to one source on purpose: the rows an import produced have no counterpart in the main
48
+ * database, so reconciling them against it would delete every one of them on the first night.
49
+ */
50
+ export async function* iterateIds(table, source) {
51
+ const connection = getStatisticsConnection();
52
+ let cursor = '';
53
+ for (;;) {
54
+ const [rows] = await connection.select(`SELECT \`id\` FROM ${connection.escapeId(table)} WHERE \`source\` = ? AND \`id\` > ? ORDER BY \`id\` ASC LIMIT ${batchSize}`, [source, cursor], { nestTables: false });
55
+ const ids = rows.map((row) => row.id);
56
+ if (ids.length === 0) {
57
+ return;
58
+ }
59
+ yield ids;
60
+ cursor = ids[ids.length - 1];
61
+ }
62
+ }
63
+ //# sourceMappingURL=writer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writer.js","sourceRoot":"","sources":["../../src/writer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAGxD;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,CAAC;AAE7B,+FAA+F;AAC/F,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,IAAI,CAAC,CAAC;AAExC,MAAM,UAAU,gBAAgB,CAC5B,KAAa,EACb,OAAiB,EACjB,QAAmC,EACnC,aAAuB,iBAAiB;IAExC,MAAM,WAAW,GAAG,OAAO;SACtB,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;SAChD,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,UAAU,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAEtE,iFAAiF;IACjF,OAAO,CACH,eAAe,QAAQ,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,mBAAmB;QACtF,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,4BAA4B,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CACvF,CAAC;AACN,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAC5B,KAAa,EACb,O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AAM,CAAC,KAAK,SAAS,CAAC,CAAC,UAAU,CAAC,KAAa,EAAE,MAAc;IAC3D,MAAM,UAAU,GAAG,uBAAuB,EAAE,CAAC;IAC7C,IAAI,MAAM,GAAG,EAAE,CAAC;IAEhB,SAAS,CAAC;QACN,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,UAAU,CAAC,MAAM,CAClC,sBAAsB,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,kEAAkE,SAAS,EAAE,EAC7H,CAAC,MAAM,EAAE,MAAM,CAAC,EAChB,EAAE,UAAU,EAAE,KAAK,EAAE,CACxB,CAAC;QACF,MAAM,GAAG,GAAI,IAAoC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACvE,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACnB,OAAO;QACX,CAAC;QAED,MAAM,GAAG,CAAC;QACV,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACjC,CAAC;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=migrations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../migrations.ts"],"names":[],"mappings":""}
@@ -0,0 +1,8 @@
1
+ import { ApplicationLifecycle } from '@stamhoofd/node-observability';
2
+ export declare const boot: (options: {
3
+ killProcess: boolean;
4
+ lifecycle?: ApplicationLifecycle;
5
+ }) => Promise<{
6
+ shutdown: () => Promise<void>;
7
+ }>;
8
+ //# sourceMappingURL=boot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boot.d.ts","sourceRoot":"","sources":["../../src/boot.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AAoCrE,eAAO,MAAM,IAAI,GAAU,SAAS;IAAE,WAAW,EAAE,OAAO,CAAC;IAAC,SAAS,CAAC,EAAE,oBAAoB,CAAA;CAAE;;EAkD7F,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Both passes read the whole administration, which is far more than the five-minute cron cadence is
3
+ * meant for and does not need to be current to the minute: the reports they feed are read by day.
4
+ */
5
+ export declare function shouldRunStatisticsSync({ now, lastRun, }: {
6
+ now: Date;
7
+ lastRun: Date | null;
8
+ }): boolean;
9
+ /**
10
+ * How the cron loop is started for this service.
11
+ *
12
+ * `allowReadOnly` is on because the syncer runs on a replica, whose MySQL is `read_only`. That flag
13
+ * guards the main database, and this service never writes to it: it reads the administration and
14
+ * writes through the separate connection to the statistics database, whose watermarks live in
15
+ * `stats_sync_state` there. Switching it off stops the sync from ever running where it is deployed.
16
+ */
17
+ export declare const statisticsCronOptions: {
18
+ allowReadOnly: boolean;
19
+ allowBeforeSeeds: boolean;
20
+ };
21
+ //# sourceMappingURL=crons.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crons.d.ts","sourceRoot":"","sources":["../../src/crons.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,EACpC,GAAG,EACH,OAAO,GACV,EAAE;IACC,GAAG,EAAE,IAAI,CAAC;IACV,OAAO,EAAE,IAAI,GAAG,IAAI,CAAC;CACxB,GAAG,OAAO,CAgBV;AAcD;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB;;;CAAkD,CAAC"}
@@ -0,0 +1,28 @@
1
+ import { DatabaseInstance } from '@stamhoofd/node-database';
2
+ import type { StatisticsEnvironment } from '@stamhoofd/types/Environment';
3
+ export type StatisticsDatabaseConfig = StatisticsEnvironment['statisticsDatabase'];
4
+ export declare function getStatisticsDatabaseConfig(): StatisticsDatabaseConfig;
5
+ /**
6
+ * Where to reach the statistics database. It lives next to Metabase rather than on the server this
7
+ * runs on, so every connection detail is spelled out instead of inherited from the main database.
8
+ */
9
+ export declare function getStatisticsPoolOptions(): {
10
+ host: string;
11
+ user: string;
12
+ password: string;
13
+ port: number | undefined;
14
+ useSSL: boolean;
15
+ ca?: string;
16
+ cert?: string;
17
+ key?: string;
18
+ };
19
+ /**
20
+ * A connection of its own to the platform statistics database.
21
+ *
22
+ * The shared `Database` stays pointed at the main administration, which the sync reads through the
23
+ * models: the statistics database lives on the Metabase server, so the two are not even the same
24
+ * MySQL to point a single connection at.
25
+ */
26
+ export declare function getStatisticsConnection(): DatabaseInstance;
27
+ export declare function endStatisticsConnection(): Promise<void>;
28
+ //# sourceMappingURL=database.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;AAInF,wBAAgB,2BAA2B,IAAI,wBAAwB,CA0BtE;AAyBD;;;GAGG;AACH,wBAAgB,wBAAwB,IAAI;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,MAAM,EAAE,OAAO,CAAC;IAChB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,GAAG,CAAC,EAAE,MAAM,CAAC;CAChB,CAwBA;AAID;;;;;;GAMG;AACH,wBAAgB,uBAAuB,IAAI,gBAAgB,CAM1D;AAED,wBAAsB,uBAAuB,IAAI,OAAO,CAAC,IAAI,CAAC,CAQ7D"}
@@ -0,0 +1,2 @@
1
+ export declare function run(): Promise<void>;
2
+ //# sourceMappingURL=migrate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../../src/migrate.ts"],"names":[],"mappings":"AAsCA,wBAAsB,GAAG,kBAKxB"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The date up to which this platform's statistics come from an imported external source. Periods
3
+ * ending before it are frozen the first time the sync sees them, so the import owns those years.
4
+ * Unset means the platform has no external history and nothing is frozen up front.
5
+ */
6
+ export declare function getImportedUntil(): Date | null;
7
+ /**
8
+ * How long after a period ended unlocking it in the administration still brings it back into the
9
+ * sync. A period is unlocked to correct something, and beyond this an old year would have all of its
10
+ * numbers written again from what the administration says today rather than the one thing corrected.
11
+ */
12
+ export declare const releaseWindow: number;
13
+ /**
14
+ * Give periods that ended before the imported history a cutoff, once. Only ever fills a cutoff that
15
+ * is still empty, so a date set by hand afterwards stays put.
16
+ */
17
+ export declare function applyImportedCutoff(importedUntil: Date | null): Promise<void>;
18
+ /**
19
+ * The periods left alone entirely, their own row included: the years an import owns and the ones
20
+ * settled by hand. The sync never wrote those rows, so it does not rewrite them either.
21
+ */
22
+ export declare function loadSettledPeriodIds(): Promise<Set<string>>;
23
+ /**
24
+ * The periods whose statistics are settled. Rows belonging to them are left exactly as they are: not
25
+ * written by the sync, and not excluded by the reconciliation either.
26
+ *
27
+ * Either because their cutoff has passed, or because the administration locked them and a full run
28
+ * has carried in what happened up to that lock.
29
+ */
30
+ export declare function loadFrozenPeriodIds(): Promise<Set<string>>;
31
+ /**
32
+ * Stop following the periods the administration has locked.
33
+ *
34
+ * Called once a whole run has come through, incremental pass and delete reconciliation both: the day
35
+ * a period is locked is a day like any other until then, and its changes and its deletions have to be
36
+ * written before it is settled. A run that failed never reaches this, so the next one reads that
37
+ * period again and settles it only once it succeeds.
38
+ */
39
+ export declare function settleLockedPeriods(): Promise<void>;
40
+ /**
41
+ * Follow a period the administration unlocked again, as long as it ended less than `releaseWindow`
42
+ * ago. An older year keeps the numbers it was settled with: what its rows would be written from is
43
+ * the administration of today, which no longer describes that year.
44
+ */
45
+ export declare function releaseUnlockedPeriods(): Promise<void>;
46
+ /**
47
+ * MySQL cannot be handed an empty IN list, and "belongs to no period" is never frozen: a row without
48
+ * a period is not part of any year's settled numbers.
49
+ */
50
+ export declare function isFrozen(frozenPeriodIds: Set<string>, periodId: string | null | undefined): boolean;
51
+ //# sourceMappingURL=periods.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"periods.d.ts","sourceRoot":"","sources":["../../src/periods.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,IAAI,GAAG,IAAI,CAY9C;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,QAA4B,CAAC;AAEvD;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,aAAa,EAAE,IAAI,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAUnF;AAeD;;;GAGG;AACH,wBAAgB,oBAAoB,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAE3D;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAE1D;AAED;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAMzD;AAED;;;;GAIG;AACH,wBAAsB,sBAAsB,IAAI,OAAO,CAAC,IAAI,CAAC,CAM5D;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CACpB,eAAe,EAAE,GAAG,CAAC,MAAM,CAAC,EAC5B,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GACpC,OAAO,CAET"}
@@ -0,0 +1,212 @@
1
+ /**
2
+ * Turns source records into the rows of the statistics database.
3
+ *
4
+ * Every function here is pure and drops most of the personal data on the way: a member arrives with a
5
+ * full `MemberDetails` and leaves as a date of birth, a gender and a postal code. Nothing in this
6
+ * file may start returning a name, an email address or a phone number — the statistics database has
7
+ * no columns for them, and the schema test would fail if it did.
8
+ *
9
+ * The inputs are described structurally rather than as model classes, so the models satisfy them
10
+ * without this package depending on how they are loaded.
11
+ */
12
+ export type StatisticsRow = Record<string, string | number | boolean | Date | null>;
13
+ /**
14
+ * The language translated names are resolved in. The statistics database holds one name per group,
15
+ * and the sync has no request to take a language from, so it picks the platform's own rather than
16
+ * whatever language happens to be set on the process.
17
+ */
18
+ export declare const statisticsLanguage = "nl";
19
+ export type RegistrationPeriodSource = {
20
+ id: string;
21
+ startDate: Date;
22
+ endDate: Date;
23
+ locked: boolean;
24
+ organizationId: string | null;
25
+ previousPeriodId: string | null;
26
+ nextPeriodId: string | null;
27
+ customName: string | null;
28
+ createdAt: Date;
29
+ updatedAt: Date;
30
+ };
31
+ export type OrganizationSource = {
32
+ id: string;
33
+ name: string;
34
+ uri: string;
35
+ address: {
36
+ postalCode: string;
37
+ city: string;
38
+ };
39
+ periodId: string;
40
+ active: boolean;
41
+ createdAt: Date;
42
+ updatedAt: Date;
43
+ };
44
+ export type GroupSource = {
45
+ id: string;
46
+ type: string;
47
+ settings: {
48
+ name: {
49
+ get: (language: string) => string;
50
+ };
51
+ };
52
+ organizationId: string;
53
+ periodId: string | null;
54
+ defaultAgeGroupId: string | null;
55
+ status: string;
56
+ deletedAt: Date | null;
57
+ createdAt: Date;
58
+ updatedAt: Date;
59
+ };
60
+ export type MemberSource = {
61
+ id: string;
62
+ details: {
63
+ birthDay: Date | null;
64
+ gender: string;
65
+ address: {
66
+ postalCode: string;
67
+ } | null;
68
+ };
69
+ organizationId: string | null;
70
+ createdAt: Date;
71
+ updatedAt: Date;
72
+ lastRegisteredAt: Date | null;
73
+ };
74
+ export type RegistrationSource = {
75
+ id: string;
76
+ organizationId: string;
77
+ memberId: string;
78
+ groupId: string;
79
+ periodId: string | null;
80
+ registeredAt: Date | null;
81
+ startDate: Date | null;
82
+ endDate: Date | null;
83
+ trialUntil: Date | null;
84
+ deactivatedAt: Date | null;
85
+ waitingList: boolean;
86
+ cycle: number;
87
+ createdAt: Date;
88
+ updatedAt: Date;
89
+ };
90
+ export type MembershipSource = {
91
+ id: string;
92
+ memberId: string;
93
+ membershipTypeId: string;
94
+ organizationId: string;
95
+ periodId: string;
96
+ startDate: Date;
97
+ endDate: Date;
98
+ expireDate: Date | null;
99
+ trialUntil: Date | null;
100
+ deletedAt: Date | null;
101
+ createdAt: Date;
102
+ updatedAt: Date;
103
+ };
104
+ export type ResponsibilityRecordSource = {
105
+ id: string;
106
+ memberId: string;
107
+ groupId: string | null;
108
+ organizationId: string | null;
109
+ responsibilityId: string;
110
+ startDate: Date;
111
+ endDate: Date | null;
112
+ };
113
+ export type NamedConfigSource = {
114
+ id: string;
115
+ name: string;
116
+ };
117
+ export type PlatformSource = {
118
+ id: string;
119
+ config: {
120
+ name: string;
121
+ financialSupport: {
122
+ priceName: string;
123
+ } | null;
124
+ };
125
+ membershipOrganizationId: string | null;
126
+ };
127
+ /**
128
+ * The label the reports show for a period, mirroring `RegistrationPeriodBase.nameShort`: the custom
129
+ * name when there is one, otherwise the years it spans.
130
+ *
131
+ * Reimplemented here rather than read off the model because the model's getter runs `$t`, which needs
132
+ * a request to take a language from and throws in the sync. The years are resolved in the application
133
+ * timezone, so a period starting at midnight in Brussels does not fall back into the previous year.
134
+ */
135
+ export declare function periodName(period: {
136
+ customName: string | null;
137
+ startDate: Date;
138
+ endDate: Date;
139
+ }): string;
140
+ export declare function flattenRegistrationPeriod(period: RegistrationPeriodSource): StatisticsRow;
141
+ /**
142
+ * One row per unit per period: the unit as it was that year. A unit that is renamed or moves keeps
143
+ * the name and the place it was counted under in the years already settled.
144
+ *
145
+ * `periodId` is the year this row describes, not the period the unit is in now — the latter lives on
146
+ * the source record and is what the netwerk links are recorded against.
147
+ *
148
+ * Only the postal code and city of the address survive: the report maps units by postal code and
149
+ * lists them by city. An organization is a legal entity rather than a natural person.
150
+ */
151
+ export declare function flattenOrganization(organization: OrganizationSource, periodId: string): StatisticsRow;
152
+ export declare function flattenGroup(group: GroupSource): StatisticsRow;
153
+ /**
154
+ * One row per member per period: what was true of this member in that year. The source holds only
155
+ * what is true of them now, so every open period gets the current details and a frozen one keeps the
156
+ * row it was counted with.
157
+ *
158
+ * The date of birth and the postal code are carried over because the report needs them: it charts
159
+ * members by age, which is only exact with the date behind it, and maps them by postal code. The
160
+ * rest of the address and everything that names or reaches the member stays behind.
161
+ *
162
+ * The birth date is written as the calendar date the application means, not as an instant. A member
163
+ * born on 17 May is held as midnight in Europe/Brussels, which a process running in UTC would write
164
+ * to a date column as 16 May — a day early for every member.
165
+ */
166
+ export declare function flattenMember(member: MemberSource, periodId: string): StatisticsRow;
167
+ export declare function flattenRegistration(registration: RegistrationSource): StatisticsRow;
168
+ /**
169
+ * `reducedPrice` is not on the source record: the price of a lidgeld is worked out from whether the
170
+ * member has financiële ondersteuning or an active UITPAS and only the amount is kept, so the caller
171
+ * passes that same status here. It is what the reports split the lidgelden by.
172
+ */
173
+ export declare function flattenMembership(membership: MembershipSource, reducedPrice: boolean): StatisticsRow;
174
+ /**
175
+ * One row per period the record runs through: it carries a date range rather than a year, and a
176
+ * functie held for three years was held in each of them.
177
+ */
178
+ export declare function flattenResponsibilityRecord(record: ResponsibilityRecordSource, periodId: string): StatisticsRow;
179
+ /**
180
+ * The platform configuration holds one name per tak, netwerk, lidgeldtype and functie: the one it
181
+ * carries today. Written per period, so renaming one changes what the years still open are reported
182
+ * under and leaves the settled ones saying what they said.
183
+ */
184
+ export declare function flattenNamedConfig(config: NamedConfigSource, periodId: string): StatisticsRow;
185
+ /**
186
+ * The platform itself, which the reports need for two things: which organization it runs on its own,
187
+ * and what it calls its verlaagd lidgeld. The first is the koepel rather than one of its local
188
+ * groups, which the jeugdbewegingen report delivers as the bovenlokale ondersteuningsstructuur.
189
+ *
190
+ * Not written per period, so the name here is the one the platform carries today. Nothing is reported
191
+ * per year out of it -- the name a sheet prints is the organization's own, per period.
192
+ *
193
+ * `reducedPriceName` is the label the platform gives its lower lidgeld, which the reports print
194
+ * beside the standaardtarief. Null when the platform names none, and the report says it in words of
195
+ * its own rather than inventing a name here.
196
+ */
197
+ export declare function flattenPlatform(platform: PlatformSource): StatisticsRow;
198
+ /**
199
+ * The label a tak carries, mirroring `DefaultAgeGroup.name`: its names joined, the last two with
200
+ * "of".
201
+ *
202
+ * Reimplemented here rather than read off the structure because the structure's getter runs `$t` on
203
+ * the separator, which resolves to the translation key in the sync.
204
+ */
205
+ export declare function defaultAgeGroupName(names: string[]): string;
206
+ export declare function flattenDefaultAgeGroup(group: {
207
+ id: string;
208
+ names: string[];
209
+ minAge: number | null;
210
+ maxAge: number | null;
211
+ }, periodId: string): StatisticsRow;
212
+ //# sourceMappingURL=rows.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rows.d.ts","sourceRoot":"","sources":["../../src/rows.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC;AAEpF;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,OAAO,CAAC;AAEvC,MAAM,MAAM,wBAAwB,GAAG;IACnC,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,IAAI,CAAC;IAChB,OAAO,EAAE,IAAI,CAAC;IACd,MAAM,EAAE,OAAO,CAAC;IAChB,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC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B,EAAE,QAAQ,EAAE,MAAM,GAAG,aAAa,CAE7F;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,cAAc,GAAG,aAAa,CAOvE;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAE3D;AAED,wBAAgB,sBAAsB,CAClC,KAAK,EAAE;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAC;IAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,EACpF,QAAQ,EAAE,MAAM,GACjB,aAAa,CAQf"}
@@ -0,0 +1,9 @@
1
+ export declare function getStatisticsMigrationsPath(): string;
2
+ /**
3
+ * Run the migrations of the platform statistics database.
4
+ *
5
+ * That database keeps its own migration history, in its own `migrations` table, so it can be moved
6
+ * to another server without dragging the main database along.
7
+ */
8
+ export declare function runStatisticsMigrations(): Promise<void>;
9
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAQA,wBAAgB,2BAA2B,IAAI,MAAM,CAEpD;AAED;;;;;GAKG;AACH,wBAAsB,uBAAuB,IAAI,OAAO,CAAC,IAAI,CAAC,CAmB7D"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Which pipeline wrote a statistics row.
3
+ *
4
+ * The reconciliation checks statistics rows against the main database, so it has to know which rows
5
+ * came from there: imported rows have no counterpart to find and would all be excluded on the first
6
+ * night.
7
+ */
8
+ export declare const syncSource = "sync";
9
+ export declare const importSource = "import";
10
+ //# sourceMappingURL=sources.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sources.d.ts","sourceRoot":"","sources":["../../src/sources.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,SAAS,CAAC;AACjC,eAAO,MAAM,YAAY,WAAW,CAAC"}
@@ -0,0 +1,20 @@
1
+ export declare const syncStateTable = "stats_sync_state";
2
+ export type SyncState = {
3
+ tableName: string;
4
+ watermark: Date | null;
5
+ lastSucceededAt: Date | null;
6
+ lastReconciledAt: Date | null;
7
+ };
8
+ /**
9
+ * The watermark to store after a run: the highest `updatedAt` that was seen, but never beyond the
10
+ * moment the run started.
11
+ *
12
+ * Source rows can carry an `updatedAt` in the future — periods in this database do. Following one
13
+ * would park the watermark weeks ahead, and every row written in the meantime would fall below it and
14
+ * be skipped. Capping costs one row that is read again on every run until the clock catches up, and
15
+ * repairs a watermark that a previous run already pushed into the future.
16
+ */
17
+ export declare function nextWatermark(previous: Date | null, seen: Date[], runStartedAt: Date): Date | null;
18
+ export declare function readSyncState(tableName: string): Promise<SyncState>;
19
+ export declare function writeSyncState(tableName: string, state: Partial<Omit<SyncState, 'tableName'>>): Promise<void>;
20
+ //# sourceMappingURL=sync-state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-state.d.ts","sourceRoot":"","sources":["../../src/sync-state.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,cAAc,qBAAqB,CAAC;AAEjD,MAAM,MAAM,SAAS,GAAG;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;IACvB,eAAe,EAAE,IAAI,GAAG,IAAI,CAAC;IAC7B,gBAAgB,EAAE,IAAI,GAAG,IAAI,CAAC;CACjC,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CACzB,QAAQ,EAAE,IAAI,GAAG,IAAI,EACrB,IAAI,EAAE,IAAI,EAAE,EACZ,YAAY,EAAE,IAAI,GACnB,IAAI,GAAG,IAAI,CAUb;AAED,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAUzE;AAED,wBAAsB,cAAc,CAChC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC,GAC7C,OAAO,CAAC,IAAI,CAAC,CAYf"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * How far before the watermark a run starts reading again. A row that was written while the previous
3
+ * run was reading may carry an `updatedAt` below the watermark that run ended on, so re-reading a
4
+ * window costs a few redundant upserts and closes that gap.
5
+ */
6
+ export declare const watermarkOverlap: number;
7
+ /** Source rows read per query. */
8
+ export declare const fetchBatchSize = 500;
9
+ /**
10
+ * Marks which pipeline wrote a row, so the reconciliation only ever checks its own against the main
11
+ * database and leaves imported rows alone.
12
+ */
13
+ export declare const sourceColumns: string[];
14
+ /** The tables the delete reconciliation walks, which is every table it may take a row out of. */
15
+ export declare function reconciledTables(): string[];
16
+ /**
17
+ * Bring the statistics database up to date with everything that changed since the last run.
18
+ *
19
+ * Written so a crash is harmless: every write is an upsert, and a table that throws keeps its old
20
+ * watermark, so the next run reads the same window again instead of skipping it.
21
+ */
22
+ export declare function syncStatistics(): Promise<void>;
23
+ /**
24
+ * Remove statistics rows whose source row is gone. Deletes leave nothing behind to notice in an
25
+ * incremental pass, so they are reconciled separately by walking the statistics ids and asking the
26
+ * source which of them still exist.
27
+ */
28
+ export declare function syncStatisticsDeletes(): Promise<void>;
29
+ /**
30
+ * One whole run of the sync: everything that changed, then the rows whose source is gone, then the
31
+ * periods the administration has locked since.
32
+ *
33
+ * The order is what makes each of the three sound, so they are run from here rather than from the
34
+ * cron that schedules them.
35
+ */
36
+ export declare function runStatisticsCycle(): Promise<void>;
37
+ //# sourceMappingURL=sync.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync.d.ts","sourceRoot":"","sources":["../../src/sync.ts"],"names":[],"mappings":"AAuCA;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,QAAgB,CAAC;AAE9C,kCAAkC;AAClC,eAAO,MAAM,cAAc,MAAM,CAAC;AAuClC;;;GAGG;AACH,eAAO,MAAM,aAAa,UAAa,CAAC;AAquBxC,iGAAiG;AACjG,wBAAgB,gBAAgB,IAAI,MAAM,EAAE,CAI3C;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAgCpD;AAED;;;;GAIG;AACH,wBAAsB,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC,CAqC3D;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC,CAYxD"}
@@ -0,0 +1,24 @@
1
+ import type { StatisticsRow } from './rows.js';
2
+ /**
3
+ * The number of statistics rows written per statement, and the number of source ids checked per
4
+ * query while reconciling deletes.
5
+ */
6
+ export declare const batchSize = 500;
7
+ /** What identifies a row when no other key is given. `members` is keyed per period as well. */
8
+ export declare const defaultKeyColumns: string[];
9
+ export declare function buildUpsertQuery(table: string, columns: string[], escapeId: (value: string) => string, keyColumns?: string[]): string;
10
+ /**
11
+ * Write rows, replacing the ones that are already there. Running the same batch twice is a no-op, so
12
+ * a run that crashed halfway can simply be repeated.
13
+ */
14
+ export declare function upsertRows(table: string, columns: string[], rows: StatisticsRow[], keyColumns?: string[]): Promise<number>;
15
+ export declare function deleteRows(table: string, ids: string[]): Promise<number>;
16
+ /**
17
+ * The ids a source still holds in a statistics table, in pages, so reconciling never keeps the whole
18
+ * table in memory.
19
+ *
20
+ * Scoped to one source on purpose: the rows an import produced have no counterpart in the main
21
+ * database, so reconciling them against it would delete every one of them on the first night.
22
+ */
23
+ export declare function iterateIds(table: string, source: string): AsyncGenerator<string[]>;
24
+ //# sourceMappingURL=writer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../../src/writer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE/C;;;GAGG;AACH,eAAO,MAAM,SAAS,MAAM,CAAC;AAE7B,+FAA+F;AAC/F,eAAO,MAAM,iBAAiB,UAAS,CAAC;AAExC,wBAAgB,gBAAgB,CAC5B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,EAAE,EACjB,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,EACnC,UAAU,GAAE,MAAM,EAAsB,GACzC,MAAM,CAUR;AAED;;;GAGG;AACH,wBAAsB,UAAU,CAC5B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,EAAE,EACjB,IAAI,EAAE,aAAa,EAAE,EACrB,UAAU,GAAE,MAAM,EAAsB,GACzC,OAAO,CAAC,MAAM,CAAC,CAqBjB;AAED,wBAAsB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAW9E;AAED;;;;;;GAMG;AACH,wBAAuB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,cAAc,CAAC,MAAM,EAAE,CAAC,CAkBzF"}
package/index.ts CHANGED
@@ -1,20 +1,17 @@
1
1
  import backendEnv from '@stamhoofd/backend-env';
2
+ import { BackendApp } from '@stamhoofd/types/BackendApp';
3
+ import { runApplication } from '@stamhoofd/node-observability';
2
4
 
3
5
  process.title = 'stamhoofd-statistics-syncer';
4
- backendEnv.load({ service: 'statistics-syncer' }).catch((error) => {
5
- console.error('Failed to load environment:', error);
6
- process.exit(1);
7
- }).then(async () => {
6
+ runApplication('stats', async (lifecycle) => {
7
+ await backendEnv.load({ service: BackendApp.StatisticsSyncer });
8
8
  if (STAMHOOFD.environment === 'development') {
9
9
  const { run } = await import('./src/migrate.js');
10
10
  await run();
11
11
  }
12
12
  const { boot } = await import('./src/boot.js');
13
13
 
14
- boot({ killProcess: true }).catch((error) => {
15
- console.error('unhandledRejection', error);
16
- process.exit(1);
17
- });
14
+ await boot({ killProcess: true, lifecycle });
18
15
  }).catch((error) => {
19
16
  console.error('Failed to start the statistics service:', error);
20
17
  process.exit(1);
package/migrations.ts CHANGED
@@ -1,14 +1,19 @@
1
1
  import backendEnv from '@stamhoofd/backend-env';
2
+ import { BackendApp } from '@stamhoofd/types/BackendApp';
2
3
 
3
4
  process.title = 'stamhoofd-statistics-syncer-migrations';
4
- backendEnv.load({ service: 'statistics-syncer' }).catch((error) => {
5
- console.error('Failed to load environment:', error);
6
- process.exit(1);
7
- }).then(async () => {
8
- const { run } = await import('./src/migrate.js');
9
- await run();
10
- process.exit(0);
11
- }).catch((error) => {
12
- console.error('Failed to run migrations:', error);
13
- process.exit(1);
14
- });
5
+ backendEnv
6
+ .load({ service: BackendApp.StatisticsSyncer })
7
+ .catch((error) => {
8
+ console.error('Failed to load environment:', error);
9
+ process.exit(1);
10
+ })
11
+ .then(async () => {
12
+ const { run } = await import('./src/migrate.js');
13
+ await run();
14
+ process.exit(0);
15
+ })
16
+ .catch((error) => {
17
+ console.error('Failed to run migrations:', error);
18
+ process.exit(1);
19
+ });