-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathConfig.ts
102 lines (93 loc) · 3.7 KB
/
Config.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
/**
* Copyright 2020 Google LLC
*
* 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 Utils from './utils';
export interface MutableConfig {
unprocessed_label: string;
processed_label: string;
processing_failed_label: string;
processing_frequency_in_minutes: number;
hour_of_day_to_run_sanity_checking: number;
go_link: string;
max_threads: number;
auto_labeling_parent_label: string;
}
export class Config implements Readonly<MutableConfig> {
public readonly unprocessed_label: string;
public readonly processed_label: string;
public readonly processing_failed_label: string;
public readonly processing_frequency_in_minutes: number;
public readonly hour_of_day_to_run_sanity_checking: number;
public readonly go_link: string;
public readonly max_threads: number;
public readonly auto_labeling_parent_label: string;
private static validate(config: Config) {
Utils.assert(config.unprocessed_label.length > 0, "unprocessed_label can't be empty");
Utils.assert(config.processing_frequency_in_minutes >= 5, "processing_frequency_in_minutes can't be smaller than 5");
Utils.assert(config.max_threads <= 100, "max_threads can't be greater than 100");
}
public static getConfig(): Config {
let config: MutableConfig = {
unprocessed_label: "unprocessed",
processed_label: "",
processing_failed_label: "error",
processing_frequency_in_minutes: 5,
hour_of_day_to_run_sanity_checking: 0,
go_link: "",
max_threads: 50,
auto_labeling_parent_label: "",
};
const values = Utils.withTimer("GetConfigValues", () => {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('configs');
if (sheet === null) {
throw "Active sheet 'configs' not found";
}
const num_rows = sheet.getLastRow();
return sheet.getRange(1, 1, num_rows, 2).getDisplayValues().map(row => row.map(cell => cell.trim()));
});
const num_rows = values.length;
for (let row = 0; row < num_rows; row++) {
const [name, value] = values[row];
if (name.length === 0) {
continue;
}
switch (name) {
case "processing_frequency_in_minutes":
case "hour_of_day_to_run_sanity_checking":
case "max_threads": {
const result = parseInt(value);
if (isNaN(result)) {
throw `Unrecognized config value of ${name}`;
}
config[name] = result;
break;
}
case "unprocessed_label":
case "processed_label":
case "processing_failed_label":
case "go_link":
case "auto_labeling_parent_label": {
config[name] = value;
break;
}
default: {
console.error(`Invalid config: ${name}`);
}
}
}
Config.validate(config);
return config;
}
}