Skip to content

Commit f611290

Browse files
authored
Update other session file create callsite (#256)
* Update other call site where we write the session * Correct spelling for var * Prep for publish * Use a utility function to create session file
1 parent 0a9f87c commit f611290

File tree

8 files changed

+28
-15
lines changed

8 files changed

+28
-15
lines changed

pkgs/unified_analytics/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.8.8
2+
3+
- [Bug fix](https://github.com/dart-lang/tools/issues/252) rewrite the other call site for the session file
4+
15
## 5.8.7
26

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

pkgs/unified_analytics/lib/src/analytics.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ class AnalyticsImpl implements Analytics {
640640
// Recreate the session and client id file; no need to
641641
// recreate the log file since it will only receives events
642642
// to persist from events sent
643-
Initializer.createClientIdFile(clientFile: _clientIdFile);
643+
Initializer.createClientIdFile(clientIdFile: _clientIdFile);
644644
Initializer.createSessionFile(sessionFile: _sessionHandler.sessionFile);
645645

646646
// Reread the client ID string so an empty string is not being

pkgs/unified_analytics/lib/src/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const int kLogFileLength = 2500;
8282
const String kLogFileName = 'dart-flutter-telemetry.log';
8383

8484
/// The current version of the package, should be in line with pubspec version.
85-
const String kPackageVersion = '5.8.7';
85+
const String kPackageVersion = '5.8.8';
8686

8787
/// The minimum length for a session.
8888
const int kSessionDurationMinutes = 30;

pkgs/unified_analytics/lib/src/initializer.dart

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class Initializer {
3535
/// Creates the text file that will contain the client ID
3636
/// which will be used across all related tools for analytics
3737
/// reporting in GA.
38-
static void createClientIdFile({required File clientFile}) {
39-
clientFile.createSync(recursive: true);
40-
clientFile.writeAsStringSync(Uuid().generateV4());
38+
static void createClientIdFile({required File clientIdFile}) {
39+
clientIdFile.createSync(recursive: true);
40+
clientIdFile.writeAsStringSync(Uuid().generateV4());
4141
}
4242

4343
/// Creates the configuration file with the default message
@@ -82,14 +82,8 @@ class Initializer {
8282
required File sessionFile,
8383
DateTime? sessionIdOverride,
8484
}) {
85-
final now = sessionIdOverride ?? clock.now();
8685
sessionFile.createSync(recursive: true);
87-
88-
// `last_ping` has been deprecated, remains included for backward
89-
// compatibility
90-
sessionFile
91-
.writeAsStringSync('{"session_id": ${now.millisecondsSinceEpoch}, '
92-
'"last_ping": ${now.millisecondsSinceEpoch}}');
86+
writeSessionContents(sessionFile: sessionFile);
9387
}
9488

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

126120
// Begin initialization checks for the session file

pkgs/unified_analytics/lib/src/session.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'constants.dart';
1212
import 'error_handler.dart';
1313
import 'event.dart';
1414
import 'initializer.dart';
15+
import 'utils.dart';
1516

1617
class Session {
1718
final Directory homeDirectory;
@@ -49,7 +50,7 @@ class Session {
4950
if (now.difference(lastPingDateTime).inMinutes > kSessionDurationMinutes) {
5051
// Update the session file with the latest session id
5152
_sessionId = now.millisecondsSinceEpoch;
52-
sessionFile.writeAsStringSync('{"session_id": $_sessionId}');
53+
writeSessionContents(sessionFile: sessionFile);
5354
} else {
5455
// Update the last modified timestamp with the current timestamp so that
5556
// we can use it for the next _lastPing calculation

pkgs/unified_analytics/lib/src/utils.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ String truncateStringToLength(String str, int maxLength) {
310310
return str.substring(0, maxLength);
311311
}
312312

313+
/// Writes the JSON string payload to the provided [sessionFile].
314+
///
315+
/// The `last_ping` key:value pair has been deprecated, it remains included
316+
/// for backward compatibility.
317+
void writeSessionContents({required File sessionFile}) {
318+
final now = clock.now();
319+
sessionFile.writeAsStringSync('{"session_id": ${now.millisecondsSinceEpoch}, '
320+
'"last_ping": ${now.millisecondsSinceEpoch}}');
321+
}
322+
313323
/// A UUID generator.
314324
///
315325
/// This will generate unique IDs in the format:

pkgs/unified_analytics/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: >-
44
to Google Analytics.
55
# When updating this, keep the version consistent with the changelog and the
66
# value in lib/src/constants.dart.
7-
version: 5.8.7
7+
version: 5.8.8
88
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics
99

1010
environment:

pkgs/unified_analytics/test/unified_analytics_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,8 @@ ${initialTool.label}=$dateStamp,$toolsMessageVersion
780780
start.millisecondsSinceEpoch);
781781

782782
secondAnalytics.send(testEvent);
783+
expect(sessionFile.readAsStringSync(),
784+
'{"session_id": ${start.millisecondsSinceEpoch}, "last_ping": ${start.millisecondsSinceEpoch}}');
783785
});
784786

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

0 commit comments

Comments
 (0)