hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CalculatorNodeExecution.cpp
Go to the documentation of this file.
1 #include <sstream>
2 #include <iterator>
3 #include <algorithm>
4 
5 #include <Poco/Timestamp.h>
6 #include <Poco/DateTimeFormatter.h>
7 
9 
10 namespace HCE
11 {
12 namespace handlers
13 {
14 //-----------------------------------------------------------------------------
16 : inherited(), order(order_)
17 {
18 }
19 //-----------------------------------------------------------------------------
21 : inherited(), order(0)
22 {
23  (*this) = rhs;
24 }
25 //-----------------------------------------------------------------------------
27 : inherited(), order(0)
28 {
29  (*this) = std::forward<CalculatorAlgorithmData>(rhs);
30 }
31 //-----------------------------------------------------------------------------
33 {
34  if (this!=&rhs)
35  {
36  order = rhs.getOrder();
37  items = rhs.getItems();
38  }
39  return *this;
40 }
41 //-----------------------------------------------------------------------------
43 {
44  if (this!=&rhs)
45  {
46  order = std::move(rhs.getOrder());
47  items = std::move(rhs.getItems());
48  }
49  return *this;
50 }
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 : inherited(), identity(identity_)
55 {
56 }
57 //-----------------------------------------------------------------------------
59 : inherited(), identity("")
60 {
61  (*this) = rhs;
62 }
63 //-----------------------------------------------------------------------------
65 : inherited(), identity("")
66 {
67  (*this) = std::forward<CalculatorNodeResourceData>(rhs);
68 }
69 //-----------------------------------------------------------------------------
71 {
72  if (this!=&rhs)
73  {
74  identity = rhs.getIdentity();
75  items = rhs.getItems();
76  }
77  return *this;
78 }
79 //-----------------------------------------------------------------------------
81 {
82  if (this!=&rhs)
83  {
84  identity = std::move(rhs.getIdentity());
85  items = std::move(rhs.getItems());
86  }
87  return *this;
88 }
89 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
92 :inherited()
93 {
94 }
95 //-----------------------------------------------------------------------------
96 std::string CalculatorAlgorithmDefault::calculate(const CalculatorAlgorithmData& algorithmData, NodeResourceDataList& nodeDataList, ResourceUsageManager& resourceUsageManager)
97 {
98  std::string identity;
99  std::vector<std::pair<std::string, double> > weights; // pair content: first is identity, second is accumulated weigth for this adentity
100 
101  for (size_t i=0;i<nodeDataList.getItemsCount();++i)
102  {
103  double weight = 0.0;
104  for (CalculatorAlgorithmData::ConstIterator iter=algorithmData.begin();iter!=algorithmData.end();++iter)
105  {
106  weight += (const_cast<CalculatorNodeResourceData&>(nodeDataList.getItem(i)).getItem((*iter).first) * (*iter).second);
107  }
108  weights.push_back(std::make_pair(nodeDataList.getItem(i).getIdentity(), weight));
109 
111  resourceUsageManager.setLastWeight(nodeDataList.getItem(i).getIdentity(), weight);
112  }
113 
114  std::function<bool(std::pair<std::string, double>, std::pair<std::string, double>)> comp;
115  if (algorithmData.getOrder())
116  comp = [](std::pair<std::string, double> lhs, std::pair<std::string, double> rhs){ return (lhs.second>rhs.second);};
117  else
118  comp = [](std::pair<std::string, double> lhs, std::pair<std::string, double> rhs){ return (lhs.second<rhs.second);};
119 
120  std::sort(weights.begin(), weights.end(), comp);
121 
122  if (!weights.empty())
123  {
124  if (weights.front().second != weights.back().second)
125  {
126  identity = weights[0].first;
127  }
128  }
129  return identity;
130 }
131 //-----------------------------------------------------------------------------
133 {
134  std::vector<double> listWeights;
135  for (ResourceUsageCollection::iterator iT=collection.begin();iT!=collection.end();++iT)
136  {
137  double weight = 0.0;
138  for (CalculatorAlgorithmData::ConstIterator iter=algorithmData.begin();iter!=algorithmData.end();++iter)
139  {
140  weight += ((*iT).getItem((*iter).first) * (*iter).second);
141  }
142  listWeights.push_back(weight);
143  }
144  return listWeights;
145 }
146 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
149 : algorithm(nullptr)
150 {
151  algorithm = createAlgorithm(type);
152 }
153 //-----------------------------------------------------------------------------
154 std::string CalculatorNodeExecution::calculate(const CalculatorAlgorithmData& algorithmData, NodeResourceDataList& nodeDataList, ResourceUsageManager& resourceUsageManager)
155 {
156  std::string identity;
157  if (!algorithm.isNull())
158  {
159  identity = algorithm->calculate(algorithmData, nodeDataList, resourceUsageManager);
160  }
161  return identity;
162 }
163 //-----------------------------------------------------------------------------
165 {
166  std::vector<double> listWeights;
167  if (!algorithm.isNull())
168  {
169  listWeights = algorithm->getListWeights(algorithmData, collection);
170  }
171  return listWeights;
172 }
173 //-----------------------------------------------------------------------------
174 Poco::SharedPtr<CalculatorAlgorithm> CalculatorNodeExecution::createAlgorithm(CalculatorAlgorithm::Types type)
175 {
176  Poco::SharedPtr<CalculatorAlgorithm> pAlg = nullptr;
177  switch(type)
178  {
179  case CalculatorAlgorithm::Types::caDefault: pAlg = new CalculatorAlgorithmDefault();
180  break;
181  //TODO add next algorithm types
182  default: pAlg = new CalculatorAlgorithmDefault();
183  }
184  return pAlg;
185 }
186 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
188 } // end namespace handlers
189 } // end namespace HCE