1//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef SHILL_NET_EVENT_HISTORY_H_
18#define SHILL_NET_EVENT_HISTORY_H_
19
20#include <deque>
21#include <string>
22#include <vector>
23
24#include <base/macros.h>
25#include <gtest/gtest_prod.h>  // for FRIEND_TEST
26
27#include "shill/net/shill_export.h"
28#include "shill/net/shill_time.h"
29
30namespace shill {
31
32// EventHistory is a list of timestamps tracking the occurrence of one or more
33// events. Events are ordered from earliest to latest. |max_events_saved|
34// can optionally be provided to limit the number of event timestamps saved
35// at any one time.
36class SHILL_EXPORT EventHistory {
37 public:
38  enum ClockType {
39    kClockTypeBoottime = 0,
40    kClockTypeMonotonic = 1,
41  };
42
43  EventHistory() : max_events_specified_(false), time_(Time::GetInstance()) {}
44  explicit EventHistory(int max_events_saved)
45      : max_events_specified_(true),
46        max_events_saved_(max_events_saved),
47        time_(Time::GetInstance()) {}
48
49  // Records the current event by adding the current time to the list.
50  // If |event_limit_specificed_| and the size of |events_| is larger than
51  // |max_events_saved_|, event timestamps are removed in FIFO order until the
52  // size of |events_| is equal to |max_events_saved_|.
53  void RecordEvent();
54
55  // Start at the head of |events_| and remove all entries that occurred
56  // more than |seconds_ago| prior to the current time. |clock_type| determines
57  // what time of clock we use for time-related calculations.
58  void ExpireEventsBefore(int seconds_ago, ClockType clock_type);
59
60  // Records the current event by adding the current time to the list, and uses
61  // this same timestamp to remove all entries that occurred more than
62  // |seconds_ago|. |clock_type| determines what time of clock we use for time-
63  // related calculations.
64  void RecordEventAndExpireEventsBefore(int seconds_ago, ClockType clock_type);
65
66  // Returns a vector of human-readable strings representing each timestamp in
67  // |events_|.
68  std::vector<std::string> ExtractWallClockToStrings() const;
69
70  // Returns the number of timestamps in |events_| within the interval spanning
71  // now and the time |seconds_ago| before now (inclusive). |clock_type|
72  // determines what time of clock we use for time-related calculations.
73  int CountEventsWithinInterval(int seconds_ago, ClockType clock_type);
74
75  size_t Size() const { return events_.size(); }
76  bool Empty() { return events_.empty(); }
77  Timestamp Front() { return events_.front(); }
78  void Clear() { events_.clear(); }
79
80 private:
81  friend class EventHistoryTest;
82  friend class ServiceTest;  // RecordEventInternal, time_
83  friend class WakeOnWiFiTest;  // time_
84
85  void RecordEventInternal(Timestamp now);
86
87  void ExpireEventsBeforeInternal(int seconds_ago, Timestamp now,
88                                  ClockType clock_type);
89
90  bool max_events_specified_;
91  int max_events_saved_;
92  std::deque<Timestamp> events_;
93  Time* time_;
94
95  DISALLOW_COPY_AND_ASSIGN(EventHistory);
96};
97
98}  // namespace shill
99
100#endif  // SHILL_NET_EVENT_HISTORY_H_
101