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 <vector>
26#include <cstdint>
27#include <optional>
28#include <boost/uuid/uuid.hpp>
29#include <boost/asio/ip/address.hpp>
30#include "ores.utility/uuid/tenant_id.hpp"
31
32namespace ores::iam::domain {
33
37enum class session_protocol {
41 binary = 0,
42
46 http = 1
47};
48
52[[nodiscard]] constexpr std::string_view to_string(session_protocol p) {
53 switch (p) {
54 case session_protocol::binary: return "binary";
55 case session_protocol::http: return "http";
56 default: return "unknown";
57 }
58}
59
63[[nodiscard]] constexpr session_protocol
64session_protocol_from_string(std::string_view s) {
65 if (s == "binary") return session_protocol::binary;
66 if (s == "http") return session_protocol::http;
67 return session_protocol::binary; // Default fallback
68}
69
80struct session final {
85
89 boost::uuids::uuid id;
90
94 boost::uuids::uuid account_id;
95
99 boost::uuids::uuid party_id = {};
100
104 std::vector<boost::uuids::uuid> visible_party_ids;
105
109 std::chrono::system_clock::time_point start_time;
110
116 std::optional<std::chrono::system_clock::time_point> end_time;
117
121 boost::asio::ip::address client_ip;
122
128 std::string client_identifier;
129
133 std::uint16_t client_version_major = 0;
134
138 std::uint16_t client_version_minor = 0;
139
143 std::uint64_t bytes_sent = 0;
144
148 std::uint64_t bytes_received = 0;
149
155 std::string country_code;
156
161
168 std::string username;
169
175 [[nodiscard]] std::optional<std::chrono::seconds> duration() const {
176 if (!end_time) {
177 return std::nullopt;
178 }
179 return std::chrono::duration_cast<std::chrono::seconds>(
181 }
182
186 [[nodiscard]] bool is_active() const {
187 return !end_time.has_value();
188 }
189};
190
194struct session_statistics final {
198 std::chrono::system_clock::time_point period_start;
199
203 std::chrono::system_clock::time_point period_end;
204
208 boost::uuids::uuid account_id;
209
213 std::uint64_t session_count = 0;
214
219
223 std::uint64_t total_bytes_sent = 0;
224
228 std::uint64_t total_bytes_received = 0;
229
233 double avg_bytes_sent = 0.0;
234
238 double avg_bytes_received = 0.0;
239
243 std::uint32_t unique_countries = 0;
244};
245
246}
247
248#endif
Domain types for identity and access management.
Definition account.hpp:29
constexpr std::string_view to_string(session_protocol p)
Converts a session_protocol to its string representation.
Definition session.hpp:52
session_protocol
Protocol used for the session connection.
Definition session.hpp:37
@ 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:64
Represents a user session in the system.
Definition session.hpp:80
std::optional< std::chrono::seconds > duration() const
Calculates the session duration.
Definition session.hpp:175
std::optional< std::chrono::system_clock::time_point > end_time
Timestamp when the session ended (logout or disconnect).
Definition session.hpp:116
std::string client_identifier
Client identifier string from handshake.
Definition session.hpp:128
std::uint16_t client_version_major
Client protocol version major number.
Definition session.hpp:133
std::uint64_t bytes_sent
Total bytes sent to the client during this session.
Definition session.hpp:143
boost::asio::ip::address client_ip
Client IP address.
Definition session.hpp:121
boost::uuids::uuid account_id
Foreign key referencing the associated account.
Definition session.hpp:94
std::vector< boost::uuids::uuid > visible_party_ids
Visible party IDs for party-level RLS.
Definition session.hpp:104
session_protocol protocol
Protocol used for this session (binary or HTTP).
Definition session.hpp:160
std::uint16_t client_version_minor
Client protocol version minor number.
Definition session.hpp:138
boost::uuids::uuid party_id
Party ID for party-level isolation.
Definition session.hpp:99
std::string username
Username of the account that owns this session.
Definition session.hpp:168
std::uint64_t bytes_received
Total bytes received from the client during this session.
Definition session.hpp:148
boost::uuids::uuid id
Unique identifier for this session.
Definition session.hpp:89
std::chrono::system_clock::time_point start_time
Timestamp when the session started (login time).
Definition session.hpp:109
std::string country_code
ISO 3166-1 alpha-2 country code from geolocation.
Definition session.hpp:155
bool is_active() const
Checks if the session is still active.
Definition session.hpp:186
utility::uuid::tenant_id tenant_id
Tenant identifier for multi-tenancy isolation.
Definition session.hpp:84
Aggregated session statistics for a time period.
Definition session.hpp:194
double avg_bytes_received
Average bytes received per session.
Definition session.hpp:238
double avg_bytes_sent
Average bytes sent per session.
Definition session.hpp:233
std::chrono::system_clock::time_point period_end
End of the time period.
Definition session.hpp:203
boost::uuids::uuid account_id
Account ID if statistics are per-account, nil for aggregate.
Definition session.hpp:208
std::uint32_t unique_countries
Number of unique countries from which sessions originated.
Definition session.hpp:243
std::uint64_t total_bytes_sent
Total bytes sent across all sessions.
Definition session.hpp:223
std::chrono::system_clock::time_point period_start
Start of the time period.
Definition session.hpp:198
std::uint64_t session_count
Total number of sessions in this period.
Definition session.hpp:213
double avg_duration_seconds
Average session duration in seconds.
Definition session.hpp:218
std::uint64_t total_bytes_received
Total bytes received across all sessions.
Definition session.hpp:228
A strongly-typed wrapper around a UUID representing a tenant identifier.
Definition tenant_id.hpp:66
static tenant_id system()
Creates a tenant_id representing the system tenant.
Definition tenant_id.cpp:41