-
Notifications
You must be signed in to change notification settings - Fork 3
/
index_kibana.js
168 lines (157 loc) · 4.25 KB
/
index_kibana.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const crypto = require("crypto");
const glob = require("glob");
const execSync = require("child_process").execSync;
const path = require("path");
const YAML = require("yaml");
const fs = require("fs");
const { Client } = require("@elastic/elasticsearch");
const client = new Client({
node: process.env.ES || "http://elastic:changeme@localhost:9200",
});
const pluginName = (n) =>
/\/plugins\/(.*?)((\/common)|(\/public)|(\/server)).*/.exec(n)?.[1];
function getCommitData() {
const stdout = execSync(`cd kibana && git log -1`, { encoding: "utf8" });
const [, hash, author, date] = /commit (\w+)\nAuthor: (.*)\nDate: (.*)/.exec(
stdout
);
return {
hash,
author: author.trim(),
date: new Date(date.trim().toString()),
};
}
function groupBy(list, groupFn) {
const map = new Map();
list.forEach((i) => {
const group = groupFn(i);
// exclude lens itself and invalid entries
if (group === 'lens' || !i || !group ) return;
if (!map.has(group)) {
map.set(group, []);
}
map.get(group).push(i);
});
return map;
}
function getUsage(commit, searchTerm, usage) {
let srcFiles = [];
try {
srcFiles = execSync(`cd kibana && ag "${searchTerm}" -l ./src/plugins`)
.toString("utf8")
.split("\n");
} catch (e) {}
let xpackFiles = [];
try {
xpackFiles = execSync(`cd kibana && ag "${searchTerm}" -l ./x-pack/plugins`)
.toString("utf8")
.split("\n");
} catch (e) {}
const srcGroups = groupBy(srcFiles, pluginName);
const xpackGroups = groupBy(xpackFiles, pluginName);
const srcUsages = [...srcGroups.entries()].map(([id, files]) => ({
commit,
usage,
name: id,
files,
occurences: files.length,
}));
const xpackUsages = [...xpackGroups.entries()].map(([id, files]) => ({
commit,
usage,
name: `x-pack/${id}`,
files,
occurences: files.length,
}));
return [...srcUsages, ...xpackUsages];
}
/*
Document structure in usages index:
Per crawling per pacakge/plugin
{
date: 2022-08-11...,
usage: elastic/charts | lens-plugin | ExploratoryViewEmbeddable
name: x-pack/lens | vis_types/timelion,
files: [ kibana/src/..., kibana/src/...],
occurences: files.length
}
elastic/charts: Something is imported from the elastic/charts package
lens: Something is imported from the lens plugin
exploratory_view: The ExploratoryViewEmbeddable component is used somewhere
*/
function collectUsages() {
const commitData = getCommitData();
return [
...getUsage(commitData, "elastic/charts", 'elastic-charts'),
...getUsage(commitData, "lens-plugin", 'lens'),
...getUsage(commitData, "/lens/", 'lens'),
...getUsage(commitData, "ExploratoryViewEmbeddable", 'exploratory-view'),
];
}
(async function () {
const usages = collectUsages();
if (fs.existsSync("./result.json")) {
fs.rmSync("./result.json");
}
fs.writeFileSync("./result.json", JSON.stringify(usages, null, 2));
console.log(`uploading ${usages.length} usages...`);
const exists = await client.indices.exists({
index: "usages",
});
if (!exists) {
await client.indices.create({
index: "usages",
mappings: {
properties: {
occurences: {
type: "long",
},
files: {
type: "keyword",
},
name: {
type: "keyword",
},
usage: {
type: "keyword",
},
commit: {
properties: {
hash: {
type: "keyword",
},
author: {
type: "keyword",
},
date: {
type: "date",
},
},
},
},
},
});
}
const chunkSize = 250;
for (let i = 0; i < usages.length; i += chunkSize) {
console.log(i);
const chunk = usages.slice(i, i + chunkSize);
const response = await client.bulk({
operations: chunk.flatMap((v) => [
{
index: {
_index: "usages",
_id: crypto.randomBytes(16).toString("hex"),
},
},
v,
]),
});
if (response.errors) {
console.log(JSON.stringify(response, null, 2));
throw new Error();
}
}
await client.indices.refresh({ index: "usages" });
console.log("done");
})();