-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathODMetricsCenter.ts
83 lines (72 loc) · 2.05 KB
/
ODMetricsCenter.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
import * as XLSX from "xlsx";
import {
getXSOSIReposInEachMonth,
getAllMetrics,
Month,
RepoName,
AllMetrics,
MetricName,
} from "./api";
import {
OPENDIGGER_METRICS_FOR_REPO,
DIRNAME_SHEETS,
FILENAME_SHEET_OD_METRICS,
} from "./const";
interface Record {
month: Month;
repo: string; // only repo name, no owner name
[metric: MetricName]: number | string; // https://stackoverflow.com/a/38262343/10369621
}
export default class ODMetricsCenter {
private reposInEachMonth: Map<Month, RepoName[]>;
private repoMetrics: Map<RepoName, AllMetrics>;
private records: Record[];
constructor() {
this.reposInEachMonth = new Map();
this.repoMetrics = new Map();
this.records = [];
}
async init() {
await this.loadReposInEachMonth();
await this.loadRepoMetrics();
this.generateRecords();
}
private async loadReposInEachMonth() {
this.reposInEachMonth = await getXSOSIReposInEachMonth();
}
private async loadRepoMetrics() {
for (const [month, repos] of this.reposInEachMonth) {
for (const repo of repos) {
if (!this.repoMetrics.has(repo)) {
this.repoMetrics.set(repo, await getAllMetrics(repo));
}
}
}
}
getRepoMetricInMonth(repo: RepoName, metric: string, month: Month) {
return this.repoMetrics.get(repo).get(metric)?.[month] ?? 0;
}
private generateRecords() {
for (const [month, repos] of this.reposInEachMonth) {
for (const repo of repos) {
const record: Record = {
month,
repo: repo.split("/")[1], // drop owner name
};
OPENDIGGER_METRICS_FOR_REPO.forEach((metric) => {
record[metric] = this.getRepoMetricInMonth(repo, metric, month);
});
this.records.push(record);
}
}
}
printRecords() {
console.table(this.records);
}
dump2xlsx() {
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(this.records);
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
XLSX.writeFile(wb, `${DIRNAME_SHEETS}/${FILENAME_SHEET_OD_METRICS}`);
}
}