-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
137 lines (123 loc) · 4.55 KB
/
script.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
$(document).ready(function() {
$.fn.dataTable.moment('DD-MM-YYYY');
$('#languageFilter').select2();
var table = $('#virusTable').DataTable({
columns: [
{ data: 'filename', title: 'Filename' },
{ data: 'hash', title: 'Hash', render: function(data, type, row) {
return `<input type="text" value="${data}" readonly style="border:none; background:transparent; width:150px;" title="${data}" />`;
}},
{ data: 'scan_date', title: 'Scan Date', render: function(data, type, row) {
return moment.unix(data).format('DD-MM-YYYY');
}},
{ data: 'positives', title: 'Positives' },
{ data: 'total', title: 'Total' },
{ data: 'file_size', title: 'File Size (KB)', render: function(data, type, row) {
return (data / 1024).toFixed(2);
}, type: 'num' },
{ data: 'link', title: 'Details', render: function(data, type, row) {
return `<a href="${data}" target="_blank" class="details-btn">Details</a>`;
}}
],
order: [[2, 'desc']],
columnDefs: [
{ type: 'num', targets: 5 },
],
autoWidth: false,
createdRow: function(row, data, dataIndex) {
$('td', row).each(function(index, td) {
$(td).attr('data-label', table.column(index).header().innerText);
});
}
});
function fetchDataAndUpdateTable() {
$.getJSON('virus_total_results.json', function(data) {
var groupedData = groupByFilename(data);
var tableData = formatData(groupedData);
table.clear().rows.add(tableData).draw();
updateTotalPositives(tableData);
disableTextSelection();
});
}
fetchDataAndUpdateTable();
setInterval(fetchDataAndUpdateTable, 30000);
function groupByFilename(data) {
return data.reduce(function(acc, item) {
var baseName = item.filename.split('-v')[0];
if (!acc[baseName]) {
acc[baseName] = [];
}
acc[baseName].push(item);
return acc;
}, {});
}
function formatData(groupedData) {
var result = [];
for (var key in groupedData) {
groupedData[key].forEach(function(item) {
result.push(item);
});
}
return result;
}
function disableTextSelection() {
$('.dataTables_paginate .paginate_button').css('user-select', 'none');
}
function updateTotalPositives(data) {
var totalPositives = data.reduce(function(sum, item) {
return sum + item.positives;
}, 0);
$('#totalPositives').text(totalPositives);
}
$('#languageFilter').on('change', function() {
var selectedLanguages = $(this).val();
if (selectedLanguages.length === 0 || selectedLanguages.includes('all')) {
table.column(0).search('').draw();
} else {
var searchRegex = selectedLanguages.map(function(lang) {
return '\\b' + lang + '\\b';
}).join('|');
table.column(0).search(searchRegex, true, false).draw();
}
});
$('#sortOptions').on('change', function() {
var sortOption = $(this).val();
var columnIdx, sortDir;
switch (sortOption) {
case 'filename-asc':
columnIdx = 0; sortDir = 'asc';
break;
case 'filename-desc':
columnIdx = 0; sortDir = 'desc';
break;
case 'date-asc':
columnIdx = 2; sortDir = 'asc';
break;
case 'date-desc':
columnIdx = 2; sortDir = 'desc';
break;
case 'positives-asc':
columnIdx = 3; sortDir = 'asc';
break;
case 'positives-desc':
columnIdx = 3; sortDir = 'desc';
break;
case 'size-asc':
columnIdx = 5; sortDir = 'asc';
break;
case 'size-desc':
columnIdx = 5; sortDir = 'desc';
break;
default:
return;
}
table.order([columnIdx, sortDir]).draw();
});
table.on('order.dt', function() {
table.columns().every(function() {
this.nodes().flatten().to$().removeClass('sorted');
});
var order = table.order()[0];
table.column(order[0]).nodes().flatten().to$().addClass('sorted');
});
});