forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-summary.js
More file actions
97 lines (88 loc) · 2.5 KB
/
create-summary.js
File metadata and controls
97 lines (88 loc) · 2.5 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
const createSummary =
({
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 })
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
const summaryCursor = await trx.step(
() => query`
WITH dmarcSummaries
INSERT ${summary} INTO dmarcSummaries
RETURN NEW
`,
)
const summaryDBInfo = await summaryCursor.next()
// create edge
await trx.step(
() => query`
WITH domains, dmarcSummaries, domainsToDmarcSummaries
LET domainId = FIRST(
FOR domain IN domains
FILTER domain.domain == ${domain}
RETURN domain._id
)
INSERT {
_from: domainId,
_to: ${summaryDBInfo._id},
startDate: ${date}
} INTO domainsToDmarcSummaries
`,
)
await trx.commit()
}
module.exports = {
createSummary,
}