sdk.h 2.9 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
template <typename T>
std::vector<T> ScalarReader<T>::records() const {
  std::vector<T> res;
  for (int i = 0; i < reader_.total_records(); i++) {
    res.push_back(reader_.record(i).data<T>(0).Get());
  }
  return res;
}

template <typename T>
std::vector<T> ScalarReader<T>::ids() const {
  std::vector<T> res;
  for (int i = 0; i < reader_.total_records(); i++) {
    res.push_back(reader_.record(i).id());
  }
  return res;
}

template <typename T>
std::vector<T> ScalarReader<T>::timestamps() const {
  std::vector<T> res;
  for (int i = 0; i < reader_.total_records(); i++) {
    res.push_back(reader_.record(i).timestamp());
  }
  return res;
}

template <typename T>
S
superjom 已提交
122 123 124
std::string ScalarReader<T>::caption() const {
  CHECK(!reader_.captions().empty()) << "no caption";
  return reader_.captions().front();
S
superjom 已提交
125 126 127 128 129 130 131
}

template <typename T>
size_t ScalarReader<T>::size() const {
  return reader_.total_records();
}

S
superjom 已提交
132
}  // namespace components
S
superjom 已提交
133
}  // namespace visualdl
S
superjom 已提交
134

S
superjom 已提交
135
#endif