forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodePushTelemetryManager.m
More file actions
127 lines (112 loc) · 5.19 KB
/
CodePushTelemetryManager.m
File metadata and controls
127 lines (112 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#import "CodePush.h"
static NSString *const DeploymentFailed = @"DeploymentFailed";
static NSString *const DeploymentKeyKey = @"deploymentKey";
static NSString *const DeploymentSucceeded = @"DeploymentSucceeded";
static NSString *const LabelKey = @"label";
static NSString *const LastDeploymentReportKey = @"CODE_PUSH_LAST_DEPLOYMENT_REPORT";
@implementation CodePushTelemetryManager
+ (NSDictionary *)getBinaryUpdateReport:(NSString *)appVersion
{
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
if (previousStatusReportIdentifier == nil) {
[self recordDeploymentStatusReported:appVersion];
return @{ @"appVersion": appVersion };
} else if (![previousStatusReportIdentifier isEqualToString:appVersion]) {
[self recordDeploymentStatusReported:appVersion];
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
return @{
@"appVersion": appVersion,
@"previousDeploymentKey": previousDeploymentKey,
@"previousLabelOrAppVersion": previousLabel
};
} else {
// Previous status report was with a binary app version.
return @{
@"appVersion": appVersion,
@"previousLabelOrAppVersion": previousStatusReportIdentifier
};
}
}
return nil;
}
+ (NSDictionary *)getRollbackReport:(NSDictionary *)lastFailedPackage
{
return @{
@"package": lastFailedPackage,
@"status": DeploymentFailed
};
}
+ (NSDictionary *)getUpdateReport:(NSDictionary *)currentPackage
{
NSString *currentPackageIdentifier = [self getPackageStatusReportIdentifier:currentPackage];
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
if (currentPackageIdentifier) {
if (previousStatusReportIdentifier == nil) {
[self recordDeploymentStatusReported:currentPackageIdentifier];
return @{
@"package": currentPackage,
@"status": DeploymentSucceeded
};
} else if (![previousStatusReportIdentifier isEqualToString:currentPackageIdentifier]) {
[self recordDeploymentStatusReported:currentPackageIdentifier];
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
return @{
@"package": currentPackage,
@"status": DeploymentSucceeded,
@"previousDeploymentKey": previousDeploymentKey,
@"previousLabelOrAppVersion": previousLabel
};
} else {
// Previous status report was with a binary app version.
return @{
@"package": currentPackage,
@"status": DeploymentSucceeded,
@"previousLabelOrAppVersion": previousStatusReportIdentifier
};
}
}
}
return nil;
}
#pragma mark - private methods
+ (NSString *)getDeploymentKeyFromStatusReportIdentifier:(NSString *)statusReportIdentifier
{
return [[statusReportIdentifier componentsSeparatedByString:@":"] firstObject];
}
+ (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package
{
// Because deploymentKeys can be dynamically switched, we use a
// combination of the deploymentKey and label as the packageIdentifier.
NSString *deploymentKey = [package objectForKey:DeploymentKeyKey];
NSString *label = [package objectForKey:LabelKey];
if (deploymentKey && label) {
return [[deploymentKey stringByAppendingString:@":"] stringByAppendingString:label];
} else {
return nil;
}
}
+ (NSString *)getPreviousStatusReportIdentifier
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSString *sentStatusReportIdentifier = [preferences objectForKey:LastDeploymentReportKey];
return sentStatusReportIdentifier;
}
+ (NSString *)getVersionLabelFromStatusReportIdentifier:(NSString *)statusReportIdentifier
{
return [[statusReportIdentifier componentsSeparatedByString:@":"] lastObject];
}
+ (BOOL)isStatusReportIdentifierCodePushLabel:(NSString *)statusReportIdentifier
{
return statusReportIdentifier != nil && [statusReportIdentifier containsString:@":"];
}
+ (void)recordDeploymentStatusReported:(NSString *)appVersionOrPackageIdentifier
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
[preferences setValue:appVersionOrPackageIdentifier forKey:LastDeploymentReportKey];
[preferences synchronize];
}
@end