mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-08 00:51:20 +00:00
fix(hetzner): Normalize CPU usage percentage by cached amount of cores
This commit is contained in:
parent
11e745f822
commit
719a5a7274
|
@ -46,6 +46,7 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
|
|
||||||
ApiAdapter _adapter;
|
ApiAdapter _adapter;
|
||||||
final Currency currency = Currency.fromType(CurrencyType.eur);
|
final Currency currency = Currency.fromType(CurrencyType.eur);
|
||||||
|
int? cachedCoreAmount;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ServerProviderType get type => ServerProviderType.hetzner;
|
ServerProviderType get type => ServerProviderType.hetzner;
|
||||||
|
@ -287,6 +288,8 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
provider: ServerProviderType.hetzner,
|
provider: ServerProviderType.hetzner,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
cachedCoreAmount = serverResult.data!.serverType.cores;
|
||||||
|
|
||||||
final createDnsResult = await _adapter.api().createReverseDns(
|
final createDnsResult = await _adapter.api().createReverseDns(
|
||||||
serverId: serverDetails.id,
|
serverId: serverDetails.id,
|
||||||
ip4: serverDetails.ip4,
|
ip4: serverDetails.ip4,
|
||||||
|
@ -775,7 +778,7 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
) async {
|
) async {
|
||||||
ServerMetrics? metrics;
|
ServerMetrics? metrics;
|
||||||
|
|
||||||
List<TimeSeriesData> serializeTimeSeries(
|
List<TimeSeriesData> serializeTimeNetworkSeries(
|
||||||
final Map<String, dynamic> json,
|
final Map<String, dynamic> json,
|
||||||
final String type,
|
final String type,
|
||||||
) {
|
) {
|
||||||
|
@ -785,6 +788,47 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
.toList();
|
.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(
|
final cpuResult = await _adapter.api().getMetrics(
|
||||||
serverId,
|
serverId,
|
||||||
start,
|
start,
|
||||||
|
@ -818,15 +862,15 @@ class HetznerServerProvider extends ServerProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics = ServerMetrics(
|
metrics = ServerMetrics(
|
||||||
cpu: serializeTimeSeries(
|
cpu: serializeTimeCpuSeries(
|
||||||
cpuResult.data,
|
cpuResult.data,
|
||||||
'cpu',
|
'cpu',
|
||||||
),
|
),
|
||||||
bandwidthIn: serializeTimeSeries(
|
bandwidthIn: serializeTimeNetworkSeries(
|
||||||
netResult.data,
|
netResult.data,
|
||||||
'network.0.bandwidth.in',
|
'network.0.bandwidth.in',
|
||||||
),
|
),
|
||||||
bandwidthOut: serializeTimeSeries(
|
bandwidthOut: serializeTimeNetworkSeries(
|
||||||
netResult.data,
|
netResult.data,
|
||||||
'network.0.bandwidth.out',
|
'network.0.bandwidth.out',
|
||||||
),
|
),
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:fl_chart/fl_chart.dart';
|
import 'package:fl_chart/fl_chart.dart';
|
||||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||||
|
@ -85,9 +83,7 @@ class CpuChart extends StatelessWidget {
|
||||||
],
|
],
|
||||||
minY: 0,
|
minY: 0,
|
||||||
// Maximal value of data by 100 step
|
// Maximal value of data by 100 step
|
||||||
maxY:
|
maxY: 100,
|
||||||
((data.map((final e) => e.value).reduce(max) - 1) / 100).ceil() *
|
|
||||||
100.0,
|
|
||||||
minX: 0,
|
minX: 0,
|
||||||
titlesData: FlTitlesData(
|
titlesData: FlTitlesData(
|
||||||
topTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
|
topTitles: AxisTitles(sideTitles: SideTitles(showTitles: false)),
|
||||||
|
|
Loading…
Reference in a new issue