sdk.h 3.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

S
superjom 已提交
9 10
const static std::string kDefaultMode{"default"};

S
superjom 已提交
11 12
class Writer {
public:
S
superjom 已提交
13
  Writer(const std::string& dir, int sync_cycle) {
S
superjom 已提交
14
    storage_.SetDir(dir);
S
superjom 已提交
15 16 17 18 19
    storage_.meta.cycle = sync_cycle;
  }
  Writer(const Writer& other) {
    storage_ = other.storage_;
    mode_ = other.mode_;
S
superjom 已提交
20 21
  }

S
superjom 已提交
22 23
  Writer AsMode(const std::string& mode) {
    Writer writer = *this;
S
superjom 已提交
24
    storage_.AddMode(mode);
S
superjom 已提交
25 26
    writer.mode_ = mode;
    return writer;
S
superjom 已提交
27 28
  }

S
superjom 已提交
29 30 31 32
  Tablet AddTablet(const std::string& tag) {
    // TODO(ChunweiYan) add string check here.
    auto tmp = mode_ + "/" + tag;
    string::TagEncode(tmp);
S
superjom 已提交
33 34 35
    auto res = storage_.AddTablet(tmp);
    res.SetCaptions(std::vector<std::string>({mode_}));
    return res;
S
superjom 已提交
36 37 38 39 40 41
  }

  Storage& storage() { return storage_; }

private:
  Storage storage_;
S
superjom 已提交
42
  std::string mode_{kDefaultMode};
S
superjom 已提交
43 44 45 46
};

class Reader {
public:
S
superjom 已提交
47 48 49 50 51 52 53
  Reader(const std::string& dir) : reader_(dir) {}

  Reader AsMode(const std::string& mode) {
    auto tmp = *this;
    tmp.mode_ = mode;
    return tmp;
  }
S
superjom 已提交
54 55 56 57 58 59 60

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

S
superjom 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  std::vector<std::string> all_tags() {
    auto tags = reader_.all_tags();
    auto it =
        std::remove_if(tags.begin(), tags.end(), [&](const std::string& tag) {
          return !TagMatchMode(tag);
        });
    tags.erase(it + 1);
    return tags;
  }

  std::vector<std::string> tags(const std::string& component) {
    auto type = Tablet::type(component);
    auto tags = reader_.tags(type);
    CHECK(!tags.empty());
    std::vector<std::string> res;
    for (const auto& tag : tags) {
      if (TagMatchMode(tag)) {
        res.push_back(GenReadableTag(tag));
      }
    }
    return res;
  }

  StorageReader& storage() { return reader_; }

protected:
  bool TagMatchMode(const std::string& tag) {
    if (tag.size() <= mode_.size()) return false;
    return tag.substr(0, mode_.size()) == mode_;
  }
  std::string GenReadableTag(const std::string& tag) {
    auto tmp = tag;
    string::TagDecode(tmp);
    return tmp.substr(mode_.size() + 1);  // including `/`
  }

S
superjom 已提交
97 98
private:
  StorageReader reader_;
S
superjom 已提交
99
  std::string mode_{kDefaultMode};
S
superjom 已提交
100 101
};

S
superjom 已提交
102 103 104 105 106 107
namespace components {

/*
 * Read and write support for Scalar component.
 */
template <typename T>
S
superjom 已提交
108
struct Scalar {
S
superjom 已提交
109 110 111 112
  Scalar(Tablet tablet) : tablet_(tablet) {
    tablet_.SetType(Tablet::Type::kScalar);
  }

S
superjom 已提交
113
  void SetCaption(const std::string cap) {
S
superjom 已提交
114
    tablet_.SetCaptions(std::vector<std::string>({cap}));
S
superjom 已提交
115
  }
S
superjom 已提交
116

S
superjom 已提交
117 118 119 120 121 122
  void AddRecord(int id, T value) {
    auto record = tablet_.AddRecord();
    record.SetId(id);
    auto entry = record.AddData<T>();
    entry.Set(value);
  }
S
superjom 已提交
123

S
superjom 已提交
124
private:
S
superjom 已提交
125
  Tablet tablet_;
S
superjom 已提交
126 127
};

S
superjom 已提交
128 129 130 131 132
template <typename T>
struct ScalarReader {
  ScalarReader(TabletReader&& reader) : reader_(reader) {}

  std::vector<T> records() const;
S
superjom 已提交
133 134
  std::vector<T> ids() const;
  std::vector<T> timestamps() const;
S
superjom 已提交
135
  std::string caption() const;
S
superjom 已提交
136
  size_t total_records() { return reader_.total_records(); }
S
superjom 已提交
137 138
  size_t size() const;

S
superjom 已提交
139
private:
S
superjom 已提交
140 141 142
  TabletReader reader_;
};

S
superjom 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/*
 * Image component writer.
 */
struct Image {
  using value_t = float;

  Image(Tablet tablet, int num_samples) : writer_(tablet) {
    writer_.SetType(Tablet::Type::kImage);
    writer_.SetNumSamples(num_samples);
    num_samples_ = num_samples;
  }
  /*
   * Start a sample period.
   */
  void StartSampling();
  /*
   * Will this sample will be taken.
   */
  int IsSampleTaken();
  /*
   * End a sample period.
   */
  void FinishSampling();

  void SetSample(int index,
                 const std::vector<int64_t>& shape,
                 const std::vector<value_t>& data);

private:
  Tablet writer_;
  Record step_;
  int num_records_{0};
  int num_samples_{0};
};

S
superjom 已提交
178
}  // namespace components
S
superjom 已提交
179
}  // namespace visualdl
S
superjom 已提交
180

S
superjom 已提交
181
#endif