CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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() const { return notSame_; }
37 
38  virtual void setName(std::string name) { name_ = name; }
39 
40  virtual std::string const& getName() const { return name_; }
41 
42  protected:
44  unsigned int updates_;
45  bool notSame_;
46  };
47 
48  class JsonMonPtr {
49  public:
50  JsonMonPtr() : ptr_(nullptr) {}
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:
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  long value() const { return theVar_; }
90 
91  void update(long sth) {
92  theVar_ = sth;
93  if (updates_ && theVar_ != sth)
94  notSame_ = true;
95  updates_++;
96  }
97 
98  void add(long sth) {
99  theVar_ += sth;
100  updates_++;
101  }
102 
103  private:
104  long theVar_;
105  };
106 
107  class DoubleJ : public JsonMonitorable {
108  public:
110  DoubleJ(double val) : JsonMonitorable(), theVar_(val) {}
111 
112  ~DoubleJ() override {}
113 
114  std::string toString() const override {
115  std::stringstream ss;
116  ss << theVar_;
117  return ss.str();
118  }
119  void resetValue() override {
120  theVar_ = 0;
121  updates_ = 0;
122  notSame_ = false;
123  }
124  void operator=(double sth) {
125  theVar_ = sth;
126  updates_ = 1;
127  notSame_ = false;
128  }
129  double& value() { return theVar_; }
130  double value() const { return theVar_; }
131  void update(double sth) {
132  theVar_ = sth;
133  if (updates_ && theVar_ != sth)
134  notSame_ = true;
135  updates_++;
136  }
137 
138  private:
139  double theVar_;
140  };
141 
142  class StringJ : public JsonMonitorable {
143  public:
145 
146  ~StringJ() override {}
147 
148  std::string toString() const override { return theVar_; }
149  void resetValue() override {
150  theVar_ = std::string();
151  updates_ = 0;
152  notSame_ = false;
153  }
154  void operator=(std::string sth) {
155  theVar_ = sth;
156  updates_ = 1;
157  notSame_ = false;
158  }
159  std::string& value() { return theVar_; }
160  std::string const& value() const { return theVar_; }
161  void concatenate(std::string const& added) {
162  if (!updates_)
163  theVar_ = added;
164  else
165  theVar_ += "," + added;
166  updates_++;
167  }
168  void update(std::string const& newStr) {
169  theVar_ = newStr;
170  updates_ = 1;
171  }
172 
173  private:
175  };
176 
177  //histograms filled at time intervals (later converted to full histograms)
178  template <class T>
179  class HistoJ : public JsonMonitorable {
180  public:
181  HistoJ(int expectedUpdates = 1, unsigned int maxUpdates = 0) {
182  expectedSize_ = expectedUpdates;
183  updates_ = 0;
184  maxUpdates_ = maxUpdates;
187  histo_.reserve(expectedSize_);
188  }
189  ~HistoJ() override {}
190 
191  std::string toCSV() const {
192  std::stringstream ss;
193  for (unsigned int i = 0; i < updates_; i++) {
194  ss << histo_[i];
195  if (i != histo_.size() - 1)
196  ss << ",";
197  }
198  return ss.str();
199  }
200 
201  std::string toString() const override {
202  std::stringstream ss;
203  ss << "[";
204  if (!histo_.empty())
205  for (unsigned int i = 0; i < histo_.size(); i++) {
206  ss << histo_[i];
207  if (i < histo_.size() - 1)
208  ss << ",";
209  }
210  ss << "]";
211  return ss.str();
212  }
213  virtual Json::Value toJsonValue() const { //TODO
214  Json::Value jsonValue(Json::arrayValue);
215  for (unsigned int i = 0; i < histo_.size(); i++) {
216  jsonValue.append(histo_[i]);
217  }
218  return jsonValue;
219  }
220  void resetValue() override {
221  histo_.clear();
222  histo_.reserve(expectedSize_);
223  updates_ = 0;
224  }
225  void operator=(std::vector<T> const& sth) { histo_ = sth; }
226 
227  std::vector<T>& value() { return histo_; }
228  std::vector<T> const& value() const { return histo_; }
229 
230  unsigned int getExpectedSize() const { return expectedSize_; }
231 
232  unsigned int getMaxUpdates() const { return maxUpdates_; }
233 
234  void setMaxUpdates(unsigned int maxUpdates) {
235  maxUpdates_ = maxUpdates;
236  if (!maxUpdates_)
237  return;
240  //truncate what is over the limit
241  if (maxUpdates_ && histo_.size() > maxUpdates_) {
242  histo_.resize(maxUpdates_);
243  } else
244  histo_.reserve(expectedSize_);
245  }
246 
247  unsigned int getSize() const { return histo_.size(); }
248 
249  void update(T val) {
250  if (maxUpdates_ && updates_ >= maxUpdates_)
251  return;
252  histo_.push_back(val);
253  updates_++;
254  }
255 
256  private:
257  std::vector<T> histo_;
258  unsigned int expectedSize_;
259  unsigned int maxUpdates_;
260  };
261 
262 } // namespace jsoncollector
263 
264 #endif
void setMaxUpdates(unsigned int maxUpdates)
std::string toString() const override
std::string toCSV() const
void operator=(std::string sth)
std::string toString() const override
void resetValue() override
std::string const & value() const
std::string toString() const override
JsonMonitorable * ptr_
void operator=(long sth)
long value() const
Value & append(const Value &value)
Append value to array at the end.
void update(std::string const &newStr)
unsigned int getExpectedSize() const
Represents a JSON value.
Definition: value.h:99
void operator=(std::vector< T > const &sth)
virtual Json::Value toJsonValue() const
std::vector< T > & value()
std::string toString() const override
void concatenate(std::string const &added)
void add(long sth)
virtual void setName(std::string name)
unsigned int expectedSize_
virtual std::string const & getName() const
unsigned int getSize() const
void resetValue() override
void resetValue() override
std::vector< T > const & value() const
JsonMonPtr(JsonMonitorable *ptr)
void operator=(double sth)
virtual std::string toString() const =0
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() const
void update(double sth)
array value (ordered list)
Definition: value.h:30
void operator=(JsonMonitorable *ptr)