mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-07 00:24:18 +00:00
Merge pull request 'fix(hetzner): Normalize CPU usage percentage by cached amount of cores' (#272) from hetzner-cpu into master
Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy.org.app/pulls/272 Reviewed-by: Inex Code <inex.code@selfprivacy.org>
This commit is contained in:
commit
1db2f53c9a
|
@ -46,6 +46,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
|
||||
ApiAdapter _adapter;
|
||||
final Currency currency = Currency.fromType(CurrencyType.eur);
|
||||
int? cachedCoreAmount;
|
||||
|
||||
@override
|
||||
ServerProviderType get type => ServerProviderType.hetzner;
|
||||
|
@ -287,6 +288,8 @@ class HetznerServerProvider extends ServerProvider {
|
|||
provider: ServerProviderType.hetzner,
|
||||
);
|
||||
|
||||
cachedCoreAmount = serverResult.data!.serverType.cores;
|
||||
|
||||
final createDnsResult = await _adapter.api().createReverseDns(
|
||||
serverId: serverDetails.id,
|
||||
ip4: serverDetails.ip4,
|
||||
|
@ -775,7 +778,7 @@ class HetznerServerProvider extends ServerProvider {
|
|||
) async {
|
||||
ServerMetrics? metrics;
|
||||
|
||||
List<TimeSeriesData> serializeTimeSeries(
|
||||
List<TimeSeriesData> serializeTimeNetworkSeries(
|
||||
final Map<String, dynamic> json,
|
||||
final String type,
|
||||
) {
|
||||
|
@ -785,6 +788,47 @@ class HetznerServerProvider extends ServerProvider {
|
|||
.toList();
|
||||
}
|
||||
|
||||
if (cachedCoreAmount == null) {
|
||||
final serversResult = await _adapter.api().getServers();
|
||||
if (serversResult.data.isEmpty || !serversResult.success) {
|
||||
return GenericResult(
|
||||
success: false,
|
||||
data: metrics,
|
||||
code: serversResult.code,
|
||||
message: serversResult.message,
|
||||
);
|
||||
}
|
||||
|
||||
for (final server in serversResult.data) {
|
||||
if (server.id == serverId) {
|
||||
cachedCoreAmount = server.serverType.cores;
|
||||
}
|
||||
}
|
||||
|
||||
if (cachedCoreAmount == null) {
|
||||
return GenericResult(
|
||||
success: false,
|
||||
data: metrics,
|
||||
message: "Couldn't find active server to cache core amount",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
List<TimeSeriesData> serializeTimeCpuSeries(
|
||||
final Map<String, dynamic> json,
|
||||
final String type,
|
||||
) {
|
||||
final List list = json['time_series'][type]['values'];
|
||||
return list
|
||||
.map(
|
||||
(final el) => TimeSeriesData(
|
||||
el[0],
|
||||
double.parse(el[1]) / cachedCoreAmount!,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
final cpuResult = await _adapter.api().getMetrics(
|
||||
serverId,
|
||||
start,
|
||||
|
@ -818,15 +862,15 @@ class HetznerServerProvider extends ServerProvider {
|
|||
}
|
||||
|
||||
metrics = ServerMetrics(
|
||||
cpu: serializeTimeSeries(
|
||||
cpu: serializeTimeCpuSeries(
|
||||
cpuResult.data,
|
||||
'cpu',
|
||||
),
|
||||
bandwidthIn: serializeTimeSeries(
|
||||
bandwidthIn: serializeTimeNetworkSeries(
|
||||
netResult.data,
|
||||
'network.0.bandwidth.in',
|
||||
),
|
||||
bandwidthOut: serializeTimeSeries(
|
||||
bandwidthOut: serializeTimeNetworkSeries(
|
||||
netResult.data,
|
||||
'network.0.bandwidth.out',
|
||||
),
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||
|
@ -85,9 +83,7 @@ class CpuChart extends StatelessWidget {
|
|||
],
|
||||
minY: 0,
|
||||
// Maximal value of data by 100 step
|
||||
maxY:
|
||||
((data.map((final e) => e.value).reduce(max) - 1) / 100).ceil() *
|
||||
100.0,
|
||||
maxY: 100,
|
||||
minX: 0,
|
||||
titlesData: FlTitlesData(
|
||||
topTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
|
||||
|
|
Loading…
Reference in a new issue