CMS 3D CMS Logo

JsonMonitorable.h
Go to the documentation of this file.
1 /*
2  * JsonMonitorable.h
3  *
4  * Created on: Oct 29, 2012
5  * Author: aspataru
6  */
7 
8 #ifndef JSON_MONITORABLE_H
9 #define JSON_MONITORABLE_H
10 
11 #include <string>
12 #include <sstream>
13 #include <vector>
14 #include <memory>
15 //#include "EventFilter/Utilities/interface/Utils.h"
16 #include <iostream>
17 #include "json.h"
18 
19 namespace jsoncollector {
20 
23 
25  public:
27 
28  virtual ~JsonMonitorable() {}
29 
30  virtual std::string toString() const = 0;
31 
32  virtual void resetValue() = 0;
33 
34  unsigned int getUpdates() { return updates_; }
35 
36  bool getNotSame() { return notSame_; }
37 
38  virtual void setName(std::string name) { name_ = name; }
39 
40  virtual std::string& getName() { return name_; }
41 
42  protected:
44  unsigned int updates_;
45  bool notSame_;
46  };
47 
48  class JsonMonPtr {
49  public:
52  void operator=(JsonMonitorable* ptr) { ptr_ = ptr; }
54  if (ptr_)
55  delete ptr_;
56  ptr_ = nullptr;
57  }
59  JsonMonitorable* get() { return ptr_; }
60  //JsonMonPtr& operator=(JsonMonPtr& ) = delete;
61  //JsonMonPtr& operator=(JsonMonPtr&& other){ptr_=other.ptr_;return *this;}
62  private:
64  };
65 
66  class IntJ : public JsonMonitorable {
67  public:
68  IntJ() : JsonMonitorable(), theVar_(0) {}
69  IntJ(long val) : JsonMonitorable(), theVar_(val) {}
70 
71  ~IntJ() override {}
72 
73  std::string toString() const override {
74  std::stringstream ss;
75  ss << theVar_;
76  return ss.str();
77  }
78  void resetValue() override {
79  theVar_ = 0;
80  updates_ = 0;
81  notSame_ = false;
82  }
83  void operator=(long sth) {
84  theVar_ = sth;
85  updates_ = 1;
86  notSame_ = false;
87  }
88  long& value() { return theVar_; }
89 
90  void update(long sth) {
91  theVar_ = sth;
92  if (updates_ && theVar_ != sth)
93  notSame_ = true;
94  updates_++;
95  }
96 
97  void add(long sth) {
98  theVar_ += sth;
99  updates_++;
100  }
101 
102  private:
103  long theVar_;
104  };
105 
106  class DoubleJ : public JsonMonitorable {
107  public:
108  DoubleJ() : JsonMonitorable(), theVar_(0) {}
109  DoubleJ(double val) : JsonMonitorable(), theVar_(val) {}
110 
111  ~DoubleJ() override {}
112 
113  std::string toString() const override {
114  std::stringstream ss;
115  ss << theVar_;
116  return ss.str();
117  }
118  void resetValue() override {
119  theVar_ = 0;
120  updates_ = 0;
121  notSame_ = false;
122  }
123  void operator=(double sth) {
124  theVar_ = sth;
125  updates_ = 1;
126  notSame_ = false;
127  }
128  double& value() { return theVar_; }
129  void update(double sth) {
130  theVar_ = sth;
131  if (updates_ && theVar_ != sth)
132  notSame_ = true;
133  updates_++;
134  }
135 
136  private:
137  double theVar_;
138  };
139 
140  class StringJ : public JsonMonitorable {
141  public:
143 
144  ~StringJ() override {}
145 
146  std::string toString() const override { return theVar_; }
147  void resetValue() override {
148  theVar_ = std::string();
149  updates_ = 0;
150  notSame_ = false;
151  }
152  void operator=(std::string sth) {
153  theVar_ = sth;
154  updates_ = 1;
155  notSame_ = false;
156  }
157  std::string& value() { return theVar_; }
158  void concatenate(std::string const& added) {
159  if (!updates_)
160  theVar_ = added;
161  else
162  theVar_ += "," + added;
163  updates_++;
164  }
165  void update(std::string const& newStr) {
166  theVar_ = newStr;
167  updates_ = 1;
168  }
169 
170  private:
172  };
173 
174  //histograms filled at time intervals (later converted to full histograms)
175  template <class T>
176  class HistoJ : public JsonMonitorable {
177  public:
178  HistoJ(int expectedUpdates = 1, unsigned int maxUpdates = 0) {
179  expectedSize_ = expectedUpdates;
180  updates_ = 0;
181  maxUpdates_ = maxUpdates;
182  if (maxUpdates_ && maxUpdates_ < expectedSize_)
183  expectedSize_ = maxUpdates_;
184  histo_.reserve(expectedSize_);
185  }
186  ~HistoJ() override {}
187 
188  std::string toCSV() const {
189  std::stringstream ss;
190  for (unsigned int i = 0; i < updates_; i++) {
191  ss << histo_[i];
192  if (i != histo_.size() - 1)
193  ss << ",";
194  }
195  return ss.str();
196  }
197 
198  std::string toString() const override {
199  std::stringstream ss;
200  ss << "[";
201  if (!histo_.empty())
202  for (unsigned int i = 0; i < histo_.size(); i++) {
203  ss << histo_[i];
204  if (i < histo_.size() - 1)
205  ss << ",";
206  }
207  ss << "]";
208  return ss.str();
209  }
210  virtual Json::Value toJsonValue() const { //TODO
211  Json::Value jsonValue(Json::arrayValue);
212  for (unsigned int i = 0; i < histo_.size(); i++) {
213  jsonValue.append(histo_[i]);
214  }
215  return jsonValue;
216  }
217  void resetValue() override {
218  histo_.clear();
219  histo_.reserve(expectedSize_);
220  updates_ = 0;
221  }
222  void operator=(std::vector<T>& sth) { histo_ = sth; }
223 
224  std::vector<T>& value() { return histo_; }
225 
226  unsigned int getExpectedSize() { return expectedSize_; }
227 
228  unsigned int getMaxUpdates() { return maxUpdates_; }
229 
230  void setMaxUpdates(unsigned int maxUpdates) {
231  maxUpdates_ = maxUpdates;
232  if (!maxUpdates_)
233  return;
234  if (expectedSize_ > maxUpdates_)
235  expectedSize_ = maxUpdates_;
236  //truncate what is over the limit
237  if (maxUpdates_ && histo_.size() > maxUpdates_) {
238  histo_.resize(maxUpdates_);
239  } else
240  histo_.reserve(expectedSize_);
241  }
242 
243  unsigned int getSize() { return histo_.size(); }
244 
245  void update(T val) {
246  if (maxUpdates_ && updates_ >= maxUpdates_)
247  return;
248  histo_.push_back(val);
249  updates_++;
250  }
251 
252  private:
253  std::vector<T> histo_;
254  unsigned int expectedSize_;
255  unsigned int maxUpdates_;
256  };
257 
258 } // namespace jsoncollector
259 
260 #endif
std::string toString() const override
void setMaxUpdates(unsigned int maxUpdates)
std::string toCSV() const
void operator=(std::string sth)
void resetValue() override
const char * ptr_
Definition: DataKey.cc:76
virtual std::string & getName()
JsonMonitorable * ptr_
void operator=(long sth)
#define nullptr
Value & append(const Value &value)
Append value to array at the end.
void update(std::string const &newStr)
Represents a JSON value.
Definition: value.h:99
unsigned int getExpectedSize()
virtual Json::Value toJsonValue() const
void operator=(std::vector< T > &sth)
std::vector< T > & value()
virtual std::string toString() const =0
void concatenate(std::string const &added)
void add(long sth)
virtual void setName(std::string name)
unsigned int expectedSize_
void resetValue() override
unsigned int getSize()
void resetValue() override
std::string toString() const override
JsonMonPtr(JsonMonitorable *ptr)
std::string toString() const override
void operator=(double sth)
std::string toString() const override
HistoJ(int expectedUpdates=1, unsigned int maxUpdates=0)
void resetValue() override
void update(long sth)
std::vector< T > histo_
JsonMonitorable * operator->()
long double T
unsigned int getMaxUpdates()
void update(double sth)
array value (ordered list)
Definition: value.h:30
void operator=(JsonMonitorable *ptr)