Repository navigation
Expand file tree
/
Copy pathnetwork_storage.php
More file actions
323 lines (310 loc) · 14.3 KB
/
Copy pathnetwork_storage.php
File metadata and controls
323 lines (310 loc) · 14.3 KB
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
<?php
require_once 'bootstrap.php';
require_once 'include/layout.php';
require_once 'include/components/range_controls.php';
require_once 'include/components/stat_card.php';
include_once 'include/graph.php';
$graphConfigs = require 'include/graph_configs.php';
use Siagraph\Utils\ApiClient;
use Siagraph\Utils\Locale;
$months = 12; // default visible range; slider covers all data
$metricsEndpoint = '/api/v1/monthly/metrics';
$growthEndpoint = '/api/v1/monthly/growth';
$intervalDefault = 'month';
$latestData = ApiClient::fetchJson($metricsEndpoint);
$dataError = !is_array($latestData);
$latest = $dataError ? [] : end($latestData);
$asOf = !$dataError && isset($latest['date']) ? $latest['date'] : null;
$prev = [];
$athData = ApiClient::fetchJson('/api/v1/storage/ath', true, 'day');
$athPayload = is_array($athData) && isset($athData['utilized_storage']) && is_array($athData['utilized_storage'])
? $athData['utilized_storage']
: [];
$daysSinceAth = isset($athPayload['days_since_ath']) && $athPayload['days_since_ath'] !== null
? (string) $athPayload['days_since_ath']
: 'N/A';
$athBytes = isset($athPayload['ath_bytes']) && $athPayload['ath_bytes'] !== null
? Locale::decimal(((float) $athPayload['ath_bytes']) / 1e15, 2) . ' PB'
: 'N/A';
$athDate = isset($athPayload['ath_date']) && $athPayload['ath_date'] !== null
? (string) $athPayload['ath_date']
: null;
?>
<?php render_header("SiaGraph - Storage"); ?>
<section id="main-content" class="sg-container">
<h1 class="sg-container__heading text-center mb-2"><i class="bi bi-hdd-network me-2"></i>Storage</h1>
<?php if ($dataError): ?>
<p class="text-center text-muted">Latest metrics unavailable.</p>
<?php endif; ?>
<div class="sg-container__row mb-4">
<div class="sg-container__row-content sg-container__row-content--center">
<div class="sg-container__column sg-container__column--one-fourth">
<?php
$valUS = isset($latest['utilized_storage']) ? Locale::decimal($latest['utilized_storage']/1e15,2).' PB' : 'N/A';
render_stat_card([
'icon' => 'bi bi-hdd-fill',
'label' => 'Utilized Storage',
'value' => $valUS,
'context' => $asOf ? ('Monthly snapshot as of ' . $asOf) : 'Monthly snapshot',
'tooltip' => 'Data currently stored by renters across the network',
]);
?>
</div>
<div class="sg-container__column sg-container__column--one-fourth">
<?php
$valTS = isset($latest['total_storage']) ? Locale::decimal($latest['total_storage']/1e15,2).' PB' : 'N/A';
render_stat_card([
'icon' => 'bi bi-hdd-network',
'label' => 'Total Storage',
'value' => $valTS,
'context' => $asOf ? ('Monthly snapshot as of ' . $asOf) : 'Monthly snapshot',
'tooltip' => 'Aggregate capacity hosts are making available',
]);
?>
</div>
<div class="sg-container__column sg-container__column--one-fourth" id="athBadgeContainer" <?php if ($athDate): ?>title="<?php echo htmlspecialchars('ATH set on ' . $athDate); ?>"<?php endif; ?>>
<div class="card">
<div class="card__content">
<div class="mb-1 text-gray-400 text-sm">
<i class="bi bi-flag me-1"></i>Days since ATH
</div>
<div class="flex items-center justify-start">
<span id="athBadgeValue" class="glanceNumber text-2xl font-semibold"><?php echo htmlspecialchars($daysSinceAth); ?></span>
</div>
</div>
</div>
</div>
<div class="sg-container__column sg-container__column--one-fourth" id="athSizeContainer" <?php if ($athDate): ?>title="<?php echo htmlspecialchars('ATH set on ' . $athDate); ?>"<?php endif; ?>>
<div class="card">
<div class="card__content">
<div class="mb-1 text-gray-400 text-sm">
<i class="bi bi-trophy me-1"></i>ATH Utilized Storage
</div>
<div class="flex items-center justify-start">
<span id="athSizeValue" class="glanceNumber text-2xl font-semibold"><?php echo htmlspecialchars($athBytes); ?></span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sg-container__row">
<div class="sg-container__row-content">
<div class="sg-container__column">
<section class="card">
<h2 class="card__heading">Network Storage</h2>
<div class="card__content">
<?php
renderGraph(
'metrics-storage',
[
$graphConfigs['utilized_storage'],
$graphConfigs['total_storage']
],
'date',
$metricsEndpoint,
null,
'line',
$intervalDefault,
true,
'true',
$months,
'false',
'bytes'
);
?>
</div>
</section>
</div>
</div>
</div>
<div class="sg-container__row mt-4">
<div class="sg-container__row-content">
<div class="sg-container__column">
<section class="card">
<h2 class="card__heading">Storage Utilization %</h2>
<div class="card__content">
<?php
renderGraph(
'utilization-percentage-inline',
[
$graphConfigs['utilization_percent']
],
'date',
$metricsEndpoint,
null,
'line',
$intervalDefault,
true,
'false',
$months,
'false',
'percentage'
);
?>
</div>
</section>
</div>
</div>
</div>
<div class="sg-container__row mt-4">
<div class="sg-container__row-content">
<div class="sg-container__column">
<section class="card">
<h2 class="card__heading">Storage Forecast</h2>
<div class="card__content">
<div class="text-end mb-2">
<button id="scaleToggle" class="btn btn-primary btn-sm">Switch to Logarithmic Forecast</button>
</div>
<div class="graph-container">
<canvas id="forecastChart" class="sg-chart-canvas"></canvas>
</div>
</div>
</section>
</div>
</div>
</div>
<script>
async function fetchGrowthData() {
return await fetchWithCache('<?php echo $growthEndpoint; ?>');
}
function formatPB(value) {
return (value / 1e15).toFixed(2) + ' PB';
}
document.addEventListener('DOMContentLoaded', async function () {
try {
const rawData = await fetchGrowthData();
const data = rawData.slice(-5);
const actualLen = data.length;
const labels = data.map(d => d.date);
const utilized = data.map(d => Number(d.utilized_storage));
const total = data.map(d => Number(d.total_storage));
const monthsForRate = Math.min(5, data.length - 1);
const utilRate = (utilized[utilized.length - 1] - utilized[utilized.length - 1 - monthsForRate]) / monthsForRate;
const totalRate = (total[total.length - 1] - total[total.length - 1 - monthsForRate]) / monthsForRate;
const utilBase = utilized[utilized.length - 1 - monthsForRate];
const totalBase = total[total.length - 1 - monthsForRate];
const utilFactor = utilBase > 0 ? Math.pow(utilized[utilized.length - 1] / utilBase, 1 / monthsForRate) : 1;
const totalFactor = totalBase > 0 ? Math.pow(total[total.length - 1] / totalBase, 1 / monthsForRate) : 1;
function computePredictions(logarithmic) {
const predUtil = new Array(actualLen + 24).fill(null);
const predTot = new Array(actualLen + 24).fill(null);
predUtil[actualLen - 1] = utilized[actualLen - 1];
predTot[actualLen - 1] = total[actualLen - 1];
let lastU = Number(utilized[actualLen - 1]);
let lastT = Number(total[actualLen - 1]);
for (let i = 1; i <= 24; i++) {
if (logarithmic) {
lastU = Math.max(0, lastU * utilFactor);
lastT = Math.max(0, lastT * totalFactor);
} else {
lastU = Math.max(0, lastU + utilRate);
lastT = Math.max(0, lastT + totalRate);
}
predUtil[actualLen - 1 + i] = lastU;
predTot[actualLen - 1 + i] = lastT;
}
return { predUtil, predTot };
}
const labelsEnd = moment(labels[labels.length - 1]);
for (let i = 1; i <= 24; i++) {
const nextDate = moment(labelsEnd).add(i, 'months').format('YYYY-MM-01');
labels.push(nextDate);
utilized.push(null);
total.push(null);
}
const initialPred = computePredictions(false);
let predUtilized = initialPred.predUtil;
let predTotal = initialPred.predTot;
const ctx = document.getElementById('forecastChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'Utilized Storage',
data: utilized,
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 2,
fill: false
},
{
label: 'Total Storage',
data: total,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 2,
fill: false
},
{
label: 'Predicted Utilized',
data: predUtilized,
borderColor: 'rgba(75, 192, 192, 0.5)',
borderDash: [5, 5],
borderWidth: 2,
fill: false,
spanGaps: true
},
{
label: 'Predicted Total',
data: predTotal,
borderColor: 'rgba(255, 99, 132, 0.5)',
borderDash: [5, 5],
borderWidth: 2,
fill: false,
spanGaps: true
}
]
},
options: {
// Match interaction behavior of other charts: show all series on x hover
interaction: { mode: 'index', intersect: false },
plugins: {
legend: { display: true },
tooltip: {
mode: 'index',
intersect: false,
callbacks: {
label: ctx => ctx.dataset.label + ': ' + formatPB(ctx.parsed.y)
}
}
},
scales: {
x: {
type: 'time',
time: { unit: 'month', displayFormats: { month: 'MMM YY' } }
},
y: {
type: 'linear',
beginAtZero: true,
ticks: { callback: value => formatPB(value) }
}
}
}
});
document.getElementById('scaleToggle').addEventListener('click', function () {
const yAxis = chart.options.scales.y;
const toLog = yAxis.type === 'linear';
if (toLog) {
yAxis.type = 'logarithmic';
this.textContent = 'Switch to Linear Forecast';
} else {
yAxis.type = 'linear';
this.textContent = 'Switch to Log Scale';
}
const preds = computePredictions(toLog);
predUtilized = preds.predUtil;
predTotal = preds.predTot;
chart.data.datasets[2].data = predUtilized;
chart.data.datasets[3].data = predTotal;
chart.update();
});
} catch (e) {
console.error('Failed to render storage forecast', e);
}
});
</script>
</section>
<?php render_footer(); ?>