sdk.h 2.0 KB
Newer Older
S
superjom 已提交
1 2
#ifndef VISUALDL_LOGIC_SDK_H
#define VISUALDL_LOGIC_SDK_H
S
superjom 已提交
3

S
superjom 已提交
4 5
#include "visualdl/storage/storage.h"
#include "visualdl/storage/tablet.h"
S
superjom 已提交
6
#include "visualdl/utils/string.h"
S
superjom 已提交
7
namespace visualdl {
S
superjom 已提交
8 9 10

class Writer {
public:
S
superjom 已提交
11
  Writer(const std::string& dir) {
S
superjom 已提交
12 13 14
    storage_.SetDir(dir);
  }

S
superjom 已提交
15 16 17 18 19 20
  Writer& AsMode(const std::string& mode) {
    mode_ = mode;
    storage_.AddMode(mode);
    return *this;
  }

S
superjom 已提交
21 22 23 24
  Tablet AddTablet(const std::string& tag) {
    // TODO(ChunweiYan) add string check here.
    auto tmp = mode_ + "/" + tag;
    string::TagEncode(tmp);
S
superjom 已提交
25 26 27
    auto res = storage_.AddTablet(tmp);
    res.SetCaptions(std::vector<std::string>({mode_}));
    return res;
S
superjom 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  }

  Storage& storage() { return storage_; }

private:
  Storage storage_;
  std::string mode_;
};

class Reader {
public:
  Reader(const std::string& mode, const std::string& dir)
      : mode_(mode), reader_(dir) {}

  TabletReader tablet(const std::string& tag) {
    auto tmp = mode_ + "/" + tag;
    string::TagEncode(tmp);
    return reader_.tablet(tmp);
  }

private:
  StorageReader reader_;
S
superjom 已提交
50
  std::string mode_{"default"};
S
superjom 已提交
51 52
};

S
superjom 已提交
53 54 55 56 57 58
namespace components {

/*
 * Read and write support for Scalar component.
 */
template <typename T>
S
superjom 已提交
59
struct Scalar {
S
superjom 已提交
60 61 62 63
  Scalar(Tablet tablet) : tablet_(tablet) {
    tablet_.SetType(Tablet::Type::kScalar);
  }

S
superjom 已提交
64
  void SetCaption(const std::string cap) {
S
superjom 已提交
65
    tablet_.SetCaptions(std::vector<std::string>({cap}));
S
superjom 已提交
66
  }
S
superjom 已提交
67

S
superjom 已提交
68 69 70 71 72 73
  void AddRecord(int id, T value) {
    auto record = tablet_.AddRecord();
    record.SetId(id);
    auto entry = record.AddData<T>();
    entry.Set(value);
  }
S
superjom 已提交
74

S
superjom 已提交
75
private:
S
superjom 已提交
76
  Tablet tablet_;
S
superjom 已提交
77 78
};

S
superjom 已提交
79 80 81 82 83
template <typename T>
struct ScalarReader {
  ScalarReader(TabletReader&& reader) : reader_(reader) {}

  std::vector<T> records() const;
S
superjom 已提交
84 85
  std::vector<T> ids() const;
  std::vector<T> timestamps() const;
S
superjom 已提交
86 87
  std::string caption() const;
  size_t total_records() {return reader_.total_records();}
S
superjom 已提交
88 89
  size_t size() const;

S
superjom 已提交
90
private:
S
superjom 已提交
91 92 93
  TabletReader reader_;
};

S
superjom 已提交
94

S
superjom 已提交
95
}  // namespace components
S
superjom 已提交
96
}  // namespace visualdl
S
superjom 已提交
97

S
superjom 已提交
98
#endif