]> git.stg.codes - stg.git/blob - sgconf/tariffs.cpp
7ff7e7112cdd9dd0d6721b3462e3c86aed3967df
[stg.git] / sgconf / tariffs.cpp
1 #include "tariffs.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "config.h"
6 #include "utils.h"
7
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/tariff_conf.h"
11 #include "stg/common.h"
12
13 #include <iostream>
14 #include <algorithm>
15 #include <sstream>
16 #include <string>
17 #include <map>
18 #include <cstdint>
19 #include <cassert>
20
21 namespace
22 {
23
24 std::string Indent(size_t level, bool dash = false)
25 {
26 if (level == 0)
27     return "";
28 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
29 }
30
31 std::string ChangePolicyToString(TARIFF::CHANGE_POLICY changePolicy)
32 {
33 switch (changePolicy)
34     {
35     case TARIFF::ALLOW: return "allow";
36     case TARIFF::TO_CHEAP: return "to_cheap";
37     case TARIFF::TO_EXPENSIVE: return "to_expensive";
38     case TARIFF::DENY: return "deny";
39     }
40 return "unknown";
41 }
42
43 std::string PeriodToString(TARIFF::PERIOD period)
44 {
45 switch (period)
46     {
47     case TARIFF::DAY:
48         return "daily";
49     case TARIFF::MONTH:
50         return "monthly";
51     }
52 return "unknown";
53 }
54
55 std::string TraffTypeToString(TARIFF::TRAFF_TYPE traffType)
56 {
57 switch (traffType)
58     {
59     case TARIFF::TRAFF_UP:
60         return "upload";
61     case TARIFF::TRAFF_DOWN:
62         return "download";
63     case TARIFF::TRAFF_UP_DOWN:
64         return "upload + download";
65     case TARIFF::TRAFF_MAX:
66         return "max(upload, download)";
67     }
68 return "unknown";
69 }
70
71 void ConvPeriod(const std::string & value, RESETABLE<TARIFF::PERIOD> & res)
72 {
73 std::string lowered = ToLower(value);
74 if (lowered == "daily")
75     res = TARIFF::DAY;
76 else if (lowered == "monthly")
77     res = TARIFF::MONTH;
78 else
79     throw SGCONF::ACTION::ERROR("Period should be 'daily' or 'monthly'. Got: '" + value + "'");
80 }
81
82 void ConvChangePolicy(const std::string & value, RESETABLE<TARIFF::CHANGE_POLICY> & res)
83 {
84 std::string lowered = ToLower(value);
85 if (lowered == "allow")
86     res = TARIFF::ALLOW;
87 else if (lowered == "to_cheap")
88     res = TARIFF::TO_CHEAP;
89 else if (lowered == "to_expensive")
90     res = TARIFF::TO_EXPENSIVE;
91 else if (lowered == "deny")
92     res = TARIFF::DENY;
93 else
94     throw SGCONF::ACTION::ERROR("Change policy should be 'allow', 'to_cheap', 'to_expensive' or 'deny'. Got: '" + value + "'");
95 }
96
97 void ConvChangePolicyTimeout(const std::string & value, RESETABLE<time_t> & res)
98 {
99 struct tm brokenTime;
100 if (stg_strptime(value.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime) == NULL)
101     throw SGCONF::ACTION::ERROR("Credit expiration should be in format 'YYYY-MM-DD HH:MM:SS'. Got: '" + value + "'");
102 res = stg_timegm(&brokenTime);
103 }
104
105 void ConvTraffType(const std::string & value, RESETABLE<TARIFF::TRAFF_TYPE> & res)
106 {
107 std::string lowered = ToLower(value);
108 lowered.erase(std::remove(lowered.begin(), lowered.end(), ' '), lowered.end());
109 if (lowered == "upload")
110     res = TARIFF::TRAFF_UP;
111 else if (lowered == "download")
112     res = TARIFF::TRAFF_DOWN;
113 else if (lowered == "upload+download")
114     res = TARIFF::TRAFF_UP_DOWN;
115 else if (lowered.substr(0, 3) == "max")
116     res = TARIFF::TRAFF_MAX;
117 else
118     throw SGCONF::ACTION::ERROR("Traff type should be 'upload', 'download', 'upload + download' or 'max'. Got: '" + value + "'");
119 }
120
121 DIRPRICE_DATA_RES ConvTimeSpan(const std::string & value)
122 {
123 size_t dashPos = value.find_first_of('-');
124 if (dashPos == std::string::npos)
125     throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
126 size_t fromColon = value.find_first_of(':');
127 if (fromColon == std::string::npos || fromColon > dashPos)
128     throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
129 size_t toColon = value.find_first_of(':', dashPos);
130 if (toColon == std::string::npos)
131     throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
132 DIRPRICE_DATA_RES res;
133 res.hDay = FromString<int>(value.substr(0, fromColon));
134 if (res.hDay.data() < 0 || res.hDay.data() > 23)
135     throw SGCONF::ACTION::ERROR("Invalid 'from' hours. Got: '" + value.substr(0, fromColon) + "'");
136 res.mDay = FromString<int>(value.substr(fromColon + 1, dashPos - fromColon - 1));
137 if (res.mDay.data() < 0 || res.mDay.data() > 59)
138     throw SGCONF::ACTION::ERROR("Invalid 'from' minutes. Got: '" + value.substr(fromColon + 1, dashPos - fromColon - 1) + "'");
139 res.hNight = FromString<int>(value.substr(dashPos + 1, toColon - dashPos - 1));
140 if (res.hNight.data() < 0 || res.hNight.data() > 23)
141     throw SGCONF::ACTION::ERROR("Invalid 'to' hours. Got: '" + value.substr(dashPos + 1, toColon - dashPos - 1) + "'");
142 res.mNight = FromString<int>(value.substr(toColon + 1, value.length() - toColon));
143 if (res.mNight.data() < 0 || res.mNight.data() > 59)
144     throw SGCONF::ACTION::ERROR("Invalid 'to' minutes. Got: '" + value.substr(toColon + 1, value.length() - toColon) + "'");
145 return res;
146 }
147
148 void Splice(std::vector<DIRPRICE_DATA_RES> & lhs, const std::vector<DIRPRICE_DATA_RES> & rhs)
149 {
150 for (size_t i = 0; i < lhs.size() && i < rhs.size(); ++i)
151     lhs[i].Splice(rhs[i]);
152 }
153
154 void ConvTimes(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
155 {
156 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
157 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvTimeSpan));
158 }
159
160 struct ConvPrice : public std::unary_function<std::string, DIRPRICE_DATA_RES>
161 {
162     typedef RESETABLE<double> (DIRPRICE_DATA_RES::* MemPtr);
163     ConvPrice(MemPtr before, MemPtr after)
164         : m_before(before), m_after(after)
165     {}
166
167     DIRPRICE_DATA_RES operator()(const std::string & value)
168     {
169     DIRPRICE_DATA_RES res;
170     size_t slashPos = value.find_first_of('/');
171     if (slashPos == std::string::npos)
172         {
173         double price = 0;
174         if (str2x(value, price) < 0)
175             throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value + "'");
176         (res.*m_before) = (res.*m_after) = price;
177         res.noDiscount = true;
178         }
179     else
180         {
181         double price = 0;
182         if (str2x(value.substr(0, slashPos), price) < 0)
183             throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value.substr(0, slashPos) + "'");
184         (res.*m_before) = price;
185         if (str2x(value.substr(slashPos + 1, value.length() - slashPos), price) < 0)
186             throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value.substr(slashPos + 1, value.length() - slashPos) + "'");
187         (res.*m_after) = price;
188         res.noDiscount = false;
189         }
190     return res;
191     }
192
193     MemPtr m_before;
194     MemPtr m_after;
195 };
196
197 void ConvDayPrices(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
198 {
199 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
200 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvPrice(&DIRPRICE_DATA_RES::priceDayA, &DIRPRICE_DATA_RES::priceDayB)));
201 }
202
203 void ConvNightPrices(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
204 {
205 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
206 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvPrice(&DIRPRICE_DATA_RES::priceNightA, &DIRPRICE_DATA_RES::priceNightB)));
207 }
208
209 DIRPRICE_DATA_RES ConvThreshold(std::string value)
210 {
211 DIRPRICE_DATA_RES res;
212 double threshold = 0;
213 if (str2x(value, threshold) < 0)
214     throw SGCONF::ACTION::ERROR("Threshold should be a floating point value. Got: '" + value + "'");
215 res.threshold = threshold;
216 return res;
217 }
218
219 void ConvThresholds(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
220 {
221 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
222 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvThreshold));
223 }
224
225 std::string TimeToString(int h, int m)
226 {
227 std::ostringstream stream;
228 stream << (h < 10 ? "0" : "") << h << ":"
229        << (m < 10 ? "0" : "") << m;
230 return stream.str();
231 }
232
233 void PrintDirPriceData(size_t dir, const DIRPRICE_DATA & data, size_t level)
234 {
235 std::string night = TimeToString(data.hNight, data.mNight);
236 std::string day = TimeToString(data.hDay, data.mDay);
237 std::cout << Indent(level, true) << "dir: " << dir << "\n"
238           << Indent(level)       << "'" << night << "' - '" << day << "': " << data.priceDayA << "/" << data.priceDayB << "\n"
239           << Indent(level)       << "'" << day << "' - '" << night << "': " << data.priceNightA << "/" << data.priceNightB << "\n"
240           << Indent(level)       << "threshold: " << data.threshold << "\n"
241           << Indent(level)       << "single price: " << (data.singlePrice ? "yes" : "no") << "\n"
242           << Indent(level)       << "discount: " << (data.noDiscount ? "no" : "yes") << "\n"; // Attention!
243 }
244
245 void PrintTariffConf(const TARIFF_CONF & conf, size_t level)
246 {
247 std::cout << Indent(level, true) << "name: " << conf.name << "\n"
248           << Indent(level)       << "fee: " << conf.fee << "\n"
249           << Indent(level)       << "free mb: " << conf.free << "\n"
250           << Indent(level)       << "passive cost: " << conf.passiveCost << "\n"
251           << Indent(level)       << "traff type: " << TraffTypeToString(conf.traffType) << "\n"
252           << Indent(level)       << "period: " << PeriodToString(conf.period) << "\n"
253           << Indent(level)       << "change policy: " << ChangePolicyToString(conf.changePolicy) << "\n"
254           << Indent(level)       << "change policy timeout: " << formatTime(conf.changePolicyTimeout) << "\n";
255 }
256
257 void PrintTariff(const STG::GET_TARIFF::INFO & info, size_t level = 0)
258 {
259 PrintTariffConf(info.tariffConf, level);
260 std::cout << Indent(level) << "dir prices:\n";
261 for (size_t i = 0; i < info.dirPrice.size(); ++i)
262     PrintDirPriceData(i, info.dirPrice[i], level + 1);
263 }
264
265 std::vector<SGCONF::API_ACTION::PARAM> GetTariffParams()
266 {
267 std::vector<SGCONF::API_ACTION::PARAM> params;
268 params.push_back(SGCONF::API_ACTION::PARAM("fee", "<fee>", "\t\ttariff fee"));
269 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traffic"));
270 params.push_back(SGCONF::API_ACTION::PARAM("passive-cost", "<cost>", "\tpassive cost"));
271 params.push_back(SGCONF::API_ACTION::PARAM("traff-type", "<type>", "\ttraffic type (up, down, up+down, max)"));
272 params.push_back(SGCONF::API_ACTION::PARAM("period", "<period>", "\ttarification period (daily, monthly)"));
273 params.push_back(SGCONF::API_ACTION::PARAM("change-policy", "<policy>", "tariff change policy (allow, to_cheap, to_expensive, deny)"));
274 params.push_back(SGCONF::API_ACTION::PARAM("change-policy-timeout", "<yyyy-mm-dd hh:mm:ss>", "tariff change policy timeout"));
275 params.push_back(SGCONF::API_ACTION::PARAM("times", "<hh:mm-hh:mm, ...>", "coma-separated day time-spans for each direction"));
276 params.push_back(SGCONF::API_ACTION::PARAM("day-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
277 params.push_back(SGCONF::API_ACTION::PARAM("night-prices", "<price/price, ...>", "coma-separated night prices for each direction"));
278 params.push_back(SGCONF::API_ACTION::PARAM("thresholds", "<threshold, ...>", "coma-separated thresholds for each direction"));
279 return params;
280 }
281
282 void SimpleCallback(bool result,
283                     const std::string & reason,
284                     void * /*data*/)
285 {
286 if (!result)
287     {
288     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
289     return;
290     }
291 std::cout << "Success.\n";
292 }
293
294 void GetTariffsCallback(bool result,
295                         const std::string & reason,
296                         const std::vector<STG::GET_TARIFF::INFO> & info,
297                         void * /*data*/)
298 {
299 if (!result)
300     {
301     std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
302     return;
303     }
304 std::cout << "Tariffs:\n";
305 for (size_t i = 0; i < info.size(); ++i)
306     PrintTariff(info[i], 1);
307 }
308
309 void GetTariffCallback(bool result,
310                        const std::string & reason,
311                        const std::vector<STG::GET_TARIFF::INFO> & info,
312                        void * data)
313 {
314 assert(data != NULL && "Expecting pointer to std::string with the tariff's name.");
315 const std::string & name = *static_cast<const std::string *>(data);
316 if (!result)
317     {
318     std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
319     return;
320     }
321 for (size_t i = 0; i < info.size(); ++i)
322     if (info[i].tariffConf.name == name)
323         PrintTariff(info[i]);
324 }
325
326 bool GetTariffsFunction(const SGCONF::CONFIG & config,
327                         const std::string & /*arg*/,
328                         const std::map<std::string, std::string> & /*options*/)
329 {
330 STG::SERVCONF proto(config.server.data(),
331                     config.port.data(),
332                     config.localAddress.data(),
333                     config.localPort.data(),
334                     config.userName.data(),
335                     config.userPass.data());
336 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
337 }
338
339 bool GetTariffFunction(const SGCONF::CONFIG & config,
340                        const std::string & arg,
341                        const std::map<std::string, std::string> & /*options*/)
342 {
343 STG::SERVCONF proto(config.server.data(),
344                     config.port.data(),
345                     config.localAddress.data(),
346                     config.localPort.data(),
347                     config.userName.data(),
348                     config.userPass.data());
349 // STG currently doesn't support <GetTariff name="..."/>.
350 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
351 std::string name(arg);
352 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
353 }
354
355 bool DelTariffFunction(const SGCONF::CONFIG & config,
356                        const std::string & arg,
357                        const std::map<std::string, std::string> & /*options*/)
358 {
359 STG::SERVCONF proto(config.server.data(),
360                     config.port.data(),
361                     config.localAddress.data(),
362                     config.localPort.data(),
363                     config.userName.data(),
364                     config.userPass.data());
365 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
366 }
367
368 bool AddTariffFunction(const SGCONF::CONFIG & config,
369                        const std::string & arg,
370                        const std::map<std::string, std::string> & options)
371 {
372 TARIFF_DATA_RES conf;
373 conf.tariffConf.name = arg;
374 SGCONF::MaybeSet(options, "fee", conf.tariffConf.fee);
375 SGCONF::MaybeSet(options, "free", conf.tariffConf.free);
376 SGCONF::MaybeSet(options, "passive-cost", conf.tariffConf.passiveCost);
377 SGCONF::MaybeSet(options, "traff-type", conf.tariffConf.traffType, ConvTraffType);
378 SGCONF::MaybeSet(options, "period", conf.tariffConf.period, ConvPeriod);
379 SGCONF::MaybeSet(options, "change-policy", conf.tariffConf.changePolicy, ConvChangePolicy);
380 SGCONF::MaybeSet(options, "change-policy-timeout", conf.tariffConf.changePolicyTimeout, ConvChangePolicyTimeout);
381 SGCONF::MaybeSet(options, "times", conf.dirPrice, ConvTimes);
382 SGCONF::MaybeSet(options, "day-prices", conf.dirPrice, ConvDayPrices);
383 SGCONF::MaybeSet(options, "night-prices", conf.dirPrice, ConvNightPrices);
384 SGCONF::MaybeSet(options, "thresholds", conf.dirPrice, ConvThresholds);
385 for (size_t i = 0; i < conf.dirPrice.size(); ++i)
386     {
387     if (!conf.dirPrice[i].priceDayA.empty() &&
388         !conf.dirPrice[i].priceNightA.empty() &&
389         !conf.dirPrice[i].priceDayB.empty() &&
390         !conf.dirPrice[i].priceNightB.empty())
391         conf.dirPrice[i].singlePrice = conf.dirPrice[i].priceDayA.data() == conf.dirPrice[i].priceNightA.data() &&
392                                        conf.dirPrice[i].priceDayB.data() == conf.dirPrice[i].priceNightB.data();
393     }
394 STG::SERVCONF proto(config.server.data(),
395                     config.port.data(),
396                     config.localAddress.data(),
397                     config.localPort.data(),
398                     config.userName.data(),
399                     config.userPass.data());
400 return proto.AddTariff(arg, conf, SimpleCallback, NULL) == STG::st_ok;
401 }
402
403 bool ChgTariffFunction(const SGCONF::CONFIG & config,
404                        const std::string & arg,
405                        const std::map<std::string, std::string> & options)
406 {
407 TARIFF_DATA_RES conf;
408 conf.tariffConf.name = arg;
409 SGCONF::MaybeSet(options, "fee", conf.tariffConf.fee);
410 SGCONF::MaybeSet(options, "free", conf.tariffConf.free);
411 SGCONF::MaybeSet(options, "passive-cost", conf.tariffConf.passiveCost);
412 SGCONF::MaybeSet(options, "traff-type", conf.tariffConf.traffType, ConvTraffType);
413 SGCONF::MaybeSet(options, "period", conf.tariffConf.period, ConvPeriod);
414 SGCONF::MaybeSet(options, "change-policy", conf.tariffConf.changePolicy, ConvChangePolicy);
415 SGCONF::MaybeSet(options, "change-policy-timeout", conf.tariffConf.changePolicyTimeout, ConvChangePolicyTimeout);
416 SGCONF::MaybeSet(options, "times", conf.dirPrice, ConvTimes);
417 SGCONF::MaybeSet(options, "day-prices", conf.dirPrice, ConvDayPrices);
418 SGCONF::MaybeSet(options, "night-prices", conf.dirPrice, ConvNightPrices);
419 SGCONF::MaybeSet(options, "thresholds", conf.dirPrice, ConvThresholds);
420 for (size_t i = 0; i < conf.dirPrice.size(); ++i)
421     {
422     if (!conf.dirPrice[i].priceDayA.empty() &&
423         !conf.dirPrice[i].priceNightA.empty() &&
424         !conf.dirPrice[i].priceDayB.empty() &&
425         !conf.dirPrice[i].priceNightB.empty())
426         conf.dirPrice[i].singlePrice = conf.dirPrice[i].priceDayA.data() == conf.dirPrice[i].priceNightA.data() &&
427                                        conf.dirPrice[i].priceDayB.data() == conf.dirPrice[i].priceNightB.data();
428     }
429 STG::SERVCONF proto(config.server.data(),
430                     config.port.data(),
431                     config.localAddress.data(),
432                     config.localPort.data(),
433                     config.userName.data(),
434                     config.userPass.data());
435 return proto.ChgTariff(conf, SimpleCallback, NULL) == STG::st_ok;
436 }
437
438 } // namespace anonymous
439
440 void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
441 {
442 std::vector<API_ACTION::PARAM> params(GetTariffParams());
443 blocks.Add("Tariff management options")
444       .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list")
445       .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", GetTariffFunction), "get tariff")
446       .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, AddTariffFunction), "add tariff")
447       .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", DelTariffFunction), "delete tariff")
448       .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, ChgTariffFunction), "change tariff");
449 }