Skip to content

Commit

Permalink
Rewrite the last_ping key again (#255)
Browse files Browse the repository at this point in the history
* Rewrite the `last_ping` key again

* Apply test fix from #247

* Use timestamp instead of null

* Format fix

* Update version

* Code review comments

* Lingering comment
  • Loading branch information
eliasyishak authored Mar 14, 2024
1 parent af09f6d commit 0a9f87c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pkgs/unified_analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.8.7

- [Bug fix](https://github.com/dart-lang/tools/issues/252) to rewrite the `last_ping` key into the session json file

## 5.8.6

- Refactored session handler class to use the last modified timestamp as the last ping value
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const int kLogFileLength = 2500;
const String kLogFileName = 'dart-flutter-telemetry.log';

/// The current version of the package, should be in line with pubspec version.
const String kPackageVersion = '5.8.6';
const String kPackageVersion = '5.8.7';

/// The minimum length for a session.
const int kSessionDurationMinutes = 30;
Expand Down
6 changes: 5 additions & 1 deletion pkgs/unified_analytics/lib/src/initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ class Initializer {
}) {
final now = sessionIdOverride ?? clock.now();
sessionFile.createSync(recursive: true);

// `last_ping` has been deprecated, remains included for backward
// compatibility
sessionFile
.writeAsStringSync('{"session_id": ${now.millisecondsSinceEpoch}}');
.writeAsStringSync('{"session_id": ${now.millisecondsSinceEpoch}, '
'"last_ping": ${now.millisecondsSinceEpoch}}');
}

/// This will check that there is a client ID populated in
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: >-
to Google Analytics.
# When updating this, keep the version consistent with the changelog and the
# value in lib/src/constants.dart.
version: 5.8.6
version: 5.8.7
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics

environment:
Expand Down
14 changes: 12 additions & 2 deletions pkgs/unified_analytics/test/unified_analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ void main() {
final timestamp = clock.now().millisecondsSinceEpoch.toString();
expect(sessionFile.readAsStringSync(), 'contents');
analytics.userProperty.preparePayload();
expect(sessionFile.readAsStringSync(), '{"session_id": $timestamp}');
expect(sessionFile.readAsStringSync(),
'{"session_id": $timestamp, "last_ping": $timestamp}');

// Attempting to fetch the session id when malformed should also
// send an error event while parsing
Expand Down Expand Up @@ -199,7 +200,8 @@ void main() {
final timestamp = clock.now().millisecondsSinceEpoch.toString();
expect(sessionFile.existsSync(), false);
analytics.userProperty.preparePayload();
expect(sessionFile.readAsStringSync(), '{"session_id": $timestamp}');
expect(sessionFile.readAsStringSync(),
'{"session_id": $timestamp, "last_ping": $timestamp}');

// Attempting to fetch the session id when malformed should also
// send an error event while parsing
Expand Down Expand Up @@ -1031,6 +1033,14 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion

test('Null values for flutter parameters is reflected properly in log file',
() {
// Because we are using the `MemoryFileSystem.test` constructor,
// we don't have a real clock in the filesystem, and because we
// are checking the last modified timestamp for the session file
// to determine if we need to update the session id, manually setting
// that timestamp will ensure we are not updating session id when it
// first gets created
sessionFile.setLastModifiedSync(DateTime.now());

// Use a for loop two initialize the second analytics instance
// twice to account for no events being sent on the first instance
// run for a given tool
Expand Down

0 comments on commit 0a9f87c

Please sign in to comment.