From 64885033355471be1c7625b27fcd100f4642d203 Mon Sep 17 00:00:00 2001 From: superjom Date: Fri, 22 Dec 2017 17:31:56 +0800 Subject: [PATCH] add scalar --- visualdl/logic/CMakeLists.txt | 2 ++ visualdl/logic/sdk.cc | 59 ++++++++++++++++++++++++++++++++ visualdl/logic/sdk.h | 23 ++++++++++--- visualdl/storage/storage_test.cc | 1 - 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/visualdl/logic/CMakeLists.txt b/visualdl/logic/CMakeLists.txt index f1a4250d..046e4048 100644 --- a/visualdl/logic/CMakeLists.txt +++ b/visualdl/logic/CMakeLists.txt @@ -1,6 +1,8 @@ #add_library(sdk ${PROJECT_SOURCE_DIR}/visualdl/logic/sdk.cc) add_library(im ${PROJECT_SOURCE_DIR}/visualdl/logic/im.cc) +add_library(sdk ${PROJECT_SOURCE_DIR}/visualdl/logic/sdk.cc) add_dependencies(im storage_proto) +add_dependencies(sdk storage_proto) #add_dependencies(sdk storage_proto) ## pybind diff --git a/visualdl/logic/sdk.cc b/visualdl/logic/sdk.cc index e69de29b..8ac54581 100644 --- a/visualdl/logic/sdk.cc +++ b/visualdl/logic/sdk.cc @@ -0,0 +1,59 @@ +#include "visualdl/logic/sdk.h" + +namespace visualdl { + +namespace components { + +template +void components::Scalar::AddRecord(int id, const std::vector &values) { + // add record data + auto record = tablet_.AddRecord(); + auto entry = record.AddData(); + for (auto v : values) { + entry.Add(v); + } + // set record id + record.SetId(id); + // set record timestamp + record.SetTimeStamp(time(NULL)); +} + +template +std::vector ScalarReader::records() const { + std::vector res; + for (int i = 0; i < reader_.total_records(); i++) { + res.push_back(reader_.record(i).data(0)); + } + return res; +} + +template +std::vector ScalarReader::ids() const { + std::vector res; + for (int i = 0; i < reader_.total_records(); i++) { + res.push_back(reader_.record(i).id()); + } + return res; +} + +template +std::vector ScalarReader::timestamps() const { + std::vector res; + for (int i = 0; i < reader_.total_records(); i++) { + res.push_back(reader_.record(i).timestamp()); + } + return res; +} + +template +std::vector ScalarReader::captions() const { + return reader_.captions(); +} + +template +size_t ScalarReader::size() const { + return reader_.total_records(); +} +} // namespace components + +} // namespace visualdl diff --git a/visualdl/logic/sdk.h b/visualdl/logic/sdk.h index b3c12f91..930fc24e 100644 --- a/visualdl/logic/sdk.h +++ b/visualdl/logic/sdk.h @@ -11,17 +11,32 @@ namespace components { * Read and write support for Scalar component. */ template -class Scalar { -public: - Scalar(Tablet tablet) : tablet_(tablet) { tablet_->SetTag(kScalar); } +struct Scalar { + Scalar(Tablet tablet) : tablet_(tablet) { tablet_.SetType(Tablet::Type::kScalar);} void SetCaption(const std::string cap) { - tablet_->SetCaptions(std::vector({cap})); + tablet_.SetCaptions(std::vector({cap})); } + void AddRecord(int id, const std::vector &values); + private: Tablet tablet_; }; +template +struct ScalarReader { + ScalarReader(TabletReader&& reader) : reader_(reader) {} + + std::vector records() const; + std::vector ids() const; + std::vector timestamps() const; + std::vector captions() const; + size_t size() const; + + private: + TabletReader reader_; +}; + } // namespace components } // namespace visualdl diff --git a/visualdl/storage/storage_test.cc b/visualdl/storage/storage_test.cc index 9eebdc41..8603bf3c 100644 --- a/visualdl/storage/storage_test.cc +++ b/visualdl/storage/storage_test.cc @@ -21,7 +21,6 @@ TEST_F(StorageTest, main) { auto tag0 = storage.AddTablet("tag0"); auto tag1 = storage.AddTablet("tag1"); - StorageReader reader("./tmp/storage_test"); auto modes = reader.Modes(); -- GitLab