Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit a808afd

Browse files
committed
metrics sdk changes
1 parent 5fc5a0a commit a808afd

File tree

5 files changed

+180
-50
lines changed

5 files changed

+180
-50
lines changed

CodePush.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ function getPromisifiedSdk(requestFetchAdapter, config) {
103103
});
104104
};
105105

106-
sdk.reportStatusDeploy = (deployedPackage, status) => {
106+
sdk.reportStatusDeploy = (deployedPackage, status, fromLabelOrAppVersion, fromDeploymentKey) => {
107107
return new Promise((resolve, reject) => {
108-
module.exports.AcquisitionSdk.prototype.reportStatusDeploy.call(sdk, deployedPackage, status, (err) => {
108+
module.exports.AcquisitionSdk.prototype.reportStatusDeploy.call(sdk, deployedPackage, status, fromLabelOrAppVersion, fromDeploymentKey, (err) => {
109109
if (err) {
110110
reject(err);
111111
} else {
@@ -140,13 +140,15 @@ async function notifyApplicationReady() {
140140
const statusReport = await NativeCodePush.getNewStatusReport();
141141
if (statusReport) {
142142
const config = await getConfiguration();
143+
const fromLabelOrAppVersion = statusReport.fromLabelOrAppVersion;
144+
const fromDeploymentKey = statusReport.fromDeploymentKey || config.deploymentKey;
143145
if (statusReport.appVersion) {
144146
const sdk = getPromisifiedSdk(requestFetchAdapter, config);
145-
sdk.reportStatusDeploy();
147+
sdk.reportStatusDeploy(/* deployedPackage */ null, /* status */ null, fromLabelOrAppVersion, fromDeploymentKey);
146148
} else {
147149
config.deploymentKey = statusReport.package.deploymentKey;
148150
const sdk = getPromisifiedSdk(requestFetchAdapter, config);
149-
sdk.reportStatusDeploy(statusReport.package, statusReport.status);
151+
sdk.reportStatusDeploy(statusReport.package, statusReport.status, fromLabelOrAppVersion, fromDeploymentKey);
150152
}
151153
}
152154
}

CodePush.m

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ - (void)dealloc
161161
[[NSNotificationCenter defaultCenter] removeObserver:self];
162162
}
163163

164+
- (NSString *)getDeploymentKeyFromStatusReportIdentifier:(NSString *)statusReportIdentifier
165+
{
166+
return [[statusReportIdentifier componentsSeparatedByString:@":"] firstObject];
167+
}
168+
164169
- (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package
165170
{
166171
// Because deploymentKeys can be dynamically switched, we use a
@@ -174,6 +179,18 @@ - (NSString *)getPackageStatusReportIdentifier:(NSDictionary *)package
174179
}
175180
}
176181

182+
- (NSString *)getPreviousStatusReportIdentifier
183+
{
184+
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
185+
NSString *sentStatusReportIdentifier = [preferences objectForKey:LastDeploymentReportKey];
186+
return sentStatusReportIdentifier;
187+
}
188+
189+
- (NSString *)getVersionLabelFromStatusReportIdentifier:(NSString *)statusReportIdentifier
190+
{
191+
return [[statusReportIdentifier componentsSeparatedByString:@":"] lastObject];
192+
}
193+
177194
- (instancetype)init
178195
{
179196
self = [super init];
@@ -212,13 +229,6 @@ - (void)initializeUpdateAfterRestart
212229
}
213230
}
214231

215-
- (BOOL)isDeploymentStatusNotYetReported:(NSString *)appVersionOrPackageIdentifier
216-
{
217-
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
218-
NSString *sentStatusReportIdentifier = [preferences objectForKey:LastDeploymentReportKey];
219-
return sentStatusReportIdentifier == nil || ![sentStatusReportIdentifier isEqualToString:appVersionOrPackageIdentifier];
220-
}
221-
222232
/*
223233
* This method checks to see whether a specific package hash
224234
* has previously failed installation.
@@ -267,6 +277,11 @@ - (BOOL)isPendingUpdate:(NSString*)packageHash
267277
return updateIsPending;
268278
}
269279

280+
- (BOOL)isStatusReportIdentifierCodePushLabel:(NSString *)statusReportIdentifier
281+
{
282+
return statusReportIdentifier != nil && [statusReportIdentifier containsString:@":"];
283+
}
284+
270285
/*
271286
* This method updates the React Native bridge's bundle URL
272287
* to point at the latest CodePush update, and then restarts
@@ -542,9 +557,13 @@ - (void)savePendingUpdate:(NSString *)packageHash
542557
NSDictionary *lastFailedPackage = [failedUpdates lastObject];
543558
if (lastFailedPackage) {
544559
NSString *lastFailedPackageIdentifier = [self getPackageStatusReportIdentifier:lastFailedPackage];
545-
if (lastFailedPackageIdentifier && [self isDeploymentStatusNotYetReported:lastFailedPackageIdentifier]) {
560+
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
561+
if (lastFailedPackageIdentifier && (previousStatusReportIdentifier == nil || ![previousStatusReportIdentifier isEqualToString:lastFailedPackageIdentifier])) {
546562
[self recordDeploymentStatusReported:lastFailedPackageIdentifier];
547-
resolve(@{ @"package": lastFailedPackage, @"status": DeploymentFailed });
563+
resolve(@{
564+
@"package": lastFailedPackage,
565+
@"status": DeploymentFailed
566+
});
548567
return;
549568
}
550569
}
@@ -555,19 +574,64 @@ - (void)savePendingUpdate:(NSString *)packageHash
555574
NSDictionary *currentPackage = [CodePushPackage getCurrentPackage:&error];
556575
if (!error && currentPackage) {
557576
NSString *currentPackageIdentifier = [self getPackageStatusReportIdentifier:currentPackage];
558-
if (currentPackageIdentifier && [self isDeploymentStatusNotYetReported:currentPackageIdentifier]) {
559-
[self recordDeploymentStatusReported:currentPackageIdentifier];
560-
resolve(@{ @"package": currentPackage, @"status": DeploymentSucceeded });
561-
return;
577+
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
578+
if (currentPackageIdentifier) {
579+
if (previousStatusReportIdentifier == nil) {
580+
[self recordDeploymentStatusReported:currentPackageIdentifier];
581+
resolve(@{
582+
@"package": currentPackage,
583+
@"status": DeploymentSucceeded
584+
});
585+
return;
586+
} else if (![previousStatusReportIdentifier isEqualToString:currentPackageIdentifier]) {
587+
[self recordDeploymentStatusReported:currentPackageIdentifier];
588+
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
589+
NSString *fromDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
590+
NSString *fromLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
591+
resolve(@{
592+
@"package": currentPackage,
593+
@"status": DeploymentSucceeded,
594+
@"fromDeploymentKey": fromDeploymentKey,
595+
@"fromLabelOrAppVersion": fromLabel
596+
});
597+
} else {
598+
// Previous status report was with a binary app version.
599+
resolve(@{
600+
@"package": currentPackage,
601+
@"status": DeploymentSucceeded,
602+
@"fromLabelOrAppVersion": previousStatusReportIdentifier
603+
});
604+
}
605+
return;
606+
}
562607
}
563608
}
564609
} else if (isRunningBinaryVersion || [_bridge.bundleURL.scheme hasPrefix:@"http"]) {
565610
// Check if the current appVersion has been reported.
566611
NSString *appVersion = [[CodePushConfig current] appVersion];
567-
if ([self isDeploymentStatusNotYetReported:appVersion]) {
612+
NSString *previousStatusReportIdentifier = [self getPreviousStatusReportIdentifier];
613+
if (previousStatusReportIdentifier == nil) {
568614
[self recordDeploymentStatusReported:appVersion];
569615
resolve(@{ @"appVersion": appVersion });
570616
return;
617+
} else if (![previousStatusReportIdentifier isEqualToString:appVersion]) {
618+
[self recordDeploymentStatusReported:appVersion];
619+
if ([self isStatusReportIdentifierCodePushLabel:previousStatusReportIdentifier]) {
620+
NSString *fromDeploymentKey = [self getDeploymentKeyFromStatusReportIdentifier:previousStatusReportIdentifier];
621+
NSString *fromLabel = [self getVersionLabelFromStatusReportIdentifier:previousStatusReportIdentifier];
622+
resolve(@{
623+
@"appVersion": appVersion,
624+
@"fromDeploymentKey": fromDeploymentKey,
625+
@"fromLabelOrAppVersion": fromLabel
626+
});
627+
} else {
628+
// Previous status report was with a binary app version.
629+
resolve(@{
630+
@"appVersion": appVersion,
631+
@"fromLabelOrAppVersion": previousStatusReportIdentifier
632+
});
633+
}
634+
return;
571635
}
572636
}
573637

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,10 @@ public String getBundleUrl(String assetsBundleFileName) {
178178
}
179179
}
180180

181-
private String getPackageStatusReportIdentifier(WritableMap updatePackage) {
182-
// Because deploymentKeys can be dynamically switched, we use a
183-
// combination of the deploymentKey and label as the packageIdentifier.
184-
String deploymentKey = CodePushUtils.tryGetString(updatePackage, DEPLOYMENT_KEY_KEY);
185-
String label = CodePushUtils.tryGetString(updatePackage, LABEL_KEY);
186-
if (deploymentKey != null && label != null) {
187-
return deploymentKey + ":" + label;
181+
private String getDeploymentKeyFromStatusReportIdentifier(String statusReportIdentifier) {
182+
String[] parsedIdentifier = statusReportIdentifier.split(":");
183+
if (parsedIdentifier.length > 0) {
184+
return parsedIdentifier[0];
188185
} else {
189186
return null;
190187
}
@@ -208,6 +205,18 @@ private JSONArray getFailedUpdates() {
208205
}
209206
}
210207

208+
private String getPackageStatusReportIdentifier(WritableMap updatePackage) {
209+
// Because deploymentKeys can be dynamically switched, we use a
210+
// combination of the deploymentKey and label as the packageIdentifier.
211+
String deploymentKey = CodePushUtils.tryGetString(updatePackage, DEPLOYMENT_KEY_KEY);
212+
String label = CodePushUtils.tryGetString(updatePackage, LABEL_KEY);
213+
if (deploymentKey != null && label != null) {
214+
return deploymentKey + ":" + label;
215+
} else {
216+
return null;
217+
}
218+
}
219+
211220
private JSONObject getPendingUpdate() {
212221
SharedPreferences settings = applicationContext.getSharedPreferences(CODE_PUSH_PREFERENCES, 0);
213222
String pendingUpdateString = settings.getString(PENDING_UPDATE_KEY, null);
@@ -226,6 +235,20 @@ private JSONObject getPendingUpdate() {
226235
}
227236
}
228237

238+
private String getPreviousStatusReportIdentifier() {
239+
SharedPreferences settings = applicationContext.getSharedPreferences(CODE_PUSH_PREFERENCES, 0);
240+
return settings.getString(LAST_DEPLOYMENT_REPORT_KEY, null);
241+
}
242+
243+
private String getVersionLabelFromStatusReportIdentifier(String statusReportIdentifier) {
244+
String[] parsedIdentifier = statusReportIdentifier.split(":");
245+
if (parsedIdentifier.length > 1) {
246+
return parsedIdentifier[1];
247+
} else {
248+
return null;
249+
}
250+
}
251+
229252
public ReactPackage getReactPackage() {
230253
if (codePushReactPackage == null) {
231254
codePushReactPackage = new CodePushReactPackage();
@@ -262,16 +285,6 @@ private void initializeUpdateAfterRestart() {
262285
}
263286
}
264287

265-
private boolean isDeploymentStatusNotYetReported(String appVersionOrPackageIdentifier) {
266-
SharedPreferences settings = applicationContext.getSharedPreferences(CODE_PUSH_PREFERENCES, 0);
267-
String lastDeploymentReportIdentifier = settings.getString(LAST_DEPLOYMENT_REPORT_KEY, null);
268-
if (lastDeploymentReportIdentifier == null) {
269-
return true;
270-
} else {
271-
return !lastDeploymentReportIdentifier.equals(appVersionOrPackageIdentifier);
272-
}
273-
}
274-
275288
private boolean isFailedHash(String packageHash) {
276289
JSONArray failedUpdates = getFailedUpdates();
277290
if (packageHash != null) {
@@ -306,6 +319,10 @@ private boolean isPendingUpdate(String packageHash) {
306319
}
307320
}
308321

322+
private boolean isStatusReportIdentifierCodePushLabel(String statusReportIdentifier) {
323+
return statusReportIdentifier != null && statusReportIdentifier.contains(":");
324+
}
325+
309326
private void recordDeploymentStatusReported(String appVersionOrPackageIdentifier) {
310327
SharedPreferences settings = applicationContext.getSharedPreferences(CODE_PUSH_PREFERENCES, 0);
311328
settings.edit().putString(LAST_DEPLOYMENT_REPORT_KEY, appVersionOrPackageIdentifier).commit();
@@ -465,7 +482,8 @@ public void getNewStatusReport(Promise promise) {
465482
JSONObject lastFailedPackageJSON = failedUpdates.getJSONObject(failedUpdates.length() - 1);
466483
WritableMap lastFailedPackage = CodePushUtils.convertJsonObjectToWriteable(lastFailedPackageJSON);
467484
String lastFailedPackageIdentifier = getPackageStatusReportIdentifier(lastFailedPackage);
468-
if (lastFailedPackage != null && isDeploymentStatusNotYetReported(lastFailedPackageIdentifier)) {
485+
String previousStatusReportIdentifier = getPreviousStatusReportIdentifier();
486+
if (lastFailedPackage != null && (previousStatusReportIdentifier == null || !previousStatusReportIdentifier.equals(lastFailedPackageIdentifier))) {
469487
recordDeploymentStatusReported(lastFailedPackageIdentifier);
470488
WritableNativeMap reportMap = new WritableNativeMap();
471489
reportMap.putMap("package", lastFailedPackage);
@@ -482,24 +500,65 @@ public void getNewStatusReport(Promise promise) {
482500
WritableMap currentPackage = codePushPackage.getCurrentPackage();
483501
if (currentPackage != null) {
484502
String currentPackageIdentifier = getPackageStatusReportIdentifier(currentPackage);
485-
if (currentPackageIdentifier != null && isDeploymentStatusNotYetReported(currentPackageIdentifier)) {
486-
recordDeploymentStatusReported(currentPackageIdentifier);
487-
WritableNativeMap reportMap = new WritableNativeMap();
488-
reportMap.putMap("package", currentPackage);
489-
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
490-
promise.resolve(reportMap);
491-
return;
503+
String previousStatusReportIdentifier = getPreviousStatusReportIdentifier();
504+
if (currentPackageIdentifier != null) {
505+
if (previousStatusReportIdentifier == null) {
506+
recordDeploymentStatusReported(currentPackageIdentifier);
507+
WritableNativeMap reportMap = new WritableNativeMap();
508+
reportMap.putMap("package", currentPackage);
509+
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
510+
promise.resolve(reportMap);
511+
return;
512+
} else if (!previousStatusReportIdentifier.equals(currentPackageIdentifier)) {
513+
recordDeploymentStatusReported(currentPackageIdentifier);
514+
if (isStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier)) {
515+
String fromDeploymentKey = getDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier);
516+
String fromLabel = getVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier);
517+
WritableNativeMap reportMap = new WritableNativeMap();
518+
reportMap.putMap("package", currentPackage);
519+
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
520+
reportMap.putString("fromDeploymentKey", fromDeploymentKey);
521+
reportMap.putString("fromLabelOrAppVersion", fromLabel);
522+
promise.resolve(reportMap);
523+
} else {
524+
// Previous status report was with a binary app version.
525+
WritableNativeMap reportMap = new WritableNativeMap();
526+
reportMap.putMap("package", currentPackage);
527+
reportMap.putString("status", DEPLOYMENT_SUCCEEDED_STATUS);
528+
reportMap.putString("fromLabelOrAppVersion", previousStatusReportIdentifier);
529+
promise.resolve(reportMap);
530+
}
531+
return;
532+
}
492533
}
493534
}
494535
} else if (isRunningBinaryVersion) {
495536
// Check if the current appVersion has been reported.
496-
String binaryIdentifier = "" + getBinaryResourcesModifiedTime();
497-
if (isDeploymentStatusNotYetReported(binaryIdentifier)) {
498-
recordDeploymentStatusReported(binaryIdentifier);
537+
String previousStatusReportIdentifier = getPreviousStatusReportIdentifier();
538+
if (previousStatusReportIdentifier == null) {
539+
recordDeploymentStatusReported(appVersion);
499540
WritableNativeMap reportMap = new WritableNativeMap();
500541
reportMap.putString("appVersion", appVersion);
501542
promise.resolve(reportMap);
502543
return;
544+
} else if (!previousStatusReportIdentifier.equals(appVersion)) {
545+
recordDeploymentStatusReported(appVersion);
546+
if (isStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier)) {
547+
String fromDeploymentKey = getDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier);
548+
String fromLabel = getVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier);
549+
WritableNativeMap reportMap = new WritableNativeMap();
550+
reportMap.putString("appVersion", appVersion);
551+
reportMap.putString("fromDeploymentKey", fromDeploymentKey);
552+
reportMap.putString("fromLabelOrAppVersion", fromLabel);
553+
promise.resolve(reportMap);
554+
} else {
555+
// Previous status report was with a binary app version.
556+
WritableNativeMap reportMap = new WritableNativeMap();
557+
reportMap.putString("appVersion", appVersion);
558+
reportMap.putString("fromLabelOrAppVersion", previousStatusReportIdentifier);
559+
promise.resolve(reportMap);
560+
}
561+
return;
503562
}
504563
}
505564

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"url": "https://github.com/Microsoft/react-native-code-push"
1717
},
1818
"dependencies": {
19-
"code-push": "1.5.1-beta",
19+
"code-push": "1.5.2-beta",
2020
"semver": "^5.1.0"
2121
},
2222
"devDependencies": {

request-fetch-adapter.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ module.exports = {
44
callback = requestBody;
55
requestBody = null;
66
}
7-
8-
var headers = {
7+
8+
const headers = {
99
"Accept": "application/json",
10-
"Content-Type": "application/json"
10+
"Content-Type": "application/json",
11+
"X-SDK-Version": getSDKVersion()
1112
};
1213

1314
if (requestBody && typeof requestBody === "object") {
@@ -30,6 +31,10 @@ module.exports = {
3031
}
3132
};
3233

34+
function getSDKVersion() {
35+
return require("./package.json").dependencies["code-push"];
36+
}
37+
3338
function getHttpMethodName(verb) {
3439
// Note: This should stay in sync with the enum definition in
3540
// https://github.com/Microsoft/code-push/blob/master/sdk/script/acquisition-sdk.ts#L6

0 commit comments

Comments
 (0)