ledger-core
BalanceHistory.hpp
1 /*
2  *
3  * BalanceHistory
4  *
5  * Created by Dimitri Sabadie on 2019/03/27.
6  *
7  * The MIT License (MIT)
8  *
9  * Copyright (c) 2019 Ledger
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in all
19  * copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  */
30 
31 #pragma once
32 
33 #include <memory>
34 #include <string>
35 #include <vector>
36 
37 #include <api/OperationType.hpp>
38 #include <api/TimePeriod.hpp>
39 #include <async/Future.hpp>
40 #include <utils/DateUtils.hpp>
41 #include <utils/Option.hpp>
42 
43 namespace ledger {
44  namespace core {
45  namespace agnostic {
46  // Get a balance history based on a time window (inclusive) and currency operations.
47  //
48  // The Op type variable is an operation strategy type that must have two methods
49  // implemented:
50  //
51  // - date(Operation op) -> std::chrono::system_clock::time_point, date of the operation
52  // - update_balance(Operation op, Value& sum)
53  //
54  // The Value type variable must be default-constructible and accept addition via the
55  // plus (+) and minus (-) operators.
56  //
57  // The OperationIt type variable must implement the pre-increment operator (++it) and
58  // be dereferencable with the * operator. It must have a value_type associated
59  // type. Finally, it must be comparable with itself.
60  template <typename Op, typename Value, typename CastValue, typename OperationIt>
61  std::vector<std::shared_ptr<CastValue>> getBalanceHistoryFor(
62  std::chrono::system_clock::time_point const& startDate,
63  std::chrono::system_clock::time_point const& endDate,
64  api::TimePeriod precision,
65  OperationIt operationIt,
66  OperationIt operationEnd,
67  Value zero
68  ) {
69  if (startDate >= endDate) {
70  throw make_exception(api::ErrorCode::INVALID_DATE_FORMAT,
71  "Start date should be strictly greater than end date");
72  }
73 
74  auto lowerDate = startDate;
75  auto upperDate = DateUtils::incrementDate(startDate, precision);
76  std::vector<std::shared_ptr<CastValue>> values;
77  Value sum = zero;
78 
79  while (lowerDate <= endDate && operationIt != operationEnd) {
80  auto operation = *operationIt;
81  auto operationDate = Op::date(operation);
82 
83  while (operationDate > upperDate && lowerDate < endDate) {
84  lowerDate = DateUtils::incrementDate(lowerDate, precision);
85  upperDate = DateUtils::incrementDate(upperDate, precision);
86  values.emplace_back(Op::value_constructor(sum));
87  }
88 
89  if (operationDate <= upperDate) {
90  Op::update_balance(operation, sum);
91  }
92 
93  ++operationIt;
94  }
95 
96  while (lowerDate < endDate) {
97  lowerDate = DateUtils::incrementDate(lowerDate, precision);
98  values.emplace_back(Op::value_constructor(sum));
99  }
100 
101  return values;
102  }
103  }
104  }
105 }
Definition: Account.cpp:8