From 7b83ba7315df472273c0403c41a8c339723e6c8f Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 28 Feb 2024 15:37:34 -0500 Subject: [PATCH 1/9] Refactor to use modified dt for _lastPing --- pkgs/unified_analytics/lib/src/analytics.dart | 5 ++ pkgs/unified_analytics/lib/src/constants.dart | 2 +- .../lib/src/initializer.dart | 7 +-- pkgs/unified_analytics/lib/src/session.dart | 51 ++++++------------- 4 files changed, 23 insertions(+), 42 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 0c3a98721..31f2c5de8 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -430,6 +430,11 @@ class AnalyticsImpl implements Analytics { homeDirectory: homeDirectory, fs: fs, errorHandler: _errorHandler, + sessionFile: fs.file(p.join( + homeDirectory.path, + kDartToolDirectoryName, + kSessionFileName, + )), ); userProperty = UserProperty( session: _sessionHandler, diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index 4dfca3e2e..8c3193a67 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -88,7 +88,7 @@ const String kPackageVersion = '5.8.5'; const int kSessionDurationMinutes = 30; /// Name for the json file where the session details will be stored. -const String kSessionFileName = 'dart-flutter-telemetry-session.json'; +const String kSessionFileName = 'dart-flutter-telemetry.session'; /// The message that should be shown to the user. const String kToolsMessage = ''' diff --git a/pkgs/unified_analytics/lib/src/initializer.dart b/pkgs/unified_analytics/lib/src/initializer.dart index 22898a92d..2e7bbb0f3 100644 --- a/pkgs/unified_analytics/lib/src/initializer.dart +++ b/pkgs/unified_analytics/lib/src/initializer.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'dart:convert'; - import 'package:clock/clock.dart'; import 'package:file/file.dart'; import 'package:path/path.dart' as p; @@ -82,10 +80,7 @@ class Initializer { static void createSessionFile({required File sessionFile}) { final now = clock.now(); sessionFile.createSync(recursive: true); - sessionFile.writeAsStringSync(jsonEncode({ - 'session_id': now.millisecondsSinceEpoch, - 'last_ping': now.millisecondsSinceEpoch, - })); + sessionFile.writeAsStringSync('${now.millisecondsSinceEpoch}'); } /// This will check that there is a client ID populated in diff --git a/pkgs/unified_analytics/lib/src/session.dart b/pkgs/unified_analytics/lib/src/session.dart index 80305ee18..f9cbacdf3 100644 --- a/pkgs/unified_analytics/lib/src/session.dart +++ b/pkgs/unified_analytics/lib/src/session.dart @@ -2,11 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'dart:convert'; - import 'package:clock/clock.dart'; import 'package:file/file.dart'; -import 'package:path/path.dart' as p; import 'constants.dart'; import 'error_handler.dart'; @@ -19,19 +16,18 @@ class Session { final File sessionFile; final ErrorHandler _errorHandler; - late int _sessionId; - late int _lastPing; + int _sessionId; Session({ required this.homeDirectory, required this.fs, required ErrorHandler errorHandler, - }) : sessionFile = fs.file(p.join( - homeDirectory.path, kDartToolDirectoryName, kSessionFileName)), + required this.sessionFile, + }) : _sessionId = int.parse(sessionFile.readAsStringSync()), _errorHandler = errorHandler; /// This will use the data parsed from the - /// session json file in the dart-tool directory + /// session file in the dart-tool directory /// to get the session id if the last ping was within /// [kSessionDurationMinutes]. /// @@ -46,24 +42,22 @@ class Session { // Convert the epoch time from the last ping into datetime and check if we // are within the kSessionDurationMinutes. - final lastPingDateTime = DateTime.fromMillisecondsSinceEpoch(_lastPing); + final lastPingDateTime = DateTime.fromMillisecondsSinceEpoch( + sessionFile.lastModifiedSync().millisecondsSinceEpoch); if (now.difference(lastPingDateTime).inMinutes > kSessionDurationMinutes) { - // In this case, we will need to change both the session id - // and the last ping value + // Update the session file with the latest session id _sessionId = now.millisecondsSinceEpoch; + sessionFile.writeAsStringSync('$_sessionId'); } - // Update the last ping to reflect session activity continuing - _lastPing = now.millisecondsSinceEpoch; - - // Rewrite the session object back to the file to persist - // for future events - sessionFile.writeAsStringSync(toJson()); + // Update the last modified timestamp with the current timestamp so that + // we can use it for the next _lastPing calculation + sessionFile.setLastModifiedSync(now); return _sessionId; } - /// Preps the [Session] class with the data found in the session json file. + /// Preps the [Session] class with the data found in the session file. /// /// We must check if telemetry is enabled to refresh the session data /// because the refresh method will write to the session file and for @@ -73,15 +67,9 @@ class Session { if (telemetryEnabled) _refreshSessionData(); } - /// Return a json formatted representation of the class. - String toJson() => jsonEncode({ - 'session_id': _sessionId, - 'last_ping': _lastPing, - }); - /// This will go to the session file within the dart-tool - /// directory and fetch the latest data from the json to update - /// the class's variables. If the json file is malformed, a new + /// directory and fetch the latest data from the session file to update + /// the class's variables. If the session file is malformed, a new /// session file will be recreated. /// /// This allows the session data in this class to always be up @@ -89,13 +77,8 @@ class Session { /// making updates to the session file. void _refreshSessionData() { /// Using a nested function here to reduce verbosity - void parseContents() { - final sessionFileContents = sessionFile.readAsStringSync(); - final sessionObj = - jsonDecode(sessionFileContents) as Map; - _sessionId = sessionObj['session_id'] as int; - _lastPing = sessionObj['last_ping'] as int; - } + void parseContents() => + _sessionId = int.parse(sessionFile.readAsStringSync()); try { parseContents(); @@ -111,7 +94,6 @@ class Session { // Fallback to setting the session id as the current time final now = clock.now(); _sessionId = now.millisecondsSinceEpoch; - _lastPing = now.millisecondsSinceEpoch; } on FileSystemException catch (err) { Initializer.createSessionFile(sessionFile: sessionFile); @@ -124,7 +106,6 @@ class Session { // Fallback to setting the session id as the current time final now = clock.now(); _sessionId = now.millisecondsSinceEpoch; - _lastPing = now.millisecondsSinceEpoch; } } } From 09e168093742b6c1c6e44399cdd41f0f0b9102f9 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:03:48 -0500 Subject: [PATCH 2/9] Fix tests + misc dartdoc fixes --- .../lib/src/initializer.dart | 6 +-- pkgs/unified_analytics/lib/src/session.dart | 2 +- .../test/unified_analytics_test.dart | 46 ++++++------------- 3 files changed, 17 insertions(+), 37 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/initializer.dart b/pkgs/unified_analytics/lib/src/initializer.dart index 2e7bbb0f3..64d3a0f2c 100644 --- a/pkgs/unified_analytics/lib/src/initializer.dart +++ b/pkgs/unified_analytics/lib/src/initializer.dart @@ -73,10 +73,8 @@ class Initializer { logFile.createSync(recursive: true); } - /// Creates the session json file which will contain - /// the current session id along with the timestamp for - /// the last ping which will be used to increment the session - /// if current timestamp is greater than the session window. + /// Creates the session file which will contain + /// the current session id which is the current timestamp. static void createSessionFile({required File sessionFile}) { final now = clock.now(); sessionFile.createSync(recursive: true); diff --git a/pkgs/unified_analytics/lib/src/session.dart b/pkgs/unified_analytics/lib/src/session.dart index f9cbacdf3..103f62706 100644 --- a/pkgs/unified_analytics/lib/src/session.dart +++ b/pkgs/unified_analytics/lib/src/session.dart @@ -23,7 +23,7 @@ class Session { required this.fs, required ErrorHandler errorHandler, required this.sessionFile, - }) : _sessionId = int.parse(sessionFile.readAsStringSync()), + }) : _sessionId = DateTime.now().millisecondsSinceEpoch, _errorHandler = errorHandler; /// This will use the data parsed from the diff --git a/pkgs/unified_analytics/test/unified_analytics_test.dart b/pkgs/unified_analytics/test/unified_analytics_test.dart index 39046f74d..c7ae7a387 100644 --- a/pkgs/unified_analytics/test/unified_analytics_test.dart +++ b/pkgs/unified_analytics/test/unified_analytics_test.dart @@ -132,7 +132,7 @@ void main() { test('Resetting session file when data is malformed', () { // Purposefully write content to the session file that - // can't be decoded as json + // can't be decoded as an integer sessionFile.writeAsStringSync('contents'); // Define the initial time to start @@ -143,8 +143,7 @@ void main() { final timestamp = clock.now().millisecondsSinceEpoch.toString(); expect(sessionFile.readAsStringSync(), 'contents'); analytics.userProperty.preparePayload(); - expect(sessionFile.readAsStringSync(), - '{"session_id":$timestamp,"last_ping":$timestamp}'); + expect(sessionFile.readAsStringSync(), timestamp); // Attempting to fetch the session id when malformed should also // send an error event while parsing @@ -158,9 +157,9 @@ void main() { test('Handles malformed session file on startup', () { // Ensure that we are able to send an error message on startup if - // we encounter an error while parsing the contents of the json file + // we encounter an error while parsing the contents of the session file // for session data - sessionFile.writeAsStringSync('contents'); + sessionFile.writeAsStringSync('not a valid session id'); analytics = Analytics.test( tool: initialTool, @@ -185,7 +184,7 @@ void main() { expect(errorEvent!.eventData['workflow'], 'Session._refreshSessionData'); expect(errorEvent.eventData['error'], 'FormatException'); expect(errorEvent.eventData['description'], - 'message: Unexpected character\nsource: contents'); + 'message: Invalid radix-10 number\nsource: not a valid session id'); }); test('Resetting session file when file is removed', () { @@ -200,8 +199,7 @@ void main() { final timestamp = clock.now().millisecondsSinceEpoch.toString(); expect(sessionFile.existsSync(), false); analytics.userProperty.preparePayload(); - expect(sessionFile.readAsStringSync(), - '{"session_id":$timestamp,"last_ping":$timestamp}'); + expect(sessionFile.readAsStringSync(), timestamp); // Attempting to fetch the session id when malformed should also // send an error event while parsing @@ -701,14 +699,10 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion ); secondAnalytics.clientShowedMessage(); - // Read the contents of the session file - final sessionFileContents = sessionFile.readAsStringSync(); - final sessionObj = - jsonDecode(sessionFileContents) as Map; - expect(secondAnalytics.userPropertyMap['session_id']?['value'], start.millisecondsSinceEpoch); - expect(sessionObj['last_ping'], start.millisecondsSinceEpoch); + expect(sessionFile.lastModifiedSync().millisecondsSinceEpoch, + start.millisecondsSinceEpoch); }); // Add time to the start time that is less than the duration @@ -739,16 +733,12 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion // no events will be sent thirdAnalytics.send(testEvent); - // Read the contents of the session file - final sessionFileContents = sessionFile.readAsStringSync(); - final sessionObj = - jsonDecode(sessionFileContents) as Map; - expect(thirdAnalytics.userPropertyMap['session_id']?['value'], start.millisecondsSinceEpoch, reason: 'The session id should not have changed since it was made ' 'within the duration'); - expect(sessionObj['last_ping'], end.millisecondsSinceEpoch, + expect(sessionFile.lastModifiedSync().millisecondsSinceEpoch, + end.millisecondsSinceEpoch, reason: 'The last_ping value should have been updated'); }); }); @@ -782,14 +772,10 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion ); secondAnalytics.clientShowedMessage(); - // Read the contents of the session file - final sessionFileContents = sessionFile.readAsStringSync(); - final sessionObj = - jsonDecode(sessionFileContents) as Map; - expect(secondAnalytics.userPropertyMap['session_id']?['value'], start.millisecondsSinceEpoch); - expect(sessionObj['last_ping'], start.millisecondsSinceEpoch); + expect(sessionFile.lastModifiedSync().millisecondsSinceEpoch, + start.millisecondsSinceEpoch); secondAnalytics.send(testEvent); }); @@ -822,16 +808,12 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion // no events will be sent thirdAnalytics.send(testEvent); - // Read the contents of the session file - final sessionFileContents = sessionFile.readAsStringSync(); - final sessionObj = - jsonDecode(sessionFileContents) as Map; - expect(thirdAnalytics.userPropertyMap['session_id']?['value'], end.millisecondsSinceEpoch, reason: 'The session id should have changed since it was made ' 'outside the duration'); - expect(sessionObj['last_ping'], end.millisecondsSinceEpoch, + expect(sessionFile.lastModifiedSync().millisecondsSinceEpoch, + end.millisecondsSinceEpoch, reason: 'The last_ping value should have been updated'); }); }); From 09caa2a98b4dff6024a3bb872d219f291808a0d8 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:07:30 -0500 Subject: [PATCH 3/9] Bump version --- pkgs/unified_analytics/CHANGELOG.md | 4 ++++ pkgs/unified_analytics/lib/src/constants.dart | 2 +- pkgs/unified_analytics/pubspec.yaml | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkgs/unified_analytics/CHANGELOG.md b/pkgs/unified_analytics/CHANGELOG.md index a35dcd692..86fb6ac25 100644 --- a/pkgs/unified_analytics/CHANGELOG.md +++ b/pkgs/unified_analytics/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.8.6 + +- Refactored session handler class to use the last modified timestamp as the last ping value + ## 5.8.5 - Fix late initialization error for `Analytics.userProperty` [bug](https://github.com/dart-lang/tools/issues/238) diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index 8c3193a67..9f85b90a1 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -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.5'; +const String kPackageVersion = '5.8.6'; /// The minimum length for a session. const int kSessionDurationMinutes = 30; diff --git a/pkgs/unified_analytics/pubspec.yaml b/pkgs/unified_analytics/pubspec.yaml index 8cd92f041..121c92170 100644 --- a/pkgs/unified_analytics/pubspec.yaml +++ b/pkgs/unified_analytics/pubspec.yaml @@ -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.5 +version: 5.8.6 repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics environment: From 3f7ee72d76a97e22ee6ce0656b13d5d1ac2b75ec Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:15:49 -0500 Subject: [PATCH 4/9] Create `sessionFile` in constructor --- pkgs/unified_analytics/lib/src/analytics.dart | 5 ----- pkgs/unified_analytics/lib/src/session.dart | 6 ++++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 31f2c5de8..0c3a98721 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -430,11 +430,6 @@ class AnalyticsImpl implements Analytics { homeDirectory: homeDirectory, fs: fs, errorHandler: _errorHandler, - sessionFile: fs.file(p.join( - homeDirectory.path, - kDartToolDirectoryName, - kSessionFileName, - )), ); userProperty = UserProperty( session: _sessionHandler, diff --git a/pkgs/unified_analytics/lib/src/session.dart b/pkgs/unified_analytics/lib/src/session.dart index 103f62706..cf09a00d8 100644 --- a/pkgs/unified_analytics/lib/src/session.dart +++ b/pkgs/unified_analytics/lib/src/session.dart @@ -4,6 +4,7 @@ import 'package:clock/clock.dart'; import 'package:file/file.dart'; +import 'package:path/path.dart' as p; import 'constants.dart'; import 'error_handler.dart'; @@ -22,8 +23,9 @@ class Session { required this.homeDirectory, required this.fs, required ErrorHandler errorHandler, - required this.sessionFile, - }) : _sessionId = DateTime.now().millisecondsSinceEpoch, + }) : sessionFile = fs.file(p.join( + homeDirectory.path, kDartToolDirectoryName, kSessionFileName)), + _sessionId = DateTime.now().millisecondsSinceEpoch, _errorHandler = errorHandler; /// This will use the data parsed from the From 00568cbea6c4caa81d18fcc55ac1a8db37bf5b8b Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:30:07 -0500 Subject: [PATCH 5/9] Simplify `lastPingDateTime` --- pkgs/unified_analytics/lib/src/session.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/session.dart b/pkgs/unified_analytics/lib/src/session.dart index cf09a00d8..b5c16d155 100644 --- a/pkgs/unified_analytics/lib/src/session.dart +++ b/pkgs/unified_analytics/lib/src/session.dart @@ -44,8 +44,7 @@ class Session { // Convert the epoch time from the last ping into datetime and check if we // are within the kSessionDurationMinutes. - final lastPingDateTime = DateTime.fromMillisecondsSinceEpoch( - sessionFile.lastModifiedSync().millisecondsSinceEpoch); + final lastPingDateTime = sessionFile.lastModifiedSync(); if (now.difference(lastPingDateTime).inMinutes > kSessionDurationMinutes) { // Update the session file with the latest session id _sessionId = now.millisecondsSinceEpoch; From 66a91987426a940e1aba4cab8b88999f679dbebe Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Thu, 29 Feb 2024 10:46:35 -0500 Subject: [PATCH 6/9] Revert filename + pass session id to initializer --- pkgs/unified_analytics/lib/src/constants.dart | 2 +- .../lib/src/initializer.dart | 13 +++++++-- pkgs/unified_analytics/lib/src/session.dart | 28 ++++++++++++++----- .../test/unified_analytics_test.dart | 6 ++-- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index 9f85b90a1..2ab6bfe2e 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -88,7 +88,7 @@ const String kPackageVersion = '5.8.6'; const int kSessionDurationMinutes = 30; /// Name for the json file where the session details will be stored. -const String kSessionFileName = 'dart-flutter-telemetry.session'; +const String kSessionFileName = 'dart-flutter-telemetry-session.json'; /// The message that should be shown to the user. const String kToolsMessage = ''' diff --git a/pkgs/unified_analytics/lib/src/initializer.dart b/pkgs/unified_analytics/lib/src/initializer.dart index 64d3a0f2c..4f84ef1d5 100644 --- a/pkgs/unified_analytics/lib/src/initializer.dart +++ b/pkgs/unified_analytics/lib/src/initializer.dart @@ -75,10 +75,17 @@ class Initializer { /// Creates the session file which will contain /// the current session id which is the current timestamp. - static void createSessionFile({required File sessionFile}) { - final now = clock.now(); + /// + /// [sessionIdOverride] can be provided as an override, otherwise it + /// will use the current timestamp from [Clock.now]. + static void createSessionFile({ + required File sessionFile, + DateTime? sessionIdOverride, + }) { + final now = sessionIdOverride ?? clock.now(); sessionFile.createSync(recursive: true); - sessionFile.writeAsStringSync('${now.millisecondsSinceEpoch}'); + sessionFile + .writeAsStringSync('{"session_id": ${now.millisecondsSinceEpoch}}'); } /// This will check that there is a client ID populated in diff --git a/pkgs/unified_analytics/lib/src/session.dart b/pkgs/unified_analytics/lib/src/session.dart index b5c16d155..a1d57d841 100644 --- a/pkgs/unified_analytics/lib/src/session.dart +++ b/pkgs/unified_analytics/lib/src/session.dart @@ -2,6 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:convert'; + import 'package:clock/clock.dart'; import 'package:file/file.dart'; import 'package:path/path.dart' as p; @@ -48,7 +50,7 @@ class Session { if (now.difference(lastPingDateTime).inMinutes > kSessionDurationMinutes) { // Update the session file with the latest session id _sessionId = now.millisecondsSinceEpoch; - sessionFile.writeAsStringSync('$_sessionId'); + sessionFile.writeAsStringSync('{"session_id": $_sessionId}'); } // Update the last modified timestamp with the current timestamp so that @@ -78,13 +80,23 @@ class Session { /// making updates to the session file. void _refreshSessionData() { /// Using a nested function here to reduce verbosity - void parseContents() => - _sessionId = int.parse(sessionFile.readAsStringSync()); + void parseContents() { + final sessionFileContents = sessionFile.readAsStringSync(); + final sessionObj = + jsonDecode(sessionFileContents) as Map; + _sessionId = sessionObj['session_id'] as int; + } try { + // Failing to parse the contents will result in the current timestamp + // being used as the session id and will get used to recreate the file parseContents(); } on FormatException catch (err) { - Initializer.createSessionFile(sessionFile: sessionFile); + final now = clock.now(); + Initializer.createSessionFile( + sessionFile: sessionFile, + sessionIdOverride: now, + ); _errorHandler.log(Event.analyticsException( workflow: 'Session._refreshSessionData', @@ -93,10 +105,13 @@ class Session { )); // Fallback to setting the session id as the current time - final now = clock.now(); _sessionId = now.millisecondsSinceEpoch; } on FileSystemException catch (err) { - Initializer.createSessionFile(sessionFile: sessionFile); + final now = clock.now(); + Initializer.createSessionFile( + sessionFile: sessionFile, + sessionIdOverride: now, + ); _errorHandler.log(Event.analyticsException( workflow: 'Session._refreshSessionData', @@ -105,7 +120,6 @@ class Session { )); // Fallback to setting the session id as the current time - final now = clock.now(); _sessionId = now.millisecondsSinceEpoch; } } diff --git a/pkgs/unified_analytics/test/unified_analytics_test.dart b/pkgs/unified_analytics/test/unified_analytics_test.dart index c7ae7a387..b68b4569b 100644 --- a/pkgs/unified_analytics/test/unified_analytics_test.dart +++ b/pkgs/unified_analytics/test/unified_analytics_test.dart @@ -143,7 +143,7 @@ void main() { final timestamp = clock.now().millisecondsSinceEpoch.toString(); expect(sessionFile.readAsStringSync(), 'contents'); analytics.userProperty.preparePayload(); - expect(sessionFile.readAsStringSync(), timestamp); + expect(sessionFile.readAsStringSync(), '{"session_id": $timestamp}'); // Attempting to fetch the session id when malformed should also // send an error event while parsing @@ -184,7 +184,7 @@ void main() { expect(errorEvent!.eventData['workflow'], 'Session._refreshSessionData'); expect(errorEvent.eventData['error'], 'FormatException'); expect(errorEvent.eventData['description'], - 'message: Invalid radix-10 number\nsource: not a valid session id'); + 'message: Unexpected character\nsource: not a valid session id'); }); test('Resetting session file when file is removed', () { @@ -199,7 +199,7 @@ void main() { final timestamp = clock.now().millisecondsSinceEpoch.toString(); expect(sessionFile.existsSync(), false); analytics.userProperty.preparePayload(); - expect(sessionFile.readAsStringSync(), timestamp); + expect(sessionFile.readAsStringSync(), '{"session_id": $timestamp}'); // Attempting to fetch the session id when malformed should also // send an error event while parsing From 60bc72250959b5d58e34321babe5194323f1e732 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Mon, 4 Mar 2024 15:00:28 -0500 Subject: [PATCH 7/9] Use nullable field for `_sessionId` --- pkgs/unified_analytics/lib/src/analytics.dart | 4 ++-- pkgs/unified_analytics/lib/src/session.dart | 5 ++--- pkgs/unified_analytics/test/unified_analytics_test.dart | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 0c3a98721..74e455991 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -454,8 +454,8 @@ class AnalyticsImpl implements Analytics { errorHandler: _errorHandler, ); - // Initialize the session handler with the session_id and last_ping - // variables by parsing the json file + // Initialize the session handler with the session_id + // by parsing the json file _sessionHandler.initialize(telemetryEnabled); } diff --git a/pkgs/unified_analytics/lib/src/session.dart b/pkgs/unified_analytics/lib/src/session.dart index a1d57d841..3b4345288 100644 --- a/pkgs/unified_analytics/lib/src/session.dart +++ b/pkgs/unified_analytics/lib/src/session.dart @@ -19,7 +19,7 @@ class Session { final File sessionFile; final ErrorHandler _errorHandler; - int _sessionId; + int? _sessionId; Session({ required this.homeDirectory, @@ -27,7 +27,6 @@ class Session { required ErrorHandler errorHandler, }) : sessionFile = fs.file(p.join( homeDirectory.path, kDartToolDirectoryName, kSessionFileName)), - _sessionId = DateTime.now().millisecondsSinceEpoch, _errorHandler = errorHandler; /// This will use the data parsed from the @@ -40,7 +39,7 @@ class Session { /// /// Note, the file will always be updated when calling this method /// because the last ping variable will always need to be persisted. - int getSessionId() { + int? getSessionId() { _refreshSessionData(); final now = clock.now(); diff --git a/pkgs/unified_analytics/test/unified_analytics_test.dart b/pkgs/unified_analytics/test/unified_analytics_test.dart index b68b4569b..8c0849338 100644 --- a/pkgs/unified_analytics/test/unified_analytics_test.dart +++ b/pkgs/unified_analytics/test/unified_analytics_test.dart @@ -739,7 +739,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion 'within the duration'); expect(sessionFile.lastModifiedSync().millisecondsSinceEpoch, end.millisecondsSinceEpoch, - reason: 'The last_ping value should have been updated'); + reason: 'The last modified value should have been updated'); }); }); @@ -814,7 +814,7 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion 'outside the duration'); expect(sessionFile.lastModifiedSync().millisecondsSinceEpoch, end.millisecondsSinceEpoch, - reason: 'The last_ping value should have been updated'); + reason: 'The last modified value should have been updated'); }); }); From ff2578aeaf3b383c04e43bcdcec88ce0167b1061 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Mon, 4 Mar 2024 15:01:24 -0500 Subject: [PATCH 8/9] Format fix --- pkgs/unified_analytics/lib/src/analytics.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/unified_analytics/lib/src/analytics.dart b/pkgs/unified_analytics/lib/src/analytics.dart index 74e455991..0a28a40c6 100644 --- a/pkgs/unified_analytics/lib/src/analytics.dart +++ b/pkgs/unified_analytics/lib/src/analytics.dart @@ -454,7 +454,7 @@ class AnalyticsImpl implements Analytics { errorHandler: _errorHandler, ); - // Initialize the session handler with the session_id + // Initialize the session handler with the session_id // by parsing the json file _sessionHandler.initialize(telemetryEnabled); } From aac63d2e4dd0442e8920ce780df4abc477e76f1d Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 6 Mar 2024 16:27:20 -0500 Subject: [PATCH 9/9] Change to wip + use `else` block for timestamp --- pkgs/unified_analytics/CHANGELOG.md | 2 +- pkgs/unified_analytics/lib/src/constants.dart | 2 +- pkgs/unified_analytics/lib/src/session.dart | 8 ++++---- pkgs/unified_analytics/pubspec.yaml | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkgs/unified_analytics/CHANGELOG.md b/pkgs/unified_analytics/CHANGELOG.md index 86fb6ac25..fd40259c2 100644 --- a/pkgs/unified_analytics/CHANGELOG.md +++ b/pkgs/unified_analytics/CHANGELOG.md @@ -1,4 +1,4 @@ -## 5.8.6 +## 5.8.6-wip - Refactored session handler class to use the last modified timestamp as the last ping value diff --git a/pkgs/unified_analytics/lib/src/constants.dart b/pkgs/unified_analytics/lib/src/constants.dart index 2ab6bfe2e..e9034bbb1 100644 --- a/pkgs/unified_analytics/lib/src/constants.dart +++ b/pkgs/unified_analytics/lib/src/constants.dart @@ -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.6-wip'; /// The minimum length for a session. const int kSessionDurationMinutes = 30; diff --git a/pkgs/unified_analytics/lib/src/session.dart b/pkgs/unified_analytics/lib/src/session.dart index 3b4345288..d19b749c5 100644 --- a/pkgs/unified_analytics/lib/src/session.dart +++ b/pkgs/unified_analytics/lib/src/session.dart @@ -50,12 +50,12 @@ class Session { // Update the session file with the latest session id _sessionId = now.millisecondsSinceEpoch; sessionFile.writeAsStringSync('{"session_id": $_sessionId}'); + } else { + // Update the last modified timestamp with the current timestamp so that + // we can use it for the next _lastPing calculation + sessionFile.setLastModifiedSync(now); } - // Update the last modified timestamp with the current timestamp so that - // we can use it for the next _lastPing calculation - sessionFile.setLastModifiedSync(now); - return _sessionId; } diff --git a/pkgs/unified_analytics/pubspec.yaml b/pkgs/unified_analytics/pubspec.yaml index 121c92170..6c4f24809 100644 --- a/pkgs/unified_analytics/pubspec.yaml +++ b/pkgs/unified_analytics/pubspec.yaml @@ -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.6-wip repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics environment: