hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEResourceUsage.cpp
Go to the documentation of this file.
1 #include <sstream>
2 #include <iterator>
3 #include <algorithm>
4 #include <Poco/JSON/Object.h>
5 #include <Poco/JSON/Array.h>
6 #include <Poco/JSON/JSON.h>
7 #include <Poco/JSON/Stringifier.h>
8 #include <Poco/JSON/Parser.h>
9 #include <Poco/JSON/JSONException.h>
10 #include <Poco/Dynamic/Var.h>
11 
12 #include "DRCEResourceUsage.hpp"
13 #include "DRCEJsonMessageConst.hpp"
14 #include "DRCEMessageConst.hpp"
15 #include "DRCEError.hpp"
16 
17 namespace HCE
18 {
19 namespace drce
20 {
21 //-----------------------------------------------------------------------------
23 : cpu(0.0), iowait(0.0), vramUsed(0), vramUsedPercent(0.0), rramUsed(0), rramUsedPercent(0.0),
24  diskUsed(0), diskUsedPercent(0.0), processes(0), processesPercent(0.0), threads(0), threadsPercent(0.0), loadAverage(0.0)
25 {
26 }
27 //-----------------------------------------------------------------------------
29 : cpu(0.0), iowait(0.0), vramUsed(0), vramUsedPercent(0.0), rramUsed(0), rramUsedPercent(0.0),
30  diskUsed(0), diskUsedPercent(0.0), processes(0), processesPercent(0.0), threads(0), threadsPercent(0.0), loadAverage(0.0)
31 {
32  (*this) = rhs;
33 }
34 //-----------------------------------------------------------------------------
36 : cpu(0.0), iowait(0.0), vramUsed(0), vramUsedPercent(0.0), rramUsed(0), rramUsedPercent(0.0),
37  diskUsed(0), diskUsedPercent(0.0), processes(0), processesPercent(0.0), threads(0), threadsPercent(0.0), loadAverage(0.0)
38 {
39  (*this) = std::forward<ResourceUsage>(rhs);
40 }
41 //-----------------------------------------------------------------------------
43 {
44  if (this != &rhs)
45  {
46  cpu = rhs.cpu;
47  iowait = rhs.iowait;
48  vramUsed = rhs.vramUsed;
50  rramUsed = rhs.rramUsed;
52  diskUsed = rhs.diskUsed;
54  processes = rhs.processes;
56  threads = rhs.threads;
59  }
60  return *this;
61 }
62 //-----------------------------------------------------------------------------
64 {
65  if (this != &rhs)
66  {
67  cpu = std::move(rhs.cpu);
68  iowait = std::move(rhs.iowait);
69  vramUsed = std::move(rhs.vramUsed);
70  vramUsedPercent = std::move(rhs.vramUsedPercent);
71  rramUsed = std::move(rhs.rramUsed);
72  rramUsedPercent = std::move(rhs.rramUsedPercent);
73  diskUsed = std::move(rhs.diskUsed);
74  diskUsedPercent = std::move(rhs.diskUsedPercent);
75  processes = std::move(rhs.processes);
76  processesPercent = std::move(rhs.processesPercent);
77  threads = std::move(rhs.threads);
78  threadsPercent = std::move(rhs.threadsPercent);
79  loadAverage = std::move(rhs.loadAverage);
80  }
81  return *this;
82 }
83 //-----------------------------------------------------------------------------
85 {
86  cpu = 0.0;
87  iowait = 0.0;
88  vramUsed = 0;
89  vramUsedPercent = 0.0;
90  rramUsed = 0;
91  rramUsedPercent = 0.0;
92  diskUsed = 0;
93  diskUsedPercent = 0.0;
94  processes = 0;
95  processesPercent = 0.0;
96  threads = 0;
97  threadsPercent = 0.0;
98  loadAverage = 0.0;
99 }
100 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 : resourceUsage()
104 {
105 }
106 //-----------------------------------------------------------------------------
108 : resourceUsage(rhs)
109 {
110 }
111 //-----------------------------------------------------------------------------
113 : resourceUsage()
114 {
115  fromString(rhs);
116 }
117 //-----------------------------------------------------------------------------
119 {
120  std::ostringstream ostr;
121  ostr << resourceUsage.cpu << delimiter
122  << resourceUsage.iowait << delimiter
123  << resourceUsage.vramUsedPercent << delimiter
124  << resourceUsage.rramUsedPercent << delimiter
125  << resourceUsage.diskUsedPercent << delimiter
126  << resourceUsage.processesPercent << delimiter
127  << resourceUsage.threadsPercent << delimiter
128  << resourceUsage.loadAverage;
129  return ostr.str();
130 }
131 //-----------------------------------------------------------------------------
133 {
134  resourceUsage = rhs;
135 }
136 //-----------------------------------------------------------------------------
137 void ResourceUsageConverter::fromString(const std::string& rhs, const char delimiter)
138 {
139  resourceUsage.clear();
140  std::string str(rhs);
141  std::replace(str.begin(), str.end(), delimiter, ' ');
142  std::stringstream istr(str);
143  std::istream_iterator<std::string> iit(istr);
144  std::istream_iterator<std::string> eos;
145 
146  if (iit != eos)
147  std::istringstream(*iit) >> resourceUsage.cpu;
148 
149  if (++iit != eos)
150  std::istringstream(*iit) >> resourceUsage.iowait;
151 
152  if (++iit != eos)
153  std::istringstream(*iit) >> resourceUsage.vramUsedPercent;
154 
155  if (++iit != eos)
156  std::istringstream(*iit) >> resourceUsage.rramUsedPercent;
157 
158  if (++iit != eos)
159  std::istringstream(*iit) >> resourceUsage.diskUsedPercent;
160 
161  if (++iit != eos)
162  std::istringstream(*iit) >> resourceUsage.processesPercent;
163 
164  if (++iit != eos)
165  std::istringstream(*iit) >> resourceUsage.threadsPercent;
166 
167  if (++iit != eos)
168  std::istringstream(*iit) >> resourceUsage.loadAverage;
169 }
170 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
173 : inherited(), resourceUsage(resourceUsage_)
174 {
175 }
176 //-----------------------------------------------------------------------------
178 : inherited(), resourceUsage(resourceUsage_)
179 {
180  if (!json.empty())
181  unserialize(json);
182 }
183 //-----------------------------------------------------------------------------
185 {
186  resetError();
187  try
188  {
189  Poco::JSON::Object::Ptr pObj = new Poco::JSON::Object();
190  if (pObj.isNull())
191  throw Poco::Exception(message_const::objectInstanceWasNotCreated);
192 
194  pObj->set(HCE::drce::drce_json_message_const::CPU, trim(resourceUsage.cpu));
195  pObj->set(HCE::drce::drce_json_message_const::IOWAIT, trim(resourceUsage.iowait));
196  pObj->set(HCE::drce::drce_json_message_const::VRAM, trim(resourceUsage.vramUsedPercent));
197  pObj->set(HCE::drce::drce_json_message_const::RRAM, trim(resourceUsage.rramUsedPercent));
198  pObj->set(HCE::drce::drce_json_message_const::DISK, trim(resourceUsage.diskUsedPercent));
199  pObj->set(HCE::drce::drce_json_message_const::PROCESSES, trim(resourceUsage.processesPercent));
200  pObj->set(HCE::drce::drce_json_message_const::THREADS, trim(resourceUsage.threadsPercent));
201  pObj->set(HCE::drce::drce_json_message_const::LOADAVG, trim(resourceUsage.loadAverage));
202 
203  std::stringstream ostr;
204  Poco::JSON::Stringifier::stringify(pObj, ostr);
205  json = ostr.str();
206  }
207  catch(Poco::Exception& e)
208  {
209  _isError = true;
210  errorMsg = e.message();
211  errorCode = e.code();
212  }
213  catch(std::exception& e)
214  {
215  _isError = true;
216  errorMsg = e.what();
218  }
219  return !_isError;
220 }
221 //-----------------------------------------------------------------------------
222 bool ResourceUsageSerializator::unserialize(const std::string& json)
223 {
224  clear();
225  resetError();
226  try
227  {
228  Poco::JSON::Parser parser;
229  Poco::Dynamic::Var res = parser.parse(json);
230  Poco::JSON::Object::Ptr pObj = res.extract<Poco::JSON::Object::Ptr>();
231  if (!pObj.isNull())
232  {
233  Poco::Dynamic::Var tmp = pObj->get(HCE::drce::drce_json_message_const::CPU);
234  resourceUsage.cpu = convertVarToNumeric<double>(tmp, 0.0);
235 
237  resourceUsage.iowait = convertVarToNumeric<double>(tmp, 0.0);
238 
240  resourceUsage.vramUsedPercent = convertVarToNumeric<double>(tmp, 0.0);
241 
243  resourceUsage.rramUsedPercent = convertVarToNumeric<double>(tmp, 0.0);
244 
246  resourceUsage.diskUsedPercent = convertVarToNumeric<double>(tmp, 0.0);
247 
249  resourceUsage.processesPercent = convertVarToNumeric<double>(tmp, 0.0);
250 
252  resourceUsage.threadsPercent = convertVarToNumeric<double>(tmp, 0.0);
253 
255  resourceUsage.loadAverage = convertVarToNumeric<double>(tmp, 0.0);
256  }
257  }
258  catch(Poco::JSON::JSONException& e)
259  {
260  _isError = true;
261  errorMsg = e.message();
263  }
264  catch(Poco::Exception& e)
265  {
266  _isError = true;
267  errorMsg = e.message();
268  errorCode = e.code();
269  }
270  catch(std::exception& e)
271  {
272  _isError = true;
273  errorMsg = e.what();
275  }
276  return !_isError;
277 }
278 //-----------------------------------------------------------------------------
280 {
281  resourceUsage.clear();
282 }
283 //-----------------------------------------------------------------------------
284 //-----------------------------------------------------------------------------
285 }// end namespace drce
286 } // end namespace HCE