-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreports_dynamic.js
More file actions
107 lines (100 loc) · 2.69 KB
/
reports_dynamic.js
File metadata and controls
107 lines (100 loc) · 2.69 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
const configs = new reports.HTTPArchiveReports()
const metrics = configs.listMetrics()
const bucket = 'httparchive'
const storagePath = '/reports/'
function generateExportQuery (metric, sql, params, ctx) {
let query = ''
if (sql.type === 'histogram') {
query = `
SELECT
* EXCEPT(date)
FROM ${ctx.self()}
WHERE date = '${params.date}'
`
} else if (sql.type === 'timeseries') {
query = `
SELECT
FORMAT_DATE('%Y_%m_%d', date) AS date,
* EXCEPT(date)
FROM ${ctx.self()}
`
} else {
throw new Error('Unknown SQL type')
}
const queryOutput = query.replace(/[\r\n]+/g, ' ')
return queryOutput
}
function generateExportPath (metric, sql, params) {
if (sql.type === 'histogram') {
return `${storagePath}${params.date.replaceAll('-', '_')}/${metric.id}.json`
} else if (sql.type === 'timeseries') {
return `${storagePath}${metric.id}.json`
} else {
throw new Error('Unknown SQL type')
}
}
const iterations = []
for (
let date = constants.currentMonth; date >= constants.currentMonth; date = constants.fnPastMonth(date)) {
iterations.push({
date,
devRankFilter: constants.devRankFilter
})
}
if (iterations.length === 1) {
const params = iterations[0]
metrics.forEach(metric => {
metric.SQL.forEach(sql => {
publish(metric.id + '_' + sql.type, {
type: 'incremental',
protected: true,
bigquery: sql.type === 'histogram' ? { partitionBy: 'date', clusterBy: ['client'] } : {},
schema: 'reports'
// tags: ['crawl_complete', 'http_reports']
}).preOps(ctx => `
--DELETE FROM ${ctx.self()}
--WHERE date = '${params.date}';
`).query(
ctx => sql.query(ctx, params)
).postOps(ctx => `
SELECT
reports.run_export_job(
JSON '''{
"destination": "cloud_storage",
"config": {
"bucket": "${bucket}",
"name": "${generateExportPath(metric, sql, params)}"
},
"query": "${generateExportQuery(metric, sql, params, ctx)}"
}'''
);
`)
})
})
} else {
iterations.forEach((params, i) => {
metrics.forEach(metric => {
metric.SQL.forEach(sql => {
operate(metric.id + '_' + sql.type + '_' + params.date, {
// tags: ['crawl_complete']
}).queries(ctx => `
DELETE FROM reports.${metric.id}_${sql.type}
WHERE date = '${params.date}';
INSERT INTO reports.${metric.id}_${sql.type}` + sql.query(ctx, params)
).postOps(ctx => `
SELECT
reports.run_export_job(
JSON '''{
"destination": "cloud_storage",
"config": {
"bucket": "${bucket}",
"name": "${generateExportPath(metric, sql, params)}"
},
"query": "${generateExportQuery(metric, sql, params, ctx)}"
}'''
);
`)
})
})
})
}