Skip to content

Commit a948501

Browse files
committed
update metrics extraction to include HTTP status codes and include HK to CN
1 parent 5e52e50 commit a948501

File tree

1 file changed

+214
-40
lines changed

1 file changed

+214
-40
lines changed

static/stats/statistics.js

Lines changed: 214 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ $(window).load(function () {
1818
const baseurl = window.location.origin;
1919
console.log("Base URL for API requests: " + baseurl);
2020

21-
// Helper function to extract API and total requests from Prometheus data
21+
// Helper function to extract API and HTTP status code requests from Prometheus data
2222
function extractMetrics(prom_to_dict) {
23-
let api_req, total_req;
23+
let api_req, status_200, status_301, status_404, status_503, status_others;
2424

2525
if (prom_to_dict.opencitations_api_requests_total) {
2626
api_req = prom_to_dict.opencitations_api_requests_total;
@@ -32,13 +32,31 @@ $(window).load(function () {
3232
api_req = 0;
3333
}
3434

35-
if (prom_to_dict.opencitations_requests_total) {
36-
total_req = prom_to_dict.opencitations_requests_total;
37-
} else {
38-
total_req = 0;
35+
// Extract HTTP status codes
36+
status_200 = 0;
37+
status_301 = 0;
38+
status_404 = 0;
39+
status_503 = 0;
40+
status_others = 0;
41+
42+
if (prom_to_dict.opencitations_requests_by_status_total) {
43+
const statusCodes = prom_to_dict.opencitations_requests_by_status_total;
44+
45+
// Main status codes
46+
status_200 = Number(statusCodes['200'] || 0);
47+
status_301 = Number(statusCodes['301'] || 0);
48+
status_404 = Number(statusCodes['404'] || 0);
49+
status_503 = Number(statusCodes['503'] || 0);
50+
51+
// Calculate others (all codes except 200, 301, 404, 503)
52+
for (const [code, count] of Object.entries(statusCodes)) {
53+
if (code !== '200' && code !== '301' && code !== '404' && code !== '503') {
54+
status_others += Number(count);
55+
}
56+
}
3957
}
4058

41-
return { api_req, total_req };
59+
return { api_req, status_200, status_301, status_404, status_503, status_others };
4260
}
4361

4462
// Helper function to extract country data
@@ -84,7 +102,23 @@ $(window).load(function () {
84102
if (countryIso !== 'Unknown' && countryIso !== 'XX') {
85103
const name = countryData[countryIso].name || countryIso;
86104
const count = countryData[countryIso].count;
87-
const tooltip = `${name} - ${countryIso}\nRequests: ${count.toLocaleString()}`;
105+
106+
let tooltip;
107+
if (countryIso === 'CN' && countryData[countryIso].details) {
108+
// Special case for China with Hong Kong details
109+
const details = countryData[countryIso].details;
110+
tooltip = `${name} (including Hong Kong)\n`;
111+
if (details.china > 0) {
112+
tooltip += `China: ${details.china.toLocaleString()}\n`;
113+
}
114+
if (details.hongkong > 0) {
115+
tooltip += `Hong Kong: ${details.hongkong.toLocaleString()}\n`;
116+
}
117+
tooltip += `Total: ${count.toLocaleString()}`;
118+
} else {
119+
tooltip = `${name} - ${countryIso}\nRequests: ${count.toLocaleString()}`;
120+
}
121+
88122
dataArray.push([countryIso, count, tooltip]);
89123
}
90124
}
@@ -290,24 +324,36 @@ $(window).load(function () {
290324
};
291325
}
292326

293-
const { api_req, total_req } = extractMetrics(prom_to_dict);
327+
const { api_req, status_200, status_301, status_404, status_503, status_others } = extractMetrics(prom_to_dict);
294328

295329
let result = {};
296330
result["api_requests"] = Number(api_req);
297-
result["total_requests"] = Number(total_req);
331+
result["status_200"] = Number(status_200);
332+
result["status_301"] = Number(status_301);
333+
result["status_404"] = Number(status_404);
334+
result["status_503"] = Number(status_503);
335+
result["status_others"] = Number(status_others);
298336

299337
key_name = months[date[2]] + " " + date[1];
300338
dict_name[key_name] = result;
301339
}
302340

303341
api_req_list = [];
304-
total_req_list = [];
342+
status_200_list = [];
343+
status_301_list = [];
344+
status_404_list = [];
345+
status_503_list = [];
346+
status_others_list = [];
305347
labels_list = [];
306348

307349
for (const key in dict_name) {
308350
labels_list.push(key);
309351
api_req_list.push(dict_name[key].api_requests);
310-
total_req_list.push(dict_name[key].total_requests);
352+
status_200_list.push(dict_name[key].status_200);
353+
status_301_list.push(dict_name[key].status_301);
354+
status_404_list.push(dict_name[key].status_404);
355+
status_503_list.push(dict_name[key].status_503);
356+
status_others_list.push(dict_name[key].status_others);
311357
}
312358

313359
var barChartData = {
@@ -316,17 +362,51 @@ $(window).load(function () {
316362
{
317363
label: "API",
318364
backgroundColor: "#3C41E5",
319-
borderColor: "blue",
365+
borderColor: "#3C41E5",
320366
borderWidth: 1,
321-
data: api_req_list
367+
data: api_req_list,
368+
stack: 'stack0'
322369
},
323370
{
324-
label: "Total Requests",
325-
backgroundColor: "#AB54FD",
326-
borderColor: "purple",
371+
label: "200 OK",
372+
backgroundColor: "#28a745",
373+
borderColor: "#28a745",
374+
borderWidth: 1,
375+
data: status_200_list,
376+
stack: 'stack1'
377+
},
378+
{
379+
label: "301 Redirect",
380+
backgroundColor: "#17a2b8",
381+
borderColor: "#17a2b8",
382+
borderWidth: 1,
383+
data: status_301_list,
384+
stack: 'stack1'
385+
},
386+
{
387+
label: "404 Not Found",
388+
backgroundColor: "#ffc107",
389+
borderColor: "#ffc107",
327390
borderWidth: 1,
328-
data: total_req_list
391+
data: status_404_list,
392+
stack: 'stack1'
329393
},
394+
{
395+
label: "503 Service Unavailable",
396+
backgroundColor: "#dc3545",
397+
borderColor: "#dc3545",
398+
borderWidth: 1,
399+
data: status_503_list,
400+
stack: 'stack1'
401+
},
402+
{
403+
label: "Other HTTP Codes",
404+
backgroundColor: "#6c757d",
405+
borderColor: "#6c757d",
406+
borderWidth: 1,
407+
data: status_others_list,
408+
stack: 'stack1'
409+
}
330410
]
331411
};
332412

@@ -630,13 +710,37 @@ $(window).load(function () {
630710
const valuePart = line.split('} ')[1];
631711
const count = Number(valuePart);
632712

633-
if (!country_aggregated[countryInfo.iso]) {
634-
country_aggregated[countryInfo.iso] = {
635-
name: countryInfo.name,
636-
count: 0
637-
};
713+
// Aggregate Hong Kong (HK) into China (CN)
714+
if (countryInfo.iso === 'HK') {
715+
if (!country_aggregated['CN']) {
716+
country_aggregated['CN'] = {
717+
name: 'China',
718+
count: 0,
719+
details: { china: 0, hongkong: 0 }
720+
};
721+
}
722+
country_aggregated['CN'].count += count;
723+
country_aggregated['CN'].details.hongkong += count;
724+
} else if (countryInfo.iso === 'CN') {
725+
if (!country_aggregated['CN']) {
726+
country_aggregated['CN'] = {
727+
name: 'China',
728+
count: 0,
729+
details: { china: 0, hongkong: 0 }
730+
};
731+
}
732+
country_aggregated['CN'].count += count;
733+
country_aggregated['CN'].details.china += count;
734+
} else {
735+
// Other countries
736+
if (!country_aggregated[countryInfo.iso]) {
737+
country_aggregated[countryInfo.iso] = {
738+
name: countryInfo.name,
739+
count: 0
740+
};
741+
}
742+
country_aggregated[countryInfo.iso].count += count;
638743
}
639-
country_aggregated[countryInfo.iso].count += count;
640744
}
641745
}
642746
}
@@ -901,22 +1005,34 @@ $(window).load(function () {
9011005
};
9021006
}
9031007

904-
const { api_req, total_req } = extractMetrics(prom_to_dict);
1008+
const { api_req, status_200, status_301, status_404, status_503, status_others } = extractMetrics(prom_to_dict);
9051009
let result = {};
9061010
result["api_requests"] = Number(api_req);
907-
result["total_requests"] = Number(total_req);
1011+
result["status_200"] = Number(status_200);
1012+
result["status_301"] = Number(status_301);
1013+
result["status_404"] = Number(status_404);
1014+
result["status_503"] = Number(status_503);
1015+
result["status_others"] = Number(status_others);
9081016
key_name = months[date[2]] + " " + date[1];
9091017
dict_name[key_name] = result;
9101018
}
9111019

9121020
api_req_list = [];
913-
total_req_list = [];
1021+
status_200_list = [];
1022+
status_301_list = [];
1023+
status_404_list = [];
1024+
status_503_list = [];
1025+
status_others_list = [];
9141026
labels_list = [];
9151027

9161028
for (const key in dict_name) {
9171029
labels_list.push(key);
9181030
api_req_list.push(dict_name[key].api_requests);
919-
total_req_list.push(dict_name[key].total_requests);
1031+
status_200_list.push(dict_name[key].status_200);
1032+
status_301_list.push(dict_name[key].status_301);
1033+
status_404_list.push(dict_name[key].status_404);
1034+
status_503_list.push(dict_name[key].status_503);
1035+
status_others_list.push(dict_name[key].status_others);
9201036
}
9211037

9221038
myBar.destroy()
@@ -927,17 +1043,51 @@ $(window).load(function () {
9271043
{
9281044
label: "API",
9291045
backgroundColor: "#3C41E5",
930-
borderColor: "blue",
1046+
borderColor: "#3C41E5",
9311047
borderWidth: 1,
932-
data: api_req_list
1048+
data: api_req_list,
1049+
stack: 'stack0'
9331050
},
9341051
{
935-
label: "Total Requests",
936-
backgroundColor: "#AB54FD",
937-
borderColor: "purple",
1052+
label: "200 OK",
1053+
backgroundColor: "#28a745",
1054+
borderColor: "#28a745",
1055+
borderWidth: 1,
1056+
data: status_200_list,
1057+
stack: 'stack1'
1058+
},
1059+
{
1060+
label: "301 Redirect",
1061+
backgroundColor: "#17a2b8",
1062+
borderColor: "#17a2b8",
1063+
borderWidth: 1,
1064+
data: status_301_list,
1065+
stack: 'stack1'
1066+
},
1067+
{
1068+
label: "404 Not Found",
1069+
backgroundColor: "#ffc107",
1070+
borderColor: "#ffc107",
1071+
borderWidth: 1,
1072+
data: status_404_list,
1073+
stack: 'stack1'
1074+
},
1075+
{
1076+
label: "503 Service Unavailable",
1077+
backgroundColor: "#dc3545",
1078+
borderColor: "#dc3545",
9381079
borderWidth: 1,
939-
data: total_req_list
1080+
data: status_503_list,
1081+
stack: 'stack1'
9401082
},
1083+
{
1084+
label: "Other HTTP Codes",
1085+
backgroundColor: "#6c757d",
1086+
borderColor: "#6c757d",
1087+
borderWidth: 1,
1088+
data: status_others_list,
1089+
stack: 'stack1'
1090+
}
9411091
]
9421092
};
9431093

@@ -1169,13 +1319,37 @@ $(window).load(function () {
11691319
const valuePart = line.split('} ')[1];
11701320
const count = Number(valuePart);
11711321

1172-
if (!country_aggregated[countryInfo.iso]) {
1173-
country_aggregated[countryInfo.iso] = {
1174-
name: countryInfo.name,
1175-
count: 0
1176-
};
1322+
// Aggregate Hong Kong (HK) into China (CN)
1323+
if (countryInfo.iso === 'HK') {
1324+
if (!country_aggregated['CN']) {
1325+
country_aggregated['CN'] = {
1326+
name: 'China',
1327+
count: 0,
1328+
details: { china: 0, hongkong: 0 }
1329+
};
1330+
}
1331+
country_aggregated['CN'].count += count;
1332+
country_aggregated['CN'].details.hongkong += count;
1333+
} else if (countryInfo.iso === 'CN') {
1334+
if (!country_aggregated['CN']) {
1335+
country_aggregated['CN'] = {
1336+
name: 'China',
1337+
count: 0,
1338+
details: { china: 0, hongkong: 0 }
1339+
};
1340+
}
1341+
country_aggregated['CN'].count += count;
1342+
country_aggregated['CN'].details.china += count;
1343+
} else {
1344+
// Other countries
1345+
if (!country_aggregated[countryInfo.iso]) {
1346+
country_aggregated[countryInfo.iso] = {
1347+
name: countryInfo.name,
1348+
count: 0
1349+
};
1350+
}
1351+
country_aggregated[countryInfo.iso].count += count;
11771352
}
1178-
country_aggregated[countryInfo.iso].count += count;
11791353
}
11801354
}
11811355
}

0 commit comments

Comments
 (0)