This repository was archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
399 lines (352 loc) · 20.2 KB
/
index.php
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
// Load dependencies
require_once (__DIR__ . '/../config/app.php');
require_once (__DIR__ . '/../library/access.php');
require_once (__DIR__ . '/../library/mysql.php');
require_once (__DIR__ . '/../../vendor/autoload.php');
// Connect database
try {
$db = new MySQL(DB_HOST, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
} catch(Exception $e) {
var_dump($e);
exit;
}
// Connect memcached
try {
$memory = new Yggverse\Cache\Memory(MEMCACHED_HOST, MEMCACHED_PORT, MEMCACHED_NAMESPACE, MEMCACHED_TIMEOUT + time());
} catch(Exception $e) {
var_dump($e);
exit;
}
// Prepare request
$requestTheme = isset($_GET['theme']) && in_array(['default'], $_GET['theme']) ? $_GET['theme'] : 'default';
$requestTime = time(); // @TODO !empty($_GET['time']) ? (int) $_GET['time'] : time();
$requestSort = isset($_GET['sort']) && in_array($_GET['sort'], ['timeOnline', 'uptimeAvg', 'sentSum', 'receivedSum', 'address']) ? $_GET['sort'] : 'timeOnline';
$requestOrder = isset($_GET['order']) && in_array($_GET['order'], ['ASC', 'DESC']) ? $_GET['order'] : 'DESC';
$requestPage = isset($_GET['page']) && $_GET['page'] > 1 ? (int) $_GET['page'] : 1;
$requestCalendar = isset($_GET['calendar']) && in_array($_GET['calendar'], ['peers', 'traffic']) ? $_GET['calendar'] : 'peers';
// app begin
$calendar = new Yggverse\Graph\Calendar\Month($requestTime);
foreach ($calendar->getNodes() as $day => $node) {
switch ($requestCalendar) {
case 'peers':
$timeThis = strtotime(sprintf('%s-%s-%s 00:00', date('Y'), date('n'), date('d')));
$timeFrom = strtotime(sprintf('%s-%s-%s 00:00', date('Y', $requestTime), date('n', $requestTime), $day));
$timeTo = strtotime('+1 day', strtotime(sprintf('%s-%s-%s 00:00', date('Y', $requestTime), date('n', $requestTime), $day)));
// Calculate today and previous days only
if ($timeFrom <= $timeThis) {
$dbPeerTotalByTimeUpdated = $memory->getByMethodCallback(
$db, 'findPeerTotalByTimeUpdated', [$timeFrom, $timeTo], time() + ($timeTo <= $timeThis ? 31556952 : 300)
);
$dbPeerTotalByTimeAdded = $memory->getByMethodCallback(
$db, 'findPeerTotalByTimeAdded', [$timeFrom, $timeTo], time() + ($timeTo <= $timeThis ? 31556952 : 300)
);
// Add daily stats
$calendar->addNode($day, $dbPeerTotalByTimeUpdated, sprintf(_('online %s'), $dbPeerTotalByTimeUpdated), 'green', 0);
$calendar->addNode($day, $dbPeerTotalByTimeAdded, sprintf(_('new %s'), $dbPeerTotalByTimeAdded), 'blue', 0);
// Add hourly stats
for ($hour = 0; $hour < 24; $hour++) {
$timeThis = strtotime(sprintf('%s-%s-%s %s:00', date('Y'), date('n'), date('d'), date('H')));
$timeFrom = strtotime(sprintf('%s-%s-%s %s:00', date('Y', $requestTime), date('n', $requestTime), $day, $hour));
$timeTo = strtotime(sprintf('%s-%s-%s %s:00', date('Y', $requestTime), date('n', $requestTime), $day, $hour + 1));
// Calculate this hour of today and previous hours only
if ($timeFrom <= $timeThis) {
$dbPeerTotalByTimeUpdated = $memory->getByMethodCallback(
$db, 'findPeerTotalByTimeUpdated', [$timeFrom, $timeTo], time() + ($timeTo <= $timeThis ? 31556952 : 300)
);
$dbPeerTotalByTimeAdded = $memory->getByMethodCallback(
$db, 'findPeerTotalByTimeAdded', [$timeFrom, $timeTo], time() + ($timeTo <= $timeThis ? 31556952 : 300)
);
} else {
$dbPeerTotalByTimeUpdated = 0;
$dbPeerTotalByTimeAdded = 0;
}
$calendar->addNode($day, $dbPeerTotalByTimeUpdated, sprintf(_('%s:00-%s:00 online %s'), $hour, $hour + 1, $dbPeerTotalByTimeUpdated), 'green', 1);
$calendar->addNode($day, $dbPeerTotalByTimeAdded, sprintf(_('%s:00-%s:00 new %s'), $hour, $hour + 1, $dbPeerTotalByTimeAdded), 'blue', 1);
}
}
break;
case 'traffic':
$timeThis = strtotime(sprintf('%s-%s-%s 00:00', date('Y'), date('n'), date('d')));
$timeFrom = strtotime(sprintf('%s-%s-%s 00:00', date('Y', $requestTime), date('n', $requestTime), $day));
$timeTo = strtotime('+1 day', strtotime(sprintf('%s-%s-%s 00:00', date('Y', $requestTime), date('n', $requestTime), $day)));
// Calculate today and previous days only
if ($timeFrom <= $timeThis) {
$dbPeerSessionSentSumByTimeUpdated = $memory->getByMethodCallback(
$db, 'findPeerSessionSentSumByTimeUpdated', [$timeFrom, $timeTo], time() + ($timeTo <= $timeThis ? 31556952 : 300)
);
$dbPeerSessionReceivedSumByTimeUpdated = $memory->getByMethodCallback(
$db, 'findPeerSessionReceivedSumByTimeUpdated', [$timeFrom, $timeTo], time() + ($timeTo <= $timeThis ? 31556952 : 300)
);
// Add daily stats
$calendar->addNode($day, $dbPeerSessionSentSumByTimeUpdated, sprintf(_('↑ %s'), number_format($dbPeerSessionSentSumByTimeUpdated / 1000000, 3)), 'red', 0);
$calendar->addNode($day, $dbPeerSessionReceivedSumByTimeUpdated, sprintf(_('↓ %s'), number_format($dbPeerSessionReceivedSumByTimeUpdated / 1000000, 3)), 'green', 0);
// Add hourly stats
for ($hour = 0; $hour < 24; $hour++) {
$timeThis = strtotime(sprintf('%s-%s-%s %s:00', date('Y'), date('n'), date('d'), date('H')));
$timeFrom = strtotime(sprintf('%s-%s-%s %s:00', date('Y', $requestTime), date('n', $requestTime), $day, $hour));
$timeTo = strtotime(sprintf('%s-%s-%s %s:00', date('Y', $requestTime), date('n', $requestTime), $day, $hour + 1));
// Calculate this hour of today and previous hours only
if ($timeFrom <= $timeThis) {
$dbPeerSessionSentSumByTimeUpdated = $memory->getByMethodCallback(
$db, 'findPeerSessionSentSumByTimeUpdated', [$timeFrom, $timeTo], time() + ($timeTo <= $timeThis ? 31556952 : 300)
);
$dbPeerSessionReceivedSumByTimeUpdated = $memory->getByMethodCallback(
$db, 'findPeerSessionReceivedSumByTimeUpdated', [$timeFrom, $timeTo], time() + ($timeTo <= $timeThis ? 31556952 : 300)
);
} else {
$dbPeerSessionSentSumByTimeUpdated = 0;
$dbPeerSessionReceivedSumByTimeUpdated = 0;
}
$calendar->addNode($day, $dbPeerSessionSentSumByTimeUpdated, sprintf(_('%s:00-%s:00 ↑ %s'), $hour, $hour + 1, number_format($dbPeerSessionSentSumByTimeUpdated / 1000000, 3)), 'red', 1);
$calendar->addNode($day, $dbPeerSessionReceivedSumByTimeUpdated, sprintf(_('%s:00-%s:00 ↓ %s'), $hour, $hour + 1, number_format($dbPeerSessionReceivedSumByTimeUpdated / 1000000, 3)), 'green', 1);
}
}
break;
}
}
$peers = $memory->getByMethodCallback(
$db,
'findPeers',
[
($requestPage - 1) * WEBSITE_PEER_REMOTE_PAGINATION_LIMIT,
$requestPage + WEBSITE_PEER_REMOTE_PAGINATION_LIMIT,
$requestSort,
$requestOrder,
]
);
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<link rel="stylesheet" type="text/css" href="<?php echo WEBSITE_URL ?>/assets/theme/<?php echo $requestTheme ?>/css/common.css?<?php echo WEBSITE_CSS_VERSION ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo WEBSITE_URL ?>/assets/theme/<?php echo $requestTheme ?>/css/framework.css?<?php echo WEBSITE_CSS_VERSION ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo WEBSITE_URL ?>/assets/theme/<?php echo $requestTheme ?>/css/yggverse/graph/calendar/month.css?<?php echo WEBSITE_CSS_VERSION ?>" />
<title>
<?php echo sprintf(_('%s - Yggdrasil network explorer since %s'), WEBSITE_NAME, date('Y', $memory->getByMethodCallback($db, 'getPeerFirstByTimeAdded')->timeAdded)) ?>
</title>
<meta name="description" content="<?php echo _('Yggdrasil network analytics: peer info, ip, traffic, timing, routing, geo-location') ?>" />
<meta name="keywords" content="yggdrasil, yggstate, yggverse, analytics, explorer, search engine, crawler, ip info, geo location, node city, node country, traffic stats, ports, node coordinates, connection time, routes, open-source, js-less" />
<meta name="author" content="YGGverse" />
<meta charset="UTF-8" />
</head>
<body>
<header>
<div class="container">
<div class="row">
<a class="logo" href="<?php echo WEBSITE_URL ?>"><?php echo str_replace('YGG', '<span>YGG</span>', WEBSITE_NAME) ?></a>
<?php if (Access::address(ADMIN_REMOTE_ADDRESS_WHITELIST)) { ?>
<sup class="label label-green font-size-12 font-width-normal cursor-default"><?php echo _('admin') ?></sup>
<?php } ?>
<form name="search" method="get" action="<?php echo WEBSITE_URL ?>/search.php">
<input type="text" name="query" value="" placeholder="<?php echo _('this, address, ip, geo, keyword...') ?>" />
<button type="submit"><?php echo _('search') ?></button>
</form>
</div>
</div>
</header>
<main>
<div class="container">
<div class="row">
<div class="column width-100">
<div class="padding-4">
<h1><?php echo _('Dashboard') ?></h1>
</div>
</div>
</div>
<div class="row">
<div class="column width-50 width-tablet-100 width-mobile-100">
<div class="padding-4">
<h2><?php echo _('Peers') ?></h2>
<table>
<?php if ($peers) { ?>
<thead>
<tr>
<th class="text-left">
<a href="<?php echo WEBSITE_URL ?>/index.php?sort=address&order=<?php echo $requestOrder == 'DESC' ? 'ASC' : 'DESC' ?>">
<?php echo _('Address') ?>
</a>
</th>
<th class="text-center">
<a href="<?php echo WEBSITE_URL ?>/index.php?sort=uptimeAvg&order=<?php echo $requestOrder == 'DESC' ? 'ASC' : 'DESC' ?>">
<?php echo _('Uptime,h') ?>
</a>
</th>
<th class="text-center">
<a href="<?php echo WEBSITE_URL ?>/index.php?sort=sentSum&order=<?php echo $requestOrder == 'DESC' ? 'ASC' : 'DESC' ?>">
<?php echo _('Sent,Mb') ?>
</a>
</th>
<th class="text-center">
<a href="<?php echo WEBSITE_URL ?>/index.php?sort=receivedSum&order=<?php echo $requestOrder == 'DESC' ? 'ASC' : 'DESC' ?>">
<?php echo _('Received,Mb') ?>
</a>
</th>
<th class="text-center">
<a href="<?php echo WEBSITE_URL ?>/index.php?sort=timeOnline&order=<?php echo $requestOrder == 'DESC' ? 'ASC' : 'DESC' ?>">
<?php echo _('Online') ?>
</a>
</th>
</tr>
</thead>
<tbody>
<?php foreach ($peers as $peer) { ?>
<tr>
<td class="text-left">
<a href="<?php echo WEBSITE_URL ?>/peer.php?peerId=<?php echo $peer->peerId ?>">
<?php echo $peer->address ?>
</a>
</td>
<td class="text-center"><?php echo $peer->uptimeAvg ? round($peer->uptimeAvg / 60 / 60, 2) : 0 ?></td>
<td class="text-center"><?php echo $peer->sentSum ? number_format($peer->sentSum / 1000000, 3) : 0 ?></td>
<td class="text-center"><?php echo $peer->receivedSum ? number_format($peer->receivedSum / 1000000, 3) : 0 ?></td>
<td class="text-center">
<span title="<?php echo date('Y-m-d H:s:i', $peer->timeOnline) ?>"
class="font-size-22 cursor-default <?php echo $peer->timeOnline + WEBSITE_PEER_REMOTE_TIME_ONLINE_TIMEOUT > time() ? 'text-color-green' : 'text-color-red' ?>">
•
</span>
</td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td colspan="5" class="text-left">
<?php if ($requestPage > 1) { ?>
<a href="<?php echo WEBSITE_URL ?>/index.php?sort=<?php echo $requestSort ?>&page=<?php echo $requestPage - 1 ?>"><?php echo _('←') ?></a>
<?php } ?>
<?php echo sprintf(_('page %s'), $requestPage) ?>
<a href="<?php echo WEBSITE_URL ?>/index.php?sort=<?php echo $requestSort ?>&page=<?php echo $requestPage + 1 ?>"><?php echo _('→') ?></a>
</td>
</tr>
</tfoot>
<?php } else { ?>
<tfoot>
<tr>
<td class="text-center"><?php echo _('Peers not found') ?></td>
</tr>
</tfoot>
<?php } ?>
</table>
</div>
</div>
<div class="column width-50 width-tablet-100 width-mobile-100">
<div class="padding-4">
<h2><?php echo _('Totals') ?></h2>
<div class="row padding-0">
<div class="column width-50">
<table class="bordered width-100 margin-bottom-8">
<thead>
<tr>
<th class="text-left" colspan="2"><?php echo _('Peers') ?></th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-left"><?php echo _('Online') ?></td>
<td class="text-right"><?php echo number_format($memory->getByMethodCallback($db, 'findPeerTotalByTimeUpdated', [time() - WEBSITE_PEER_REMOTE_TIME_ONLINE_TIMEOUT,
strtotime('+1 month', strtotime(sprintf('%s-%s-%s 00:00', date('Y'), date('n'), 1)))])) ?></td>
</tr>
<tr>
<td class="text-left"><?php echo _('New') ?></td>
<td class="text-right"><?php echo number_format($memory->getByMethodCallback($db, 'findPeerTotalByTimeAdded', [strtotime(sprintf('%s-%s-%s 00:00', date('Y'), date('n'), 1)),
strtotime('+1 month', strtotime(sprintf('%s-%s-%s 00:00', date('Y'), date('n'), 1)))])) ?></td>
</tr>
<tr>
<td class="text-left"><?php echo _('Active') ?></td>
<td class="text-right"><?php echo number_format($memory->getByMethodCallback($db, 'findPeerTotalByTimeUpdated', [strtotime(sprintf('%s-%s-%s 00:00', date('Y'), date('n'), 1)),
strtotime('+1 month', strtotime(sprintf('%s-%s-%s 00:00', date('Y'), date('n'), 1)))])) ?></td>
</tr>
<tr>
<td class="text-left"><?php echo _('Total') ?></td>
<td class="text-right"><?php echo number_format($memory->getByMethodCallback($db, 'getPeersTotal')) ?></td>
</tr>
</tbody>
</table>
</div>
<div class="column width-50">
<div class="margin-left-16">
<table class="bordered width-100 margin-bottom-8">
<thead>
<tr>
<th class="text-left" colspan="2"><?php echo _('Traffic, Mb') ?></th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-left"><?php echo _('Sent') ?></td>
<td class="text-right"><?php echo number_format($memory->getByMethodCallback($db, 'getPeerSessionSentSum') / 1000000, 3) ?></td>
</tr>
<tr>
<td class="text-left"><?php echo _('Received') ?></td>
<td class="text-right"><?php echo number_format($memory->getByMethodCallback($db, 'getPeerSessionReceivedSum') / 1000000, 3) ?></td>
</tr>
<tr>
<td class="text-left"><?php echo _('Sum') ?></td>
<td class="text-right"><?php echo number_format(($memory->getByMethodCallback($db, 'getPeerSessionSentSum') +
$memory->getByMethodCallback($db, 'getPeerSessionReceivedSum')) / 1000000, 3) ?></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<h2>
<?php if ($requestCalendar == 'peers') { ?>
<?php echo _('Peers') ?> | <a href="<?php echo WEBSITE_URL ?>/index.php?calendar=traffic"><?php echo _('Traffic') ?></a>
<?php } else { ?>
<a href="<?php echo WEBSITE_URL ?>/index.php?calendar=peers"><?php echo _('Peers') ?></a> | <?php echo _('Traffic') ?>
<?php } ?>
<span class="float-right">
<?php echo sprintf(_('%s - %s'), date('Y.m.d', strtotime(sprintf('%s-%s-%s 00:00', date('Y', $requestTime), date('n', $requestTime), 1), $requestTime)),
date('Y.m.d', strtotime('+1 month', strtotime(sprintf('%s-%s-%s 00:00', date('Y', $requestTime), date('n', $requestTime), 1), $requestTime)))) ?>
</span>
</h2>
<div class="yggverse_graph_calendar__month">
<?php foreach ($calendar->getNodes() as $day => $node) { ?>
<div class="day">
<div class="number <?php echo $day > date('j') ? 'disabled' : false ?>">
<?php echo $day ?>
</div>
<?php if ($day <= date('j')) { ?>
<?php foreach ($node as $i => $layers) { ?>
<div class="layer layer<?php echo $i ?>">
<div class="label">
<?php foreach ($layers as $layer) { ?>
<div class="<?php echo $layer->class ?>"><?php echo $layer->label ?></div>
<?php } ?>
</div>
<?php foreach ($layers as $layer) { ?>
<div title="<?php echo $layer->label ?>"
class="value <?php echo $layer->class ?>"
style="width:<?php echo $layer->width ?>%;height:<?php echo $layer->height ?>%;left:<?php echo $layer->offset ?>%"></div>
<?php } ?>
</div>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
</div>
</main>
<footer>
<div class="container">
<div class="row">
<div class="column width-50 width-tablet-100 width-mobile-100">
<a href="https://github.com/YGGverse/YGGstate"><?php echo _('GitHub') ?></a>
</div>
<div class="column width-50 width-tablet-100 width-mobile-100 text-right">
<?php echo sprintf(_('server time: %s / %s'), time(), date('c')) ?>
<br />
<?php echo sprintf(_('database since %s contains %s peers'),
date('M, Y', $memory->getByMethodCallback($db, 'getPeerFirstByTimeAdded')->timeAdded),
number_format($memory->getByMethodCallback($db, 'getPeersTotal'))) ?>
</div>
</div>
</div>
</footer>
</body>
</html>