ORE Studio 0.0.4
Loading...
Searching...
No Matches
session.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_IAM_DOMAIN_SESSION_HPP
21#define ORES_IAM_DOMAIN_SESSION_HPP
22
23#include <chrono>
24#include <string>
25#include <cstdint>
26#include <optional>
27#include <boost/uuid/uuid.hpp>
28#include <boost/asio/ip/address.hpp>
29
30namespace ores::iam::domain {
31
35enum class session_protocol {
39 binary = 0,
40
44 http = 1
45};
46
50[[nodiscard]] constexpr std::string_view to_string(session_protocol p) {
51 switch (p) {
52 case session_protocol::binary: return "binary";
53 case session_protocol::http: return "http";
54 default: return "unknown";
55 }
56}
57
61[[nodiscard]] constexpr session_protocol
62session_protocol_from_string(std::string_view s) {
63 if (s == "binary") return session_protocol::binary;
64 if (s == "http") return session_protocol::http;
65 return session_protocol::binary; // Default fallback
66}
67
78struct session final {
82 boost::uuids::uuid id;
83
87 boost::uuids::uuid account_id;
88
92 std::chrono::system_clock::time_point start_time;
93
99 std::optional<std::chrono::system_clock::time_point> end_time;
100
104 boost::asio::ip::address client_ip;
105
111 std::string client_identifier;
112
116 std::uint16_t client_version_major = 0;
117
121 std::uint16_t client_version_minor = 0;
122
126 std::uint64_t bytes_sent = 0;
127
131 std::uint64_t bytes_received = 0;
132
138 std::string country_code;
139
144
151 std::string username;
152
158 [[nodiscard]] std::optional<std::chrono::seconds> duration() const {
159 if (!end_time) {
160 return std::nullopt;
161 }
162 return std::chrono::duration_cast<std::chrono::seconds>(
164 }
165
169 [[nodiscard]] bool is_active() const {
170 return !end_time.has_value();
171 }
172};
173
177struct session_statistics final {
181 std::chrono::system_clock::time_point period_start;
182
186 std::chrono::system_clock::time_point period_end;
187
191 boost::uuids::uuid account_id;
192
196 std::uint64_t session_count = 0;
197
202
206 std::uint64_t total_bytes_sent = 0;
207
211 std::uint64_t total_bytes_received = 0;
212
216 double avg_bytes_sent = 0.0;
217
221 double avg_bytes_received = 0.0;
222
226 std::uint32_t unique_countries = 0;
227};
228
229}
230
231#endif
Domain types for identity and access management.
Definition account.hpp:27
constexpr std::string_view to_string(session_protocol p)
Converts a session_protocol to its string representation.
Definition session.hpp:50
session_protocol
Protocol used for the session connection.
Definition session.hpp:35
@ http
HTTP/REST API with JWT authentication.
@ binary
ORE Studio binary protocol over TCP.
constexpr session_protocol session_protocol_from_string(std::string_view s)
Converts a string to session_protocol.
Definition session.hpp:62
Represents a user session in the system.
Definition session.hpp:78
std::optional< std::chrono::seconds > duration() const
Calculates the session duration.
Definition session.hpp:158
std::optional< std::chrono::system_clock::time_point > end_time
Timestamp when the session ended (logout or disconnect).
Definition session.hpp:99
std::string client_identifier
Client identifier string from handshake.
Definition session.hpp:111
std::uint16_t client_version_major
Client protocol version major number.
Definition session.hpp:116
std::uint64_t bytes_sent
Total bytes sent to the client during this session.
Definition session.hpp:126
boost::asio::ip::address client_ip
Client IP address.
Definition session.hpp:104
boost::uuids::uuid account_id
Foreign key referencing the associated account.
Definition session.hpp:87
session_protocol protocol
Protocol used for this session (binary or HTTP).
Definition session.hpp:143
std::uint16_t client_version_minor
Client protocol version minor number.
Definition session.hpp:121
std::string username
Username of the account that owns this session.
Definition session.hpp:151
std::uint64_t bytes_received
Total bytes received from the client during this session.
Definition session.hpp:131
boost::uuids::uuid id
Unique identifier for this session.
Definition session.hpp:82
std::chrono::system_clock::time_point start_time
Timestamp when the session started (login time).
Definition session.hpp:92
std::string country_code
ISO 3166-1 alpha-2 country code from geolocation.
Definition session.hpp:138
bool is_active() const
Checks if the session is still active.
Definition session.hpp:169
Aggregated session statistics for a time period.
Definition session.hpp:177
double avg_bytes_received
Average bytes received per session.
Definition session.hpp:221
double avg_bytes_sent
Average bytes sent per session.
Definition session.hpp:216
std::chrono::system_clock::time_point period_end
End of the time period.
Definition session.hpp:186
boost::uuids::uuid account_id
Account ID if statistics are per-account, nil for aggregate.
Definition session.hpp:191
std::uint32_t unique_countries
Number of unique countries from which sessions originated.
Definition session.hpp:226
std::uint64_t total_bytes_sent
Total bytes sent across all sessions.
Definition session.hpp:206
std::chrono::system_clock::time_point period_start
Start of the time period.
Definition session.hpp:181
std::uint64_t session_count
Total number of sessions in this period.
Definition session.hpp:196
double avg_duration_seconds
Average session duration in seconds.
Definition session.hpp:201
std::uint64_t total_bytes_received
Total bytes received across all sessions.
Definition session.hpp:211