forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupsert-summary.js
More file actions
121 lines (112 loc) · 3.56 KB
/
upsert-summary.js
File metadata and controls
121 lines (112 loc) · 3.56 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const upsertSummary =
({
transaction,
collections,
query,
loadCategoryTotals,
loadDkimFailureTable,
loadDmarcFailureTable,
loadFullPassTable,
loadSpfFailureTable,
calculatePercentages,
}) =>
async ({ date, domain }) => {
let categoryTotals
let dkimFailureTable
let dmarcFailureTable
let fullPassTable
let spfFailureTable
if (date === 'thirtyDays') {
categoryTotals = await loadCategoryTotals({ domain, date: 'thirty_days' })
dkimFailureTable = await loadDkimFailureTable({
domain,
date: 'thirty_days',
})
dmarcFailureTable = await loadDmarcFailureTable({
domain,
date: 'thirty_days',
})
fullPassTable = await loadFullPassTable({ domain, date: 'thirty_days' })
spfFailureTable = await loadSpfFailureTable({
domain,
date: 'thirty_days',
})
} else {
categoryTotals = await loadCategoryTotals({ domain, date })
dkimFailureTable = await loadDkimFailureTable({ domain, date })
dmarcFailureTable = await loadDmarcFailureTable({ domain, date })
fullPassTable = await loadFullPassTable({ domain, date })
spfFailureTable = await loadSpfFailureTable({ domain, date })
}
const categoryPercentages = calculatePercentages({ ...categoryTotals })
// get current summary info
const edgeCursor = await query`
WITH domains, dmarcSummaries, domainsToDmarcSummaries
LET domainId = FIRST(
FOR domain IN domains
FILTER domain.domain == ${domain}
RETURN domain._id
)
FOR item IN domainsToDmarcSummaries
FILTER item._from == domainId
FILTER item.startDate == ${date}
RETURN item._to
`
const summaryId = await edgeCursor.next()
const summary = {
...categoryPercentages,
categoryTotals,
detailTables: {
dkimFailure: dkimFailureTable,
dmarcFailure: dmarcFailureTable,
fullPass: fullPassTable,
spfFailure: spfFailureTable,
},
}
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// setup Transaction
const trx = await transaction(collectionStrings)
// create summary
await trx.step(
() => query`
WITH dmarcSummaries
FOR summary IN dmarcSummaries
FILTER summary._key == PARSE_IDENTIFIER(${summaryId}).key
UPSERT { _key: summary._key }
INSERT ${summary}
UPDATE {
categoryPercentages: ${summary.categoryPercentages},
categoryTotals: ${summary.categoryTotals},
detailTables: {
dkimFailure: UNION_DISTINCT(
summary.detailTables.dkimFailure,
${summary.detailTables.dkimFailure}
),
dmarcFailure: UNION_DISTINCT(
summary.detailTables.dmarcFailure,
${summary.detailTables.dmarcFailure}
),
fullPass: UNION_DISTINCT(
summary.detailTables.fullPass,
${summary.detailTables.fullPass}
),
spfFailure: UNION_DISTINCT(
summary.detailTables.spfFailure,
${summary.detailTables.spfFailure}
),
},
totalMessages: ${summary.totalMessages},
}
IN dmarcSummaries
RETURN NEW
`,
)
await trx.commit()
}
module.exports = {
upsertSummary,
}