-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
* Refactor to use modified dt for _lastPing * Fix tests + misc dartdoc fixes * Bump version * Create `sessionFile` in constructor * Simplify `lastPingDateTime` * Revert filename + pass session id to initializer * Use nullable field for `_sessionId` * Format fix * Change to wip + use `else` block for timestamp
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,7 @@ class Session { | |
final File sessionFile; | ||
final ErrorHandler _errorHandler; | ||
|
||
late int _sessionId; | ||
late int _lastPing; | ||
int? _sessionId; | ||
|
||
Session({ | ||
required this.homeDirectory, | ||
|
@@ -31,7 +30,7 @@ class Session { | |
_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]. | ||
/// | ||
|
@@ -40,30 +39,27 @@ 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(); | ||
|
||
// 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 = sessionFile.lastModifiedSync(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
eliasyishak
Author
Contributor
|
||
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('{"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 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()); | ||
|
||
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 +69,9 @@ class Session { | |
if (telemetryEnabled) _refreshSessionData(); | ||
} | ||
|
||
/// Return a json formatted representation of the class. | ||
String toJson() => jsonEncode(<String, int>{ | ||
'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 | ||
|
@@ -94,13 +84,18 @@ class Session { | |
final sessionObj = | ||
jsonDecode(sessionFileContents) as Map<String, Object?>; | ||
_sessionId = sessionObj['session_id'] as int; | ||
_lastPing = sessionObj['last_ping'] 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', | ||
|
@@ -109,11 +104,13 @@ 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); | ||
final now = clock.now(); | ||
Initializer.createSessionFile( | ||
sessionFile: sessionFile, | ||
sessionIdOverride: now, | ||
); | ||
|
||
_errorHandler.log(Event.analyticsException( | ||
workflow: 'Session._refreshSessionData', | ||
|
@@ -122,9 +119,7 @@ class Session { | |
)); | ||
|
||
// Fallback to setting the session id as the current time | ||
final now = clock.now(); | ||
_sessionId = now.millisecondsSinceEpoch; | ||
_lastPing = now.millisecondsSinceEpoch; | ||
} | ||
} | ||
} |
Hey, this doesn't quite work with the existing tests: fake filesystem uses a fake clock, but many test cases don't. As a result, this time is always far away from
now
, so_sessionId
gets updated. In particular, I seetools/pkgs/unified_analytics/test/unified_analytics_test.dart
Line 1089 in f611290