ORE Studio 0.0.4
Loading...
Searching...
No Matches
telemetry_repository.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_TELEMETRY_REPOSITORY_TELEMETRY_REPOSITORY_HPP
21#define ORES_TELEMETRY_REPOSITORY_TELEMETRY_REPOSITORY_HPP
22
23#include <string>
24#include <vector>
25#include <optional>
26#include <boost/uuid/uuid.hpp>
27#include <sqlgen/postgres.hpp>
28#include "ores.logging/make_logger.hpp"
29#include "ores.database/domain/context.hpp"
30#include "ores.telemetry/domain/telemetry_log_entry.hpp"
31#include "ores.telemetry/domain/telemetry_query.hpp"
32#include "ores.telemetry/domain/telemetry_stats.hpp"
33#include "ores.telemetry/domain/telemetry_batch.hpp"
34#include "ores.telemetry/domain/nats_server_sample.hpp"
35#include "ores.telemetry/domain/nats_stream_sample.hpp"
36#include "ores.telemetry/domain/nats_samples_query.hpp"
37#include "ores.telemetry/domain/service_sample.hpp"
38
39namespace ores::telemetry::database::repository {
40
48private:
49 inline static std::string_view logger_name =
50 "ores.telemetry.repository.telemetry_repository";
51
52 [[nodiscard]] static auto& lg() {
53 using namespace ores::logging;
54 static auto instance = make_logger(logger_name);
55 return instance;
56 }
57
58public:
60
64 std::string sql();
65
69 void create(context ctx, const domain::telemetry_log_entry& entry);
70
78 std::size_t create_batch(context ctx, const domain::telemetry_batch& batch);
79
83 std::vector<domain::telemetry_log_entry> query(context ctx,
85
91 std::uint64_t count(context ctx, const domain::telemetry_query& q);
92
101 std::vector<domain::telemetry_log_entry> read_by_session(context ctx,
102 const boost::uuids::uuid& session_id,
103 std::uint32_t limit = 1000);
104
114 std::vector<domain::telemetry_log_entry> read_by_account(context ctx,
115 const boost::uuids::uuid& account_id,
116 const std::chrono::system_clock::time_point& start,
117 const std::chrono::system_clock::time_point& end,
118 std::uint32_t limit = 1000);
119
123 std::vector<domain::telemetry_stats> read_hourly_stats(context ctx,
125
129 std::vector<domain::telemetry_stats> read_daily_stats(context ctx,
131
138 domain::telemetry_summary get_summary(context ctx, std::uint32_t hours = 24);
139
147 std::uint64_t count_errors(context ctx, const std::string& source_name,
148 std::uint32_t hours = 1);
149
161 std::uint64_t delete_old_logs(context ctx,
162 const std::chrono::system_clock::time_point& older_than);
163
168 const domain::nats_server_sample& sample);
169
174 const std::vector<domain::nats_stream_sample>& samples);
175
179 std::vector<domain::nats_server_sample> query_server_samples(context ctx,
181
185 std::vector<domain::nats_stream_sample> query_stream_samples(context ctx,
187
195 const domain::service_sample& sample);
196
203 std::vector<domain::service_sample> list_service_samples(context ctx);
204};
205
206}
207
208#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Context for the operations on a postgres database.
Definition context.hpp:47
Repository for telemetry log persistence and querying.
Definition telemetry_repository.hpp:47
std::vector< domain::nats_stream_sample > query_stream_samples(context ctx, const domain::nats_stream_samples_query &q)
Queries NATS stream samples within a time range.
Definition telemetry_repository.cpp:580
void create(context ctx, const domain::telemetry_log_entry &entry)
Creates a single log entry.
Definition telemetry_repository.cpp:44
std::uint64_t count_errors(context ctx, const std::string &source_name, std::uint32_t hours=1)
Counts error logs in the last N hours for a source.
Definition telemetry_repository.cpp:468
std::vector< domain::telemetry_log_entry > query(context ctx, const domain::telemetry_query &q)
Queries log entries with filters.
Definition telemetry_repository.cpp:182
std::vector< domain::telemetry_log_entry > read_by_account(context ctx, const boost::uuids::uuid &account_id, const std::chrono::system_clock::time_point &start, const std::chrono::system_clock::time_point &end, std::uint32_t limit=1000)
Reads logs for a specific account.
Definition telemetry_repository.cpp:298
std::vector< domain::telemetry_log_entry > read_by_session(context ctx, const boost::uuids::uuid &session_id, std::uint32_t limit=1000)
Reads logs for a specific session.
Definition telemetry_repository.cpp:270
std::vector< domain::service_sample > list_service_samples(context ctx)
Returns the latest heartbeat per (service_name, instance_id).
Definition telemetry_repository.cpp:624
std::uint64_t count(context ctx, const domain::telemetry_query &q)
Counts log entries matching a query.
Definition telemetry_repository.cpp:240
void insert_stream_samples(context ctx, const std::vector< domain::nats_stream_sample > &samples)
Inserts a batch of NATS per-stream metrics samples.
Definition telemetry_repository.cpp:526
domain::telemetry_summary get_summary(context ctx, std::uint32_t hours=24)
Gets a summary of telemetry activity.
Definition telemetry_repository.cpp:415
std::vector< domain::telemetry_stats > read_hourly_stats(context ctx, const domain::telemetry_stats_query &q)
Reads hourly statistics.
Definition telemetry_repository.cpp:332
std::size_t create_batch(context ctx, const domain::telemetry_batch &batch)
Creates multiple log entries in a batch.
Definition telemetry_repository.cpp:57
std::vector< domain::telemetry_stats > read_daily_stats(context ctx, const domain::telemetry_stats_query &q)
Reads daily statistics.
Definition telemetry_repository.cpp:372
std::string sql()
Returns the SQL created by sqlgen to construct the table.
Definition telemetry_repository.cpp:40
std::uint64_t delete_old_logs(context ctx, const std::chrono::system_clock::time_point &older_than)
Deletes logs older than the specified retention period.
Definition telemetry_repository.cpp:497
void insert_service_sample(context ctx, const domain::service_sample &sample)
Inserts a single service heartbeat sample.
Definition telemetry_repository.cpp:609
std::vector< domain::nats_server_sample > query_server_samples(context ctx, const domain::nats_server_samples_query &q)
Queries NATS server samples within a time range.
Definition telemetry_repository.cpp:552
void insert_server_sample(context ctx, const domain::nats_server_sample &sample)
Inserts a single NATS server-level metrics sample.
Definition telemetry_repository.cpp:514
Query parameters for retrieving NATS server samples.
Definition nats_samples_query.hpp:32
Query parameters for retrieving NATS stream samples.
Definition nats_samples_query.hpp:41
A single point-in-time sample of NATS server-level metrics.
Definition nats_server_sample.hpp:34
A single heartbeat received from a running domain service.
Definition service_sample.hpp:38
A batch of telemetry log entries from a single source.
Definition telemetry_batch.hpp:37
A persisted telemetry log entry.
Definition telemetry_log_entry.hpp:41
Query parameters for retrieving telemetry logs.
Definition telemetry_query.hpp:38
Query parameters for retrieving telemetry statistics.
Definition telemetry_query.hpp:140
Summary statistics for a telemetry overview.
Definition telemetry_stats.hpp:91