@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.
- package/LICENSE.md +7 -7
- package/README.md +1 -1
- package/dist/index.js +5 -8
- package/dist/index.js.map +1 -0
- package/dist/migrations.js +19 -0
- package/dist/migrations.js.map +1 -0
- package/dist/src/boot.js +75 -0
- package/dist/src/boot.js.map +1 -0
- package/dist/src/crons.js +39 -0
- package/dist/src/crons.js.map +1 -0
- package/dist/src/database.js +88 -0
- package/dist/src/database.js.map +1 -0
- package/dist/src/migrate.js +38 -0
- package/dist/src/migrate.js.map +1 -0
- package/dist/src/migrations/1785958573-platform-statistics-schema.sql +382 -0
- package/dist/src/migrations/1786203367-postal-code-coordinates.sql +1203 -0
- package/dist/src/periods.js +86 -0
- package/dist/src/periods.js.map +1 -0
- package/dist/src/rows.js +209 -0
- package/dist/src/rows.js.map +1 -0
- package/dist/src/schema.js +28 -0
- package/dist/src/schema.js.map +1 -0
- package/dist/src/sources.js +10 -0
- package/dist/src/sources.js.map +1 -0
- package/dist/src/sync-state.js +36 -0
- package/dist/src/sync-state.js.map +1 -0
- package/dist/src/sync.js +608 -0
- package/dist/src/sync.js.map +1 -0
- package/dist/src/writer.js +63 -0
- package/dist/src/writer.js.map +1 -0
- package/dist-types/index.d.ts +2 -0
- package/dist-types/index.d.ts.map +1 -0
- package/dist-types/migrations.d.ts +2 -0
- package/dist-types/migrations.d.ts.map +1 -0
- package/dist-types/src/boot.d.ts +8 -0
- package/dist-types/src/boot.d.ts.map +1 -0
- package/dist-types/src/crons.d.ts +21 -0
- package/dist-types/src/crons.d.ts.map +1 -0
- package/dist-types/src/database.d.ts +28 -0
- package/dist-types/src/database.d.ts.map +1 -0
- package/dist-types/src/migrate.d.ts +2 -0
- package/dist-types/src/migrate.d.ts.map +1 -0
- package/dist-types/src/periods.d.ts +51 -0
- package/dist-types/src/periods.d.ts.map +1 -0
- package/dist-types/src/rows.d.ts +212 -0
- package/dist-types/src/rows.d.ts.map +1 -0
- package/dist-types/src/schema.d.ts +9 -0
- package/dist-types/src/schema.d.ts.map +1 -0
- package/dist-types/src/sources.d.ts +10 -0
- package/dist-types/src/sources.d.ts.map +1 -0
- package/dist-types/src/sync-state.d.ts +20 -0
- package/dist-types/src/sync-state.d.ts.map +1 -0
- package/dist-types/src/sync.d.ts +37 -0
- package/dist-types/src/sync.d.ts.map +1 -0
- package/dist-types/src/writer.d.ts +24 -0
- package/dist-types/src/writer.d.ts.map +1 -0
- package/index.ts +5 -8
- package/migrations.ts +16 -11
- package/package.json +37 -23
- package/src/boot.ts +7 -3
- package/src/crons.test.ts +33 -8
- package/src/crons.ts +7 -1
- package/src/database.test.ts +48 -11
- package/src/database.ts +30 -7
- package/src/migrate.ts +1 -1
- package/src/periods.ts +5 -2
- package/src/rows.test.ts +96 -16
- package/src/rows.ts +28 -6
- package/src/schema.test.ts +48 -19
- package/src/schema.ts +14 -4
- package/src/sync-state.test.ts +3 -1
- package/src/sync-state.ts +13 -4
- package/src/sync.test.ts +145 -32
- package/src/sync.ts +422 -91
- package/src/writer.ts +32 -10
- package/tsconfig.build.json +4 -11
- package/tsconfig.json +1 -1
- package/tsconfig.test.json +1 -3
- package/tsconfig.types.json +7 -0
- package/.gitignore +0 -221
- package/tests/vitest.global.setup.ts +0 -29
- package/tests/vitest.setup.ts +0 -24
- package/vitest.config.js +0 -18
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { getStatisticsConnection } from './database.js';
|
|
2
|
+
/**
|
|
3
|
+
* The date up to which this platform's statistics come from an imported external source. Periods
|
|
4
|
+
* ending before it are frozen the first time the sync sees them, so the import owns those years.
|
|
5
|
+
* Unset means the platform has no external history and nothing is frozen up front.
|
|
6
|
+
*/
|
|
7
|
+
export function getImportedUntil() {
|
|
8
|
+
const configured = STAMHOOFD.IMPORTED_UNTIL;
|
|
9
|
+
if (!configured) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
const parsed = new Date(configured);
|
|
13
|
+
if (Number.isNaN(parsed.getTime())) {
|
|
14
|
+
throw new Error(`STAMHOOFD.IMPORTED_UNTIL is not a valid date: ${configured}`);
|
|
15
|
+
}
|
|
16
|
+
return parsed;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* How long after a period ended unlocking it in the administration still brings it back into the
|
|
20
|
+
* sync. A period is unlocked to correct something, and beyond this an old year would have all of its
|
|
21
|
+
* numbers written again from what the administration says today rather than the one thing corrected.
|
|
22
|
+
*/
|
|
23
|
+
export const releaseWindow = 365 * 24 * 60 * 60 * 1000;
|
|
24
|
+
/**
|
|
25
|
+
* Give periods that ended before the imported history a cutoff, once. Only ever fills a cutoff that
|
|
26
|
+
* is still empty, so a date set by hand afterwards stays put.
|
|
27
|
+
*/
|
|
28
|
+
export async function applyImportedCutoff(importedUntil) {
|
|
29
|
+
if (!importedUntil) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const connection = getStatisticsConnection();
|
|
33
|
+
await connection.update('UPDATE `registration_periods` SET `cutoffAt` = ? WHERE `cutoffAt` IS NULL AND `endDate` < ?', [importedUntil, importedUntil]);
|
|
34
|
+
}
|
|
35
|
+
async function loadPeriodIds(where, params) {
|
|
36
|
+
const connection = getStatisticsConnection();
|
|
37
|
+
const [rows] = await connection.select(`SELECT \`id\` FROM \`registration_periods\` WHERE ${where}`, params, { nestTables: false });
|
|
38
|
+
return new Set(rows.map((row) => row.id));
|
|
39
|
+
}
|
|
40
|
+
const settledWhere = '`cutoffAt` IS NOT NULL AND `cutoffAt` <= ?';
|
|
41
|
+
/**
|
|
42
|
+
* The periods left alone entirely, their own row included: the years an import owns and the ones
|
|
43
|
+
* settled by hand. The sync never wrote those rows, so it does not rewrite them either.
|
|
44
|
+
*/
|
|
45
|
+
export function loadSettledPeriodIds() {
|
|
46
|
+
return loadPeriodIds(settledWhere, [new Date()]);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The periods whose statistics are settled. Rows belonging to them are left exactly as they are: not
|
|
50
|
+
* written by the sync, and not excluded by the reconciliation either.
|
|
51
|
+
*
|
|
52
|
+
* Either because their cutoff has passed, or because the administration locked them and a full run
|
|
53
|
+
* has carried in what happened up to that lock.
|
|
54
|
+
*/
|
|
55
|
+
export function loadFrozenPeriodIds() {
|
|
56
|
+
return loadPeriodIds(`(${settledWhere}) OR \`lockedAt\` IS NOT NULL`, [new Date()]);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Stop following the periods the administration has locked.
|
|
60
|
+
*
|
|
61
|
+
* Called once a whole run has come through, incremental pass and delete reconciliation both: the day
|
|
62
|
+
* a period is locked is a day like any other until then, and its changes and its deletions have to be
|
|
63
|
+
* written before it is settled. A run that failed never reaches this, so the next one reads that
|
|
64
|
+
* period again and settles it only once it succeeds.
|
|
65
|
+
*/
|
|
66
|
+
export async function settleLockedPeriods() {
|
|
67
|
+
const connection = getStatisticsConnection();
|
|
68
|
+
await connection.update('UPDATE `registration_periods` SET `lockedAt` = ? WHERE `lockedAt` IS NULL AND `locked` = 1', [new Date()]);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Follow a period the administration unlocked again, as long as it ended less than `releaseWindow`
|
|
72
|
+
* ago. An older year keeps the numbers it was settled with: what its rows would be written from is
|
|
73
|
+
* the administration of today, which no longer describes that year.
|
|
74
|
+
*/
|
|
75
|
+
export async function releaseUnlockedPeriods() {
|
|
76
|
+
const connection = getStatisticsConnection();
|
|
77
|
+
await connection.update('UPDATE `registration_periods` SET `lockedAt` = NULL WHERE `lockedAt` IS NOT NULL AND `locked` = 0 AND `endDate` > ?', [new Date(Date.now() - releaseWindow)]);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* MySQL cannot be handed an empty IN list, and "belongs to no period" is never frozen: a row without
|
|
81
|
+
* a period is not part of any year's settled numbers.
|
|
82
|
+
*/
|
|
83
|
+
export function isFrozen(frozenPeriodIds, periodId) {
|
|
84
|
+
return typeof periodId === 'string' && frozenPeriodIds.has(periodId);
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=periods.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"periods.js","sourceRoot":"","sources":["../../src/periods.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAExD;;;;GAIG;AACH,MAAM,UAAU,gBAAgB;IAC5B,MAAM,UAAU,GAAG,SAAS,CAAC,cAAc,CAAC;IAC5C,IAAI,CAAC,UAAU,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC;IACpC,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,iDAAiD,UAAU,EAAE,CAAC,CAAC;IACnF,CAAC;IAED,OAAO,MAAM,CAAC;AAClB,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAEvD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,aAA0B;IAChE,IAAI,CAAC,aAAa,EAAE,CAAC;QACjB,OAAO;IACX,CAAC;IAED,MAAM,UAAU,GAAG,uBAAuB,EAAE,CAAC;IAC7C,MAAM,UAAU,CAAC,MAAM,CACnB,6FAA6F,EAC7F,CAAC,aAAa,EAAE,aAAa,CAAC,CACjC,CAAC;AACN,CAAC;AAED,KAAK,UAAU,aAAa,CAAC,KAAa,EAAE,MAAiB;IACzD,MAAM,UAAU,GAAG,uBAAuB,EAAE,CAAC;IAC7C,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,UAAU,CAAC,MAAM,CAClC,qDAAqD,KAAK,EAAE,EAC5D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* The language translated names are resolved in. The statistics database holds one name per group,
|
|
4
|
+
* and the sync has no request to take a language from, so it picks the platform's own rather than
|
|
5
|
+
* whatever language happens to be set on the process.
|
|
6
|
+
*/
|
|
7
|
+
export const statisticsLanguage = 'nl';
|
|
8
|
+
/**
|
|
9
|
+
* The label the reports show for a period, mirroring `RegistrationPeriodBase.nameShort`: the custom
|
|
10
|
+
* name when there is one, otherwise the years it spans.
|
|
11
|
+
*
|
|
12
|
+
* Reimplemented here rather than read off the model because the model's getter runs `$t`, which needs
|
|
13
|
+
* a request to take a language from and throws in the sync. The years are resolved in the application
|
|
14
|
+
* timezone, so a period starting at midnight in Brussels does not fall back into the previous year.
|
|
15
|
+
*/
|
|
16
|
+
export function periodName(period) {
|
|
17
|
+
const customName = period.customName?.trim();
|
|
18
|
+
if (customName) {
|
|
19
|
+
return customName;
|
|
20
|
+
}
|
|
21
|
+
const startYear = Formatter.year(period.startDate);
|
|
22
|
+
const endYear = Formatter.year(period.endDate);
|
|
23
|
+
return startYear === endYear ? `${startYear}` : `${startYear} - ${endYear}`;
|
|
24
|
+
}
|
|
25
|
+
export function flattenRegistrationPeriod(period) {
|
|
26
|
+
return {
|
|
27
|
+
id: period.id,
|
|
28
|
+
startDate: period.startDate,
|
|
29
|
+
endDate: period.endDate,
|
|
30
|
+
locked: period.locked,
|
|
31
|
+
organizationId: period.organizationId,
|
|
32
|
+
previousPeriodId: period.previousPeriodId,
|
|
33
|
+
nextPeriodId: period.nextPeriodId,
|
|
34
|
+
customName: period.customName,
|
|
35
|
+
name: periodName(period),
|
|
36
|
+
createdAt: period.createdAt,
|
|
37
|
+
updatedAt: period.updatedAt,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* One row per unit per period: the unit as it was that year. A unit that is renamed or moves keeps
|
|
42
|
+
* the name and the place it was counted under in the years already settled.
|
|
43
|
+
*
|
|
44
|
+
* `periodId` is the year this row describes, not the period the unit is in now — the latter lives on
|
|
45
|
+
* the source record and is what the netwerk links are recorded against.
|
|
46
|
+
*
|
|
47
|
+
* Only the postal code and city of the address survive: the report maps units by postal code and
|
|
48
|
+
* lists them by city. An organization is a legal entity rather than a natural person.
|
|
49
|
+
*/
|
|
50
|
+
export function flattenOrganization(organization, periodId) {
|
|
51
|
+
return {
|
|
52
|
+
id: organization.id,
|
|
53
|
+
periodId,
|
|
54
|
+
name: organization.name,
|
|
55
|
+
uri: organization.uri,
|
|
56
|
+
postalCode: emptyToNull(organization.address.postalCode),
|
|
57
|
+
city: emptyToNull(organization.address.city),
|
|
58
|
+
active: organization.active,
|
|
59
|
+
createdAt: organization.createdAt,
|
|
60
|
+
updatedAt: organization.updatedAt,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export function flattenGroup(group) {
|
|
64
|
+
return {
|
|
65
|
+
id: group.id,
|
|
66
|
+
type: group.type,
|
|
67
|
+
name: group.settings.name.get(statisticsLanguage),
|
|
68
|
+
organizationId: group.organizationId,
|
|
69
|
+
periodId: group.periodId,
|
|
70
|
+
defaultAgeGroupId: group.defaultAgeGroupId,
|
|
71
|
+
status: group.status,
|
|
72
|
+
deletedAt: group.deletedAt,
|
|
73
|
+
createdAt: group.createdAt,
|
|
74
|
+
updatedAt: group.updatedAt,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* One row per member per period: what was true of this member in that year. The source holds only
|
|
79
|
+
* what is true of them now, so every open period gets the current details and a frozen one keeps the
|
|
80
|
+
* row it was counted with.
|
|
81
|
+
*
|
|
82
|
+
* The date of birth and the postal code are carried over because the report needs them: it charts
|
|
83
|
+
* members by age, which is only exact with the date behind it, and maps them by postal code. The
|
|
84
|
+
* rest of the address and everything that names or reaches the member stays behind.
|
|
85
|
+
*
|
|
86
|
+
* The birth date is written as the calendar date the application means, not as an instant. A member
|
|
87
|
+
* born on 17 May is held as midnight in Europe/Brussels, which a process running in UTC would write
|
|
88
|
+
* to a date column as 16 May — a day early for every member.
|
|
89
|
+
*/
|
|
90
|
+
export function flattenMember(member, periodId) {
|
|
91
|
+
return {
|
|
92
|
+
id: member.id,
|
|
93
|
+
periodId,
|
|
94
|
+
birthDate: member.details.birthDay ? Formatter.dateIso(member.details.birthDay) : null,
|
|
95
|
+
gender: member.details.gender,
|
|
96
|
+
postalCode: member.details.address ? emptyToNull(member.details.address.postalCode) : null,
|
|
97
|
+
organizationId: member.organizationId,
|
|
98
|
+
createdAt: member.createdAt,
|
|
99
|
+
updatedAt: member.updatedAt,
|
|
100
|
+
lastRegisteredAt: member.lastRegisteredAt,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export function flattenRegistration(registration) {
|
|
104
|
+
return {
|
|
105
|
+
id: registration.id,
|
|
106
|
+
organizationId: registration.organizationId,
|
|
107
|
+
memberId: registration.memberId,
|
|
108
|
+
groupId: registration.groupId,
|
|
109
|
+
periodId: registration.periodId,
|
|
110
|
+
registeredAt: registration.registeredAt,
|
|
111
|
+
startDate: registration.startDate,
|
|
112
|
+
endDate: registration.endDate,
|
|
113
|
+
trialUntil: registration.trialUntil,
|
|
114
|
+
deactivatedAt: registration.deactivatedAt,
|
|
115
|
+
waitingList: registration.waitingList,
|
|
116
|
+
cycle: registration.cycle,
|
|
117
|
+
createdAt: registration.createdAt,
|
|
118
|
+
updatedAt: registration.updatedAt,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* `reducedPrice` is not on the source record: the price of a lidgeld is worked out from whether the
|
|
123
|
+
* member has financiële ondersteuning or an active UITPAS and only the amount is kept, so the caller
|
|
124
|
+
* passes that same status here. It is what the reports split the lidgelden by.
|
|
125
|
+
*/
|
|
126
|
+
export function flattenMembership(membership, reducedPrice) {
|
|
127
|
+
return {
|
|
128
|
+
id: membership.id,
|
|
129
|
+
memberId: membership.memberId,
|
|
130
|
+
membershipTypeId: membership.membershipTypeId,
|
|
131
|
+
reducedPrice,
|
|
132
|
+
organizationId: membership.organizationId,
|
|
133
|
+
periodId: membership.periodId,
|
|
134
|
+
startDate: membership.startDate,
|
|
135
|
+
endDate: membership.endDate,
|
|
136
|
+
expireDate: membership.expireDate,
|
|
137
|
+
trialUntil: membership.trialUntil,
|
|
138
|
+
deletedAt: membership.deletedAt,
|
|
139
|
+
createdAt: membership.createdAt,
|
|
140
|
+
updatedAt: membership.updatedAt,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* One row per period the record runs through: it carries a date range rather than a year, and a
|
|
145
|
+
* functie held for three years was held in each of them.
|
|
146
|
+
*/
|
|
147
|
+
export function flattenResponsibilityRecord(record, periodId) {
|
|
148
|
+
return {
|
|
149
|
+
id: record.id,
|
|
150
|
+
periodId,
|
|
151
|
+
memberId: record.memberId,
|
|
152
|
+
groupId: record.groupId,
|
|
153
|
+
organizationId: record.organizationId,
|
|
154
|
+
responsibilityId: record.responsibilityId,
|
|
155
|
+
startDate: record.startDate,
|
|
156
|
+
endDate: record.endDate,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The platform configuration holds one name per tak, netwerk, lidgeldtype and functie: the one it
|
|
161
|
+
* carries today. Written per period, so renaming one changes what the years still open are reported
|
|
162
|
+
* under and leaves the settled ones saying what they said.
|
|
163
|
+
*/
|
|
164
|
+
export function flattenNamedConfig(config, periodId) {
|
|
165
|
+
return { id: config.id, periodId, name: config.name };
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The platform itself, which the reports need for two things: which organization it runs on its own,
|
|
169
|
+
* and what it calls its verlaagd lidgeld. The first is the koepel rather than one of its local
|
|
170
|
+
* groups, which the jeugdbewegingen report delivers as the bovenlokale ondersteuningsstructuur.
|
|
171
|
+
*
|
|
172
|
+
* Not written per period, so the name here is the one the platform carries today. Nothing is reported
|
|
173
|
+
* per year out of it -- the name a sheet prints is the organization's own, per period.
|
|
174
|
+
*
|
|
175
|
+
* `reducedPriceName` is the label the platform gives its lower lidgeld, which the reports print
|
|
176
|
+
* beside the standaardtarief. Null when the platform names none, and the report says it in words of
|
|
177
|
+
* its own rather than inventing a name here.
|
|
178
|
+
*/
|
|
179
|
+
export function flattenPlatform(platform) {
|
|
180
|
+
return {
|
|
181
|
+
id: platform.id,
|
|
182
|
+
name: platform.config.name,
|
|
183
|
+
membershipOrganizationId: platform.membershipOrganizationId,
|
|
184
|
+
reducedPriceName: platform.config.financialSupport?.priceName ?? null,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The label a tak carries, mirroring `DefaultAgeGroup.name`: its names joined, the last two with
|
|
189
|
+
* "of".
|
|
190
|
+
*
|
|
191
|
+
* Reimplemented here rather than read off the structure because the structure's getter runs `$t` on
|
|
192
|
+
* the separator, which resolves to the translation key in the sync.
|
|
193
|
+
*/
|
|
194
|
+
export function defaultAgeGroupName(names) {
|
|
195
|
+
return Formatter.joinLast(names, ', ', ' of ');
|
|
196
|
+
}
|
|
197
|
+
export function flattenDefaultAgeGroup(group, periodId) {
|
|
198
|
+
return {
|
|
199
|
+
id: group.id,
|
|
200
|
+
periodId,
|
|
201
|
+
name: defaultAgeGroupName(group.names),
|
|
202
|
+
minAge: group.minAge,
|
|
203
|
+
maxAge: group.maxAge,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function emptyToNull(value) {
|
|
207
|
+
return value.length === 0 ? null : value;
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=rows.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rows.js","sourceRoot":"","sources":["../../src/rows.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAgB/C;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAkGvC;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAC,MAI1B;IACG,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,EAAE,IAAI,EAA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9B,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { DatabaseInstance, Migration } from '@stamhoofd/node-database';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { getStatisticsConnection, getStatisticsDatabaseConfig, getStatisticsPoolOptions, } from './database.js';
|
|
4
|
+
export function getStatisticsMigrationsPath() {
|
|
5
|
+
return path.join(import.meta.dirname, 'migrations');
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Run the migrations of the platform statistics database.
|
|
9
|
+
*
|
|
10
|
+
* That database keeps its own migration history, in its own `migrations` table, so it can be moved
|
|
11
|
+
* to another server without dragging the main database along.
|
|
12
|
+
*/
|
|
13
|
+
export async function runStatisticsMigrations() {
|
|
14
|
+
const database = getStatisticsDatabaseConfig().DB_DATABASE;
|
|
15
|
+
const globalDatabase = new DatabaseInstance({ ...getStatisticsPoolOptions(), database: null });
|
|
16
|
+
try {
|
|
17
|
+
await globalDatabase.statement(`CREATE DATABASE IF NOT EXISTS ${globalDatabase.escapeId(database)} DEFAULT CHARACTER SET = \`utf8mb4\` DEFAULT COLLATE = \`utf8mb4_0900_ai_ci\``);
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
await globalDatabase.end();
|
|
21
|
+
}
|
|
22
|
+
if (!(await Migration.runAll(getStatisticsMigrationsPath(), {
|
|
23
|
+
database: getStatisticsConnection(),
|
|
24
|
+
}))) {
|
|
25
|
+
throw new Error('Platform statistics migrations failed');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACH,uBAAuB,EACvB,2BAA2B,EAC3B,wBAAwB,GAC3B,MAAM,eAAe,CAAC;AAEvB,MAAM,UAAU,2BAA2B;IACvC,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;AACxD,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB;IACzC,MAAM,QAAQ,GAAG,2BAA2B,EAAE,CAAC,WAAW,CAAC;IAC3D,MAAM,cAAc,GAAG,IAAI,gBAAgB,CAAC,EAAE,GAAG,wBAAwB,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAE/F,IAAI,CAAC;QACD,MAAM,cAAc,CAAC,SAAS,CAC1B,iCAAiC,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,+EAA+E,CACpJ,CAAC;IACN,CAAC;YAAS,CAAC;QACP,MAAM,cAAc,CAAC,GAAG,EAAE,CAAC;IAC/B,CAAC;IAED,IACI,CAAC,CAAC,MAAM,SAAS,CAAC,MAAM,CAAC,2BAA2B,EAAE,EAAE;QACpD,QAAQ,EAAE,uBAAuB,EAAE;KACtC,CAAC,CAAC,EACL,CAAC;QACC,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC7D,CAAC;AACL,CAAC"}
|
|
@@ -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 const syncSource = 'sync';
|
|
9
|
+
export const importSource = 'import';
|
|
10
|
+
//# sourceMappingURL=sources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sources.js","sourceRoot":"","sources":["../../src/sources.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC;AACjC,MAAM,CAAC,MAAM,YAAY,GAAG,QAAQ,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { getStatisticsConnection } from './database.js';
|
|
2
|
+
export const syncStateTable = 'stats_sync_state';
|
|
3
|
+
/**
|
|
4
|
+
* The watermark to store after a run: the highest `updatedAt` that was seen, but never beyond the
|
|
5
|
+
* moment the run started.
|
|
6
|
+
*
|
|
7
|
+
* Source rows can carry an `updatedAt` in the future — periods in this database do. Following one
|
|
8
|
+
* would park the watermark weeks ahead, and every row written in the meantime would fall below it and
|
|
9
|
+
* be skipped. Capping costs one row that is read again on every run until the clock catches up, and
|
|
10
|
+
* repairs a watermark that a previous run already pushed into the future.
|
|
11
|
+
*/
|
|
12
|
+
export function nextWatermark(previous, seen, runStartedAt) {
|
|
13
|
+
let watermark = previous;
|
|
14
|
+
for (const date of seen) {
|
|
15
|
+
if (!watermark || date > watermark) {
|
|
16
|
+
watermark = date;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return watermark && watermark > runStartedAt ? runStartedAt : watermark;
|
|
20
|
+
}
|
|
21
|
+
export async function readSyncState(tableName) {
|
|
22
|
+
const connection = getStatisticsConnection();
|
|
23
|
+
const [rows] = await connection.select(`SELECT \`tableName\`, \`watermark\`, \`lastSucceededAt\`, \`lastReconciledAt\` FROM ${connection.escapeId(syncStateTable)} WHERE \`tableName\` = ?`, [tableName], { nestTables: false });
|
|
24
|
+
const row = rows[0];
|
|
25
|
+
return row ?? { tableName, watermark: null, lastSucceededAt: null, lastReconciledAt: null };
|
|
26
|
+
}
|
|
27
|
+
export async function writeSyncState(tableName, state) {
|
|
28
|
+
const connection = getStatisticsConnection();
|
|
29
|
+
const columns = ['tableName', 'updatedAt', ...Object.keys(state)];
|
|
30
|
+
const values = [tableName, new Date(), ...Object.values(state)];
|
|
31
|
+
const assignments = columns
|
|
32
|
+
.filter((column) => column !== 'tableName')
|
|
33
|
+
.map((column) => `${connection.escapeId(column)} = new.${connection.escapeId(column)}`);
|
|
34
|
+
await connection.insert(`INSERT INTO ${connection.escapeId(syncStateTable)} (${columns.map((column) => connection.escapeId(column)).join(', ')}) VALUES (?) AS new ON DUPLICATE KEY UPDATE ${assignments.join(', ')}`, [values]);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=sync-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync-state.js","sourceRoot":"","sources":["../../src/sync-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAExD,MAAM,CAAC,MAAM,cAAc,GAAG,kBAAkB,CAAC;AASjD;;;;;;;;GAQG;AACH,MAAM,UAAU,aAAa,CACzB,QAAqB,EACrB,IAAY,EACZ,YAAkB;IAElB,IAAI,SAAS,GAAG,QAAQ,CAAC;IAEzB,KAAK,MAAM,IAAI,IAAI,IAAI,EAAE,CAAC;QACtB,IAAI,CAAC,SAAS,IAAI,IAAI,GAAG,SAAS,EAAE,CAAC;YACjC,SAAS,GAAG,IAAI,CAAC;QACrB,CAAC;IACL,CAAC;IAED,OAAO,SAAS,IAAI,SAAS,GAAG,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC;AAC5E,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,SAAiB;IACjD,MAAM,UAAU,GAAG,uBAAuB,EAAE,CAAC;IAC7C,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,UAAU,CAAC,MAAM,CAClC,uFAAuF,UAAU,CAAC,QAAQ,CAAC,cAAc,CAAC,0BAA0B,EACpJ,CAAC,SAAS,CAAC,EACX,EAAE,UAAU,EAAE,KAAK,EAAE,CACxB,CAAC;IACF,MAAM,GAAG,GAAI,IAA+B,CAA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