From 859f392ec5a59eeeadffcba7eb1cbff6e502bc00 Mon Sep 17 00:00:00 2001 From: Bryan Oltman Date: Fri, 7 Jul 2023 14:01:47 -0400 Subject: [PATCH] feat(shorebird_code_push_protocol): add `currentPeriodCost` to GetUsageResponse (#778) Co-authored-by: Felix Angelov --- .../src/commands/account/account_usage_command.dart | 6 ++++++ .../test/src/code_push_client_wrapper_test.dart | 1 + .../commands/account/account_usage_command_test.dart | 10 ++++++++++ .../test/src/code_push_client_test.dart | 1 + .../lib/src/messages/get_usage/get_usage_response.dart | 5 +++++ .../src/messages/get_usage/get_usage_response.g.dart | 4 ++++ .../messages/get_usage/get_usage_response_test.dart | 1 + 7 files changed, 28 insertions(+) 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 7a596db4..1065d26f 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 @@ -42,6 +42,8 @@ class AccountUsageCommand extends ShorebirdCommand extension on GetUsageResponse { String prettyPrint() { + final currencyFormatter = NumberFormat('#,##0.00', 'en_US'); + const cellStyle = CellStyle( paddingLeft: 1, paddingRight: 1, @@ -60,6 +62,9 @@ extension on GetUsageResponse { : '${patchInstallLimit! - totalPatchInstalls}'; return ''' + +You are on the ${lightCyan.wrap(plan.name)} plan. + ${Table( cellStyle: cellStyle, header: const TableSection( @@ -94,6 +99,7 @@ ${Table( ${styleBold.wrap('${lightCyan.wrap(remainingPatchInstalls)} patch installs remaining in the current billing period.')} Current Billing Period: ${lightCyan.wrap(DateFormat.yMMMd().format(currentPeriodStart))} - ${lightCyan.wrap(DateFormat.yMMMd().format(currentPeriodEnd))} +Month-to-date cost: ${lightCyan.wrap('\$${currencyFormatter.format(currentPeriodCost * 100.0)}')} ${styleBold.wrap('*Usage data is not reported in real-time and may be delayed by up to 48 hours.')}'''; } 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 3880c794..11be65c6 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 @@ -1693,6 +1693,7 @@ Please bump your version number and try again.''', ), ], patchInstallLimit: 20000, + currentPeriodCost: 0, currentPeriodStart: DateTime(2023), currentPeriodEnd: DateTime(2023, 2), ); 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 2e52fdc1..d31dafd4 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 @@ -87,6 +87,7 @@ void main() { ), ], patchInstallLimit: 20000, + currentPeriodCost: 0, currentPeriodStart: DateTime(2023), currentPeriodEnd: DateTime(2023, 2), ); @@ -102,6 +103,9 @@ void main() { () => logger.info( any( that: contains(''' + +You are on the ${lightCyan.wrap('Hobby')} plan. + ┌────────────┬────────────────┐ │ App │ Patch Installs │ ├────────────┼────────────────┤ @@ -115,6 +119,7 @@ void main() { ${styleBold.wrap('${lightCyan.wrap('${20000 - 84}')} patch installs remaining in the current billing period.')} Current Billing Period: ${lightCyan.wrap(DateFormat.yMMMd().format(usage.currentPeriodStart))} - ${lightCyan.wrap(DateFormat.yMMMd().format(usage.currentPeriodEnd))} +Month-to-date cost: ${lightCyan.wrap(r'$0.00')} ${styleBold.wrap('*Usage data is not reported in real-time and may be delayed by up to 48 hours.')}'''), ), @@ -142,6 +147,7 @@ ${styleBold.wrap('*Usage data is not reported in real-time and may be delayed by patchInstallCount: 42, ), ], + currentPeriodCost: 0, currentPeriodStart: DateTime(2023), currentPeriodEnd: DateTime(2023, 2), ); @@ -157,6 +163,9 @@ ${styleBold.wrap('*Usage data is not reported in real-time and may be delayed by () => logger.info( any( that: contains(''' + +You are on the ${lightCyan.wrap('Hobby')} plan. + ┌────────────┬────────────────┐ │ App │ Patch Installs │ ├────────────┼────────────────┤ @@ -170,6 +179,7 @@ ${styleBold.wrap('*Usage data is not reported in real-time and may be delayed by ${styleBold.wrap('${lightCyan.wrap('∞')} patch installs remaining in the current billing period.')} Current Billing Period: ${lightCyan.wrap(DateFormat.yMMMd().format(usage.currentPeriodStart))} - ${lightCyan.wrap(DateFormat.yMMMd().format(usage.currentPeriodEnd))} +Month-to-date cost: ${lightCyan.wrap(r'$0.00')} ${styleBold.wrap('*Usage data is not reported in real-time and may be delayed by up to 48 hours.')}'''), ), 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 320f34be..63497017 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 @@ -1944,6 +1944,7 @@ void main() { ) ], patchInstallLimit: 1337, + currentPeriodCost: 0, currentPeriodStart: DateTime(2023), currentPeriodEnd: DateTime(2023, 2), ); 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 86e52928..90cae455 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 @@ -14,6 +14,7 @@ class GetUsageResponse { required this.apps, required this.currentPeriodStart, required this.currentPeriodEnd, + required this.currentPeriodCost, this.patchInstallLimit, }); @@ -36,6 +37,10 @@ class GetUsageResponse { /// The end of the current billing period. final DateTime currentPeriodEnd; + /// The total cost so far for the current billing period. Includes base + /// monthly cost and any overages. + final int currentPeriodCost; + /// The upper limit of patch installs for the current billing period. /// If `null`, there is no limit. final int? patchInstallLimit; 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 1e93d7c6..a37589b9 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 @@ -25,6 +25,8 @@ GetUsageResponse _$GetUsageResponseFromJson(Map json) => 'current_period_start', (v) => DateTime.parse(v as String)), currentPeriodEnd: $checkedConvert( 'current_period_end', (v) => DateTime.parse(v as String)), + currentPeriodCost: + $checkedConvert('current_period_cost', (v) => v as int), patchInstallLimit: $checkedConvert('patch_install_limit', (v) => v as int?), ); @@ -33,6 +35,7 @@ GetUsageResponse _$GetUsageResponseFromJson(Map json) => fieldKeyMap: const { 'currentPeriodStart': 'current_period_start', 'currentPeriodEnd': 'current_period_end', + 'currentPeriodCost': 'current_period_cost', 'patchInstallLimit': 'patch_install_limit' }, ); @@ -43,6 +46,7 @@ Map _$GetUsageResponseToJson(GetUsageResponse instance) => 'apps': instance.apps.map((e) => e.toJson()).toList(), 'current_period_start': instance.currentPeriodStart.toIso8601String(), 'current_period_end': instance.currentPeriodEnd.toIso8601String(), + 'current_period_cost': instance.currentPeriodCost, 'patch_install_limit': instance.patchInstallLimit, }; 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 ae3293e7..5fcaf405 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 @@ -15,6 +15,7 @@ void main() { const AppUsage(id: 'app-id', name: 'My app', patchInstallCount: 1337), ], patchInstallLimit: 42, + currentPeriodCost: 0, currentPeriodStart: DateTime(2021), currentPeriodEnd: DateTime(2021, 1, 2), );