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:
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:
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:
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;
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;
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
jsoncollector::OPSUM
Definition: JsonMonitorable.h:22
jsoncollector::TYPEDOUBLE
Definition: JsonMonitorable.h:21
jsoncollector::HistoJ::getExpectedSize
unsigned int getExpectedSize()
Definition: JsonMonitorable.h:226
jsoncollector::TYPEUNDEFINED
Definition: JsonMonitorable.h:21
jsoncollector::StringJ::operator=
void operator=(std::string sth)
Definition: JsonMonitorable.h:152
mps_fire.i
i
Definition: mps_fire.py:428
jsoncollector::MonType
MonType
Definition: JsonMonitorable.h:21
funct::false
false
Definition: Factorize.h:29
jsoncollector::HistoJ::expectedSize_
unsigned int expectedSize_
Definition: JsonMonitorable.h:254
jsoncollector::HistoJ::value
std::vector< T > & value()
Definition: JsonMonitorable.h:224
jsoncollector::HistoJ::resetValue
void resetValue() override
Definition: JsonMonitorable.h:217
Json::arrayValue
array value (ordered list)
Definition: value.h:30
jsoncollector::IntJ::IntJ
IntJ()
Definition: JsonMonitorable.h:68
jsoncollector::JsonMonitorable::updates_
unsigned int updates_
Definition: JsonMonitorable.h:44
jsoncollector::HistoJ::HistoJ
HistoJ(int expectedUpdates=1, unsigned int maxUpdates=0)
Definition: JsonMonitorable.h:178
jsoncollector::HistoJ
Definition: JsonMonitorable.h:176
jsoncollector::OperationType
OperationType
Definition: JsonMonitorable.h:22
jsoncollector::JsonMonPtr::operator=
void operator=(JsonMonitorable *ptr)
Definition: JsonMonitorable.h:52
jsoncollector::HistoJ::toCSV
std::string toCSV() const
Definition: JsonMonitorable.h:188
jsoncollector::OPUNKNOWN
Definition: JsonMonitorable.h:22
jsoncollector::IntJ::operator=
void operator=(long sth)
Definition: JsonMonitorable.h:83
jsoncollector::IntJ::update
void update(long sth)
Definition: JsonMonitorable.h:90
jsoncollector::DoubleJ::DoubleJ
DoubleJ()
Definition: JsonMonitorable.h:108
jsoncollector::OPSAME
Definition: JsonMonitorable.h:22
jsoncollector::JsonMonitorable::resetValue
virtual void resetValue()=0
jsoncollector::JsonMonPtr::JsonMonPtr
JsonMonPtr(JsonMonitorable *ptr)
Definition: JsonMonitorable.h:51
jsoncollector::JsonMonitorable::getName
virtual std::string & getName()
Definition: JsonMonitorable.h:40
jsoncollector::JsonMonPtr
Definition: JsonMonitorable.h:48
jsoncollector::HistoJ::getMaxUpdates
unsigned int getMaxUpdates()
Definition: JsonMonitorable.h:228
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
jsoncollector::JsonMonitorable::getUpdates
unsigned int getUpdates()
Definition: JsonMonitorable.h:34
jsoncollector::JsonMonitorable::setName
virtual void setName(std::string name)
Definition: JsonMonitorable.h:38
jsoncollector::HistoJ::setMaxUpdates
void setMaxUpdates(unsigned int maxUpdates)
Definition: JsonMonitorable.h:230
jsoncollector::TYPESTRING
Definition: JsonMonitorable.h:21
jsoncollector::DoubleJ::DoubleJ
DoubleJ(double val)
Definition: JsonMonitorable.h:109
jsoncollector::HistoJ::toJsonValue
virtual Json::Value toJsonValue() const
Definition: JsonMonitorable.h:210
jsoncollector::DoubleJ
Definition: JsonMonitorable.h:106
jsoncollector::DoubleJ::toString
std::string toString() const override
Definition: JsonMonitorable.h:113
jsoncollector::IntJ::resetValue
void resetValue() override
Definition: JsonMonitorable.h:78
jsoncollector::DoubleJ::resetValue
void resetValue() override
Definition: JsonMonitorable.h:118
jsoncollector::TYPEUINT
Definition: JsonMonitorable.h:21
jsoncollector::StringJ::~StringJ
~StringJ() override
Definition: JsonMonitorable.h:144
jsoncollector::IntJ::IntJ
IntJ(long val)
Definition: JsonMonitorable.h:69
jsoncollector::StringJ::toString
std::string toString() const override
Definition: JsonMonitorable.h:146
jsoncollector::HistoJ::~HistoJ
~HistoJ() override
Definition: JsonMonitorable.h:186
jsoncollector::TYPEINT
Definition: JsonMonitorable.h:21
jsoncollector::OPCAT
Definition: JsonMonitorable.h:22
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
jsoncollector::HistoJ::update
void update(T val)
Definition: JsonMonitorable.h:245
jsoncollector::HistoJ::toString
std::string toString() const override
Definition: JsonMonitorable.h:198
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
jsoncollector::JsonMonitorable::notSame_
bool notSame_
Definition: JsonMonitorable.h:45
jsoncollector::JsonMonitorable::JsonMonitorable
JsonMonitorable()
Definition: JsonMonitorable.h:26
jsoncollector::JsonMonitorable::~JsonMonitorable
virtual ~JsonMonitorable()
Definition: JsonMonitorable.h:28
jsoncollector::DoubleJ::value
double & value()
Definition: JsonMonitorable.h:128
jsoncollector::JsonMonPtr::JsonMonPtr
JsonMonPtr()
Definition: JsonMonitorable.h:50
jsoncollector::DoubleJ::~DoubleJ
~DoubleJ() override
Definition: JsonMonitorable.h:111
jsoncollector::StringJ::concatenate
void concatenate(std::string const &added)
Definition: JsonMonitorable.h:158
jsoncollector::OPAVG
Definition: JsonMonitorable.h:22
jsoncollector::JsonMonitorable
Definition: JsonMonitorable.h:24
jsoncollector::IntJ::value
long & value()
Definition: JsonMonitorable.h:88
jsoncollector
Definition: DataPoint.h:26
jsoncollector::IntJ::toString
std::string toString() const override
Definition: JsonMonitorable.h:73
jsoncollector::JsonMonPtr::~JsonMonPtr
~JsonMonPtr()
Definition: JsonMonitorable.h:53
jsoncollector::StringJ::update
void update(std::string const &newStr)
Definition: JsonMonitorable.h:165
jsoncollector::JsonMonPtr::get
JsonMonitorable * get()
Definition: JsonMonitorable.h:59
heppy_batch.val
val
Definition: heppy_batch.py:351
jsoncollector::JsonMonPtr::ptr_
JsonMonitorable * ptr_
Definition: JsonMonitorable.h:63
jsoncollector::IntJ::~IntJ
~IntJ() override
Definition: JsonMonitorable.h:71
jsoncollector::JsonMonitorable::toString
virtual std::string toString() const =0
jsoncollector::IntJ::theVar_
long theVar_
Definition: JsonMonitorable.h:103
jsoncollector::JsonMonitorable::getNotSame
bool getNotSame()
Definition: JsonMonitorable.h:36
T
long double T
Definition: Basic3DVectorLD.h:48
jsoncollector::StringJ::theVar_
std::string theVar_
Definition: JsonMonitorable.h:171
json.h
jsoncollector::DoubleJ::update
void update(double sth)
Definition: JsonMonitorable.h:129
jsoncollector::IntJ::add
void add(long sth)
Definition: JsonMonitorable.h:97
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
jsoncollector::StringJ::value
std::string & value()
Definition: JsonMonitorable.h:157
jsoncollector::IntJ
Definition: JsonMonitorable.h:66
jsoncollector::HistoJ::maxUpdates_
unsigned int maxUpdates_
Definition: JsonMonitorable.h:255
jsoncollector::HistoJ::operator=
void operator=(std::vector< T > &sth)
Definition: JsonMonitorable.h:222
jsoncollector::DoubleJ::theVar_
double theVar_
Definition: JsonMonitorable.h:137
jsoncollector::OPHISTO
Definition: JsonMonitorable.h:22
jsoncollector::JsonMonitorable::name_
std::string name_
Definition: JsonMonitorable.h:43
jsoncollector::HistoJ::histo_
std::vector< T > histo_
Definition: JsonMonitorable.h:253
jsoncollector::DoubleJ::operator=
void operator=(double sth)
Definition: JsonMonitorable.h:123
jsoncollector::StringJ::StringJ
StringJ()
Definition: JsonMonitorable.h:142
jsoncollector::StringJ::resetValue
void resetValue() override
Definition: JsonMonitorable.h:147
jsoncollector::HistoJ::getSize
unsigned int getSize()
Definition: JsonMonitorable.h:243
jsoncollector::StringJ
Definition: JsonMonitorable.h:140
Json::Value
Represents a JSON value.
Definition: value.h:99
jsoncollector::JsonMonPtr::operator->
JsonMonitorable * operator->()
Definition: JsonMonitorable.h:58