forked from Jigsaw-Code/outline-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_server_report.ts
118 lines (107 loc) · 3.82 KB
/
post_server_report.ts
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
// Copyright 2018 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as bigquery from '@google-cloud/bigquery';
// TODO(dborkan): HourlyServerMetricsReport and HourlyUserMetricsReport are
// copied from src/shadowbox/server/metrics.ts - find a way to share these
// definitions between shadowbox and the metrics_server.
export interface HourlyServerMetricsReport {
serverId: string;
startUtcMs: number;
endUtcMs: number;
userReports: HourlyUserMetricsReport[];
}
interface HourlyUserMetricsReport {
userId: string;
countries: string[];
bytesTransferred: number;
}
interface ConnectionRow {
serverId: string;
startTimestamp: string; // ISO formatted string.
endTimestamp: string; // ISO formatted string.
userId: string;
bytesTransferred: number;
countries: string[];
}
// Instantiates a client
const bigqueryProject = bigquery({
projectId: 'uproxysite'
});
export function postServerReport(datasetName: string, tableName: string, serverReport: HourlyServerMetricsReport) {
const dataset = bigqueryProject.dataset(datasetName);
const table = dataset.table(tableName);
const rows = getConnectionRowsFromServerReport(serverReport);
return new Promise((fulfill, reject) => {
table.insert(rows, (err: Error) => {
if (err) {
reject(err);
} else {
fulfill();
}
});
});
}
function getConnectionRowsFromServerReport(serverReport: HourlyServerMetricsReport): ConnectionRow[] {
const startTimestampStr = new Date(serverReport.startUtcMs).toISOString();
const endTimestampStr = new Date(serverReport.endUtcMs).toISOString();
const rows = [];
for (const userReport of serverReport.userReports) {
rows.push({
serverId: serverReport.serverId,
startTimestamp: startTimestampStr,
endTimestamp: endTimestampStr,
userId: userReport.userId,
bytesTransferred: userReport.bytesTransferred,
countries: userReport.countries
});
}
return rows;
}
// Returns true iff testObject contains a valid HourlyServerMetricsReport.
// tslint:disable-next-line:no-any
export function isValidServerReport(testObject: any): boolean {
// Check that all required fields are present.
const requiredServerReportFields = ['serverId', 'startUtcMs', 'endUtcMs', 'userReports'];
for (const fieldName of requiredServerReportFields) {
if (!testObject[fieldName]) {
return false;
}
}
// Check that startUtcMs is not after endUtcMs.
if (testObject.startUtcMs >= testObject.endUtcMs) {
return false;
}
// Check that userReports is an array of 1 or more item.
if (!(testObject.userReports.length >= 1)) {
return false;
}
const requiredUserReportFields = ['userId', 'countries', 'bytesTransferred'];
const MIN_BYTES_TRANSFERRED = 0;
const MAX_BYTES_TRANSFERRED = 1 * Math.pow(2, 40); // 1 TB.
for (const userReport of testObject.userReports) {
// Test that each userReport contains valid fields.
for (const fieldName of requiredUserReportFields) {
if (!userReport[fieldName]) {
return false;
}
}
// Check that bytesTransferred is between min and max transfer limits
if (userReport.bytesTransferred < MIN_BYTES_TRANSFERRED ||
userReport.bytesTransferred > MAX_BYTES_TRANSFERRED) {
return false;
}
}
// Request is a valid HourlyServerMetricsReport.
return true;
}