sdk.cc 4.4 KB
Newer Older
S
superjom 已提交
1
#include "visualdl/backend/logic/sdk.h"
S
superjom 已提交
2
#include <google/protobuf/text_format.h>
S
superjom 已提交
3 4 5

namespace visualdl {

S
superjom 已提交
6 7 8 9 10
#define IMPL_ENTRY_SET_OR_ADD(method__, ctype__, dtype__, opr__) \
  template <>                                                    \
  void EntryHelper<ctype__>::method__(ctype__ v) {               \
    entry->set_dtype(storage::DataType::dtype__);                \
    entry->opr__(v);                                             \
S
superjom 已提交
11 12 13 14 15 16 17 18 19 20 21
  }

IMPL_ENTRY_SET_OR_ADD(Set, int32_t, kInt32, set_i32);
IMPL_ENTRY_SET_OR_ADD(Set, int64_t, kInt64, set_i64);
IMPL_ENTRY_SET_OR_ADD(Set, bool, kBool, set_b);
IMPL_ENTRY_SET_OR_ADD(Set, float, kFloat, set_f);
IMPL_ENTRY_SET_OR_ADD(Set, double, kDouble, set_d);
IMPL_ENTRY_SET_OR_ADD(Add, int32_t, kInt32s, add_i32s);
IMPL_ENTRY_SET_OR_ADD(Add, int64_t, kInt64s, add_i64s);
IMPL_ENTRY_SET_OR_ADD(Add, float, kFloats, add_fs);
IMPL_ENTRY_SET_OR_ADD(Add, double, kDoubles, add_ds);
S
superjom 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
IMPL_ENTRY_SET_OR_ADD(Add, std::string, kStrings, add_ss);
IMPL_ENTRY_SET_OR_ADD(Add, bool, kBools, add_bs);

#define IMPL_ENTRY_GET(T, fieldname__) \
  template <>                          \
  T EntryHelper<T>::Get() const {      \
    return entry->fieldname__();       \
  }
IMPL_ENTRY_GET(int32_t, i32);
IMPL_ENTRY_GET(int64_t, i64);
IMPL_ENTRY_GET(float, f);
IMPL_ENTRY_GET(double, d);
IMPL_ENTRY_GET(std::string, s);
IMPL_ENTRY_GET(bool, b);

#define IMPL_ENTRY_GET_MULTI(T, fieldname__)            \
  template <>                                           \
  std::vector<T> EntryHelper<T>::GetMulti() const {     \
    return std::vector<T>(entry->fieldname__().begin(), \
                          entry->fieldname__().end());  \
  }

IMPL_ENTRY_GET_MULTI(int32_t, i32s);
IMPL_ENTRY_GET_MULTI(int64_t, i64s);
IMPL_ENTRY_GET_MULTI(float, fs);
IMPL_ENTRY_GET_MULTI(double, ds);
IMPL_ENTRY_GET_MULTI(std::string, ss);
IMPL_ENTRY_GET_MULTI(bool, bs);
S
superjom 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62

std::string StorageHelper::human_readable_buffer() const {
  std::string buffer;
  google::protobuf::TextFormat::PrintToString(*data_, &buffer);
  return buffer;
}

std::string TabletHelper::human_readable_buffer() const {
  std::string buffer;
  google::protobuf::TextFormat::PrintToString(*data_, &buffer);
  return buffer;
}

S
superjom 已提交
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 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
void ImHelper::PersistToDisk() const {
  InformationMaintainer::Global().PersistToDisk();
}

// implementations for components
namespace components {

template <typename T>
void ScalarHelper<T>::SetCaptions(const std::vector<std::string> &captions) {
  for (int i = 0; i < captions.size(); i++) {
    data_->add_captions(captions[i]);
  }
}

template <typename T>
void ScalarHelper<T>::AddRecord(int id, const std::vector<T> &values) {
  CHECK_NOTNULL(data_);
  CHECK_GT(data_->captions_size(), 0UL) << "captions should be set first";
  CHECK_EQ(data_->captions_size(), values.size())
      << "number of values in a record should be compatible with the "
         "captions";
  // add record data
  auto *record = data_->add_records();
  auto *data = record->add_data();
  EntryHelper<T> entry_helper(data);
  for (auto v : values) {
    entry_helper.Add(v);
  }
  // set record id
  record->set_id(id);
  // set record timestamp
  record->set_timestamp(time(NULL));
}

template <typename T>
std::vector<std::vector<T>> ScalarHelper<T>::GetRecords() const {
  std::vector<std::vector<T>> result;
  EntryHelper<T> entry_helper;
  for (int i = 0; i < data_->records_size(); i++) {
    auto *entry = data_->mutable_records(i)->mutable_data(0);
    entry_helper(entry);
    auto datas = entry_helper.GetMulti();
    result.push_back(std::move(datas));
  }
  return result;
}

template <typename T>
std::vector<int> ScalarHelper<T>::GetIds() const {
  CHECK_NOTNULL(data_);
  std::vector<int> result;
  for (int i = 0; i < data_->records_size(); i++) {
    result.push_back(data_->records(i).id());
  }
  return result;
}

template <typename T>
std::vector<int> ScalarHelper<T>::GetTimestamps() const {
  CHECK_NOTNULL(data_);
  std::vector<int> result;
  for (int i = 0; i < data_->records_size(); i++) {
    result.push_back(data_->records(i).timestamp());
  }
  return result;
}

template <typename T>
std::vector<std::string> ScalarHelper<T>::GetCaptions() const {
  return std::vector<std::string>(data_->captions().begin(),
                                  data_->captions().end());
}

template class ScalarHelper<int32_t>;
template class ScalarHelper<int64_t>;
template class ScalarHelper<float>;
template class ScalarHelper<double>;

}  // namespace components

}  // namespace visualdl