File size: 11,782 Bytes
2a2a47f
 
 
38e70c4
2a2a47f
 
 
 
 
38e70c4
 
 
 
 
 
 
 
 
 
 
 
cef1f1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38e70c4
 
 
 
 
 
 
 
cef1f1e
38e70c4
 
1bddee8
 
 
cef1f1e
 
 
38e70c4
 
 
 
 
 
cef1f1e
38e70c4
 
 
 
 
 
 
 
 
1bddee8
38e70c4
1bddee8
 
 
38e70c4
 
43f35d8
38e70c4
 
 
 
 
 
 
 
 
1bddee8
 
19d66b4
 
 
1bddee8
 
 
 
19d66b4
1bddee8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38e70c4
 
 
 
 
 
43f35d8
fe8da28
b1bc81d
38e70c4
43f35d8
38e70c4
43f35d8
38e70c4
 
 
cef1f1e
38e70c4
cef1f1e
 
 
38e70c4
 
 
 
 
43f35d8
 
38e70c4
43f35d8
38e70c4
43f35d8
38e70c4
 
 
cef1f1e
d947152
 
cef1f1e
 
6090892
d947152
cef1f1e
6090892
cef1f1e
38e70c4
cef1f1e
38e70c4
 
 
 
 
 
 
 
cef1f1e
b5bfbc4
 
38e70c4
 
43f35d8
38e70c4
 
cef1f1e
 
 
38e70c4
2a2a47f
38e70c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bfbc4
 
 
 
 
 
38e70c4
 
 
 
 
cef1f1e
 
 
38e70c4
2a2a47f
38e70c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bfbc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cef1f1e
 
 
b5bfbc4
2a2a47f
b5bfbc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38e70c4
 
 
 
 
 
 
 
 
 
 
b5bfbc4
 
 
b1bc81d
b5bfbc4
 
38e70c4
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
let dark = document.location.search.includes('dark-theme=true');

if (dark)
    document.body.classList.add('dark-theme');


var COLORS = dark ?
    ['#FF0000', '#00FF00', '#0000FF', '#FF00FF', '#FFFF00', '#0000FF', '#F090F0', '#90F0F0', '#F0F090'] :
    ['#CC0000', '#00CC00', '#0000CC', '#CC00CC', '#CCCC00', '#0000CC', '#C060C0', '#60C0C0', '#C0C060']

const load = () => {
    const l0 = document.createElement('div')
    const l1 = document.createElement('div')
    const l2 = document.createElement('div')
    l0.classList.add('lds-ripple')

    l0.appendChild(l1)
    l0.appendChild(l2)
    return l0
}

const getCheckedOptions = () => {
    const options = Array.from(document.querySelectorAll('.option-div'))
        .map(e => Array.from(e.children)
        .filter(e => e.nodeName == 'DIV'))
        .filter(e => e.length)
        .flat()
        .map(e => e.id)
        .filter(e => document.querySelector(`#${e}-checkbox`).checked)

    const optionsDict = {}
    for (let option of options) {
        const key = option.split('-option-')[0]
        const value = option.split('-option-')[1]

        if (key in optionsDict)
            optionsDict[key].push(value)
        else
            optionsDict[key] = [value]
    }

    return optionsDict;
}

const addOption = (category, optionName) => {
    /* Options for the issue div */
    const issueDiv = document.getElementById(`${category}Div`);
    const div = document.createElement('div')

    let found = false;
    let optionNumber = 0;
    while (!found && ++optionNumber < 100) {
        let previousOption = document.getElementById(`${category}-option-${optionNumber}`);
        found = previousOption === null;
    }

    div.id = `${category}-option-${optionNumber}`;
    issueDiv.appendChild(div);

    const checkBox = document.createElement('input');
    checkBox.type = 'checkbox'
    checkBox.id = `${category}-option-${optionNumber}-checkbox`

    const checkBoxLabel = document.createElement('label');
    const labelSpan = document.createElement('span')
    labelSpan.textContent = optionName;
    checkBoxLabel.appendChild(checkBox)
    checkBoxLabel.appendChild(labelSpan)
    div.appendChild(checkBoxLabel)

    return optionNumber
}

let charts = [];

