File indexing completed on 2021-07-28 03:09:50
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef JSON_MONITORABLE_H
0009 #define JSON_MONITORABLE_H
0010
0011 #include <string>
0012 #include <sstream>
0013 #include <vector>
0014 #include <memory>
0015
0016 #include <iostream>
0017 #include "json.h"
0018
0019 namespace jsoncollector {
0020
0021 enum MonType { TYPEINT, TYPEUINT, TYPEDOUBLE, TYPESTRING, TYPEUNDEFINED };
0022 enum OperationType { OPSUM, OPAVG, OPSAME, OPHISTO, OPCAT, OPUNKNOWN };
0023
0024 class JsonMonitorable {
0025 public:
0026 JsonMonitorable() : updates_(0), notSame_(false) {}
0027
0028 virtual ~JsonMonitorable() {}
0029
0030 virtual std::string toString() const = 0;
0031
0032 virtual void resetValue() = 0;
0033
0034 unsigned int getUpdates() { return updates_; }
0035
0036 bool getNotSame() const { return notSame_; }
0037
0038 virtual void setName(std::string name) { name_ = name; }
0039
0040 virtual std::string const& getName() const { return name_; }
0041
0042 protected:
0043 std::string name_;
0044 unsigned int updates_;
0045 bool notSame_;
0046 };
0047
0048 class JsonMonPtr {
0049 public:
0050 JsonMonPtr() : ptr_(nullptr) {}
0051 JsonMonPtr(JsonMonitorable* ptr) : ptr_(ptr) {}
0052 void operator=(JsonMonitorable* ptr) { ptr_ = ptr; }
0053 ~JsonMonPtr() {
0054 if (ptr_)
0055 delete ptr_;
0056 ptr_ = nullptr;
0057 }
0058 JsonMonitorable* operator->() { return ptr_; }
0059 JsonMonitorable* get() { return ptr_; }
0060
0061
0062 private:
0063 JsonMonitorable* ptr_;
0064 };
0065
0066 class IntJ : public JsonMonitorable {
0067 public:
0068 IntJ() : JsonMonitorable(), theVar_(0) {}
0069 IntJ(long val) : JsonMonitorable(), theVar_(val) {}
0070
0071 ~IntJ() override {}
0072
0073 std::string toString() const override {
0074 std::stringstream ss;
0075 ss << theVar_;
0076 return ss.str();
0077 }
0078 void resetValue() override {
0079 theVar_ = 0;
0080 updates_ = 0;
0081 notSame_ = false;
0082 }
0083 void operator=(long sth) {
0084 theVar_ = sth;
0085 updates_ = 1;
0086 notSame_ = false;
0087 }
0088 long& value() { return theVar_; }
0089 long value() const { return theVar_; }
0090
0091 void update(long sth) {
0092 theVar_ = sth;
0093 if (updates_ && theVar_ != sth)
0094 notSame_ = true;
0095 updates_++;
0096 }
0097
0098 void add(long sth) {
0099 theVar_ += sth;
0100 updates_++;
0101 }
0102
0103 private:
0104 long theVar_;
0105 };
0106
0107 class DoubleJ : public JsonMonitorable {
0108 public:
0109 DoubleJ() : JsonMonitorable(), theVar_(0) {}
0110 DoubleJ(double val) : JsonMonitorable(), theVar_(val) {}
0111
0112 ~DoubleJ() override {}
0113
0114 std::string toString() const override {
0115 std::stringstream ss;
0116 ss << theVar_;
0117 return ss.str();
0118 }
0119 void resetValue() override {
0120 theVar_ = 0;
0121 updates_ = 0;
0122 notSame_ = false;
0123 }
0124 void operator=(double sth) {
0125 theVar_ = sth;
0126 updates_ = 1;
0127 notSame_ = false;
0128 }
0129 double& value() { return theVar_; }
0130 double value() const { return theVar_; }
0131 void update(double sth) {
0132 theVar_ = sth;
0133 if (updates_ && theVar_ != sth)
0134 notSame_ = true;
0135 updates_++;
0136 }
0137
0138 private:
0139 double theVar_;
0140 };
0141
0142 class StringJ : public JsonMonitorable {
0143 public:
0144 StringJ() : JsonMonitorable() {}
0145
0146 ~StringJ() override {}
0147
0148 std::string toString() const override { return theVar_; }
0149 void resetValue() override {
0150 theVar_ = std::string();
0151 updates_ = 0;
0152 notSame_ = false;
0153 }
0154 void operator=(std::string sth) {
0155 theVar_ = sth;
0156 updates_ = 1;
0157 notSame_ = false;
0158 }
0159 std::string& value() { return theVar_; }
0160 std::string const& value() const { return theVar_; }
0161 void concatenate(std::string const& added) {
0162 if (!updates_)
0163 theVar_ = added;
0164 else
0165 theVar_ += "," + added;
0166 updates_++;
0167 }
0168 void update(std::string const& newStr) {
0169 theVar_ = newStr;
0170 updates_ = 1;
0171 }
0172
0173 private:
0174 std::string theVar_;
0175 };
0176
0177
0178 template <class T>
0179 class HistoJ : public JsonMonitorable {
0180 public:
0181 HistoJ(int expectedUpdates = 1, unsigned int maxUpdates = 0) {
0182 expectedSize_ = expectedUpdates;
0183 updates_ = 0;
0184 maxUpdates_ = maxUpdates;
0185 if (maxUpdates_ && maxUpdates_ < expectedSize_)
0186 expectedSize_ = maxUpdates_;
0187 histo_.reserve(expectedSize_);
0188 }
0189 ~HistoJ() override {}
0190
0191 std::string toCSV() const {
0192 std::stringstream ss;
0193 for (unsigned int i = 0; i < updates_; i++) {
0194 ss << histo_[i];
0195 if (i != histo_.size() - 1)
0196 ss << ",";
0197 }
0198 return ss.str();
0199 }
0200
0201 std::string toString() const override {
0202 std::stringstream ss;
0203 ss << "[";
0204 if (!histo_.empty())
0205 for (unsigned int i = 0; i < histo_.size(); i++) {
0206 ss << histo_[i];
0207 if (i < histo_.size() - 1)
0208 ss << ",";
0209 }
0210 ss << "]";
0211 return ss.str();
0212 }
0213 virtual Json::Value toJsonValue() const {
0214 Json::Value jsonValue(Json::arrayValue);
0215 for (unsigned int i = 0; i < histo_.size(); i++) {
0216 jsonValue.append(histo_[i]);
0217 }
0218 return jsonValue;
0219 }
0220 void resetValue() override {
0221 histo_.clear();
0222 histo_.reserve(expectedSize_);
0223 updates_ = 0;
0224 }
0225 void operator=(std::vector<T> const& sth) { histo_ = sth; }
0226
0227 std::vector<T>& value() { return histo_; }
0228 std::vector<T> const& value() const { return histo_; }
0229
0230 unsigned int getExpectedSize() const { return expectedSize_; }
0231
0232 unsigned int getMaxUpdates() const { return maxUpdates_; }
0233
0234 void setMaxUpdates(unsigned int maxUpdates) {
0235 maxUpdates_ = maxUpdates;
0236 if (!maxUpdates_)
0237 return;
0238 if (expectedSize_ > maxUpdates_)
0239 expectedSize_ = maxUpdates_;
0240
0241 if (maxUpdates_ && histo_.size() > maxUpdates_) {
0242 histo_.resize(maxUpdates_);
0243 } else
0244 histo_.reserve(expectedSize_);
0245 }
0246
0247 unsigned int getSize() const { return histo_.size(); }
0248
0249 void update(T val) {
0250 if (maxUpdates_ && updates_ >= maxUpdates_)
0251 return;
0252 histo_.push_back(val);
0253 updates_++;
0254 }
0255
0256 private:
0257 std::vector<T> histo_;
0258 unsigned int expectedSize_;
0259 unsigned int maxUpdates_;
0260 };
0261
0262 }
0263
0264 #endif