forked from alphagov/pay-performance-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackfill.js
43 lines (38 loc) · 1.05 KB
/
backfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const F = require('./functions')
const { PERFORMANCE_PLATFORM_API_KEY, CONNECTOR_URL, DAYS_TO_BACKFILL } = process.env
if (!PERFORMANCE_PLATFORM_API_KEY) {
console.error('PERFORMANCE_PLATFORM_API_KEY not found')
process.exit(1)
}
if (!CONNECTOR_URL) {
console.error('CONNECTOR_URL not found')
process.exit(1)
}
const dateRange = Array
.from(new Array(DAYS_TO_BACKFILL + 1).keys())
.filter(daysAgo => daysAgo > 0)
.map(daysAgo => {
let today = new Date()
today.setDate(today.getDate() - daysAgo)
return today
})
Promise.all(
dateRange
.map(date => {
return F
.retrieveDailyPerformanceStatsForDate(CONNECTOR_URL, date)
.then(report => {
return {date, report}
})
})
).then(datedReports => {
return Promise.all(datedReports.map(datedReport => {
console.info(datedReport.date, datedReport.report)
return F
.sendStatsToPerformancePlatform(
datedReport.date,
datedReport.report,
PERFORMANCE_PLATFORM_API_KEY
)
}))
}).then(results => process.exit(0))