Skip to content

Commit 41f3259

Browse files
authored
Merge pull request #1738 from Mephistic/logs-for-backfill
Adding logs for nextDigestAt backfill
2 parents e42d31d + 935407c commit 41f3259

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

scripts/firebase-admin/backfillNextDigestAt.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,58 @@
1111
import { getNextDigestAt } from "../../functions/src/notifications/helpers"
1212
import { Profile } from "../../components/db/profile/types"
1313
import { Script } from "./types"
14-
import { Boolean, Record } from "runtypes"
14+
import { Boolean, Optional, Record } from "runtypes"
1515

1616
const Args = Record({
17-
dryRun: Boolean
17+
dryRun: Optional(Boolean)
1818
})
1919

2020
export const script: Script = async ({ db, args }) => {
21-
const profilesSnapshot = await db.collection("profiles").get()
21+
const { dryRun } = Args.check(args)
22+
23+
let numProfiles = 0,
24+
numMissingFrequency = 0,
25+
numWithNextDigestAt = 0,
26+
numNeedingNextDigestAt = 0
2227

28+
// There are only ~500 profiles at the time of writing
29+
// so the bulkWriter is unnecessary
30+
const profilesSnapshot = await db.collection("profiles").get()
2331
console.log(profilesSnapshot.docs.length, "profiles found")
2432

2533
const updatePromises = profilesSnapshot.docs.map(async profileDoc => {
2634
const profile = profileDoc.data() as Profile
35+
numProfiles++
2736

2837
if (profile.notificationFrequency) {
38+
const hasNextDigestAt = !!profile.nextDigestAt
39+
if (hasNextDigestAt) {
40+
numWithNextDigestAt++
41+
} else {
42+
numNeedingNextDigestAt++
43+
}
44+
2945
const nextDigestAt = getNextDigestAt(profile.notificationFrequency)
3046

31-
if (!args.dryRun) {
47+
if (!dryRun) {
3248
await profileDoc.ref.update({ nextDigestAt })
3349
}
3450
console.log(
3551
`Updated nextDigestAt for ${profileDoc.id} to ${nextDigestAt?.toDate()}`
3652
)
3753
} else {
54+
numMissingFrequency++
3855
console.log(
3956
`Profile ${profileDoc.id} does not have notificationFrequency - skipping`
4057
)
4158
}
4259
})
4360

4461
await Promise.all(updatePromises)
62+
63+
console.log("Num profiles:", numProfiles)
64+
console.log("Num missing frequency:", numMissingFrequency)
65+
console.log("Num with nextDigestAt:", numWithNextDigestAt)
66+
console.log("Num needing nextDigestAt:", numNeedingNextDigestAt)
4567
console.log("Backfill complete")
4668
}

0 commit comments

Comments
 (0)