-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpuinfo_parser.ts
70 lines (60 loc) · 1.49 KB
/
cpuinfo_parser.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
type Cpuinfo = {
key: string;
value: string;
};
type RetVal = {
[key: string]: string;
};
export default function CpuinfoParse(raw: string): Array<Array<Cpuinfo>> {
const lines = raw.split("\n");
const t: Array<Array<Cpuinfo>> = [];
let tmp: Array<Cpuinfo> = [];
for (let i = 0; i < lines.length; i++) {
if (lines[i] === "") {
t.push(tmp);
tmp = [];
continue;
}
let parts = lines[i].split(":");
if (parts.length < 2) {
continue;
}
parts[0] = parts[0].replace(":", "").trim();
parts = parts.map((p) => p.trim());
const key = typeof parts[0] === "undefined" ? "" : parts[0];
const value = typeof parts[1] === "undefined" ? "" : parts[1];
tmp.push({
key: key.replace(" ", "_"),
value: value,
});
}
t[t.length - 1].length === 0 && t.pop(); // delete last element that is empty
return t;
}
export function extractAndMergeCpuInfo(v: Array<Array<Cpuinfo>>): RetVal {
const t: any = {
cores: [],
};
for (let i = 0; i < v.length; i++) {
for (const cpu of v[i]) {
if (
!cpu.key.includes("cpu_") && !cpu.key.includes("core_") &&
!cpu.key.includes("processor")
) {
t[cpu.key] = cpu.value;
} else {
if (t["cores"][i]) {
t["cores"][i] = {
...t["cores"][i],
[cpu.key]: cpu.value,
};
} else {
t["cores"].push({
[cpu.key]: cpu.value,
});
}
}
}
}
return t;
}