From 999fe59f28acc10db2efd4b37a8436c2d8f0bb56 Mon Sep 17 00:00:00 2001 From: 0xG0nzo0 <8682922+0xg0nz0@users.noreply.github.com> Date: Mon, 1 Jan 2024 19:17:36 +0000 Subject: [PATCH] Initial commit of Stats model --- CMakeLists.txt | 4 +- sdk/message.cc | 4 - sdk/message.h | 20 ---- sdk/models.cc | 28 +++++ sdk/models.h | 132 ++++++++++++++++++++++ tests/{message_test.cc => models_test.cc} | 8 +- 6 files changed, 166 insertions(+), 30 deletions(-) delete mode 100644 sdk/message.cc delete mode 100644 sdk/message.h create mode 100644 sdk/models.cc create mode 100644 sdk/models.h rename tests/{message_test.cc => models_test.cc} (62%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ddc18c4..26d3072 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ FetchContent_MakeAvailable(googletest) add_library( iggy - sdk/message.cc + sdk/models.cc ) # set up GoogleTest @@ -29,7 +29,7 @@ enable_testing() add_executable( iggy_cpp_test - tests/message_test.cc + tests/models_test.cc ) target_link_libraries( iggy_cpp_test diff --git a/sdk/message.cc b/sdk/message.cc deleted file mode 100644 index 1be1fc6..0000000 --- a/sdk/message.cc +++ /dev/null @@ -1,4 +0,0 @@ -#include "message.h" - -Message::Message() noexcept { -} \ No newline at end of file diff --git a/sdk/message.h b/sdk/message.h deleted file mode 100644 index 0b0ec10..0000000 --- a/sdk/message.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef MESSAGE_H -#define MESSAGE_H - -/** - * @class Message - * @brief A class to represent a message. - * - * This class is used to represent a simple message. It currently has a default constructor. - */ -class Message { -public: - /** - * @brief Default constructor for Message. - * - * Constructs a new Message object with default parameters. - */ - Message() noexcept; -}; - -#endif // MESSAGE_H \ No newline at end of file diff --git a/sdk/models.cc b/sdk/models.cc new file mode 100644 index 0000000..b17908f --- /dev/null +++ b/sdk/models.cc @@ -0,0 +1,28 @@ +#include "models.h" + +std::ostream& iggy::models::operator<<(std::ostream& os, const iggy::models::Stats& stats) { + // Write the size of the string, then the string itself + size_t size = stats.os_version.size(); + os.write(reinterpret_cast(&size), sizeof(size)); + os.write(stats.os_version.c_str(), size); + + size = stats.kernel_version.size(); + os.write(reinterpret_cast(&size), sizeof(size)); + os.write(stats.kernel_version.c_str(), size); + + return os; +} + +std::istream& iggy::models::operator>>(std::istream& is, iggy::models::Stats& stats) { + // Read the size of the string, then the string itself + size_t size; + is.read(reinterpret_cast(&size), sizeof(size)); + stats.os_version.resize(size); + is.read(&stats.os_version[0], size); + + is.read(reinterpret_cast(&size), sizeof(size)); + stats.kernel_version.resize(size); + is.read(&stats.kernel_version[0], size); + + return is; +} \ No newline at end of file diff --git a/sdk/models.h b/sdk/models.h new file mode 100644 index 0000000..4979d4e --- /dev/null +++ b/sdk/models.h @@ -0,0 +1,132 @@ +#pragma once + +#include +#include +#include + +namespace iggy { +namespace models { + +/// @brief Mapping for fractional values between 0 and 1. +typedef float percent_t; + +/// @brief Mapping for absolute times and time durations; currently in seconds. +typedef unsigned long long time_val_t; + +/// @brief Mapping for values that measure byte counts. +typedef unsigned long long byte_cnt_t; + +/// @brief Mapping for values that measure object counts. +typedef unsigned int obj_cnt_t; + +/// @brief Mapping for values that measure (potentially very large) message counts. +typedef unsigned long long msg_cnt_t; + +/** + * @class Stats + * @brief Model class holding server performance statistics. + * + * This class is used to represent the result of the GetStats call, + * which gets a snapshot of latest server performance statistics. + * + * @see [GetStats definition](https://docs.iggy.rs/specification/binary#get-stats) + * @see [stats.rs](https://github.com/iggy-rs/iggy/blob/master/iggy/src/models/stats.rs) + */ +class Stats { +private: + pid_t process_id; + percent_t cpu_usage; + byte_cnt_t memory_usage; + byte_cnt_t total_memory; + byte_cnt_t available_memory; + time_val_t run_time; + time_val_t start_time; + byte_cnt_t read_bytes; + byte_cnt_t written_bytes; + byte_cnt_t messages_size_bytes; + obj_cnt_t streams_count; + obj_cnt_t topics_count; + obj_cnt_t partitions_count; + obj_cnt_t segments_count; + msg_cnt_t messages_count; + obj_cnt_t clients_count; + obj_cnt_t consumer_groups_count; + std::string hostname; + std::string os_name; + std::string os_version; + std::string kernel_version; + +public: + Stats() = default; + + /// @brief Get the server process ID (PID) + pid_t getProcessId() const { return process_id; } + + /// @brief Get the server process CPU usage. + percent_t getCpuUsage() const { return cpu_usage; } + + /// @brief Get the server total memory usage. + byte_cnt_t getMemoryUsage() const { return memory_usage; } + + /// @brief Get the server total memory. + byte_cnt_t getTotalMemory() const { return total_memory; } + + /// @brief Get the server available memory. + byte_cnt_t getAvailableMemory() const { return available_memory; } + + /// @brief Get how long the server has been running, in seconds. + time_val_t getRunTime() const { return run_time; } + + /// @brief Get the server start time, in seconds since the UNIX epoch. + time_val_t getStartTime() const { return start_time; } + + /// @brief Get the number of bytes read since process start. + byte_cnt_t getReadBytes() const { return read_bytes; } + + /// @brief Get the number of bytes written since process start. + byte_cnt_t getWrittenBytes() const { return written_bytes; } + + /// @brief Get the total size of all messages processed. + byte_cnt_t getMessagesSizeBytes() const { return messages_size_bytes; } + + /// @brief Get the total number of streams in the server. + obj_cnt_t getStreamsCount() const { return streams_count; } + + /// @brief Get the total number of topics on the server. + obj_cnt_t getTopicsCount() const { return topics_count; } + + /// @brief Get the total number of topic partitions on the server across all topics. + obj_cnt_t getPartitionsCount() const { return partitions_count; } + + /// @brief Get the total number of fixed-size segments used for topic storage on disk. + obj_cnt_t getSegmentsCount() const { return segments_count; } + + /// @brief Get the total number of messages processed by the server across all topics. + msg_cnt_t getMessagesCount() const { return messages_count; } + + /// @brief Get the total number of active connected clients. + obj_cnt_t getClientsCount() const { return clients_count; } + + /// @brief Get the total number of active consumer groups on the server. + obj_cnt_t getConsumerGroupsCount() const { return consumer_groups_count; } + + /// @brief Get the name of the host that the server process is running on. + std::string getHostname() const { return hostname; } + + /// @brief Get the name of the operating system that the server process is running on. + std::string getOsName() const { return os_name; } + + /// @brief Get the version of the operating system that the server process is running on. + std::string getOsVersion() const { return os_version; } + + /// @brief Get the version of the OS kernel that the server process is running on. + std::string getKernelVersion() const { return kernel_version; } + + /// @brief Helper that writes the Stats object into a stream. + friend std::ostream& operator<<(std::ostream& os, const Stats& stats); + + /// @brief Helper that reads the Stats object from a stream. + friend std::istream& operator>>(std::istream& is, Stats& stats); +}; +} // namespace models +} // namespace iggy \ No newline at end of file diff --git a/tests/message_test.cc b/tests/models_test.cc similarity index 62% rename from tests/message_test.cc rename to tests/models_test.cc index a9ee210..b957c3c 100644 --- a/tests/message_test.cc +++ b/tests/models_test.cc @@ -1,11 +1,11 @@ #include -#include "../sdk/message.h" +#include "../sdk/models.h" -TEST(MessageTest, DefaultConstructor) { +TEST(ModelsTest, DefaultConstructor) { // Create a Message object using the default constructor - Message message; + iggy::models::Stats stats; // Perform assertions to verify the expected behavior // For example, you can check if the message object is not null - ASSERT_NE(nullptr, &message); + ASSERT_NE(nullptr, &stats); }