-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglefit-gcs-bq.js
254 lines (241 loc) · 9.69 KB
/
googlefit-gcs-bq.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
'use strict';
const fs = require('fs');
const path = require('path');
const http = require('http');
const url = require('url');
const opn = require('open');
const destroyer = require('server-destroy');
const {google} = require('googleapis');
const fitness = google.fitness('v1');
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const now = require('nano-time');
var count=1;
const GCLOUD_PROJECT = 'spiritual-pride-280717';
const GOOGLE_CLOUD_PROJECT = 'spiritual-pride-280717';
const dataTypes = [
// 'com.google.activity.segment',
// 'com.google.calories.bmr',
// 'com.google.calories.expended',
// 'com.google.cycling.pedaling.cadence',
// 'com.google.cycling.pedaling.cumulative',
// 'com.google.heart_minutes',
// 'com.google.active_minutes',
// 'com.google.power.sample',
// 'com.google.step_count.cadence',
// 'com.google.step_count.delta',
// 'com.google.activity.exercise',
// 'com.google.body.fat.percentage',
// 'com.google.heart_rate.bpm',
'com.google.height',
'com.google.weight',
// 'com.google.cycling.wheel_revolution.rpm',
// 'com.google.cycling.wheel_revolution.cumulative',
// 'com.google.distance.delta',
// 'com.google.location.sample',
// 'com.google.speed',
// 'com.google.hydration',
// 'com.google.nutrition',
// 'com.google.blood_glucose',
// 'com.google.blood_pressure',
// 'com.google.oxygen_saturation',
// 'com.google.body.temperature',
// 'com.google.menstruation',
// 'com.google.vaginal_spotting'
];
const scopes = ['https://www.googleapis.com/auth/fitness.activity.read',
'https://www.googleapis.com/auth/fitness.activity.write',
'https://www.googleapis.com/auth/fitness.blood_glucose.read',
'https://www.googleapis.com/auth/fitness.blood_pressure.read',
'https://www.googleapis.com/auth/fitness.body.read',
'https://www.googleapis.com/auth/fitness.body_temperature.read',
'https://www.googleapis.com/auth/fitness.location.read',
'https://www.googleapis.com/auth/fitness.nutrition.read',
'https://www.googleapis.com/auth/fitness.oxygen_saturation.read',
'https://www.googleapis.com/auth/fitness.reproductive_health.read'
];
let keys = { "client_id":"",
"project_id":"",
"auth_uri":"",
"token_uri":"",
"auth_provider_x509_cert_url":"",
"client_secret":"",
"redirect_uris":[""],
"javascript_origins":[""]}
const oauth2Client = new google.auth.OAuth2(
keys.client_id,
keys.client_secret,
keys.redirect_uris[0]
);
google.options({auth: oauth2Client});
exports.bigQueryStreamer = async event => {
async function authenticate(scopes) {
return new Promise((resolve, reject) => {
try{
oauth2Client.credentials = {
access_token: '',
refresh_token: '',
scope: 'https://www.googleapis.com/auth/fitness.body.read https://www.googleapis.com/auth/fitness.nutrition.read https://www.googleapis.com/auth/fitness.activity.read https://www.googleapis.com/auth/fitness.blood_pressure.read https://www.googleapis.com/auth/fitness.oxygen_saturation.read https://www.googleapis.com/auth/fitness.reproductive_health.read https://www.googleapis.com/auth/fitness.body_temperature.read https://www.googleapis.com/auth/fitness.blood_glucose.read https://www.googleapis.com/auth/fitness.location.read https://www.googleapis.com/auth/fitness.activity.write',
token_type: 'Bearer',
expiry_date: 1594654441066
}
resolve(oauth2Client);
}
catch(e){
reject(e);
}
})
}
async function localStoragetoGCS(bucketName, filename) {
async function uploadFile() {
await storage.bucket(bucketName).upload(filename, {
gzip: true,
metadata: {
// Enable long-lived HTTP caching headers Use only if the contents of the file will never change (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'no-cache',
},
});
console.log(`${filename} uploaded to ${bucketName}.`);
}
uploadFile().catch(console.error);
}
async function GCStoBQ(datasetId, tableId,bucketName,filename) {
async function loadJSONFromGCS() {
const metadata = {
sourceFormat: 'NEWLINE_DELIMITED_JSON',
schema : {
fields : [
{
"name": "minStartTimeNs",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "maxEndTimeNs",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "dataSourceId",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "point",
"type": "RECORD",
"mode": "REPEATED",
"fields": [
{
"name": "startTimeNanos",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "endTimeNanos",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "dataTypeName",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "originDataSourceId",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "modifiedTimeMillis",
"type": "STRING",
"mode": "NULLABLE"
},
{
"name": "value",
"type": "RECORD",
"mode": "REPEATED",
"fields" : [
{
"name": "intVal",
"type": "INT64",
"mode": "NULLABLE"
},
{
"name": "mapVal",
"type": "RECORD",
"mode": "REPEATED",
"fields" : [
{
"name": "Key",
"type": "FLOAT64",
"mode": "NULLABLE"
}
]
}
]
}
]
}
],
},
location: 'US',
};
const [job] = await bigquery
.dataset(datasetId)
.table(tableId)
.load(storage.bucket(bucketName).file(filename), metadata);
console.log(`Job ${job.id} completed.`);
const errors = job.status.errors;
if (errors && errors.length > 0) {
throw errors;
}
}
loadJSONFromGCS();
}
async function runDataset(client, dname){
//var cT = now();
var dataset2 = `1588344866000000-1593615266000000`
//var dataset = `1585034375000000-${cT}`
const res = await fitness.users.dataSources.list({
"userId": "me",
"dataTypeName": dname
})
.then(function (response) {
response.data.dataSource.forEach(async function (item) {
console.log(item.dataStreamId);
const resx = await fitness.users.dataSources.datasets.get({
"userId": "me",
"dataSourceId": item.dataStreamId,
"datasetId": dataset2,
})
.then(async function (response2) {
count++;
fs.writeFileSync(path.join("/tmp", `${count}${dname}mergedsource.json`),
JSON.stringify(response2.data));
const res2 = await localStoragetoGCS('',`/tmp/${count}${dname}mergedsource.json`)
.then(
function(res2){
GCStoBQ('','','',
`${count}${dname}mergedsource.json`);
},
function(err){
console.error("Execute error", err);
});
})
})
},
function (err) { console.error("Execute error", err); });
}
const res2 = await authenticate(scopes)
.then(function(res2){
dataTypes.forEach(function(item){
runDataset(oauth2Client,item);
}
)},
function(err){
console.error("Execute error", err);
});
return;
};