This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
verify-data-sharing.js
137 lines (112 loc) · 4.59 KB
/
verify-data-sharing.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
const pg = require('pg');
const request = require('request');
const BUILD_GOCD_URL = process.env.BUILD_GOCD_URL;
const BUILD_GOCD_USERNAME = process.env.BUILD_GOCD_USERNAME;
const BUILD_GOCD_PASSWORD = process.env.BUILD_GOCD_PASSWORD;
const USAGE_DATA_COLLECTOR_APP_URL = process.env.USAGE_DATA_COLLECTOR_APP_URL;
const USAGE_DATA_COLLECTOR_APP_DB = process.env.USAGE_DATA_COLLECTOR_APP_DB;
const USAGE_DATA_COLLECTOR_APP_DB_PORT = process.env.USAGE_DATA_COLLECTOR_APP_DB_PORT;
const USAGE_DATA_COLLECTOR_APP_USERNAME = process.env.USAGE_DATA_COLLECTOR_APP_USERNAME;
const USAGE_DATA_COLLECTOR_APP_PASSWORD = process.env.USAGE_DATA_COLLECTOR_APP_PASSWORD;
const pgClient = () => {
const dbHost = USAGE_DATA_COLLECTOR_APP_URL;
const username = USAGE_DATA_COLLECTOR_APP_USERNAME;
const password = USAGE_DATA_COLLECTOR_APP_PASSWORD;
const dbName = USAGE_DATA_COLLECTOR_APP_DB;
const dbPort = USAGE_DATA_COLLECTOR_APP_DB_PORT;
const connectionString = "postgres://" + username + ":" + password + "@" + dbHost + ":" + dbPort + "/" + dbName;
return new pg.Client({connectionString});
};
const getBuildGoCDDataSharingInformation = (data) => {
const requestConfig = {
'url': `${BUILD_GOCD_URL}/api/internal/data_sharing/usagedata`,
'method': 'GET',
'auth': {
'username': BUILD_GOCD_USERNAME,
'password': BUILD_GOCD_PASSWORD
},
'headers': {'Accept': 'application/vnd.go.cd.v3+json'}
};
console.log("Fetching data sharing server id information from build.gocd.org...");
return new Promise((fulfil, reject) => request(requestConfig, (err, res) => {
if (err || res.statusCode >= 400) {
const msg = err ? err : res.body;
return reject(msg);
}
data.gocd_data_sharing_info = JSON.parse(res.body);
fulfil(data);
}));
};
const getBuildGoCDVersion = (data) => {
const requestConfig = {
'url': `${BUILD_GOCD_URL}/api/version`,
'method': 'GET',
'auth': {
'username': BUILD_GOCD_USERNAME,
'password': BUILD_GOCD_PASSWORD
},
'headers': {'Accept': 'application/vnd.go.cd.v1+json'}
};
console.log("Fetching currently deployed build.gocd.org version information...");
return new Promise((fulfil, reject) => request(requestConfig, (err, res) => {
if (err || res.statusCode >= 400) {
const msg = err ? err : res.body;
return reject(msg);
}
data.gocd_version = JSON.parse(res.body);
fulfil(data);
}));
};
const getUsageDataInformationFromDB = (data) => {
const gocdDataSharingInformation = data.gocd_data_sharing_info;
const queryString = `SELECT * FROM usagedata
WHERE serverid='${gocdDataSharingInformation.server_id}'
ORDER BY id DESC
limit 10;`;
console.log("Fetching build.gocd.org's usage data information from usage-data-collector-app db...");
return new Promise((fulfil, reject) => {
const client = pgClient();
client.connect().then(() => {
client.query(queryString, (error, result) => {
client.end();
if (error) {
return reject(error);
}
data.usage_data_info = result.rows;
fulfil(data);
});
});
});
};
const isToday = function (otherDay) {
const TODAY = new Date();
return otherDay.toDateString() === TODAY.toDateString();
};
const assertUsageDataExists = function (data) {
console.log("Start verifying build.gocd.org usage data reporting..");
const GoCDVersion = `${data.gocd_version.version}-${data.gocd_version.build_number}`;
const lastTenUsageData = data.usage_data_info;
const usageDataReportedToday = lastTenUsageData.filter((usageData) => {
return isToday(new Date(usageData.timestamp))
});
if (usageDataReportedToday.length === 0) {
throw new Error(`build.gocd.org hasn't reported usage data on date: ${new Date().toString()}`);
}
const usageDataMatchingVersion = usageDataReportedToday.filter((usageData) => {
return (usageData.gocdversion === GoCDVersion);
});
if (usageDataMatchingVersion.length === 0) {
console.warn(`build.gocd.org hasn't reported usage data for currently deployed version: ${GoCDVersion}`);
}
console.log("Done verifying build.gocd.org usage data reporting..");
};
const printError = (err) => {
let errMsg = `Failed Verifying Usage Data Reporting for build.gocd.org server.\n Reason: ${err}`;
console.error(errMsg);
process.exit(1);
};
getBuildGoCDVersion({})
.then(getBuildGoCDDataSharingInformation)
.then(getUsageDataInformationFromDB)
.then(assertUsageDataExists)
.catch(printError);