artdaq_utilities  v1_04_10
report_metric.cc
1 // report_metric.cc: Periodic Report Metric Plugin
2 // Author: Eric Flumerfelt
3 // Last Modified: 11/06/2014
4 //
5 // An implementation of the MetricPlugin for creating "Periodic Report" messages
6 
7 #include "artdaq-utilities/Plugins/MetricMacros.hh"
8 #include "fhiclcpp/ParameterSet.h"
9 
10 #include <fstream>
11 #include <ctime>
12 #include <string>
13 #include <sstream>
14 #include <mutex>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include "tracemf.h"
18 
19 namespace artdaq
20 {
25  {
26  private:
27  std::chrono::steady_clock::time_point last_report_time_;
28 
29  std::map<std::string, std::string> metrics_;
30 
31  std::mutex report_mutex_;
32  public:
42  explicit PeriodicReportMetric(fhicl::ParameterSet const& config, std::string const& app_name) : MetricPlugin(config, app_name)
43  , last_report_time_(std::chrono::steady_clock::now())
44  ,metrics_()
45  {
46  startMetrics();
47  }
48 
53  {
54  stopMetrics();
55  }
56 
61  std::string getLibName() const override { return "report"; }
62 
69  void sendMetric_(const std::string& name, const std::string& value, const std::string& unit) override
70  {
71  if (!inhibit_)
72  {
73  metrics_[name] = value + " " + unit;
74  writeReportMessage_(false);
75  }
76  }
77 
84  void sendMetric_(const std::string& name, const int& value, const std::string& unit) override
85  {
86  sendMetric_(name, std::to_string(value), unit);
87  }
88 
95  void sendMetric_(const std::string& name, const double& value, const std::string& unit) override
96  {
97  sendMetric_(name, std::to_string(value), unit);
98  }
99 
106  void sendMetric_(const std::string& name, const float& value, const std::string& unit) override
107  {
108  sendMetric_(name, std::to_string(value), unit);
109  }
110 
117  void sendMetric_(const std::string& name, const unsigned long int& value, const std::string& unit) override
118  {
119  sendMetric_(name, std::to_string(value), unit);
120  }
121 
125  void startMetrics_() override
126  {
127  }
128 
132  void stopMetrics_() override
133  {
134  writeReportMessage_(true);
135  metrics_.clear();
136  }
137 
138  private:
139  void writeReportMessage_(bool force)
140  {
141  std::unique_lock<std::mutex> lk(report_mutex_);
142  if(force || std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>(std::chrono::steady_clock::now() - last_report_time_).count() >= accumulationTime_)
143  {
144  if(metrics_.size() == 0) return;
145  last_report_time_ = std::chrono::steady_clock::now();
146  std::ostringstream str;
147 
148  int count = 0;
149  int live_metrics = 0;
150  for(auto& metric : metrics_)
151  {
152  if(count != 0) str << "," << std::endl;
153  str << "\t" << metric.first << ": " << metric.second;
154  if(metric.second != "NOT REPORTED") live_metrics++;
155  metric.second = "NOT REPORTED";
156  count++;
157  }
158  if(live_metrics > 0) TLOG_INFO(app_name_) << "Periodic report: " << live_metrics << " active metrics:" << std::endl << str.str();
159  else TLOG_INFO(app_name_) << "Periodic report: No active metrics in last reporting interval!";
160  }
161  }
162  };
163 } //End namespace artdaq
164 
165 DEFINE_ARTDAQ_METRIC(artdaq::PeriodicReportMetric)
void sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Write metric data to a file.
virtual ~PeriodicReportMetric()
PeriodicReportMetric Destructor. Calls stopMetrics and then closes the file.
PeriodicReportMetric(fhicl::ParameterSet const &config, std::string const &app_name)
PeriodicReportMetric Constructor.
std::string app_name_
Name of the application which is sending metrics to this plugin.
std::string getLibName() const override
Get the library name for the PeriodicReport metric.
void sendMetric_(const std::string &name, const int &value, const std::string &unit) override
Write metric data to a file.
void sendMetric_(const std::string &name, const float &value, const std::string &unit) override
Write metric data to a file.
PeriodicReportMetric writes metric data to a file on disk.
void startMetrics_() override
Perform startup actions.
void stopMetrics_() override
Perform shutdown actions.
void sendMetric_(const std::string &name, const unsigned long int &value, const std::string &unit) override
Write metric data to a file.
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit) override
Write metric data to a file.
bool inhibit_
Whether to inhibit all metric sending.