#include <chrono>
#include <iostream>
#include <thread>
std::cout << "=== Time Series Storage Example ===" << std::endl;
config.memtable_size_mb = 16;
auto engine = std::make_unique<timeseries_engine>(config);
std::cout << "\nWriting test metrics..." << std::endl;
auto now = std::chrono::steady_clock::now();
for (int i = 0; i < 10; ++i) {
double value = 100.0 + i * 10;
auto timestamp = now - std::chrono::seconds(60 * (10 - i));
engine->write("cpu.usage", value, timestamp, {{"host", "server1"}});
engine->write("memory.usage", value * 0.8, timestamp, {{"host", "server1"}});
std::cout << " Written metric at t-" << (10 - i) << " minutes: " << value << std::endl;
}
std::cout << "\nQuerying CPU usage for last hour..." << std::endl;
auto results = engine->query("cpu.usage",
now - std::chrono::hours(1),
now,
{{"host", "server1"}});
for (const auto& series : results) {
std::cout << "Series: " << series.metric_name << std::endl;
std::cout << " Points: " << series.points.size() << std::endl;
std::cout << " Min: " << series.min_value << std::endl;
std::cout << " Max: " << series.max_value << std::endl;
std::cout << " Avg: " << series.average() << std::endl;
}
std::cout << "\n=== Metric Database Example ===" << std::endl;
database_config db_config;
db_config.data_directory = "./metrics_test_db";
db_config.partition_strategy = partition_strategy::by_metric_name;
auto database = std::make_unique<metric_database>(db_config);
std::vector<metric> batch;
for (int i = 0; i < 5; ++i) {
m.
timestamp = std::chrono::system_clock::now();
m.
tags = {{
"env",
"test"}, {
"instance", std::to_string(i)}};
batch.push_back(m);
}
size_t written = database->write_batch(batch);
std::cout << "Written " << written << " metrics to database" << std::endl;
std::cout << "\n=== Query Engine Example ===" << std::endl;
auto query_engine = std::make_unique<metric_query_engine>(database.get());
std::string query_str = "SELECT cpu.usage WHERE host='server1' FROM -1h";
std::cout << "Query: " << query_str << std::endl;
auto stats = database->get_stats();
std::cout << "\nDatabase Statistics:" << std::endl;
std::cout << " Total metrics: " << stats.total_metrics << std::endl;
std::cout << " Total points: " << stats.total_points << std::endl;
std::cout << " Total partitions: " << stats.total_partitions << std::endl;
std::cout << "\nStorage example completed successfully!" << std::endl;
return 0;
}
Represents a single metric value with metadata.
Basic metric structure for interface compatibility.
std::chrono::system_clock::time_point timestamp
std::variant< double, int64_t, std::string > value
std::unordered_map< std::string, std::string > tags
std::string data_directory
compression_algorithm compression