-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathLogSessionDataProvider.cpp
More file actions
205 lines (188 loc) · 6.77 KB
/
LogSessionDataProvider.cpp
File metadata and controls
205 lines (188 loc) · 6.77 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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#include "LogSessionDataProvider.hpp"
#include "utils/FileUtils.hpp"
#include "utils/StringUtils.hpp"
#include "utils/Utils.hpp"
#include<cstring>
#include<cstdlib>
#include<errno.h>
#ifndef _WIN32 /* Avoid warning under Windows */
extern int errno;
#endif
namespace MAT_NS_BEGIN
{
static const char* sessionFirstLaunchTimeName = "sessionfirstlaunchtime";
static const char* sessionSdkUidName = "sessionsdkuid";
void LogSessionDataProvider::CreateLogSessionData()
{
if (m_storageType == SessionStorageType::FileStore)
{
CreateLogSessionDataFromFile();
} else
{
CreateLogSessionDataFromDB();
}
}
void LogSessionDataProvider::ResetLogSessionData()
{
DeleteLogSessionData();
CreateLogSessionData();
}
void LogSessionDataProvider::DeleteLogSessionData()
{
if (m_storageType == SessionStorageType::FileStore)
{
DeleteLogSessionDataFromFile();
}
else
{
DeleteLogSessionDataFromDB();
}
}
LogSessionData* LogSessionDataProvider::GetLogSessionData() noexcept
{
return m_logSessionData.get();
}
void LogSessionDataProvider::CreateLogSessionDataFromDB()
{
if (nullptr == m_offlineStorage) {
LOG_WARN(" offline storage not available. Session data won't be initialized");
return;
}
uint64_t sessionFirstTimeLaunch = 0;
std::string sessionSDKUid = m_offlineStorage->GetSetting(sessionSdkUidName);
sessionFirstTimeLaunch = convertStrToLong(m_offlineStorage->GetSetting(sessionFirstLaunchTimeName));
if ((sessionFirstTimeLaunch == 0) || sessionSDKUid.empty())
{
sessionFirstTimeLaunch = PAL::getUtcSystemTimeMs();
sessionSDKUid = PAL::generateUuidString();
if (!m_offlineStorage->StoreSetting(sessionFirstLaunchTimeName, std::to_string(sessionFirstTimeLaunch)))
{
LOG_WARN("Unable to save session analytics to DB for %d", sessionFirstLaunchTimeName);
}
if (!m_offlineStorage->StoreSetting(sessionSdkUidName, sessionSDKUid)) {
LOG_WARN("Unable to save session analytics to DB for %s", sessionSDKUid.c_str());
}
}
m_logSessionData.reset(new LogSessionData(sessionFirstTimeLaunch, sessionSDKUid));
}
void LogSessionDataProvider::DeleteLogSessionDataFromDB()
{
if (nullptr == m_offlineStorage) {
LOG_WARN(" offline storage not available. Session data won't be deleted");
return ;
}
if (!m_offlineStorage->DeleteSetting(sessionFirstLaunchTimeName))
{
LOG_WARN("Unable to delete session analytics from DB for %d", sessionFirstLaunchTimeName);
}
if (!m_offlineStorage->DeleteSetting(sessionSdkUidName))
{
LOG_WARN("Unable to delete session analytics from DB for %d", sessionSdkUidName);
}
}
void LogSessionDataProvider::DeleteLogSessionDataFromFile()
{
std::string sessionPath = m_cacheFilePath.empty() ? "" : (m_cacheFilePath + ".ses").c_str();
if (!sessionPath.empty() && MAT::FileExists(sessionPath.c_str()))
{
MAT::FileDelete(sessionPath.c_str());
}
}
void LogSessionDataProvider::CreateLogSessionDataFromFile()
{
uint64_t sessionFirstTimeLaunch = 0;
std::string sessionSDKUid;
std::string sessionPath = m_cacheFilePath.empty() ? "" : (m_cacheFilePath + ".ses").c_str();
if (!sessionPath.empty())
{
if (MAT::FileExists(sessionPath.c_str()))
{
auto content = MAT::FileGetContents(sessionPath.c_str());
if (!parse (content, sessionFirstTimeLaunch, sessionSDKUid))
{
sessionFirstTimeLaunch = PAL::getUtcSystemTimeMs();
sessionSDKUid = PAL::generateUuidString();
writeFileContents(sessionPath, sessionFirstTimeLaunch, sessionSDKUid);
}
} else
{
sessionFirstTimeLaunch = PAL::getUtcSystemTimeMs();
sessionSDKUid = PAL::generateUuidString();
writeFileContents(sessionPath, sessionFirstTimeLaunch, sessionSDKUid);
}
}
m_logSessionData.reset(new LogSessionData(sessionFirstTimeLaunch, sessionSDKUid));
}
bool LogSessionDataProvider::parse(
const std::string &content,
uint64_t& sessionFirstTimeLaunch,
std::string& sessionSDKUid)
{
if (content.empty()) {
return false;
}
std::vector<std::string> v;
StringUtils::SplitString(content, '\n', v);
if (v.size() != 3) {
return false;
}
remove_eol(v[0]);
remove_eol(v[1]);
sessionFirstTimeLaunch = convertStrToLong(v[0]);
if (sessionFirstTimeLaunch == 0 ) {
return false;
}
sessionSDKUid = v[1];
return true;
}
uint64_t LogSessionDataProvider::convertStrToLong(const std::string& s) noexcept
{
uint64_t res = 0ull;
char *endptr = nullptr;
res = std::strtoll(s.c_str(), &endptr, 10);
if (errno == ERANGE && (res == LONG_MAX || res == 0 ))
{
LOG_WARN ("Converted value falls out of uint64_t range.");
res = 0;
}
else if ( 0 != errno && 0 == res )
{
LOG_WARN("Conversion cannot be performed.");
}
else if (std::strlen(endptr) > 0)
{
LOG_WARN ("Conversion cannot be performed. Alphanumeric characters present");
res = 0;
}
return res;
}
void LogSessionDataProvider::writeFileContents(
const std::string &path,
uint64_t sessionFirstTimeLaunch,
const std::string &sessionSDKUid)
{
std::string contents;
contents += toString(sessionFirstTimeLaunch);
contents += '\n';
contents += sessionSDKUid;
// Valid line ends with newline as per posix specs ( https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206)
contents += '\n';
//TBD (labhas) - validate if file is NOT a symlink/junction before trying to write.
if (!MAT::FileWrite(path.c_str(), contents.c_str()))
{
LOG_WARN("Unable to save session analytics to %s", path.c_str());
}
}
void LogSessionDataProvider::remove_eol(std::string& result)
{
if (!result.empty() && result[result.length() - 1] == '\n')
{
result.erase(result.length() - 1);
}
}
}
MAT_NS_END