const createButton = (title, libraries, methods) => {
    const button = document.createElement('button')
    button.textContent = title;
    button.onclick = async () => {
        document.getElementById('pip-graph').innerHTML = ''
        document.getElementById('star-graph').innerHTML = ''
        document.getElementById('issue-graph').innerHTML = ''
        const e = load()
        document.body.appendChild(e)
        const selectedInternalLibraries = libraries.internal.filter(e => document.querySelector(`#${e}Checkbox`).checked);
        const selectedExternalLibraries = libraries.external.filter(e => document.querySelector(`#${e}Checkbox`).checked);
        const selectedLibraries = selectedInternalLibraries.concat(selectedExternalLibraries);

        const relevantOptions = getCheckedOptions();

        if (charts.length !== 0) {
            for (const chart of charts) {
                chart.destroy()
            }
        }
        for (const method of methods()) {
            charts.push(await method(selectedLibraries, relevantOptions))
        }
        document.body.removeChild(e)
    };

    return button;
}

const initialize = async () => {
    const inferResponse = await fetch(`initialize`);
    console.log(inferResponse);
    const inferJson = await inferResponse.json();
    console.log(inferJson);

    const warnings = document.getElementById("warnings")
    const librarySelector = document.getElementById('library-selector');
    const graphSelector = document.getElementById('graph-selector');
    const selectorSubmit = document.getElementById('selector-submit');

    const introSpan = document.createElement("h3")
    introSpan.textContent = "Select libraries to display"
    librarySelector.appendChild(introSpan);

    const graphSpan = document.createElement("h3")
    graphSpan.textContent = "Select graphs to display"
    graphSelector.appendChild(graphSpan);

    if (inferJson.warnings.length > 0) {
        for (const warning of inferJson.warnings) {
            const div = document.createElement('div');
            div.classList.add('warning-div')

            const labelSpan = document.createElement('span');
            labelSpan.textContent = `Warning: ${warning}`;

            div.appendChild(labelSpan);
            warnings.appendChild(div);
        }
    }

    for (const element of inferJson.internal) {
        const div = document.createElement('div');
        const checkBox = document.createElement('input');
        checkBox.type = 'checkbox'
        checkBox.id = `${element}Checkbox`;

        const checkBoxLabel = document.createElement('label');
        const labelSpan = document.createElement('span')

        labelSpan.textContent = element.charAt(0).toUpperCase() + element.slice(1)
        checkBoxLabel.appendChild(checkBox)
        checkBoxLabel.appendChild(labelSpan)

        div.appendChild(checkBoxLabel)
        librarySelector.appendChild(div)
    }

    const externalLibs = document.createElement("h3")
    externalLibs.textContent = "External Libraries"
    librarySelector.appendChild(externalLibs);

    for (const element of inferJson.external) {
        const div = document.createElement('div');
        const checkBox = document.createElement('input');
        checkBox.type = 'checkbox'
        checkBox.id = `${element}Checkbox`;

        const checkBoxLabel = document.createElement('label');
        const labelSpan = document.createElement('span')

        labelSpan.textContent = element.charAt(0).toUpperCase() + element.slice(1)
        checkBoxLabel.appendChild(checkBox)
        checkBoxLabel.appendChild(labelSpan)

        div.appendChild(checkBoxLabel)
        librarySelector.appendChild(div)
    }

    for (const element of ['pip', 'stars', 'issue']) {
        const div = document.createElement('div');
        div.classList.add('option-div')
        div.id = `${element}Div`;

        const checkBox = document.createElement('input');
        checkBox.type = 'checkbox'
        checkBox.id = `${element}CheckboxGraph`;

        const checkBoxLabel = document.createElement('label');
        const labelSpan = document.createElement('span')
        labelSpan.textContent = element.charAt(0).toUpperCase() + element.slice(1)
        checkBoxLabel.appendChild(checkBox)
        checkBoxLabel.appendChild(labelSpan)

        div.appendChild(checkBoxLabel)
        graphSelector.appendChild(div)
    }

    addOption('pip', "Cumulated");
    addOption('pip', "Week over week");

    addOption('issue', "Exclude org members");
    addOption('issue', "Week over week");
    addOption('issue', "Cumulated");

    addOption('stars', "Week over week");
    addOption('stars', "Cumulated");

    const fetchButton = createButton('Fetch', inferJson, () => {
        const graphNames = ['pip', 'stars', 'issue'].filter(e => document.querySelector(`#${e}CheckboxGraph`).checked);
        const graphs = []

        if (graphNames.includes('pip'))
            graphs.push(retrievePipInstalls)

        if (graphNames.includes('stars'))
            graphs.push(retrieveStars)

        if (graphNames.includes('issue'))
            graphs.push(retrieveIssues)

        return graphs
    })
    selectorSubmit.appendChild(fetchButton);
};

