Skip to content

Commit

Permalink
Initial commit of Stats model
Browse files Browse the repository at this point in the history
  • Loading branch information
0xg0nz0 committed Jan 1, 2024
1 parent ea1a058 commit 999fe59
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 30 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FetchContent_MakeAvailable(googletest)
add_library(
iggy

sdk/message.cc
sdk/models.cc
)

# set up GoogleTest
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions sdk/message.cc

This file was deleted.

20 changes: 0 additions & 20 deletions sdk/message.h

This file was deleted.

28 changes: 28 additions & 0 deletions sdk/models.cc
Original file line number Diff line number Diff line change
@@ -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<const char*>(&size), sizeof(size));
os.write(stats.os_version.c_str(), size);

size = stats.kernel_version.size();
os.write(reinterpret_cast<const char*>(&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<char*>(&size), sizeof(size));
stats.os_version.resize(size);
is.read(&stats.os_version[0], size);

is.read(reinterpret_cast<char*>(&size), sizeof(size));
stats.kernel_version.resize(size);
is.read(&stats.kernel_version[0], size);

return is;
}
132 changes: 132 additions & 0 deletions sdk/models.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#pragma once

#include <sys/types.h>
#include <iostream>
#include <string>

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
8 changes: 4 additions & 4 deletions tests/message_test.cc → tests/models_test.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <gtest/gtest.h>
#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);
}

0 comments on commit 999fe59

Please sign in to comment.