forked from snowplow/snowplow-cpp-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_session.cpp
More file actions
149 lines (118 loc) · 4.51 KB
/
client_session.cpp
File metadata and controls
149 lines (118 loc) · 4.51 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
/*
Copyright (c) 2022 Snowplow Analytics Ltd. All rights reserved.
This program is licensed to you under the Apache License Version 2.0,
and you may not use this file except in compliance with the Apache License Version 2.0.
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing,
software distributed under the Apache License Version 2.0 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
#include "client_session.hpp"
#include "constants.hpp"
#include "storage.hpp"
#include "utils.hpp"
using namespace snowplow;
using std::lock_guard;
using std::unique_lock;
ClientSession::ClientSession(const string &db_name, unsigned long long foreground_timeout, unsigned long long background_timeout) {
Storage::init(db_name);
this->m_foreground_timeout = foreground_timeout;
this->m_background_timeout = background_timeout;
this->m_session_storage = "SQLITE";
this->m_is_background = false;
this->m_is_new_session = true;
// Check for existing session
list<json> *session_rows = new list<json>;
Storage::instance()->select_all_session_rows(session_rows);
if (session_rows->size() == 1) {
try {
json session_context = session_rows->front();
this->m_user_id = session_context[SNOWPLOW_SESSION_USER_ID].get<std::string>();
this->m_current_session_id = session_context[SNOWPLOW_SESSION_ID].get<std::string>();
this->m_session_index = session_context[SNOWPLOW_SESSION_INDEX].get<unsigned long long>();
} catch (...) {
this->m_user_id = Utils::get_uuid4();
this->m_current_session_id = "";
this->m_session_index = 0;
Storage::instance()->delete_all_session_rows();
}
} else {
this->m_user_id = Utils::get_uuid4();
this->m_current_session_id = "";
this->m_session_index = 0;
}
this->update_last_session_check_at();
session_rows->clear();
delete (session_rows);
}
// --- Public
void ClientSession::start_new_session() {
this->m_is_new_session = true;
}
SelfDescribingJson ClientSession::update_and_get_session_context(const string &event_id) {
json session_context_data;
bool save_to_storage = false;
{
lock_guard<mutex> guard(this->m_safe_get);
if (this->should_update_session()) {
this->update_session(event_id);
save_to_storage = true;
}
this->update_last_session_check_at();
session_context_data = this->m_session_context_data;
}
if (save_to_storage) {
Storage::instance()->insert_update_session(session_context_data);
}
SelfDescribingJson sdj(SNOWPLOW_SCHEMA_CLIENT_SESSION, session_context_data);
return sdj;
}
void ClientSession::set_is_background(bool is_background) {
lock_guard<mutex> guard(this->m_safe_get);
if (this->should_update_session()) {
this->start_new_session();
}
this->update_last_session_check_at();
this->m_is_background = is_background;
}
bool ClientSession::get_is_background() {
lock_guard<mutex> guard(this->m_safe_get);
return this->m_is_background;
}
// --- Private
bool ClientSession::should_update_session() {
if (m_is_new_session) {
return true;
}
unsigned long long now = Utils::get_unix_epoch_ms();
return now < this->m_last_session_check_at || now - this->m_last_session_check_at > this->get_timeout();
}
void ClientSession::update_last_session_check_at() {
this->m_last_session_check_at = Utils::get_unix_epoch_ms();
}
void ClientSession::update_session(const string &event_id) {
this->m_is_new_session = false;
this->m_first_event_id = event_id;
this->m_previous_session_id = this->m_current_session_id;
this->m_current_session_id = Utils::get_uuid4();
this->m_session_index += 1;
// update session context data
json j;
j[SNOWPLOW_SESSION_USER_ID] = this->m_user_id;
j[SNOWPLOW_SESSION_ID] = this->m_current_session_id;
j[SNOWPLOW_SESSION_INDEX] = this->m_session_index;
j[SNOWPLOW_SESSION_STORAGE] = this->m_session_storage;
if (this->m_previous_session_id == "") {
j[SNOWPLOW_SESSION_PREVIOUS_ID] = nullptr;
} else {
j[SNOWPLOW_SESSION_PREVIOUS_ID] = this->m_previous_session_id;
}
if (this->m_first_event_id != "") {
j[SNOWPLOW_SESSION_FIRST_ID] = this->m_first_event_id;
}
this->m_session_context_data = j;
}
unsigned long long ClientSession::get_timeout() {
return this->m_is_background ? this->m_background_timeout : this->m_foreground_timeout;
}