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 // Advance window if shift >= 1
58 leading_edge_ = current_edge;
61 if (shift >= window_size_){
62 // gap between current time and leading edge
63 // exceeds window size. Clear everything ...
65 head = window_size_ -1;
66 std::fill(sliding_window_.begin(), sliding_window_.end(), 0);
67 sliding_window_[head] = events ;
72 for(int i = 0; i < shift; i++){
73 // Advance tail & head by requisite amount, reducing net event rate ..
74 net_events -= sliding_window_[tail];
75 sliding_window_[tail] = 0;
77 if (tail >= window_size_){
82 if (head >= window_size_){
87 sliding_window_[head] += events;
90 //std::cout <<net_events << "," << window_size_ << "," << shift << std::endl;
94 // modifies window size. resets all parameters
95 bool sliding_window::resize_window(unsigned int window_size){
96 if (window_size < MIN_WINDOW_SIZE || window_size > MAX_WINDOW_SIZE){
98 ss << "Error ::" << __FILE__ << ","<< __LINE__ << " window size must be in [ " << MIN_WINDOW_SIZE << "," << MAX_WINDOW_SIZE << "]" << std::endl;
99 error_string = ss.str();
103 window_size_ = window_size;
104 sliding_window_.clear();
105 sliding_window_.resize(window_size_);
107 head = window_size_ -1;
109 leading_edge_ = std::chrono::steady_clock::now();
113 std::string sliding_window::display_window(void){
114 std::stringstream ss;
115 for(unsigned int i = 0; i < sliding_window_.size(); i++){
123 ss <<sliding_window_[i] <<",";
125 ss <<" [Events] = " << net_events << std::endl;
130 void sliding_window::clear(void){
132 resize_window(window_size_);