Skip to content

Commit 2958121

Browse files
committed
Clickhouse - introduce Stats
1 parent 39adf38 commit 2958121

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @file
3+
* @author Michal Sedlak <[email protected]>
4+
* @brief Statistics tracking and reporting
5+
* @date 2025
6+
*
7+
* Copyright(c) 2025 CESNET z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#include "stats.h"
12+
#include "plugin.h"
13+
14+
static constexpr int STATS_PRINT_INTERVAL_SECS = 1;
15+
16+
Stats::Stats(Logger logger, Plugin& plugin) : m_logger(logger), m_plugin(plugin) {}
17+
18+
void Stats::add_recs(uint64_t count)
19+
{
20+
m_recs_processed_since_last += count;
21+
m_recs_processed_total += count;
22+
}
23+
24+
void Stats::add_rows(uint64_t count)
25+
{
26+
m_rows_written_total += count;
27+
}
28+
29+
void Stats::add_dropped(uint64_t count)
30+
{
31+
m_recs_dropped_total += count;
32+
}
33+
34+
void Stats::print_stats_throttled(time_t now)
35+
{
36+
if (m_start_time == 0) {
37+
m_start_time = now;
38+
}
39+
40+
if ((now - m_last_stats_print_time) > STATS_PRINT_INTERVAL_SECS) {
41+
double total_rps = m_recs_processed_total / std::max<double>(1, now - m_start_time);
42+
double immediate_rps = m_recs_processed_since_last / std::max<double>(1, now - m_last_stats_print_time);
43+
m_logger.info("STATS - RECS: %lu (%lu dropped), ROWS: %lu, AVG: %.2f recs/sec, AVG_IMMEDIATE: %.2f recs/sec, BLK_AVAIL_Q: %lu, BLK_FILL_Q: %lu",
44+
m_recs_processed_total,
45+
m_recs_dropped_total,
46+
m_rows_written_total,
47+
total_rps,
48+
immediate_rps,
49+
m_plugin.m_avail_blocks.size(),
50+
m_plugin.m_filled_blocks.size()
51+
);
52+
m_recs_processed_since_last = 0;
53+
m_last_stats_print_time = now;
54+
}
55+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @file
3+
* @author Michal Sedlak <[email protected]>
4+
* @brief Statistics tracking and reporting
5+
* @date 2025
6+
*
7+
* Copyright(c) 2025 CESNET z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#pragma once
12+
13+
#include "common.h"
14+
#include <cstdint>
15+
#include <ctime>
16+
17+
class Plugin; // Forward declaration of Plugin class.
18+
19+
/**
20+
* @class Stats
21+
* @brief A class for tracking and reporting statistics related to data processing.
22+
*
23+
* The Stats class is responsible for maintaining counters and timestamps
24+
* to track the progress of data processing and periodically printing
25+
* statistics in a throttled manner.
26+
*/
27+
class Stats {
28+
public:
29+
/**
30+
* @brief Constructs a Stats object.
31+
*
32+
* @param logger A Logger instance for logging statistics.
33+
* @param plugin A reference to the Plugin instance associated with this Stats object.
34+
*/
35+
Stats(Logger logger, Plugin& plugin);
36+
37+
/**
38+
* @brief Adds a specified number of records to the processed count.
39+
*
40+
* @param count The number of records to add.
41+
*/
42+
void add_recs(uint64_t count);
43+
44+
/**
45+
* @brief Adds a specified number of rows to the written count.
46+
*
47+
* @param count The number of rows to add.
48+
*/
49+
void add_rows(uint64_t count);
50+
51+
/**
52+
* @brief Adds a specified number of records to the dropped count.
53+
*
54+
* @param count The number of records to add.
55+
*/
56+
void add_dropped(uint64_t count);
57+
58+
/**
59+
* @brief Prints the statistics if sufficient time has passed since the last print.
60+
*/
61+
void print_stats_throttled(time_t now);
62+
63+
private:
64+
Logger m_logger; ///< Logger instance for logging statistics.
65+
Plugin &m_plugin; ///< Reference to the associated Plugin instance.
66+
uint64_t m_rows_written_total = 0; ///< Total number of rows written.
67+
uint64_t m_recs_processed_total = 0; ///< Total number of records processed.
68+
uint64_t m_recs_processed_since_last = 0; ///< Records processed since the last statistics print.
69+
uint64_t m_recs_dropped_total = 0; ///< Total number of records dropped.
70+
time_t m_start_time = 0; ///< Start time of the statistics tracking.
71+
time_t m_last_stats_print_time = 0; ///< Time of the last statistics print.
72+
};

0 commit comments

Comments
 (0)