Skip to content

Commit

Permalink
Update other session file create callsite (#256)
Browse files Browse the repository at this point in the history
* Update other call site where we write the session

* Correct spelling for var

* Prep for publish

* Use a utility function to create session file
  • Loading branch information
eliasyishak authored Mar 15, 2024
1 parent 0a9f87c commit f611290
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 15 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.8

- [Bug fix](https://github.com/dart-lang/tools/issues/252) rewrite the other call site for the session file

## 5.8.7

- [Bug fix](https://github.com/dart-lang/tools/issues/252) to rewrite the `last_ping` key into the session json file
Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/lib/src/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ class AnalyticsImpl implements Analytics {
// Recreate the session and client id file; no need to
// recreate the log file since it will only receives events
// to persist from events sent
Initializer.createClientIdFile(clientFile: _clientIdFile);
Initializer.createClientIdFile(clientIdFile: _clientIdFile);
Initializer.createSessionFile(sessionFile: _sessionHandler.sessionFile);

// Reread the client ID string so an empty string is not being
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.7';
const String kPackageVersion = '5.8.8';

/// The minimum length for a session.
const int kSessionDurationMinutes = 30;
Expand Down
16 changes: 5 additions & 11 deletions pkgs/unified_analytics/lib/src/initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class Initializer {
/// Creates the text file that will contain the client ID
/// which will be used across all related tools for analytics
/// reporting in GA.
static void createClientIdFile({required File clientFile}) {
clientFile.createSync(recursive: true);
clientFile.writeAsStringSync(Uuid().generateV4());
static void createClientIdFile({required File clientIdFile}) {
clientIdFile.createSync(recursive: true);
clientIdFile.writeAsStringSync(Uuid().generateV4());
}

/// Creates the configuration file with the default message
Expand Down Expand Up @@ -82,14 +82,8 @@ class Initializer {
required File sessionFile,
DateTime? sessionIdOverride,
}) {
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}, '
'"last_ping": ${now.millisecondsSinceEpoch}}');
writeSessionContents(sessionFile: sessionFile);
}

/// This will check that there is a client ID populated in
Expand Down Expand Up @@ -120,7 +114,7 @@ class Initializer {
final clientFile = fs.file(
p.join(homeDirectory.path, kDartToolDirectoryName, kClientIdFileName));
if (!clientFile.existsSync()) {
createClientIdFile(clientFile: clientFile);
createClientIdFile(clientIdFile: clientFile);
}

// Begin initialization checks for the session file
Expand Down
3 changes: 2 additions & 1 deletion pkgs/unified_analytics/lib/src/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'constants.dart';
import 'error_handler.dart';
import 'event.dart';
import 'initializer.dart';
import 'utils.dart';

class Session {
final Directory homeDirectory;
Expand Down Expand Up @@ -49,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('{"session_id": $_sessionId}');
writeSessionContents(sessionFile: sessionFile);
} else {
// Update the last modified timestamp with the current timestamp so that
// we can use it for the next _lastPing calculation
Expand Down
10 changes: 10 additions & 0 deletions pkgs/unified_analytics/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ String truncateStringToLength(String str, int maxLength) {
return str.substring(0, maxLength);
}

/// Writes the JSON string payload to the provided [sessionFile].
///
/// The `last_ping` key:value pair has been deprecated, it remains included
/// for backward compatibility.
void writeSessionContents({required File sessionFile}) {
final now = clock.now();
sessionFile.writeAsStringSync('{"session_id": ${now.millisecondsSinceEpoch}, '
'"last_ping": ${now.millisecondsSinceEpoch}}');
}

/// A UUID generator.
///
/// This will generate unique IDs in the format:
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.7
version: 5.8.8
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics

environment:
Expand Down
4 changes: 4 additions & 0 deletions pkgs/unified_analytics/test/unified_analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,8 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
start.millisecondsSinceEpoch);

secondAnalytics.send(testEvent);
expect(sessionFile.readAsStringSync(),
'{"session_id": ${start.millisecondsSinceEpoch}, "last_ping": ${start.millisecondsSinceEpoch}}');
});

// Add time to the start time that is less than the duration
Expand Down Expand Up @@ -817,6 +819,8 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
expect(sessionFile.lastModifiedSync().millisecondsSinceEpoch,
end.millisecondsSinceEpoch,
reason: 'The last modified value should have been updated');
expect(sessionFile.readAsStringSync(),
'{"session_id": ${end.millisecondsSinceEpoch}, "last_ping": ${end.millisecondsSinceEpoch}}');
});
});

Expand Down

0 comments on commit f611290

Please sign in to comment.