Skip to content

Commit 3409974

Browse files
authored
Merge pull request #13 from MinaFoundation/investigate-leaderboard-web
PM-1973 - Revise Leaderboard web
2 parents 0eea446 + 3e74faf commit 3409974

11 files changed

+122
-863
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*_test.env
22

3-
/.env
3+
.env

api/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ attrs==21.4.0
22
click==8.1.2
33
flasgger==0.9.7.1
44
Flask==2.2.5
5-
Flask-Caching==1.10.1
5+
Flask-Caching==2.3.0
66
importlib-metadata==4.11.3
77
importlib-resources==5.7.1
88
itsdangerous==2.1.2

web/config.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

web/delegationPolicyNew.html

Lines changed: 0 additions & 117 deletions
This file was deleted.

web/getPageDataForSnark.php

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,66 @@
11
<?php
2-
require_once ("connectionsnark.php");
2+
require_once("connectionsnark.php");
33

4-
if (! (isset($_GET['pageNumber']))) {
5-
$pageNumber = 1;
6-
} else {
7-
$pageNumber = $_GET['pageNumber'];
8-
}
4+
// Validate and sanitize pageNumber from GET
5+
$pageNumber = isset($_GET['pageNumber']) ? filter_var($_GET['pageNumber'], FILTER_VALIDATE_INT, ['options' => ['default' => 1, 'min_range' => 1]]) : 1;
96

107
$perPageCount = 120;
118

129
// Check if IGNORE_APPLICATION_STATUS is set to 1
1310
$ignoreApplicationStatus = getenv('IGNORE_APPLICATION_STATUS') == 1;
1411

1512
// Modify SQL query based on IGNORE_APPLICATION_STATUS
16-
$sqlCondition = $ignoreApplicationStatus ? "score is not null" : "application_status = true and score is not null";
13+
$sqlCondition = $ignoreApplicationStatus ? "score IS NOT NULL" : "application_status = TRUE AND score IS NOT NULL";
1714

1815
$sql = "SELECT COUNT(*) FROM nodes WHERE {$sqlCondition}";
19-
2016
if ($result = pg_query($conn, $sql)) {
2117
$row = pg_fetch_row($result);
22-
$rowCount = $row[0];
18+
$rowCount = (int)$row[0];
2319
pg_free_result($result);
2420
}
2521

2622
$pagesCount = ceil($rowCount / $perPageCount);
27-
2823
$lowerLimit = ($pageNumber - 1) * $perPageCount;
2924

3025
// Use the modified SQL condition for the main query as well
3126
$sqlQuery = "SELECT block_producer_key, score, score_percent FROM nodes WHERE {$sqlCondition} ORDER BY score DESC";
3227

28+
// Execute the main query and sanitize the results
3329
$results = pg_query($conn, $sqlQuery);
34-
$row = pg_fetch_all($results);
30+
$row = pg_fetch_all($results);
3531

36-
$maxScoreSnark= " WITH recentone as (
37-
SELECT batch_end_epoch end_epoch, extract('epoch' FROM (to_timestamp(batch_end_epoch) - interval '90' day )) start_epoch
32+
$maxScoreSnark = "
33+
WITH recentone AS (
34+
SELECT batch_end_epoch end_epoch,
35+
extract('epoch' FROM (to_timestamp(batch_end_epoch) - interval '90' day)) start_epoch
3836
FROM bot_logs b
39-
where file_timestamps <= CURRENT_TIMESTAMP
40-
ORDER BY batch_end_epoch DESC LIMIT 1
41-
)
42-
SELECT COUNT(1), to_char(to_timestamp(end_epoch), 'DD-MM-YYYY hh24:mi') as last_modified
43-
FROM bot_logs , recentone
44-
WHERE batch_start_epoch >= start_epoch and batch_end_epoch <= end_epoch
45-
AND files_processed > -1
46-
group by 2 ";
47-
48-
$maxScoreSnarkresult = pg_query($conn, $maxScoreSnark);
49-
$maxScoreRow = pg_fetch_row($maxScoreSnarkresult);
50-
$maxScore = $maxScoreRow[0];
51-
$last_modified=$maxScoreRow[1];
52-
53-
echo json_encode(array('row' => $row, 'rowCount' => $rowCount, 'maxScore' => $maxScore, 'last_modified'=>$last_modified));
37+
WHERE file_timestamps <= CURRENT_TIMESTAMP
38+
ORDER BY batch_end_epoch DESC
39+
LIMIT 1
40+
)
41+
SELECT COUNT(1), to_char(to_timestamp(end_epoch), 'DD-MM-YYYY hh24:mi') AS last_modified
42+
FROM bot_logs, recentone
43+
WHERE batch_start_epoch >= start_epoch
44+
AND batch_end_epoch <= end_epoch
45+
AND files_processed > -1
46+
GROUP BY 2
47+
";
48+
49+
// Execute the query
50+
$maxScoreSnarkResult = pg_query($conn, $maxScoreSnark);
51+
$maxScoreRow = pg_fetch_row($maxScoreSnarkResult);
52+
53+
// Sanitize database output before using it
54+
$maxScore = (int)$maxScoreRow[0];
55+
$last_modified = htmlspecialchars($maxScoreRow[1], ENT_QUOTES, 'UTF-8');
56+
57+
// Ensure to sanitize the JSON output
58+
foreach ($row as &$r) {
59+
$r['block_producer_key'] = htmlspecialchars($r['block_producer_key'], ENT_QUOTES, 'UTF-8');
60+
$r['score'] = (float)$r['score'];
61+
$r['score_percent'] = (float)$r['score_percent'];
62+
}
63+
64+
echo json_encode(array('row' => $row, 'rowCount' => $rowCount, 'maxScore' => $maxScore, 'last_modified' => $last_modified));
5465

5566
?>

web/imageDesign.html

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)