DoubleJ

HistoJ

IntJ

JsonMonPtr

JsonMonitorable

MonType

OperationType

StringJ

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/*
 * JsonMonitorable.h
 *
 *  Created on: Oct 29, 2012
 *      Author: aspataru
 */

#ifndef JSON_MONITORABLE_H
#define JSON_MONITORABLE_H

#include <string>
#include <sstream>
#include <vector>
#include <memory>
//#include "EventFilter/Utilities/interface/Utils.h"
#include <iostream>
#include "json.h"

namespace jsoncollector {

  enum MonType { TYPEINT, TYPEUINT, TYPEDOUBLE, TYPESTRING, TYPEUNDEFINED };
  enum OperationType { OPSUM, OPAVG, OPSAME, OPHISTO, OPCAT, OPUNKNOWN };

  class JsonMonitorable {
  public:
    JsonMonitorable() : updates_(0), notSame_(false) {}

    virtual ~JsonMonitorable() {}

    virtual std::string toString() const = 0;

    virtual void resetValue() = 0;

    unsigned int getUpdates() { return updates_; }

    bool getNotSame() const { return notSame_; }

    virtual void setName(std::string name) { name_ = name; }

    virtual std::string const& getName() const { return name_; }

  protected:
    std::string name_;
    unsigned int updates_;
    bool notSame_;
  };

  class JsonMonPtr {
  public:
    JsonMonPtr() : ptr_(nullptr) {}
    JsonMonPtr(JsonMonitorable* ptr) : ptr_(ptr) {}
    void operator=(JsonMonitorable* ptr) { ptr_ = ptr; }
    ~JsonMonPtr() {
      if (ptr_)
        delete ptr_;
      ptr_ = nullptr;
    }
    JsonMonitorable* operator->() { return ptr_; }
    JsonMonitorable* get() { return ptr_; }
    //JsonMonPtr& operator=(JsonMonPtr& ) = delete;
    //JsonMonPtr& operator=(JsonMonPtr&& other){ptr_=other.ptr_;return *this;}
  private:
    JsonMonitorable* ptr_;
  };

  class IntJ : public JsonMonitorable {
  public:
    IntJ() : JsonMonitorable(), theVar_(0) {}
    IntJ(long val) : JsonMonitorable(), theVar_(val) {}

    ~IntJ() override {}

    std::string toString() const override {
      std::stringstream ss;
      ss << theVar_;
      return ss.str();
    }
    void resetValue() override {
      theVar_ = 0;
      updates_ = 0;
      notSame_ = false;
    }
    void operator=(long sth) {
      theVar_ = sth;
      updates_ = 1;
      notSame_ = false;
    }
    long& value() { return theVar_; }
    long value() const { return theVar_; }

    void update(long sth) {
      theVar_ = sth;
      if (updates_ && theVar_ != sth)
        notSame_ = true;
      updates_++;
    }

    void add(long sth) {
      theVar_ += sth;
      updates_++;
    }

  private:
    long theVar_;
  };

  class DoubleJ : public JsonMonitorable {
  public:
    DoubleJ() : JsonMonitorable(), theVar_(0) {}
    DoubleJ(double val) : JsonMonitorable(), theVar_(val) {}

    ~DoubleJ() override {}

    std::string toString() const override {
      std::stringstream ss;
      ss << theVar_;
      return ss.str();
    }
    void resetValue() override {
      theVar_ = 0;
      updates_ = 0;
      notSame_ = false;
    }
    void operator=(double sth) {
      theVar_ = sth;
      updates_ = 1;
      notSame_ = false;
    }
    double& value() { return theVar_; }
    double value() const { return theVar_; }
    void update(double sth) {
      theVar_ = sth;
      if (updates_ && theVar_ != sth)
        notSame_ = true;
      updates_++;
    }

  private:
    double theVar_;
  };

  class StringJ : public JsonMonitorable {
  public:
    StringJ() : JsonMonitorable() {}
    StringJ(StringJ const& sJ) : JsonMonitorable() { theVar_ = sJ.value(); }

    ~StringJ() override {}

    std::string toString() const override { return theVar_; }
    void resetValue() override {
      theVar_ = std::string();
      updates_ = 0;
      notSame_ = false;
    }
    void operator=(std::string sth) {
      theVar_ = sth;
      updates_ = 1;
      notSame_ = false;
    }
    std::string& value() { return theVar_; }
    std::string const& value() const { return theVar_; }
    void concatenate(std::string const& added) {
      if (!updates_)
        theVar_ = added;
      else
        theVar_ += "," + added;
      updates_++;
    }
    void update(std::string const& newStr) {
      theVar_ = newStr;
      updates_ = 1;
    }

  private:
    std::string theVar_;
  };

  //histograms filled at time intervals (later converted to full histograms)
  template <class T>
  class HistoJ : public JsonMonitorable {
  public:
    HistoJ(int expectedUpdates = 1, unsigned int maxUpdates = 0) {
      expectedSize_ = expectedUpdates;
      updates_ = 0;
      maxUpdates_ = maxUpdates;
      if (maxUpdates_ && maxUpdates_ < expectedSize_)
        expectedSize_ = maxUpdates_;
      histo_.reserve(expectedSize_);
    }
    ~HistoJ() override {}

    std::string toCSV() const {
      std::stringstream ss;
      for (unsigned int i = 0; i < updates_; i++) {
        ss << histo_[i];
        if (i != histo_.size() - 1)
          ss << ",";
      }
      return ss.str();
    }

    std::string toString() const override {
      std::stringstream ss;
      ss << "[";
      if (!histo_.empty())
        for (unsigned int i = 0; i < histo_.size(); i++) {
          ss << histo_[i];
          if (i < histo_.size() - 1)
            ss << ",";
        }
      ss << "]";
      return ss.str();
    }
    virtual Json::Value toJsonValue() const {  //TODO
      Json::Value jsonValue(Json::arrayValue);
      for (unsigned int i = 0; i < histo_.size(); i++) {
        jsonValue.append(histo_[i]);
      }
      return jsonValue;
    }
    void resetValue() override {
      histo_.clear();
      histo_.reserve(expectedSize_);
      updates_ = 0;
    }
    void operator=(std::vector<T> const& sth) { histo_ = sth; }

    std::vector<T>& value() { return histo_; }
    std::vector<T> const& value() const { return histo_; }

    unsigned int getExpectedSize() const { return expectedSize_; }

    unsigned int getMaxUpdates() const { return maxUpdates_; }

    void setMaxUpdates(unsigned int maxUpdates) {
      maxUpdates_ = maxUpdates;
      if (!maxUpdates_)
        return;
      if (expectedSize_ > maxUpdates_)
        expectedSize_ = maxUpdates_;
      //truncate what is over the limit
      if (maxUpdates_ && histo_.size() > maxUpdates_) {
        histo_.resize(maxUpdates_);
      } else
        histo_.reserve(expectedSize_);
    }

    unsigned int getSize() const { return histo_.size(); }

    void update(T val) {
      if (maxUpdates_ && updates_ >= maxUpdates_)
        return;
      histo_.push_back(val);
      updates_++;
    }

  private:
    std::vector<T> histo_;
    unsigned int expectedSize_;
    unsigned int maxUpdates_;
  };

}  // namespace jsoncollector

#endif