refactor(code_push_protocol)!: simplify AppUsage model (#707)

This commit is contained in:
Felix Angelov
2023-06-23 15:11:11 -05:00
committed by GitHub
parent 2a6a096f8f
commit 6fbe9a6f05
7 changed files with 57 additions and 216 deletions
@@ -51,13 +51,7 @@ extension on List<AppUsage> {
);
var totalPatchInstalls = 0;
for (final appUsage in this) {
for (final platformUsage in appUsage.platforms) {
for (final archUsage in platformUsage.arches) {
for (final patchUsage in archUsage.patches) {
totalPatchInstalls += patchUsage.installCount;
}
}
}
totalPatchInstalls += appUsage.patchInstallCount;
}
return Table(
@@ -1574,34 +1574,8 @@ Please bump your version number and try again.''',
const usage = [
AppUsage(
id: 'test-app-id',
platforms: [
PlatformUsage(
name: 'android',
arches: [
ArchUsage(
name: 'aarch64',
patches: [
PatchUsage(id: 0, installCount: 10),
PatchUsage(id: 1, installCount: 10)
],
),
ArchUsage(
name: 'arm',
patches: [
PatchUsage(id: 0, installCount: 10),
PatchUsage(id: 1, installCount: 10)
],
),
ArchUsage(
name: 'x86',
patches: [
PatchUsage(id: 0, installCount: 1),
PatchUsage(id: 1, installCount: 1)
],
)
],
)
],
name: 'test app',
patchInstallCount: 42,
),
];
when(() => codePushClient.getUsage()).thenAnswer((_) async => usage);
@@ -69,34 +69,8 @@ void main() {
final usage = [
const AppUsage(
id: 'test-app-id',
platforms: [
PlatformUsage(
name: 'android',
arches: [
ArchUsage(
name: 'aarch64',
patches: [
PatchUsage(id: 0, installCount: 10),
PatchUsage(id: 1, installCount: 10)
],
),
ArchUsage(
name: 'arm',
patches: [
PatchUsage(id: 0, installCount: 10),
PatchUsage(id: 1, installCount: 10)
],
),
ArchUsage(
name: 'x86',
patches: [
PatchUsage(id: 0, installCount: 1),
PatchUsage(id: 1, installCount: 1)
],
)
],
)
],
name: 'test-app-name',
patchInstallCount: 42,
),
];
when(
@@ -1915,7 +1915,8 @@ void main() {
final expected = [
AppUsage(
id: 'test-app-id',
platforms: [],
name: 'Test App',
patchInstallCount: 42,
)
];
@@ -1923,7 +1924,9 @@ void main() {
(_) async => http.StreamedResponse(
Stream.value(
utf8.encode(
json.encode(GetUsageResponse(apps: expected)),
json.encode(
GetUsageResponse(apps: expected, patchInstallLimit: 1337),
),
),
),
HttpStatus.ok,
@@ -9,7 +9,12 @@ part 'get_usage_response.g.dart';
@JsonSerializable()
class GetUsageResponse {
/// {@macro get_usage_response}
const GetUsageResponse({required this.apps});
const GetUsageResponse({
required this.apps,
required this.patchInstallLimit,
this.currentPeriodStart,
this.currentPeriodEnd,
});
/// Converts a Map<String, dynamic> to a [GetUsageResponse].
factory GetUsageResponse.fromJson(Map<String, dynamic> json) =>
@@ -20,6 +25,15 @@ class GetUsageResponse {
/// The usage per app.
final List<AppUsage> apps;
/// The start of the current billin period.
final DateTime? currentPeriodStart;
/// The end of the current billin period.
final DateTime? currentPeriodEnd;
/// The upper limit of patch installs for the current billing period.
final int patchInstallLimit;
}
/// {@template app_usage}
@@ -28,7 +42,11 @@ class GetUsageResponse {
@JsonSerializable()
class AppUsage {
/// {@macro app_usage}
const AppUsage({required this.id, required this.platforms});
const AppUsage({
required this.id,
required this.name,
required this.patchInstallCount,
});
/// Converts a Map<String, dynamic> to a [AppUsage].
factory AppUsage.fromJson(Map<String, dynamic> json) =>
@@ -40,72 +58,9 @@ class AppUsage {
/// The id of the app.
final String id;
/// The usage per platform.
final List<PlatformUsage> platforms;
}
/// {@template platform_usage}
/// The usage for a single platform.
/// {@endtemplate}
@JsonSerializable()
class PlatformUsage {
/// {@macro platform_usage}
const PlatformUsage({required this.name, required this.arches});
/// Converts a Map<String, dynamic> to a [PlatformUsage].
factory PlatformUsage.fromJson(Map<String, dynamic> json) =>
_$PlatformUsageFromJson(json);
/// Converts a [PlatformUsage] to a Map<String, dynamic>.
Json toJson() => _$PlatformUsageToJson(this);
/// The name of the platform.
/// The display name of the app.
final String name;
/// The usage per arch.
final List<ArchUsage> arches;
}
/// {@template arch_usage}
/// The usage for a single architecture.
/// {@endtemplate}
@JsonSerializable()
class ArchUsage {
/// {@macro arch_usage}
const ArchUsage({required this.name, required this.patches});
/// Converts a Map<String, dynamic> to a [ArchUsage].
factory ArchUsage.fromJson(Map<String, dynamic> json) =>
_$ArchUsageFromJson(json);
/// Converts a [ArchUsage] to a Map<String, dynamic>.
Json toJson() => _$ArchUsageToJson(this);
/// The name of the architecture.
final String name;
/// The usage per patch.
final List<PatchUsage> patches;
}
/// {@template patch_usage}
/// The usage for a single patch.
/// {@endtemplate}
@JsonSerializable()
class PatchUsage {
/// {@macro patch_usage}
const PatchUsage({required this.id, required this.installCount});
/// Converts a Map<String, dynamic> to a [PatchUsage].
factory PatchUsage.fromJson(Map<String, dynamic> json) =>
_$PatchUsageFromJson(json);
/// Converts a [PatchUsage] to a Map<String, dynamic>.
Json toJson() => _$PatchUsageToJson(this);
/// The id of the patch.
final int id;
/// The number of times the patch has been installed.
final int installCount;
/// The number of patch installs for the app.
final int patchInstallCount;
}
@@ -19,14 +19,28 @@ GetUsageResponse _$GetUsageResponseFromJson(Map<String, dynamic> json) =>
(v) => (v as List<dynamic>)
.map((e) => AppUsage.fromJson(e as Map<String, dynamic>))
.toList()),
patchInstallLimit:
$checkedConvert('patch_install_limit', (v) => v as int),
currentPeriodStart: $checkedConvert('current_period_start',
(v) => v == null ? null : DateTime.parse(v as String)),
currentPeriodEnd: $checkedConvert('current_period_end',
(v) => v == null ? null : DateTime.parse(v as String)),
);
return val;
},
fieldKeyMap: const {
'patchInstallLimit': 'patch_install_limit',
'currentPeriodStart': 'current_period_start',
'currentPeriodEnd': 'current_period_end'
},
);
Map<String, dynamic> _$GetUsageResponseToJson(GetUsageResponse instance) =>
<String, dynamic>{
'apps': instance.apps.map((e) => e.toJson()).toList(),
'current_period_start': instance.currentPeriodStart?.toIso8601String(),
'current_period_end': instance.currentPeriodEnd?.toIso8601String(),
'patch_install_limit': instance.patchInstallLimit,
};
AppUsage _$AppUsageFromJson(Map<String, dynamic> json) => $checkedCreate(
@@ -35,80 +49,17 @@ AppUsage _$AppUsageFromJson(Map<String, dynamic> json) => $checkedCreate(
($checkedConvert) {
final val = AppUsage(
id: $checkedConvert('id', (v) => v as String),
platforms: $checkedConvert(
'platforms',
(v) => (v as List<dynamic>)
.map((e) => PlatformUsage.fromJson(e as Map<String, dynamic>))
.toList()),
name: $checkedConvert('name', (v) => v as String),
patchInstallCount:
$checkedConvert('patch_install_count', (v) => v as int),
);
return val;
},
fieldKeyMap: const {'patchInstallCount': 'patch_install_count'},
);
Map<String, dynamic> _$AppUsageToJson(AppUsage instance) => <String, dynamic>{
'id': instance.id,
'platforms': instance.platforms.map((e) => e.toJson()).toList(),
};
PlatformUsage _$PlatformUsageFromJson(Map<String, dynamic> json) =>
$checkedCreate(
'PlatformUsage',
json,
($checkedConvert) {
final val = PlatformUsage(
name: $checkedConvert('name', (v) => v as String),
arches: $checkedConvert(
'arches',
(v) => (v as List<dynamic>)
.map((e) => ArchUsage.fromJson(e as Map<String, dynamic>))
.toList()),
);
return val;
},
);
Map<String, dynamic> _$PlatformUsageToJson(PlatformUsage instance) =>
<String, dynamic>{
'name': instance.name,
'arches': instance.arches.map((e) => e.toJson()).toList(),
};
ArchUsage _$ArchUsageFromJson(Map<String, dynamic> json) => $checkedCreate(
'ArchUsage',
json,
($checkedConvert) {
final val = ArchUsage(
name: $checkedConvert('name', (v) => v as String),
patches: $checkedConvert(
'patches',
(v) => (v as List<dynamic>)
.map((e) => PatchUsage.fromJson(e as Map<String, dynamic>))
.toList()),
);
return val;
},
);
Map<String, dynamic> _$ArchUsageToJson(ArchUsage instance) => <String, dynamic>{
'name': instance.name,
'patches': instance.patches.map((e) => e.toJson()).toList(),
};
PatchUsage _$PatchUsageFromJson(Map<String, dynamic> json) => $checkedCreate(
'PatchUsage',
json,
($checkedConvert) {
final val = PatchUsage(
id: $checkedConvert('id', (v) => v as int),
installCount: $checkedConvert('install_count', (v) => v as int),
);
return val;
},
fieldKeyMap: const {'installCount': 'install_count'},
);
Map<String, dynamic> _$PatchUsageToJson(PatchUsage instance) =>
<String, dynamic>{
'id': instance.id,
'install_count': instance.installCount,
'patch_install_count': instance.patchInstallCount,
};
@@ -4,23 +4,13 @@ import 'package:test/test.dart';
void main() {
group(GetUsageResponse, () {
test('can be (de)serialized', () {
const response = GetUsageResponse(
final response = GetUsageResponse(
apps: [
AppUsage(
id: 'app-id',
platforms: [
PlatformUsage(
name: 'android',
arches: [
ArchUsage(
name: 'arm64',
patches: [PatchUsage(id: 1, installCount: 42)],
),
],
)
],
),
const AppUsage(id: 'app-id', name: 'My app', patchInstallCount: 1337),
],
patchInstallLimit: 42,
currentPeriodStart: DateTime(2021),
currentPeriodEnd: DateTime(2021, 1, 2),
);
expect(
GetUsageResponse.fromJson(response.toJson()).toJson(),