This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
collector.hpp
119 lines (98 loc) · 3.47 KB
/
collector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef COLLECTOR__COLLECTOR_HPP_
#define COLLECTOR__COLLECTOR_HPP_
#include <mutex>
#include <string>
#include "metric_details_interface.hpp"
#include "moving_average_statistics/moving_average.hpp"
#include "moving_average_statistics/types.hpp"
#include "rcpputils/thread_safety_annotations.hpp"
namespace collector
{
/**
* Simple class in order to collect observed data and generate statistics for the given observations.
*/
class Collector : public MetricDetailsInterface
{
public:
Collector() = default;
virtual ~Collector() = default;
/**
* Add an observed measurement. This aggregates the measurement and calculates statistics
* via the moving_average class.
*
* @param the measurement observed
*/
virtual void AcceptData(const double measurement);
/**
* Return the statistics for all of the observed data.
*
* @return the StatisticData for all the observed measurements
*/
virtual moving_average_statistics::StatisticData GetStatisticsResults() const;
/**
* Clear / reset all current measurements.
*/
virtual void ClearCurrentMeasurements();
/**
* Return true is start has been called, false otherwise.
*
* @return the started state of this collector
*/
bool IsStarted() const;
/**
* Return a pretty printed status representation of this class
*
* @return a string detailing the current status
*/
virtual std::string GetStatusString() const;
// TODO(dabonnie): uptime (once start has been called)
/**
* Start collecting data. Meant to be called after construction. Note: this locks the recursive mutex class
* member 'mutex'. This method is public in order for the caller to manually manage starting and
* stopping this collector.
*
* @return true if started, false if an error occurred
*/
virtual bool Start();
/**
* Stop collecting data. Meant to be a teardown method (before destruction, but should place the
* class in a restartable state, i.e., start can be called to be able to resume collection. This method
* is public in order for the caller to manually manage starting and stopping this collector.
*
* This calls ClearCurrentMeasurements.
*
* @return true if stopped, false if an error occurred
*/
virtual bool Stop();
private:
/**
* Override in order to perform necessary starting steps.
*
* @return true if setup was successful, false otherwise.
*/
virtual bool SetupStart() = 0 RCPPUTILS_TSA_REQUIRES(mutex_);
/**
* Override in order to perform necessary teardown.
*
* @return true if teardown was successful, false otherwise.
*/
virtual bool SetupStop() = 0 RCPPUTILS_TSA_REQUIRES(mutex_);
mutable std::mutex mutex_;
moving_average_statistics::MovingAverageStatistics collected_data_;
bool started_{false} RCPPUTILS_TSA_GUARDED_BY(mutex_);
};
} // namespace collector
#endif // COLLECTOR__COLLECTOR_HPP_