MetroCollect  2.3.4
MetricsArray.cc
Go to the documentation of this file.
1 //
2 // MetricsArray.cc
3 //
4 // Created on July 23rd 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #include <algorithm>
22 
23 #include "MetricsArray.h"
24 #include "Statistics.h"
25 
26 using namespace std::literals;
27 
28 
29 namespace MetroCollect {
30  template<typename T>
31  MetricsArray<T>::MetricsArray() noexcept : sourceInterests_(MetricsSource::makeSourceInterests()) {
32  this->setSize();
33  }
34 
35  template<typename T>
36  MetricsArray<T>::MetricsArray(MetricsSource::SourceInterests sourceInterests) noexcept : sourceInterests_(sourceInterests) {
37  this->setSize();
38  }
39 
40 
41  template<typename T>
42  MetricsArray<T>::MetricsArray(const MetricsArray& other) noexcept : sourceInterests_(other.sourceInterests_), timestamp_(other.timestamp_), data_(other.data_) {
43  this->setIterators();
44  }
45 
46  template<typename T>
48  this->sourceInterests_ = other.sourceInterests_;
49  this->timestamp_ = other.timestamp_;
50  this->data_ = other.data_;
51  this->setIterators();
52  return *this;
53  }
54 
55 
56  template<typename T>
58  return this->sourceInterests_;
59  }
60 
61  template<typename T>
63  this->sourceInterests_ = sourceInterests;
64  }
65 
66 
67  template<typename T>
68  const std::chrono::system_clock::time_point& MetricsArray<T>::timestamp() const noexcept {
69  return this->timestamp_;
70  }
71 
72 
73  template<typename T>
74  size_t MetricsArray<T>::fieldCount() const noexcept {
75  size_t size = 0;
76 
77  this->sources_.forEach([&](auto& source, size_t ) {
78  size += source.fieldCount();
79  });
80 
81  return size;
82  }
83 
84  template<typename T>
85  const std::vector<size_t> MetricsArray<T>::indexesOfFieldName(const MetricsSource::FieldName& fieldName, bool setInterest) const noexcept {
86  std::vector<size_t> indexes;
87  size_t baseIndex = 0;
88 
89  this->sources_.forEach([&](auto& source, size_t sourceIndex) {
90  if (fieldName.front() == source.fieldNameSourcePrefix()) {
91  MetricsSource::Interests* interests = (setInterest ? &(*this->sourceInterests_)[sourceIndex] : nullptr);
92  indexes = source.indexesOfFieldName(fieldName, interests);
93  for (auto& i : indexes)
94  i += baseIndex;
95  } else
96  baseIndex += std::distance(this->sourceIterators_[sourceIndex].first, this->sourceIterators_[sourceIndex].second);
97  });
98 
99  return indexes;
100  }
101 
102  template<typename T>
103  const MetricsSource::FieldInfo MetricsArray<T>::fieldInfoAtIndex(size_t index) const noexcept {
105  bool stop = false;
106 
107  this->sources_.forEach([&](auto& source, size_t sourceIndex) {
108  if (stop)
109  return;
110  if (index < source.fieldCount()) {
111  info = source.fieldInfoAtIndex(index);
112  stop = true;
113  }
114  else
115  index -= std::distance(this->sourceIterators_[sourceIndex].first, this->sourceIterators_[sourceIndex].second);
116  });
117 
118  return info;
119  }
120 
121  template<typename T>
122  const std::vector<MetricsSource::FieldInfo> MetricsArray<T>::allFieldsInfo() const noexcept {
123  std::vector<MetricsSource::FieldInfo> allInfo;
124 
125  this->sources_.forEach([&](auto& source, size_t ) {
126  std::vector<MetricsSource::FieldInfo> info = source.allFieldsInfo();
127  std::move(info.begin(), info.end(), std::back_inserter(allInfo));
128  });
129 
130  return allInfo;
131  }
132 
133 
134  template<typename T>
135  size_t MetricsArray<T>::size() const noexcept {
136  return this->data_.size();
137  }
138 
139  template<typename T>
140  T& MetricsArray<T>::at(size_t index) {
141  return this->data_.at(index);
142  }
143 
144  template<typename T>
145  const T& MetricsArray<T>::at(size_t index) const {
146  return this->data_.at(index);
147  }
148 
149 
150  template<typename T>
152  return this->sourceIterators_[sourceIndex];
153  }
154 
155 
156  template<typename T>
158  return this->data_.begin();
159  }
160 
161  template<typename T>
162  const typename MetricsArray<T>::ConstIterator MetricsArray<T>::begin() const noexcept {
163  return this->data_.begin();
164  }
165 
166  template<typename T>
168  return this->data_.end();
169  }
170 
171  template<typename T>
172  const typename MetricsArray<T>::ConstIterator MetricsArray<T>::end() const noexcept {
173  return this->data_.end();
174  }
175 
176 
177  template<typename T>
178  void MetricsArray<T>::setSize() noexcept {
179  size_t size = this->fieldCount();
180  this->data_.resize(size);
181  this->setIterators();
182  }
183 
184  template<typename T>
186  auto itr = this->data_.begin();
187  this->sources_.forEach([&](auto& source, size_t sourceIndex) {
188  this->sourceIterators_[sourceIndex].first = itr;
189  itr += source.fieldCount();
190  this->sourceIterators_[sourceIndex].second = itr;
191  });
192  }
193 
194 
195  template class MetricsArray<DataValueType>;
196  template class MetricsArray<DiffValueType>;
197  template class MetricsArray<size_t>;
198  template class MetricsArray<Statistics::Stats>;
199 }
Object used to describe a field (or metric)
Definition: SourceField.h:37
MetricsSource::SourceInterests sourceInterests_
Boolean arrays to store source interests.
Definition: MetricsArray.h:47
std::shared_ptr< std::vector< MetricsSource::Interests > > SourceInterests
Type used to store and share interests of multiple sources.
std::vector< std::string > FieldName
Type used for field names (an array of strings)
Definition: SourceField.h:31
Iterator begin() noexcept
Returns an iterator to the first element of the container.
T & at(size_t index)
Access specified element.
Generic class to store and manage metrics.
Definition: MetricsArray.h:40
SourceInterests makeSourceInterests(bool value)
Construct a new shared pointer to a SourceInterests object.
Boolean array to keep track of which fields a source has to fetch metrics for.
typename std::vector< T >::iterator Iterator
Iterator type.
Definition: MetricsArray.h:43
Iterator end() noexcept
Returns an iterator to the element following the last element of the container.
typename std::vector< T >::const_iterator ConstIterator
Const iterator type.
Definition: MetricsArray.h:44
std::vector< DataValueType >::iterator Iterator
Iterator type of MetricsDataArray.
Definition: MetricTypes.h:36
size_t size() const noexcept
Returns the size of the underlying array.