Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Reworked the way we make sure iOS metrics are being sent once per UTC… (
Browse files Browse the repository at this point in the history
#1705)

* Reworked the way we make sure iOS metrics are being sent once per UTC day

* Merged active-user metric with scheduled-check-started-today so that we send from the same place

* Added publishNativeActiveUserMetric call in the second registerPeriodicTask
  • Loading branch information
craigzour authored Jun 17, 2021
1 parent 4a923a8 commit fd8aac3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
10 changes: 2 additions & 8 deletions ios/CovidShield/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
[[TSBackgroundFetch sharedInstance] didFinishLaunching];
}

MetricsService *metricsService = [MetricsService sharedInstance];

// Will be called when the app starts from either foreground or background
[metricsService publishMetric:ActiveUser bridge:bridge];

if(application.applicationState == UIApplicationStateBackground) {
[metricsService publishMetric:ScheduledCheckStartedToday bridge:bridge];
}
// Will be called when the user manually starts the application
[[MetricsService sharedInstance] publishMetric:ActiveUser bridge:bridge];

// Define UNUserNotificationCenter
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
Expand Down
4 changes: 3 additions & 1 deletion ios/CovidShield/DebugMetrics.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ @implementation DebugMetrics

RCT_REMAP_METHOD(publishNativeActiveUserMetric, withResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[[MetricsService sharedInstance] publishMetric:ActiveUser bridge:_bridge];
MetricsService *metricsService = [MetricsService sharedInstance];
[metricsService publishMetric:ActiveUser bridge:_bridge];
[metricsService publishMetric:ScheduledCheckStartedToday bridge:_bridge];

resolve(nil);
}
Expand Down
7 changes: 0 additions & 7 deletions ios/CovidShield/ExposureNotification.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#import <React/RCTConvert.h>
#import <TSBackgroundFetch/TSBackgroundFetch.h>
#import "MetricsService.h"

@interface ExposureNotification ()
@property (nonatomic) NSMutableArray *reportedSummaries;
Expand Down Expand Up @@ -71,12 +70,6 @@ + (ExposureNotificationSupportType) exposureNotificationSupportType {
if ([ExposureNotification exposureNotificationSupportType] == ENSupportTypeVersion12dot5) {
[self.enManager setLaunchActivityHandler:^(ENActivityFlags activityFlags) {
if (activityFlags & ENActivityFlagsPeriodicRun) {

MetricsService *metricsService = [MetricsService sharedInstance];

[metricsService publishMetric:ActiveUser bridge:self->_bridge];
[metricsService publishMetric:ScheduledCheckStartedToday bridge:self->_bridge];

// Your app now has 3.5 minutes to perform download and detection.
TSBackgroundFetch *fetchManager = [TSBackgroundFetch sharedInstance];
[fetchManager performFetchWithCompletionHandler:^void(UIBackgroundFetchResult r) {}
Expand Down
36 changes: 31 additions & 5 deletions ios/UniqueDailyMetricsHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

@interface UniqueDailyMetricsHelper ()

@property (nonatomic, strong) NSUserDefaults *userDefaults;
@property (nonatomic, strong) NSFileManager *fileManager;
@property (nonatomic, strong) NSString *filePath;

@end

Expand All @@ -20,15 +21,18 @@ - (instancetype)init
{
self = [super init];
if (self) {
self.userDefaults = [NSUserDefaults standardUserDefaults];
self.fileManager = [NSFileManager defaultManager];
self.filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"unique-daily-metrics-savestate.json"];
}
return self;
}

- (BOOL)canPublishMetricWithIdentifier:(NSString *)identifier currentDate:(NSDate *)currentDate
{
NSDate *retrieveUTCDate = [self.userDefaults objectForKey:identifier];
if (retrieveUTCDate != nil) {
NSDictionary *savestate = [self readSavestate];
NSNumber *retrievedTimestamp = [savestate objectForKey:identifier];
if (retrievedTimestamp != nil) {
NSDate *retrieveUTCDate = [NSDate dateWithTimeIntervalSince1970:retrievedTimestamp.doubleValue];
return ![DateUtils isSameDay:retrieveUTCDate date2:currentDate];
} else {
return YES;
Expand All @@ -37,7 +41,29 @@ - (BOOL)canPublishMetricWithIdentifier:(NSString *)identifier currentDate:(NSDat

- (void)markMetricAsPublishedWithIdentifier:(NSString *)identifier currentDate:(NSDate *)currentDate
{
[self.userDefaults setObject:currentDate forKey:identifier];
NSMutableDictionary *savestate = [[self readSavestate] mutableCopy];
[savestate setObject:@(currentDate.timeIntervalSince1970) forKey:identifier];
[self writeSavestate:savestate];
}

- (NSDictionary *)readSavestate
{
if ([self.fileManager fileExistsAtPath:self.filePath]) {
NSData *data = [self.fileManager contentsAtPath:self.filePath];
return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
} else {
return @{};
}
}

- (void)writeSavestate:(NSDictionary *)dictionary
{
if ([self.fileManager fileExistsAtPath:self.filePath] == NO) {
[self.fileManager createFileAtPath:self.filePath contents:nil attributes:nil];
}

NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:nil];
[data writeToFile:self.filePath atomically:YES];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const ExposureNotificationServiceProvider = ({
}
// re-register the background tasks upon app launch
backgroundScheduler.registerPeriodicTask(async () => {
publishNativeActiveUserMetric();
await exposureNotificationService.updateExposureStatusInBackground();
}, exposureNotificationService);
};
Expand Down

0 comments on commit fd8aac3

Please sign in to comment.