2 ==================================================================================
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
20 #include "sliding_window.hpp"
23 sliding_window::sliding_window(unsigned int window_size){
25 if (window_size < MIN_WINDOW_SIZE){
27 ss << "Error ::" << __FILE__ << ","<< __LINE__ << " window size must be >= " << MIN_WINDOW_SIZE << " Specified = " << window_size << std::endl;
28 error_string = ss.str();
29 throw std::logic_error(error_string);
32 if (window_size > MAX_WINDOW_SIZE){
34 ss << "Error ::" << __FILE__ << ","<< __LINE__ << " window size must be <= " << MAX_WINDOW_SIZE << " Specified = " << window_size << std::endl;
35 std::string error_string = ss.str();
36 throw std::logic_error(error_string);
39 window_size_ = window_size;
40 sliding_window_.resize(window_size_);
42 head = window_size_ -1;
45 leading_edge_ = std::chrono::steady_clock::now();
48 // expect to be called very frequently ....
49 bool sliding_window::update_window(unsigned int events){
51 auto current_edge = std::chrono::steady_clock::now();
53 int shift = std::chrono::duration_cast<std::chrono::seconds>(current_edge - leading_edge_).count();
56 // we do not update any events if they happened in the past
57 // by more than unit of window .. (1 second currently)
61 // Advance window if shift >= 1
63 leading_edge_ = current_edge;
66 if (shift >= window_size_){
67 // gap between current time and leading edge
68 // exceeds window size. Clear everything ...
70 head = window_size_ -1;
71 std::fill(sliding_window_.begin(), sliding_window_.end(), 0);
72 sliding_window_[head] = events ;
77 for(int i = 0; i < shift; i++){
78 // Advance tail & head by requisite amount, reducing net event rate ..
79 net_events -= sliding_window_[tail];
80 sliding_window_[tail] = 0;
82 if (tail >= window_size_){
87 if (head >= window_size_){
92 sliding_window_[head] += events;
95 //std::cout <<net_events << "," << window_size_ << "," << shift << std::endl;
99 // modifies window size. resets all parameters
100 bool sliding_window::resize_window(unsigned int window_size){
101 if (window_size < MIN_WINDOW_SIZE || window_size > MAX_WINDOW_SIZE){
102 std::stringstream ss;
103 ss << "Error ::" << __FILE__ << ","<< __LINE__ << " window size must be in [ " << MIN_WINDOW_SIZE << "," << MAX_WINDOW_SIZE << "]" << std::endl;
104 error_string = ss.str();
108 window_size_ = window_size;
109 sliding_window_.clear();
110 sliding_window_.resize(window_size_);
112 head = window_size_ -1;
114 leading_edge_ = std::chrono::steady_clock::now();
118 std::string sliding_window::display_window(void){
119 std::stringstream ss;
120 for(unsigned int i = 0; i < sliding_window_.size(); i++){
128 ss <<sliding_window_[i] <<",";
130 ss <<" [Events] = " << net_events << std::endl;
135 void sliding_window::clear(void){
137 resize_window(window_size_);