]> git.stg.codes - stg.git/blob - projects/sgconf/tariffs.cpp
3da1001a683e92538f9dff7bd0cc34d0de04981c
[stg.git] / projects / sgconf / tariffs.cpp
1 #include "tariffs.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "config.h"
6
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/tariff_conf.h"
10 #include "stg/os_int.h"
11
12 #include <iostream>
13 #include <sstream>
14 #include <string>
15 #include <map>
16 #include <cassert>
17
18 namespace
19 {
20
21 std::string Indent(size_t level, bool dash = false)
22 {
23 if (level == 0)
24     return "";
25 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
26 }
27
28 std::string PeriodToString(TARIFF::PERIOD period)
29 {
30 switch (period)
31     {
32     case TARIFF::DAY:
33         return "daily";
34     case TARIFF::MONTH:
35         return "monthly";
36     }
37 return "unknown";
38 }
39
40 std::string TraffTypeToString(int traffType)
41 {
42 switch (traffType)
43     {
44     case TRAFF_UP:
45         return "upload";
46     case TRAFF_DOWN:
47         return "download";
48     case TRAFF_UP_DOWN:
49         return "upload + download";
50     case TRAFF_MAX:
51         return "max(upload, download)";
52     }
53 return "unknown";
54 }
55
56 std::string TimeToString(int h, int m)
57 {
58 std::ostringstream stream;
59 stream << (h < 10 ? "0" : "") << h << ":"
60        << (m < 10 ? "0" : "") << m;
61 return stream.str();
62 }
63
64 void PrintDirPriceData(size_t dir, const DIRPRICE_DATA & data, size_t level)
65 {
66 std::string night = TimeToString(data.hNight, data.mNight);
67 std::string day = TimeToString(data.hDay, data.mDay);
68 std::cout << Indent(level, true) << "dir: " << dir << "\n"
69           << Indent(level)       << "'" << night << "' - '" << day << "': " << data.priceDayA << "/" << data.priceDayB << "\n"
70           << Indent(level)       << "'" << day << "' - '" << night << "': " << data.priceNightA << "/" << data.priceNightB << "\n"
71           << Indent(level)       << "threshold: " << data.threshold << "\n"
72           << Indent(level)       << "single price: " << (data.singlePrice ? "yes" : "no") << "\n"
73           << Indent(level)       << "discount: " << (data.noDiscount ? "no" : "yes") << "\n"; // Attention!
74 }
75
76 void PrintTariffConf(const TARIFF_CONF & conf, size_t level)
77 {
78 std::cout << Indent(level, true) << "name: " << conf.name << "\n"
79           << Indent(level)       << "fee: " << conf.fee << "\n"
80           << Indent(level)       << "free mb: " << conf.free << "\n"
81           << Indent(level)       << "passive cost: " << conf.passiveCost << "\n"
82           << Indent(level)       << "traff type: " << TraffTypeToString(conf.traffType) << "\n"
83           << Indent(level)       << "period: " << PeriodToString(conf.period) << "\n";
84 }
85
86 void PrintTariff(const STG::GET_TARIFF::INFO & info, size_t level = 0)
87 {
88 PrintTariffConf(info.tariffConf, level);
89 std::cout << Indent(level) << "dir prices:\n";
90 for (size_t i = 0; i < info.dirPrice.size(); ++i)
91     PrintDirPriceData(i, info.dirPrice[i], level + 1);
92 }
93
94 std::vector<SGCONF::API_ACTION::PARAM> GetTariffParams()
95 {
96 std::vector<SGCONF::API_ACTION::PARAM> params;
97 params.push_back(SGCONF::API_ACTION::PARAM("fee", "<fee>", "\t\ttariff fee"));
98 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traff"));
99 params.push_back(SGCONF::API_ACTION::PARAM("passive-cost", "<cost>", "\tpassive cost"));
100 params.push_back(SGCONF::API_ACTION::PARAM("traff-type", "<type>", "\ttraff type (up, dow, up+down, max)"));
101 params.push_back(SGCONF::API_ACTION::PARAM("period", "<period>", "\ttarification period (daily, monthly)"));
102 params.push_back(SGCONF::API_ACTION::PARAM("times", "<hh:mm-hh:mm, ...>", "coma-separated day time-spans for each direction"));
103 params.push_back(SGCONF::API_ACTION::PARAM("day-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
104 params.push_back(SGCONF::API_ACTION::PARAM("night-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
105 params.push_back(SGCONF::API_ACTION::PARAM("thresholds", "<threshold, ...>", "coma-separated thresholds for each direction"));
106 return params;
107 }
108
109 void SimpleCallback(bool result,
110                     const std::string & reason,
111                     void * /*data*/)
112 {
113 if (!result)
114     {
115     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
116     return;
117     }
118 std::cout << "Success.\n";
119 }
120
121 void GetTariffsCallback(bool result,
122                         const std::string & reason,
123                         const std::vector<STG::GET_TARIFF::INFO> & info,
124                         void * /*data*/)
125 {
126 if (!result)
127     {
128     std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
129     return;
130     }
131 std::cout << "Tariffs:\n";
132 for (size_t i = 0; i < info.size(); ++i)
133     PrintTariff(info[i], 1);
134 }
135
136 void GetTariffCallback(bool result,
137                        const std::string & reason,
138                        const std::vector<STG::GET_TARIFF::INFO> & info,
139                        void * data)
140 {
141 assert(data != NULL && "Expecting pointer to std::string with the tariff's name.");
142 const std::string & name = *static_cast<const std::string *>(data);
143 if (!result)
144     {
145     std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
146     return;
147     }
148 for (size_t i = 0; i < info.size(); ++i)
149     if (info[i].tariffConf.name == name)
150         PrintTariff(info[i]);
151 }
152
153 bool GetTariffsFunction(const SGCONF::CONFIG & config,
154                         const std::string & /*arg*/,
155                         const std::map<std::string, std::string> & /*options*/)
156 {
157 STG::SERVCONF proto(config.server.data(),
158                     config.port.data(),
159                     config.userName.data(),
160                     config.userPass.data());
161 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
162 }
163
164 bool GetTariffFunction(const SGCONF::CONFIG & config,
165                        const std::string & arg,
166                        const std::map<std::string, std::string> & /*options*/)
167 {
168 STG::SERVCONF proto(config.server.data(),
169                     config.port.data(),
170                     config.userName.data(),
171                     config.userPass.data());
172 // STG currently doesn't support <GetTariff name="..."/>.
173 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
174 std::string name(arg);
175 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
176 }
177
178 bool DelTariffFunction(const SGCONF::CONFIG & config,
179                        const std::string & arg,
180                        const std::map<std::string, std::string> & /*options*/)
181 {
182 STG::SERVCONF proto(config.server.data(),
183                     config.port.data(),
184                     config.userName.data(),
185                     config.userPass.data());
186 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
187 }
188
189 bool AddTariffFunction(const SGCONF::CONFIG & config,
190                        const std::string & arg,
191                        const std::map<std::string, std::string> & /*options*/)
192 {
193 // TODO
194 std::cerr << "Unimplemented.\n";
195 return false;
196 }
197
198 bool ChgTariffFunction(const SGCONF::CONFIG & config,
199                        const std::string & arg,
200                        const std::map<std::string, std::string> & options)
201 {
202 // TODO
203 std::cerr << "Unimplemented.\n";
204 return false;
205 }
206
207 } // namespace anonymous
208
209 void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
210 {
211 std::vector<API_ACTION::PARAM> params(GetTariffParams());
212 blocks.Add("Tariff management options")
213       .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list")
214       .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", GetTariffFunction), "get tariff")
215       .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, AddTariffFunction), "add tariff")
216       .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", DelTariffFunction), "del tariff")
217       .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, ChgTariffFunction), "change tariff");
218 }