-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedtest.js
37 lines (31 loc) · 1.04 KB
/
speedtest.js
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
const BigQuery = require('@google-cloud/bigquery');
const projectId = 'mrh-cloud-101-workshop';
const datasetId = 'speedTest';
const tableId = 'test_results';
async function writeEventToBQ(receivedEvent) {
const event = {...receivedEvent, timestamp: new Date(receivedEvent.timestamp)};
const bigquery = new BigQuery({
projectId: projectId,
});
console.log('About to insert: ', event);
await bigquery
.dataset(datasetId)
.table(tableId)
.insert([event])
.then(() => {
console.log(`Inserted the event`);
})
.catch(err => {
if (err && err.name === 'PartialFailureError') {
if (err.errors && err.errors.length > 0) {
console.log('Insert errors:');
err.errors.forEach(err => console.error(err));
}
} else {
console.error('ERROR:', err);
}
});
};
exports.handleSpeedtestEvent = async (speedtestEvent) => {
await writeEventToBQ(speedtestEvent);
};