Skip to content

Commit 7b8047e

Browse files
committed
sync storage export
1 parent 1996661 commit 7b8047e

9 files changed

Lines changed: 466 additions & 204 deletions

File tree

infra/bigquery-export/index.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
1-
import { StorageUpload } from './storage.js'
21
import { FirestoreBatch } from './firestore.js'
32

43
async function main () {
54
const { query, destination, config } = process.env.EXPORT_CONFIG && JSON.parse(process.env.EXPORT_CONFIG)
6-
if (!destination) {
7-
throw new Error('No destination found')
8-
}
9-
10-
if (destination === 'cloud_storage') {
11-
console.info('Cloud Storage export')
12-
console.log(query, config)
135

14-
const storage = new StorageUpload(config.bucket)
15-
await storage.exportToJson(query, config.name)
16-
} else if (destination === 'firestore') {
17-
console.info('Firestore export')
6+
if (destination === 'firestore') {
187
console.log(query, config)
198

209
const firestore = new FirestoreBatch()
2110
await firestore.export(query, config)
22-
} else {
23-
throw new Error('Bad Request: destination unknown')
2411
}
12+
2513
console.info('Export finished successfully')
2614
return 'OK'
2715
}

infra/bigquery-export/package-lock.json

Lines changed: 1 addition & 135 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infra/bigquery-export/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"type": "module",
1010
"dependencies": {
1111
"@google-cloud/bigquery": "8.1.1",
12-
"@google-cloud/firestore": "7.11.3",
13-
"@google-cloud/storage": "7.16.0"
12+
"@google-cloud/firestore": "7.11.3"
1413
},
1514
"author": "@max-ostapenko"
1615
}

infra/dataform-service/bigquery.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { BigQuery } from '@google-cloud/bigquery'
2+
3+
export class BigQueryExport {
4+
constructor (options = {}) {
5+
this.bigquery = new BigQuery(options)
6+
}
7+
8+
async queryResults (query) {
9+
const options = {
10+
query,
11+
projectId: this.projectId,
12+
location: this.location
13+
}
14+
15+
const [job] = await this.bigquery.createQueryJob(options)
16+
console.info(`Running BigQuery query: ${job.id}`)
17+
const [rows] = await job.getQueryResults()
18+
console.log('Fetching query results completed')
19+
return rows
20+
}
21+
22+
async queryResultsStream (query) {
23+
const options = {
24+
query,
25+
projectId: this.projectId,
26+
location: this.location
27+
}
28+
29+
const [job] = await this.bigquery.createQueryJob(options)
30+
console.info(`Running BigQuery query: ${job.id}`)
31+
const rows = job.getQueryResultsStream()
32+
return rows
33+
}
34+
}

infra/dataform-service/index.js

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import functions from '@google-cloud/functions-framework'
2-
import { BigQuery } from '@google-cloud/bigquery'
32

3+
import { BigQueryExport } from './bigquery.js'
44
import { callRunJob } from './cloud_run.js'
55
import { getCompilationResults, runWorkflow } from './dataform.js'
6+
import { StorageUpload } from './storage.js'
67

78
const projectId = 'httparchive'
89
const location = 'us-central1'
910
const jobId = 'bigquery-export'
1011

12+
const bigquery = new BigQueryExport({
13+
projectId,
14+
location: 'US'
15+
})
16+
1117
const TRIGGERS = {
1218
crux_ready: {
1319
type: 'poller',
@@ -83,8 +89,22 @@ async function handleExport (req, res) {
8389
return
8490
}
8591

86-
const jobName = `projects/${projectId}/locations/${location}/jobs/${jobId}`
87-
await callRunJob(jobName, payload)
92+
const { query, destination, config } = payload
93+
94+
if (destination === 'cloud_storage') {
95+
console.info('Cloud Storage export')
96+
console.log(query, config)
97+
98+
const data = await bigquery.queryResults(query)
99+
const storage = new StorageUpload(config.bucket)
100+
await storage.exportToJson(data, config.name)
101+
} else if (destination === 'firestore') {
102+
console.info('Firestore export')
103+
const jobName = `projects/${projectId}/locations/${location}/jobs/${jobId}`
104+
await callRunJob(jobName, payload)
105+
} else {
106+
throw new Error('Bad Request: destination unknown')
107+
}
88108

89109
res.status(200).json({
90110
replies: [200],
@@ -136,7 +156,9 @@ async function handleTrigger (req, res) {
136156
const trigger = TRIGGERS[eventName]
137157
if (trigger.type === 'poller') {
138158
console.info(`Poller action ${eventName}`)
139-
const result = await runQuery(trigger.query)
159+
160+
const rows = await bigquery.queryResults(trigger.query)
161+
const result = rows.length > 0 && rows[0][Object.keys(rows[0])[0]] === true
140162
console.info(`Query result: ${result}`)
141163
if (result) {
142164
await executeAction(trigger.action, trigger.actionArgs)
@@ -160,22 +182,6 @@ async function handleTrigger (req, res) {
160182
}
161183
}
162184

163-
/**
164-
* Run BigQuery poll query.
165-
*
166-
* @param {string} query Polling query.
167-
* @returns {boolean} Query result.
168-
*/
169-
async function runQuery (query) {
170-
const bigquery = new BigQuery()
171-
172-
const [job] = await bigquery.createQueryJob({ query })
173-
console.info(`Query job ${job.id} started.`)
174-
175-
const [rows] = await job.getQueryResults()
176-
return rows.length > 0 && rows[0][Object.keys(rows[0])[0]] === true
177-
}
178-
179185
/**
180186
* Execute action based on the trigger configuration.
181187
*

0 commit comments

Comments
 (0)