-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
119 lines (97 loc) · 3.08 KB
/
utils.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
119
import { sheets_v4 } from "googleapis";
import logger from "./logger";
function columnToLetter(column: number) {
column++;
var temp,
letter = "";
while (column > 0) {
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
function letterToColumn(letter: string) {
var column = 0,
length = letter.length;
for (var i = 0; i < length; i++) {
column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);
}
return --column;
}
async function getNamesList(sheets: sheets_v4.Sheets, spreadsheetId: string) {
const names = (await sheets.spreadsheets.values.get({
spreadsheetId: spreadsheetId,
range: "TotalHours!A:B",
majorDimension: "ROWS",
})).data.values;
if (!names || names.length === 0) {
logger.error("Empty names list"); // ERROR!
return [];
}
return names;
}
function getName(names: string[][], firstName: string, lastName: string): number {
let nameIndex = -1;
for (let i = 0; i < names.length; i++) {
if (names[i][0] === firstName && names[i][1] === lastName) {
nameIndex = i;
}
}
return nameIndex;
}
async function getNextColumnIndex(sheets: sheets_v4.Sheets, spreadsheetId: string) {
const columnTitles = (await sheets.spreadsheets.values.get({
spreadsheetId: spreadsheetId,
range: "TotalHours!A1:1",
majorDimension: "COLUMNS",
})).data.values;
if (!columnTitles) {
console.error("Column not found!"); // ERROR!
return -1;
}
return columnTitles.length;
}
async function getColumnIndexFromColumnTitle(sheets: sheets_v4.Sheets, spreadsheetId: string, columnTitle: string): Promise<number> {
const columnTitles = (await sheets.spreadsheets.values.get({
spreadsheetId: spreadsheetId,
range: "TotalHours!A1:1",
majorDimension: "COLUMNS",
})).data.values;
if (!columnTitles) {
return -1;
}
return columnTitles.flat().indexOf(columnTitle);
}
function datesToHours(startDate: Date, endDate: Date): number {
return Math.trunc((endDate.valueOf() - startDate.valueOf()) / 36000) / 100;
}
async function asyncExponentialBackoff(f: Function, maxAttempts = 15) {
let succeeded = false;
let attempts = 0;
let backoffTime = 0; // In seconds
while (!succeeded) {
try {
return await f();
} catch (err) {
logger.warn("Exponential backoff error caught!");
backoffTime = Math.min(Math.pow(2, attempts) + Math.random(), 64);
logger.warn(`Attempt: ${attempts} Backoff Time: ${backoffTime}`);
await new Promise(resolve => setTimeout(resolve, backoffTime * 1000));
attempts++;
if (attempts > maxAttempts) {
throw err;
}
}
}
}
export {
columnToLetter,
letterToColumn,
getNamesList,
getName,
getNextColumnIndex,
getColumnIndexFromColumnTitle,
datesToHours,
asyncExponentialBackoff,
}