forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-https-connections-by-domain-id.js
More file actions
128 lines (113 loc) · 3.58 KB
/
Copy pathload-https-connections-by-domain-id.js
File metadata and controls
128 lines (113 loc) · 3.58 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
122
123
124
125
126
127
128
const { aql } = require('arangojs')
const { fromGlobalId, toGlobalId } = require('graphql-relay')
const { t } = require('@lingui/macro')
const httpsLoaderConnectionsByDomainId = (
query,
userId,
cleanseInput,
i18n,
) => async ({ domainId, startDate, endDate, after, before, first, last }) => {
let afterTemplate = aql``
let beforeTemplate = aql``
let startDateTemplate = aql``
let endDateTemplate = aql``
let limitTemplate = aql``
if (typeof after !== 'undefined') {
const { id: afterId } = fromGlobalId(cleanseInput(after))
afterTemplate = aql`FILTER TO_NUMBER(httpsScan._key) > TO_NUMBER(${afterId})`
}
if (typeof before !== 'undefined') {
const { id: beforeId } = fromGlobalId(cleanseInput(before))
beforeTemplate = aql`FILTER TO_NUMBER(httpsScan._key) < TO_NUMBER(${beforeId})`
}
if (typeof startDate !== 'undefined') {
startDateTemplate = aql`FILTER httpsScan.timestamp >= ${startDate}`
}
if (typeof endDate !== 'undefined') {
endDateTemplate = aql`FILTER httpsScan.timestamp <= ${endDate}`
}
if (typeof first !== 'undefined' && typeof last === 'undefined') {
limitTemplate = aql`SORT httpsScan._key ASC LIMIT TO_NUMBER(${first + 1})`
} else if (typeof first === 'undefined' && typeof last !== 'undefined') {
limitTemplate = aql`SORT httpsScan._key DESC LIMIT TO_NUMBER(${last + 1})`
} else if (typeof first !== 'undefined' && typeof last !== 'undefined') {
console.warn(
`User: ${userId} had first and last arguments set when trying to gather https scans for domain: ${domainId}`,
)
throw new Error(
i18n._(
t`Unable to have both first, and last arguments set at the same time.`,
),
)
}
let httpsScanCursor
try {
httpsScanCursor = await query`
LET httpsIds = (FOR v, e IN 1 ANY ${domainId} domainsHTTPS RETURN e._to)
FOR httpsScan IN https
FILTER httpsScan._id IN httpsIds
${afterTemplate}
${beforeTemplate}
${startDateTemplate}
${endDateTemplate}
${limitTemplate}
RETURN httpsScan
`
} catch (err) {
console.error(
`Database error occurred while user: ${userId} was trying to get https information for ${domainId}, error: ${err}`,
)
throw new Error(i18n._(t`Unable to load https scans. Please try again.`))
}
let httpsScans
try {
httpsScans = await httpsScanCursor.all()
} catch (err) {
console.error(
`Cursor error occurred while user: ${userId} was trying to get https information for ${domainId}, error: ${err}`,
)
throw new Error(i18n._(t`Unable to load https scans. Please try again.`))
}
const hasNextPage = !!(
typeof first !== 'undefined' && httpsScans.length > first
)
const hasPreviousPage = !!(
typeof last !== 'undefined' && httpsScans.length > last
)
if (httpsScans.length > first || httpsScans.length > last) {
httpsScans.pop()
}
const edges = httpsScans.map((httpsScan) => {
httpsScan.id = httpsScan._key
httpsScan.domainId = domainId
return {
cursor: toGlobalId('https', httpsScan._key),
node: httpsScan,
}
})
if (edges.length === 0) {
return {
edges: [],
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
startCursor: '',
endCursor: '',
},
}
}
const startCursor = toGlobalId('https', httpsScans[0]._key)
const endCursor = toGlobalId('https', httpsScans[httpsScans.length - 1]._key)
return {
edges,
pageInfo: {
hasNextPage,
hasPreviousPage,
startCursor,
endCursor,
},
}
}
module.exports = {
httpsLoaderConnectionsByDomainId,
}