-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraffic-monitoring.js
47 lines (39 loc) · 1.37 KB
/
traffic-monitoring.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
const fs = require('fs');
const CredentialsStore = require('./credential-store');
const { BigQuery } = require('@google-cloud/bigquery');
const path = require('path');
var keyFileContents = fs.readFileSync('./keys.crd', 'utf8');
var credentialStore = new CredentialsStore(keyFileContents);
async function queryUrlsWithPercentageDecrease(client, percentageDecrease, days) {
const sqlFilePath = path.join(__dirname, 'SQL Scripts', 'queries','traffic-decrease.sql');
const query = fs.readFileSync(sqlFilePath, 'utf8');
console.log(sqlFilePath)
const options = {
query: query,
params: {
'percentage_decrease': percentageDecrease,
'days': days,
'floor': floor
},
};
console.log(options)
const [rows] = await client.query(options);
console.log('Query Results:');
rows.forEach(row => console.log(row));
//return rows;
}
// Usage example:
const bigquery = new BigQuery({
keyFilename: './bq-account-key.json',
projectId: credentialStore.getCredential('bigQueryProjectId'),
});
const percentageDecrease = 80; // Adjust the percentage decrease as needed
const days = 14; // Adjust the number of days as needed
const floor = 100; //the minimum "old" traffic, must be above 0
queryUrlsWithPercentageDecrease(bigquery, percentageDecrease, days)
.then(results => {
console.log(results);
})
.catch(err => {
console.error('Error:', err);
});