client-cpp  0.9.0
RecordCountWithTimeLimitLogUploadStrategy.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
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 
18 #ifndef RECORDCOUNTWITHTIMELIMITLOGUPLOADSTRATEGY_HPP_
19 #define RECORDCOUNTWITHTIMELIMITLOGUPLOADSTRATEGY_HPP_
20 
21 #include <cstdlib>
22 #include <chrono>
23 
24 #include "kaa/logging/Log.hpp"
27 
28 namespace kaa {
29 
31 public:
32  RecordCountWithTimeLimitLogUploadStrategy(std::size_t countThreshold, std::size_t logUploadCheckReriod, IKaaClientContext &context)
33  : DefaultLogUploadStrategy(context), lastUploadTime_(Clock::now())
34  {
35  setCountThreshold(countThreshold);
36  setLogUploadCheckPeriod(logUploadCheckReriod);
37  }
38 
40  {
41  auto now = Clock::now();
42  auto currentRecordCount = status.getRecordsCount();
43 
44  if ((currentRecordCount >= uploadCountThreshold_) || (now >= (lastUploadTime_ + std::chrono::seconds(logUploadCheckReriod_)))) {
45  KAA_LOG_INFO(boost::format("Need to upload logs - current count: %llu, threshold: %llu, lastUploadedTime: %llu, timeLimit: %llu sec")
46  % currentRecordCount % uploadCountThreshold_ % lastUploadTime_.time_since_epoch().count() % logUploadCheckReriod_);
47  lastUploadTime_ = now;
49  }
50 
52  }
53 
54 private:
55  typedef std::chrono::system_clock Clock;
56  std::chrono::time_point<Clock> lastUploadTime_;
57 };
58 
59 }
60 
61 #endif /* RECORDCOUNTWITHTIMELIMITLOGUPLOADSTRATEGY_HPP_ */
void setLogUploadCheckPeriod(std::size_t period)
The default ILogUploadStrategy implementation.
void setCountThreshold(std::size_t maxCount)
#define KAA_LOG_INFO(message)
Definition: Log.hpp:64
RecordCountWithTimeLimitLogUploadStrategy(std::size_t countThreshold, std::size_t logUploadCheckReriod, IKaaClientContext &context)
virtual LogUploadStrategyDecision isUploadNeeded(ILogStorageStatus &status) override
Decides whether the log upload is needed.
LogUploadStrategyDecision
Log upload decisions.
virtual std::size_t getRecordsCount()=0
Returns the number of collected logs.
The public interface to represent the current log storage state.