selfprivacy.org.app/lib/ui/pages/server_details/charts/chart.dart

156 lines
3.9 KiB
Dart
Raw Normal View History

2022-09-15 18:40:02 +03:00
part of '../server_details_screen.dart';
2021-04-10 06:04:23 +03:00
class _Chart extends StatelessWidget {
@override
2022-06-05 22:36:32 +03:00
Widget build(final BuildContext context) {
final HetznerMetricsCubit cubit = context.watch<HetznerMetricsCubit>();
final Period period = cubit.state.period;
final HetznerMetricsState state = cubit.state;
2021-04-10 06:04:23 +03:00
List<Widget> charts;
if (state is HetznerMetricsLoading) {
charts = [
Container(
height: 200,
alignment: Alignment.center,
child: Text('basis.loading'.tr()),
)
];
} else if (state is HetznerMetricsLoaded) {
charts = [
FilledCard(
2022-09-15 18:40:02 +03:00
clipped: false,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'CPU Usage',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
color: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 16),
getCpuChart(state),
],
),
),
),
2022-05-24 21:55:39 +03:00
const SizedBox(height: 1),
2021-04-10 06:04:23 +03:00
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
BrandText.small('Public Network interface bytes per sec'),
2022-05-24 21:55:39 +03:00
const SizedBox(width: 10),
const Legend(color: Colors.red, text: 'IN'),
const SizedBox(width: 5),
const Legend(color: Colors.green, text: 'OUT'),
2021-04-10 06:04:23 +03:00
],
),
2022-05-24 21:55:39 +03:00
const SizedBox(height: 20),
2021-04-10 06:04:23 +03:00
getBandwidthChart(state),
];
} else {
throw 'wrong state';
}
return Column(
children: [
SegmentedButtons(
isSelected: [
period == Period.month,
period == Period.day,
period == Period.hour,
],
onPressed: (final index) {
switch (index) {
case 0:
cubit.changePeriod(Period.month);
break;
case 1:
cubit.changePeriod(Period.day);
break;
case 2:
cubit.changePeriod(Period.hour);
break;
}
},
titles: [
'providers.server.chart.month'.tr(),
'providers.server.chart.day'.tr(),
'providers.server.chart.hour'.tr()
],
),
...charts,
],
2021-04-10 06:04:23 +03:00
);
}
2022-06-05 22:36:32 +03:00
Widget getCpuChart(final HetznerMetricsLoaded state) {
final data = state.cpu;
2021-04-10 06:04:23 +03:00
2022-05-24 21:55:39 +03:00
return SizedBox(
2021-04-10 06:04:23 +03:00
height: 200,
child: CpuChart(
data: data,
period: state.period,
start: state.start,
),
2021-04-10 06:04:23 +03:00
);
}
2022-06-05 22:36:32 +03:00
Widget getBandwidthChart(final HetznerMetricsLoaded state) {
final ppsIn = state.bandwidthIn;
final ppsOut = state.bandwidthOut;
2021-04-10 06:04:23 +03:00
2022-05-24 21:55:39 +03:00
return SizedBox(
2021-04-10 06:04:23 +03:00
height: 200,
child: NetworkChart(
listData: [ppsIn, ppsOut],
period: state.period,
start: state.start,
2021-04-10 06:04:23 +03:00
),
);
}
}
class Legend extends StatelessWidget {
const Legend({
required this.color,
required this.text,
final super.key,
});
2021-04-10 06:04:23 +03:00
final String text;
final Color color;
@override
Widget build(final BuildContext context) => Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
_ColoredBox(color: color),
const SizedBox(width: 5),
BrandText.small(text),
],
);
2021-04-10 06:04:23 +03:00
}
class _ColoredBox extends StatelessWidget {
const _ColoredBox({
required this.color,
});
2021-04-10 06:04:23 +03:00
final Color color;
@override
Widget build(final BuildContext context) => Container(
width: 10,
height: 10,
decoration: BoxDecoration(
2021-04-10 06:04:23 +03:00
color: color.withOpacity(0.3),
border: Border.all(
color: color,
),
),
);
2021-04-10 06:04:23 +03:00
}