CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 
26 public:
27 
29 
30  virtual ~JsonMonitorable() {}
31 
32  virtual std::string toString() const = 0;
33 
34  virtual void resetValue() = 0;
35 
36  unsigned int getUpdates() {return updates_;}
37 
38  bool getNotSame() {return notSame_;}
39 
40  virtual void setName(std::string name) {
41  name_=name;
42  }
43 
44  virtual std::string & getName() {
45  return name_;
46  }
47 
48 
49 
50 protected:
52  unsigned int updates_;
53  bool notSame_;
54 };
55 
56 class JsonMonPtr {
57 public:
60  void operator=(JsonMonitorable* ptr ){ptr_=ptr;}
61  ~JsonMonPtr() {if (ptr_) delete ptr_;ptr_=nullptr;}
63  JsonMonitorable* get() {return ptr_;}
64  //JsonMonPtr& operator=(JsonMonPtr& ) = delete;
65  //JsonMonPtr& operator=(JsonMonPtr&& other){ptr_=other.ptr_;return *this;}
66 private:
68 };
69 
70 
71 
72 
73 class IntJ: public JsonMonitorable {
74 
75 public:
77  IntJ(long val) : JsonMonitorable(), theVar_(val) {}
78 
79  virtual ~IntJ() {}
80 
81  virtual std::string toString() const {
82  std::stringstream ss;
83  ss << theVar_;
84  return ss.str();
85  }
86  virtual void resetValue() {
87  theVar_=0;
88  updates_=0;
89  notSame_=0;
90  }
91  void operator=(long sth) {
92  theVar_ = sth;
93  updates_=1;
94  notSame_=0;
95  }
96  long & value() {
97  return theVar_;
98  }
99 
100  void update(long sth) {
101  theVar_=sth;
102  if (updates_ && theVar_!=sth) notSame_=true;
103  updates_++;
104  }
105 
106  void add(long sth) {
107  theVar_+=sth;
108  updates_++;
109  }
110 
111 private:
112  long theVar_;
113 };
114 
115 
116 class DoubleJ: public JsonMonitorable {
117 
118 public:
120  DoubleJ(double val) : JsonMonitorable(), theVar_(val) {}
121 
122  virtual ~DoubleJ() {}
123 
124  virtual std::string toString() const {
125  std::stringstream ss;
126  ss << theVar_;
127  return ss.str();
128  }
129  virtual void resetValue() {
130  theVar_=0;
131  updates_=0;
132  notSame_=0;
133  }
134  void operator=(double sth) {
135  theVar_ = sth;
136  updates_=1;
137  notSame_=0;
138  }
139  double & value() {
140  return theVar_;
141  }
142  void update(double sth) {
143  theVar_=sth;
144  if (updates_ && theVar_!=sth) notSame_=true;
145  updates_++;
146  }
147 
148 private:
149  double theVar_;
150 };
151 
152 
153 class StringJ: public JsonMonitorable {
154 
155 public:
157 
158  virtual ~StringJ() {}
159 
160  virtual std::string toString() const {
161  return theVar_;
162  }
163  virtual void resetValue() {
165  updates_ = 0;
166  notSame_=0;
167  }
168  void operator=(std::string sth) {
169  theVar_ = sth;
170  updates_=1;
171  notSame_=0;
172  }
174  return theVar_;
175  }
176  void concatenate(std::string const& added) {
177  if (!updates_)
178  theVar_=added;
179  else
180  theVar_+=","+added;
181  updates_++;
182  }
183  void update(std::string const& newStr) {
184  theVar_=newStr;
185  updates_=1;
186  }
187 
188 private:
190 };
191 
192 //histograms filled at time intervals (later converted to full histograms)
193 template<class T> class HistoJ: public JsonMonitorable {
194 
195 public:
196  HistoJ( int expectedUpdates = 1 , unsigned int maxUpdates = 0 ){
197  expectedSize_=expectedUpdates;
198  updates_ = 0;
199  maxUpdates_ = maxUpdates;
201  histo_.reserve(expectedSize_);
202  }
203  virtual ~HistoJ() {}
204 
205  std::string toCSV() const {
206  std::stringstream ss;
207  for (unsigned int i=0;i<updates_;i++) {
208  ss << histo_[i];
209  if (i!=histo_.size()-1) ss<<",";
210  }
211  return ss.str();
212  }
213 
214  virtual std::string toString() const {
215  std::stringstream ss;
216  ss << "[";
217  if (histo_.size())
218  for (unsigned int i = 0; i < histo_.size(); i++) {
219  ss << histo_[i];
220  if (i<histo_.size()-1) ss << ",";
221  }
222  ss << "]";
223  return ss.str();
224  }
225  virtual Json::Value toJsonValue() const { //TODO
226  Json::Value jsonValue(Json::arrayValue);
227  for (unsigned int i = 0; i < histo_.size(); i++) {
228  jsonValue.append(histo_[i]);
229  }
230  return jsonValue;
231  }
232  virtual void resetValue() {
233  histo_.clear();
234  histo_.reserve(expectedSize_);
235  updates_=0;
236  }
237  void operator=(std::vector<T> & sth) {
238  histo_ = sth;
239  }
240 
241  std::vector<T> & value() {
242  return histo_;
243  }
244 
245  unsigned int getExpectedSize() {
246  return expectedSize_;
247  }
248 
249  unsigned int getMaxUpdates() {
250  return maxUpdates_;
251  }
252 
253  void setMaxUpdates(unsigned int maxUpdates) {
254  maxUpdates_=maxUpdates;
255  if (!maxUpdates_) return;
257  //truncate what is over the limit
258  if (maxUpdates_ && histo_.size()>maxUpdates_) {
259  histo_.resize(maxUpdates_);
260  }
261  else histo_.reserve(expectedSize_);
262  }
263 
264  unsigned int getSize() {
265  return histo_.size();
266  }
267 
268  void update(T val) {
269  if (maxUpdates_ && updates_>=maxUpdates_) return;
270  histo_.push_back(val);
271  updates_++;
272  }
273 
274 private:
275  std::vector<T> histo_;
276  unsigned int expectedSize_;
277  unsigned int maxUpdates_;
278 };
279 
280 }
281 
282 #endif
void setMaxUpdates(unsigned int maxUpdates)
std::string toCSV() const
int i
Definition: DBlmapReader.cc:9
void operator=(std::string sth)
virtual void resetValue()
virtual std::string & getName()
JsonMonitorable * ptr_
void operator=(long sth)
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:111
virtual std::string toString() const
#define nullptr
unsigned int getExpectedSize()
virtual Json::Value toJsonValue() const
void operator=(std::vector< T > &sth)
std::vector< T > & value()
void concatenate(std::string const &added)
void add(long sth)
virtual std::string toString() const
virtual void setName(std::string name)
unsigned int expectedSize_
unsigned int getSize()
virtual std::string toString() const
JsonMonPtr(JsonMonitorable *ptr)
void operator=(double sth)
virtual void resetValue()
virtual void resetValue()
virtual std::string toString() const =0
virtual std::string toString() const
HistoJ(int expectedUpdates=1, unsigned int maxUpdates=0)
void update(long sth)
std::vector< T > histo_
JsonMonitorable * operator->()
volatile std::atomic< bool > shutdown_flag false
long double T
virtual void resetValue()
unsigned int getMaxUpdates()
void update(double sth)
array value (ordered list)
Definition: value.h:31
void operator=(JsonMonitorable *ptr)