const retrievePipInstalls = async (libraryNames, options) => {
    const relevantOptions = options['pip']
    const inferResponse = await fetch(`retrievePipInstalls?input=${libraryNames}&options=${relevantOptions}`);
    const inferJson = await inferResponse.json();
    const colors = [...COLORS];

    const labels = Array.from(inferJson['day']).map(e => new Date(e))
    const datasets = [];
    for (const element in inferJson) {
        if (element === 'day')
            continue

        const color = colors.pop()
        datasets.push({
            label: element,
            data: inferJson[element],
            backgroundColor: color,
            borderColor: color,
            tension: 0.01,
            pointRadius: 1,
            borderWidth: 2,
            fill: false
        })
    }

    const ctx = document.getElementById('pip-graph');

    const myChart = new Chart(ctx, {
        type: 'line',
        data: {labels, datasets},
        options: {
            scales: {
                y: {
                    beginAtZero: true
                },
                x: {
                    type: 'time',
                }
            },
            plugins: {
                title: {
                    display: true,
                    text: 'Pip installs'
                }
            }
        }
    });
    return myChart;
};

const retrieveStars = async (libraryNames, options) => {
    const relevantOptions = options['stars']
    const inferResponse = await fetch(`retrieveStars?input=${libraryNames}&options=${relevantOptions}`);
    const inferJson = await inferResponse.json();
    const colors = [...COLORS];

    const labels = Array.from(inferJson['day']).map(e => new Date(e))
    const datasets = [];
    for (const element in inferJson) {
        if (element === 'day')
            continue

        const color = colors.pop()
        datasets.push({
            label: element,
            data: inferJson[element],
            backgroundColor: color,
            borderColor: color,
            tension: 0.01,
            pointRadius: 1,
            borderWidth: 2,
            fill: false
        })
    }

    const ctx = document.getElementById('star-graph');

    const myChart = new Chart(ctx, {
        title: "Stars",
        type: 'line',
        data: {labels, datasets},
        options: {
            scales: {
                y: {
                    beginAtZero: true
                },
                x: {
                    type: 'time',
                }
            },
            plugins: {
                title: {
                    display: true,
                    text: 'Number of stargazers'
                }
            }
        }
    });
    return myChart;
};

const retrieveIssues = async (libraryNames, options) => {
    const relevantOptions = options['issue']
    const inferResponse = await fetch(`retrieveIssues?input=${libraryNames}&options=${relevantOptions}`);
    const inferJson = await inferResponse.json();
    const colors = [...COLORS];

    const labels = Array.from(inferJson['day']).map(e => new Date(e))
    const datasets = [];
    for (const element in inferJson) {
        if (element === 'day')
            continue

        const color = colors.pop()
        datasets.push({
            label: element,
            data: inferJson[element],
            backgroundColor: color,
            borderColor: color,
            tension: 0.01,
            pointRadius: 1,
            borderWidth: 2,
            fill: false
        })
    }

    const ctx = document.getElementById('issue-graph');

    const myChart = new Chart(ctx, {
        title: "Issues",
        type: 'line',
        data: {labels, datasets},
        options: {
            scales: {
                y: {
                    beginAtZero: true
                },
                x: {
                    type: 'time',
                }
            },
            plugins: {
                title: {
                    display: true,
                    text: 'Cumulated number of issues, PRs, and comments'
                }
            }
        }
    });
    return myChart;
};

(
    async () => {
        const e = load()
        document.body.appendChild(e)
        await initialize()
        document.body.removeChild(e)
    }
)();