2022-08-24 05:35:49 +00:00
|
|
|
import 'package:selfprivacy/logic/models/disk_size.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/server_disk_volume.dart';
|
|
|
|
|
2022-07-31 23:10:37 +00:00
|
|
|
class DiskVolume {
|
2022-09-06 10:27:18 +00:00
|
|
|
DiskVolume({
|
|
|
|
this.name = '',
|
|
|
|
this.sizeTotal = const DiskSize(byte: 0),
|
|
|
|
this.sizeUsed = const DiskSize(byte: 0),
|
|
|
|
this.root = false,
|
|
|
|
this.isResizable = false,
|
|
|
|
this.serverDiskVolume,
|
|
|
|
this.providerVolume,
|
|
|
|
});
|
|
|
|
|
|
|
|
DiskVolume.fromServerDiscVolume(
|
|
|
|
final ServerDiskVolume volume,
|
|
|
|
final ServerVolume? providerVolume,
|
|
|
|
) : this(
|
|
|
|
name: volume.name,
|
|
|
|
sizeTotal: DiskSize(
|
|
|
|
byte:
|
|
|
|
volume.totalSpace == 'None' ? 0 : int.parse(volume.totalSpace),
|
|
|
|
),
|
|
|
|
sizeUsed: DiskSize(
|
|
|
|
byte: volume.usedSpace == 'None' ? 0 : int.parse(volume.usedSpace),
|
|
|
|
),
|
|
|
|
root: volume.root,
|
|
|
|
isResizable: providerVolume != null,
|
|
|
|
serverDiskVolume: volume,
|
|
|
|
providerVolume: providerVolume,
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Get the display name of the volume
|
|
|
|
///
|
|
|
|
/// If it is sda1 and root the name is "System disk"
|
|
|
|
/// If there is a mapping to providerVolume, the name is "Expandable volume"
|
|
|
|
/// Otherwise the name is the name of the volume
|
|
|
|
String get displayName {
|
|
|
|
if (root) {
|
|
|
|
return 'System disk';
|
|
|
|
} else if (providerVolume != null) {
|
|
|
|
return 'Expandable volume';
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DiskSize sizeUsed;
|
|
|
|
DiskSize sizeTotal;
|
|
|
|
String name;
|
|
|
|
bool root;
|
|
|
|
bool isResizable;
|
2022-08-24 05:35:49 +00:00
|
|
|
ServerDiskVolume? serverDiskVolume;
|
|
|
|
ServerVolume? providerVolume;
|
2022-07-31 23:10:37 +00:00
|
|
|
|
|
|
|
/// from 0.0 to 1.0
|
2022-09-06 10:27:18 +00:00
|
|
|
double get percentage =>
|
|
|
|
sizeTotal.byte == 0 ? 0 : sizeUsed.byte / sizeTotal.byte;
|
|
|
|
bool get isDiskOkay =>
|
|
|
|
percentage < 0.8 && sizeTotal.gibibyte - sizeUsed.gibibyte > 2.0;
|
2022-09-14 16:46:38 +00:00
|
|
|
|
|
|
|
DiskVolume copyWith({
|
|
|
|
final DiskSize? sizeUsed,
|
|
|
|
final DiskSize? sizeTotal,
|
|
|
|
final String? name,
|
|
|
|
final bool? root,
|
|
|
|
final bool? isResizable,
|
|
|
|
final ServerDiskVolume? serverDiskVolume,
|
|
|
|
final ServerVolume? providerVolume,
|
2022-09-15 21:08:32 +00:00
|
|
|
}) =>
|
|
|
|
DiskVolume(
|
|
|
|
sizeUsed: sizeUsed ?? this.sizeUsed,
|
|
|
|
sizeTotal: sizeTotal ?? this.sizeTotal,
|
|
|
|
name: name ?? this.name,
|
|
|
|
root: root ?? this.root,
|
|
|
|
isResizable: isResizable ?? this.isResizable,
|
|
|
|
serverDiskVolume: serverDiskVolume ?? this.serverDiskVolume,
|
|
|
|
providerVolume: providerVolume ?? this.providerVolume,
|
|
|
|
);
|
2022-07-31 23:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DiskStatus {
|
2022-09-02 06:05:35 +00:00
|
|
|
DiskStatus.fromVolumes(
|
|
|
|
final List<ServerDiskVolume> serverVolumes,
|
|
|
|
final List<ServerVolume> providerVolumes,
|
|
|
|
) {
|
|
|
|
diskVolumes = serverVolumes.map((
|
|
|
|
final ServerDiskVolume volume,
|
|
|
|
) {
|
2022-09-06 10:27:18 +00:00
|
|
|
ServerVolume? providerVolume;
|
2022-09-02 06:05:35 +00:00
|
|
|
|
2022-09-06 10:27:18 +00:00
|
|
|
for (final ServerVolume iterableProviderVolume in providerVolumes) {
|
|
|
|
if (iterableProviderVolume.linuxDevice == null ||
|
2022-09-02 06:05:35 +00:00
|
|
|
volume.model == null ||
|
|
|
|
volume.serial == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:03:21 +00:00
|
|
|
final String deviceId =
|
|
|
|
iterableProviderVolume.linuxDevice!.split('/').last;
|
2022-09-02 06:05:35 +00:00
|
|
|
if (deviceId.contains(volume.model!) &&
|
|
|
|
deviceId.contains(volume.serial!)) {
|
2022-09-06 10:27:18 +00:00
|
|
|
providerVolume = iterableProviderVolume;
|
2022-09-02 06:05:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:03:21 +00:00
|
|
|
final DiskVolume diskVolume =
|
|
|
|
DiskVolume.fromServerDiscVolume(volume, providerVolume);
|
2022-09-06 09:17:44 +00:00
|
|
|
|
2022-09-02 06:05:35 +00:00
|
|
|
return diskVolume;
|
|
|
|
}).toList();
|
|
|
|
}
|
2022-09-06 09:17:44 +00:00
|
|
|
DiskStatus() {
|
|
|
|
diskVolumes = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get isDiskOkay => diskVolumes.every((final volume) => volume.isDiskOkay);
|
2022-09-02 06:05:35 +00:00
|
|
|
|
2022-07-31 23:10:37 +00:00
|
|
|
List<DiskVolume> diskVolumes = [];
|
|
|
|
}
|