Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-26 00:19:10

0001 /*
0002  * JsonMonitorable.h
0003  *
0004  *  Created on: Oct 29, 2012
0005  *      Author: aspataru
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 //#include "EventFilter/Utilities/interface/Utils.h"
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     //JsonMonPtr& operator=(JsonMonPtr& ) = delete;
0061     //JsonMonPtr& operator=(JsonMonPtr&& other){ptr_=other.ptr_;return *this;}
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     StringJ(StringJ const& sJ) : JsonMonitorable() { theVar_ = sJ.value(); }
0146 
0147     ~StringJ() override {}
0148 
0149     std::string toString() const override { return theVar_; }
0150     void resetValue() override {
0151       theVar_ = std::string();
0152       updates_ = 0;
0153       notSame_ = false;
0154     }
0155     void operator=(std::string sth) {
0156       theVar_ = sth;
0157       updates_ = 1;
0158       notSame_ = false;
0159     }
0160     std::string& value() { return theVar_; }
0161     std::string const& value() const { return theVar_; }
0162     void concatenate(std::string const& added) {
0163       if (!updates_)
0164         theVar_ = added;
0165       else
0166         theVar_ += "," + added;
0167       updates_++;
0168     }
0169     void update(std::string const& newStr) {
0170       theVar_ = newStr;
0171       updates_ = 1;
0172     }
0173 
0174   private:
0175     std::string theVar_;
0176   };
0177 
0178   //histograms filled at time intervals (later converted to full histograms)
0179   template <class T>
0180   class HistoJ : public JsonMonitorable {
0181   public:
0182     HistoJ(int expectedUpdates = 1, unsigned int maxUpdates = 0) {
0183       expectedSize_ = expectedUpdates;
0184       updates_ = 0;
0185       maxUpdates_ = maxUpdates;
0186       if (maxUpdates_ && maxUpdates_ < expectedSize_)
0187         expectedSize_ = maxUpdates_;
0188       histo_.reserve(expectedSize_);
0189     }
0190     ~HistoJ() override {}
0191 
0192     std::string toCSV() const {
0193       std::stringstream ss;
0194       for (unsigned int i = 0; i < updates_; i++) {
0195         ss << histo_[i];
0196         if (i != histo_.size() - 1)
0197           ss << ",";
0198       }
0199       return ss.str();
0200     }
0201 
0202     std::string toString() const override {
0203       std::stringstream ss;
0204       ss << "[";
0205       if (!histo_.empty())
0206         for (unsigned int i = 0; i < histo_.size(); i++) {
0207           ss << histo_[i];
0208           if (i < histo_.size() - 1)
0209             ss << ",";
0210         }
0211       ss << "]";
0212       return ss.str();
0213     }
0214     virtual Json::Value toJsonValue() const {  //TODO
0215       Json::Value jsonValue(Json::arrayValue);
0216       for (unsigned int i = 0; i < histo_.size(); i++) {
0217         jsonValue.append(histo_[i]);
0218       }
0219       return jsonValue;
0220     }
0221     void resetValue() override {
0222       histo_.clear();
0223       histo_.reserve(expectedSize_);
0224       updates_ = 0;
0225     }
0226     void operator=(std::vector<T> const& sth) { histo_ = sth; }
0227 
0228     std::vector<T>& value() { return histo_; }
0229     std::vector<T> const& value() const { return histo_; }
0230 
0231     unsigned int getExpectedSize() const { return expectedSize_; }
0232 
0233     unsigned int getMaxUpdates() const { return maxUpdates_; }
0234 
0235     void setMaxUpdates(unsigned int maxUpdates) {
0236       maxUpdates_ = maxUpdates;
0237       if (!maxUpdates_)
0238         return;
0239       if (expectedSize_ > maxUpdates_)
0240         expectedSize_ = maxUpdates_;
0241       //truncate what is over the limit
0242       if (maxUpdates_ && histo_.size() > maxUpdates_) {
0243         histo_.resize(maxUpdates_);
0244       } else
0245         histo_.reserve(expectedSize_);
0246     }
0247 
0248     unsigned int getSize() const { return histo_.size(); }
0249 
0250     void update(T val) {
0251       if (maxUpdates_ && updates_ >= maxUpdates_)
0252         return;
0253       histo_.push_back(val);
0254       updates_++;
0255     }
0256 
0257   private:
0258     std::vector<T> histo_;
0259     unsigned int expectedSize_;
0260     unsigned int maxUpdates_;
0261   };
0262 
0263 }  // namespace jsoncollector
0264 
0265 #endif