forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodePushTelemetryManager.cpp
More file actions
213 lines (192 loc) · 9.37 KB
/
CodePushTelemetryManager.cpp
File metadata and controls
213 lines (192 loc) · 9.37 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "CodePushTelemetryManager.h"
#include "CodePushNativeModule.h"
#include "winrt/Windows.Storage.h"
#include "winrt/Windows.Data.Json.h"
#include <string_view>
namespace Microsoft::CodePush::ReactNative
{
using namespace winrt;
using namespace Windows::Data::Json;
using namespace Windows::Storage;
static const std::wstring_view AppVersionKey{ L"appVersion" };
static const std::wstring_view DeploymentFailed{ L"DeploymentFailed" };
static const std::wstring_view DeploymentKeyKey{ L"deploymentKey" };
static const std::wstring_view DeploymentSucceeded{ L"DeploymentSucceeded" };
static const std::wstring_view LabelKey{ L"label" };
static const std::wstring_view LastDeploymentReportKey{ L"CODE_PUSH_LAST_DEPLOYMENT_REPORT" };
static const std::wstring_view PackageKey{ L"package" };
static const std::wstring_view PreviousDeploymentKeyKey{ L"previousDeploymentKey" };
static const std::wstring_view PreviousLabelOrAppVersionKey{ L"previousLabelOrAppVersion" };
static const std::wstring_view RetryDeploymentReportKey{ L"CODE_PUSH_RETRY_DEPLOYMENT_REPORT" };
static const std::wstring_view StatusKey{ L"status" };
/*static*/ JsonObject CodePushTelemetryManager::GetBinaryUpdateReport(std::wstring_view appVersion)
{
auto previousStatusReportIdentifier{ GetPreviousStatusReportIdentifier() };
if (previousStatusReportIdentifier.empty())
{
ClearRetryStatusReport();
JsonObject out;
out.Insert(AppVersionKey, JsonValue::CreateStringValue(appVersion));
return out;
}
else if (previousStatusReportIdentifier != appVersion)
{
if (IsStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier))
{
auto previousDeploymentKey{ GetDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier) };
auto previousLabel{ GetVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier) };
ClearRetryStatusReport();
JsonObject out;
out.Insert(AppVersionKey, JsonValue::CreateStringValue(appVersion));
out.Insert(PreviousDeploymentKeyKey, JsonValue::CreateStringValue(previousDeploymentKey));
out.Insert(PreviousLabelOrAppVersionKey, JsonValue::CreateStringValue(previousLabel));
return out;
}
else
{
ClearRetryStatusReport();
// Previous status report was with a binary app version.
JsonObject out;
out.Insert(AppVersionKey, JsonValue::CreateStringValue(appVersion));
out.Insert(PreviousLabelOrAppVersionKey, JsonValue::CreateStringValue(previousStatusReportIdentifier));
return out;
}
}
return nullptr;
}
/*static*/ JsonObject CodePushTelemetryManager::GetRetryStatusReport()
{
auto localSettings{ CodePushNativeModule::GetLocalSettings() };
auto retryStatusReportData{ localSettings.Values().TryLookup(RetryDeploymentReportKey) };
if (retryStatusReportData != nullptr)
{
auto retryStatusReportString{ unbox_value<hstring>(retryStatusReportData) };
JsonObject retryStatusReport;
auto success{ JsonObject::TryParse(retryStatusReportString, retryStatusReport) };
if (success)
{
return retryStatusReport;
}
}
return nullptr;
}
/*static*/ JsonObject CodePushTelemetryManager::GetRollbackReport(const JsonObject& lastFailedPackage)
{
JsonObject out;
out.Insert(PackageKey, lastFailedPackage);
out.Insert(StatusKey, JsonValue::CreateStringValue(DeploymentFailed));
return out;
}
/*static*/ JsonObject CodePushTelemetryManager::GetUpdateReport(const JsonObject& currentPackage)
{
auto currentPackageIdentifier{ GetPackageStatusReportIdentifier(currentPackage) };
auto previousStatusReportIdentifier{ GetPreviousStatusReportIdentifier() };
if (currentPackageIdentifier.empty())
{
if (!previousStatusReportIdentifier.empty())
{
ClearRetryStatusReport();
JsonObject out;
out.Insert(PackageKey, currentPackage);
out.Insert(StatusKey, JsonValue::CreateStringValue(DeploymentSucceeded));
return out;
}
else if (previousStatusReportIdentifier != currentPackageIdentifier)
{
ClearRetryStatusReport();
if (IsStatusReportIdentifierCodePushLabel(previousStatusReportIdentifier))
{
auto previousDeploymentKey{ GetDeploymentKeyFromStatusReportIdentifier(previousStatusReportIdentifier) };
auto previousLabel{ GetVersionLabelFromStatusReportIdentifier(previousStatusReportIdentifier) };
JsonObject out;
out.Insert(PackageKey, currentPackage);
out.Insert(StatusKey, JsonValue::CreateStringValue(DeploymentSucceeded));
out.Insert(PreviousDeploymentKeyKey, JsonValue::CreateStringValue(previousDeploymentKey));
out.Insert(PreviousLabelOrAppVersionKey, JsonValue::CreateStringValue(previousLabel));
return out;
}
else
{
// Previous status report was with a binary app version.
JsonObject out;
out.Insert(PackageKey, currentPackage);
out.Insert(StatusKey, JsonValue::CreateStringValue(DeploymentSucceeded));
out.Insert(PreviousLabelOrAppVersionKey, JsonValue::CreateStringValue(previousStatusReportIdentifier));
return out;
}
}
}
return nullptr;
}
/*static*/ void CodePushTelemetryManager::RecordStatusReported(const JsonObject& statusReport)
{
// We don't need to record rollback reports, so exit early if that's what was specified.
auto status{ statusReport.TryLookup(StatusKey) };
if (status != nullptr && status.ValueType() == JsonValueType::String && status.GetString() == DeploymentFailed)
{
return;
}
if (statusReport.HasKey(AppVersionKey))
{
SaveStatusReportedForIdentifier(statusReport.GetNamedString(AppVersionKey));
}
else if (statusReport.HasKey(PackageKey))
{
auto packageIdentifier{ GetPackageStatusReportIdentifier(statusReport.GetNamedObject(PackageKey)) };
SaveStatusReportedForIdentifier(packageIdentifier);
}
}
/*static*/ void CodePushTelemetryManager::SaveStatusReportForRetry(const JsonObject& statusReport)
{
auto localSettings{ CodePushNativeModule::GetLocalSettings() };
localSettings.Values().Insert(RetryDeploymentReportKey, box_value(statusReport.Stringify()));
}
/*static*/ void CodePushTelemetryManager::ClearRetryStatusReport()
{
auto localSettings{ CodePushNativeModule::GetLocalSettings() };
localSettings.Values().Remove(RetryDeploymentReportKey);
}
/*static*/ std::wstring_view CodePushTelemetryManager::GetDeploymentKeyFromStatusReportIdentifier(std::wstring_view statusReportIdentifier)
{
return statusReportIdentifier.substr(0, statusReportIdentifier.find(':'));
}
/*static*/ hstring CodePushTelemetryManager::GetPackageStatusReportIdentifier(const JsonObject& package)
{
// Because deploymentKeys can be dynamically switched, we use a
// combination of the deploymentKey and label as the packageIdentifier.
if (package.HasKey(DeploymentKeyKey) && package.HasKey(LabelKey))
{
return L"";
}
auto deploymentKey{ package.GetNamedString(DeploymentKeyKey) };
auto label{ package.GetNamedString(LabelKey) };
return deploymentKey + L":" + label;
}
/*static*/ hstring CodePushTelemetryManager::GetPreviousStatusReportIdentifier()
{
auto localSettings{ CodePushNativeModule::GetLocalSettings() };
auto sentStatusReportIdentifierData{ localSettings.Values().TryLookup(LastDeploymentReportKey) };
if (sentStatusReportIdentifierData != nullptr)
{
auto sentStatusReportIdentifier{ unbox_value<hstring>(sentStatusReportIdentifierData) };
return sentStatusReportIdentifier;
}
return L"";
}
/*static*/ std::wstring_view CodePushTelemetryManager::GetVersionLabelFromStatusReportIdentifier(std::wstring_view statusReportIdentifier)
{
return statusReportIdentifier.substr(statusReportIdentifier.rfind(':') + 1);
}
/*static*/ bool CodePushTelemetryManager::IsStatusReportIdentifierCodePushLabel(std::wstring_view statusReportIdentifier)
{
return !statusReportIdentifier.empty() && statusReportIdentifier.find(':') != std::wstring_view::npos;
}
/*static*/ void CodePushTelemetryManager::SaveStatusReportedForIdentifier(std::wstring_view appVersionOrPackageIdentifier)
{
auto localSettings{ CodePushNativeModule::GetLocalSettings() };
localSettings.Values().Insert(LastDeploymentReportKey, box_value(appVersionOrPackageIdentifier));
}
}