diff --git a/packages/shorebird_cli/lib/src/commands/account/account_usage_command.dart b/packages/shorebird_cli/lib/src/commands/account/account_usage_command.dart index 66390e8f..ce4550e2 100644 --- a/packages/shorebird_cli/lib/src/commands/account/account_usage_command.dart +++ b/packages/shorebird_cli/lib/src/commands/account/account_usage_command.dart @@ -51,13 +51,7 @@ extension on List { ); 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( diff --git a/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart b/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart index a3509b98..2a28ee16 100644 --- a/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart +++ b/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart @@ -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); diff --git a/packages/shorebird_cli/test/src/commands/account/account_usage_command_test.dart b/packages/shorebird_cli/test/src/commands/account/account_usage_command_test.dart index 0fae0745..be02fefa 100644 --- a/packages/shorebird_cli/test/src/commands/account/account_usage_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/account/account_usage_command_test.dart @@ -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( diff --git a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart index 0670cb67..5ba4c0b0 100644 --- a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart +++ b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart @@ -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, diff --git a/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.dart b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.dart index 36fa83b4..10b9d6fb 100644 --- a/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.dart +++ b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.dart @@ -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 to a [GetUsageResponse]. factory GetUsageResponse.fromJson(Map json) => @@ -20,6 +25,15 @@ class GetUsageResponse { /// The usage per app. final List 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 to a [AppUsage]. factory AppUsage.fromJson(Map json) => @@ -40,72 +58,9 @@ class AppUsage { /// The id of the app. final String id; - /// The usage per platform. - final List 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 to a [PlatformUsage]. - factory PlatformUsage.fromJson(Map json) => - _$PlatformUsageFromJson(json); - - /// Converts a [PlatformUsage] to a Map. - Json toJson() => _$PlatformUsageToJson(this); - - /// The name of the platform. + /// The display name of the app. final String name; - /// The usage per arch. - final List 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 to a [ArchUsage]. - factory ArchUsage.fromJson(Map json) => - _$ArchUsageFromJson(json); - - /// Converts a [ArchUsage] to a Map. - Json toJson() => _$ArchUsageToJson(this); - - /// The name of the architecture. - final String name; - - /// The usage per patch. - final List 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 to a [PatchUsage]. - factory PatchUsage.fromJson(Map json) => - _$PatchUsageFromJson(json); - - /// Converts a [PatchUsage] to a Map. - 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; } diff --git a/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.g.dart b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.g.dart index 2655f087..66c89581 100644 --- a/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.g.dart +++ b/packages/shorebird_code_push_protocol/lib/src/messages/get_usage/get_usage_response.g.dart @@ -19,14 +19,28 @@ GetUsageResponse _$GetUsageResponseFromJson(Map json) => (v) => (v as List) .map((e) => AppUsage.fromJson(e as Map)) .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 _$GetUsageResponseToJson(GetUsageResponse instance) => { '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 json) => $checkedCreate( @@ -35,80 +49,17 @@ AppUsage _$AppUsageFromJson(Map json) => $checkedCreate( ($checkedConvert) { final val = AppUsage( id: $checkedConvert('id', (v) => v as String), - platforms: $checkedConvert( - 'platforms', - (v) => (v as List) - .map((e) => PlatformUsage.fromJson(e as Map)) - .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 _$AppUsageToJson(AppUsage instance) => { 'id': instance.id, - 'platforms': instance.platforms.map((e) => e.toJson()).toList(), - }; - -PlatformUsage _$PlatformUsageFromJson(Map json) => - $checkedCreate( - 'PlatformUsage', - json, - ($checkedConvert) { - final val = PlatformUsage( - name: $checkedConvert('name', (v) => v as String), - arches: $checkedConvert( - 'arches', - (v) => (v as List) - .map((e) => ArchUsage.fromJson(e as Map)) - .toList()), - ); - return val; - }, - ); - -Map _$PlatformUsageToJson(PlatformUsage instance) => - { 'name': instance.name, - 'arches': instance.arches.map((e) => e.toJson()).toList(), - }; - -ArchUsage _$ArchUsageFromJson(Map json) => $checkedCreate( - 'ArchUsage', - json, - ($checkedConvert) { - final val = ArchUsage( - name: $checkedConvert('name', (v) => v as String), - patches: $checkedConvert( - 'patches', - (v) => (v as List) - .map((e) => PatchUsage.fromJson(e as Map)) - .toList()), - ); - return val; - }, - ); - -Map _$ArchUsageToJson(ArchUsage instance) => { - 'name': instance.name, - 'patches': instance.patches.map((e) => e.toJson()).toList(), - }; - -PatchUsage _$PatchUsageFromJson(Map 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 _$PatchUsageToJson(PatchUsage instance) => - { - 'id': instance.id, - 'install_count': instance.installCount, + 'patch_install_count': instance.patchInstallCount, }; diff --git a/packages/shorebird_code_push_protocol/test/src/messages/get_usage/get_usage_response_test.dart b/packages/shorebird_code_push_protocol/test/src/messages/get_usage/get_usage_response_test.dart index a81802c2..c9449e6b 100644 --- a/packages/shorebird_code_push_protocol/test/src/messages/get_usage/get_usage_response_test.dart +++ b/packages/shorebird_code_push_protocol/test/src/messages/get_usage/get_usage_response_test.dart @@ -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(),