MetroCollect  2.3.4
MetricsArray.h
Go to the documentation of this file.
1 //
2 // MetricsArray.h
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 #pragma once
22 
23 #include <array>
24 #include <chrono>
25 #include <utility>
26 #include <vector>
27 
28 #include "MetricsSourceArray.h"
29 
30 
31 namespace MetroCollect {
32  class MetricsDiffArray;
33 
39  template<typename T>
40  class MetricsArray {
41  public:
42  using ValueType = T;
43  using Iterator = typename std::vector<T>::iterator;
44  using ConstIterator = typename std::vector<T>::const_iterator;
45 
46  protected:
48  std::chrono::system_clock::time_point timestamp_;
49 
50  std::vector<T> data_;
52  std::array<std::pair<Iterator, Iterator>, MetricsSource::MetricsSourceArray::count> sourceIterators_;
53 
55 
56  void setSize() noexcept;
57  void setIterators() noexcept;
58 
59  public:
63  MetricsArray() noexcept;
64 
70  explicit MetricsArray(MetricsSource::SourceInterests sourceInterests) noexcept;
71 
72 
78  MetricsArray(const MetricsArray& other) noexcept;
79 
86  MetricsArray& operator=(const MetricsArray& other) noexcept;
87 
88 
94  MetricsSource::SourceInterests sourceInterests() const noexcept;
95 
101  void setSourcesInterests(MetricsSource::SourceInterests sourceInterests) noexcept;
102 
103 
109  const std::chrono::system_clock::time_point& timestamp() const noexcept;
110 
111 
119  size_t fieldCount() const noexcept;
120 
128  const std::vector<size_t> indexesOfFieldName(const MetricsSource::FieldName& fieldName, bool setInterest = true) const noexcept;
129 
136  const MetricsSource::FieldInfo fieldInfoAtIndex(size_t index) const noexcept;
137 
143  const std::vector<MetricsSource::FieldInfo> allFieldsInfo() const noexcept;
144 
145 
153  size_t size() const noexcept;
154 
161  T& at(size_t index);
162 
169  const T& at(size_t index) const;
170 
177  inline T& operator[](size_t index) { return this->data_[index]; }
178 
185  inline const T& operator[](size_t index) const { return this->data_[index]; }
186 
187 
194  std::pair<Iterator, Iterator> metricsSliceForSource(size_t sourceIndex);
195 
196 
202  Iterator begin() noexcept;
203 
209  const ConstIterator begin() const noexcept;
210 
216  Iterator end() noexcept;
217 
223  const ConstIterator end() const noexcept;
224  };
225 }
MetricsSource::SourceInterests sourceInterests() const noexcept
Returns the source interests.
Definition: MetricsArray.cc:57
MetricsSource::SourceInterests sourceInterests_
Boolean arrays to store source interests.
Definition: MetricsArray.h:47
static constexpr size_t count
Number of sources.
std::pair< Iterator, Iterator > metricsSliceForSource(size_t sourceIndex)
Returns the iterator pair associated with a source.
const std::vector< size_t > indexesOfFieldName(const MetricsSource::FieldName &fieldName, bool setInterest=true) const noexcept
Finds indexes of a field from its name across sources.
Definition: MetricsArray.cc:85
std::chrono::system_clock::time_point timestamp_
Time at which metrics were collected.
Definition: MetricsArray.h:48
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.
void setIterators() noexcept
Update internal iterators pairs.
T & at(size_t index)
Access specified element.
STL namespace.
Simple struct to hold references to each source singleton.
Generic class to store and manage metrics.
Definition: MetricsArray.h:40
friend MetricsDiffArray
To allow internal access by subclass.
Definition: MetricsArray.h:54
void setSourcesInterests(MetricsSource::SourceInterests sourceInterests) noexcept
Set the source interests.
Definition: MetricsArray.cc:62
std::vector< T > data_
Underlying data storage.
Definition: MetricsArray.h:50
const MetricsSource::FieldInfo fieldInfoAtIndex(size_t index) const noexcept
Get details about a specific metric field.
typename std::vector< DataValueType >::iterator Iterator
Iterator type.
Definition: MetricsArray.h:43
const std::vector< MetricsSource::FieldInfo > allFieldsInfo() const noexcept
Get details about all fields.
std::array< std::pair< Iterator, Iterator >, MetricsSource::MetricsSourceArray::count > sourceIterators_
Iterators pairs associating metrics to a source.
Definition: MetricsArray.h:52
DataValueType ValueType
Type of stored metrics on the container.
Definition: MetricsArray.h:42
Iterator end() noexcept
Returns an iterator to the element following the last element of the container.
typename std::vector< DataValueType >::const_iterator ConstIterator
Const iterator type.
Definition: MetricsArray.h:44
size_t fieldCount() const noexcept
Returns the total number of available metrics.
Definition: MetricsArray.cc:74
const std::chrono::system_clock::time_point & timestamp() const noexcept
Returns the timestamp of stored metrics.
Definition: MetricsArray.cc:68
MetricsSource::MetricsSourceArray sources_
Array of sources to fetch data from.
Definition: MetricsArray.h:51
const T & operator[](size_t index) const
Access specified element.
Definition: MetricsArray.h:185
size_t size() const noexcept
Returns the size of the underlying array.
void setSize() noexcept
Resize the underlying container to fit the total number of metrics.