-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathCodePushTelemetryManager.m
175 lines (155 loc) · 6.89 KB
/
CodePushTelemetryManager.m
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#import "CodePush.h"
static NSString *const AppVersionKey = @"appVersion";
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";
static NSString *const PackageKey = @"package";
static NSString *const PreviousDeploymentKeyKey = @"previousDeploymentKey";
static NSString *const PreviousLabelOrAppVersionKey = @"previousLabelOrAppVersion";
static NSString *const RetryDeploymentReportKey = @"CODE_PUSH_RETRY_DEPLOYMENT_REPORT";
static NSString *const StatusKey = @"status";
@implementation CodePushTelemetryManager
+ (NSDictionary *)getBinaryUpdateReport:(NSString *)appVersion
{
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
if (previousStatusReportIdentifier == nil) {
[self clearRetryStatusReport];
return @{ AppVersionKey: appVersion };
} else if (![previousStatusReportIdentifier isEqualToString:appVersion]) {
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
[self clearRetryStatusReport];
return @{
AppVersionKey: appVersion,
PreviousDeploymentKeyKey: previousDeploymentKey,
PreviousLabelOrAppVersionKey: previousLabel
};
} else {
[self clearRetryStatusReport];
// Previous status report was with a binary app version.
return @{
AppVersionKey: appVersion,
PreviousLabelOrAppVersionKey: previousStatusReportIdentifier
};
}
}
return nil;
}
+ (NSDictionary *)getRetryStatusReport
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSDictionary *retryStatusReport = [preferences objectForKey:RetryDeploymentReportKey];
if (retryStatusReport) {
[self clearRetryStatusReport];
return retryStatusReport;
} else {
return nil;
}
}
+ (NSDictionary *)getRollbackReport:(NSDictionary *)lastFailedPackage
{
return @{
PackageKey: lastFailedPackage,
StatusKey: DeploymentFailed
};
}
+ (NSDictionary *)getUpdateReport:(NSDictionary *)currentPackage
{
NSString *currentPackageIdentifier = [self getPackageStatusReportIdentifier:currentPackage];
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
if (currentPackageIdentifier) {
if (previousStatusReportIdentifier == nil) {
[self clearRetryStatusReport];
return @{
PackageKey: currentPackage,
StatusKey: DeploymentSucceeded
};
} else if (![previousStatusReportIdentifier isEqualToString:currentPackageIdentifier]) {
[self clearRetryStatusReport];
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
NSString *previousDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
NSString *previousLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
return @{
PackageKey: currentPackage,
StatusKey: DeploymentSucceeded,
PreviousDeploymentKeyKey: previousDeploymentKey,
PreviousLabelOrAppVersionKey: previousLabel
};
} else {
// Previous status report was with a binary app version.
return @{
PackageKey: currentPackage,
StatusKey: DeploymentSucceeded,
PreviousLabelOrAppVersionKey: previousStatusReportIdentifier
};
}
}
}
return nil;
}
+ (void)recordStatusReported:(NSDictionary *)statusReport
{
// We don't need to record rollback reports, so exit early if that's what was specified.
if ([DeploymentFailed isEqualToString:statusReport[StatusKey]]) {
return;
}
if (statusReport[AppVersionKey]) {
[self saveStatusReportedForIdentifier:statusReport[AppVersionKey]];
} else if (statusReport[PackageKey]) {
NSString *packageIdentifier = [self getPackageStatusReportIdentifier:statusReport[PackageKey]];
[self saveStatusReportedForIdentifier:packageIdentifier];
}
}
+ (void)saveStatusReportForRetry:(NSDictionary *)statusReport
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
[preferences setValue:statusReport forKey:RetryDeploymentReportKey];
[preferences synchronize];
}
#pragma mark - private methods
+ (void)clearRetryStatusReport
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
[preferences setValue:nil forKey:RetryDeploymentReportKey];
[preferences synchronize];
}
+ (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 rangeOfString:@":"].location != NSNotFound;
}
+ (void)saveStatusReportedForIdentifier:(NSString *)appVersionOrPackageIdentifier
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
[preferences setValue:appVersionOrPackageIdentifier forKey:LastDeploymentReportKey];
[preferences synchronize];
}
@end