3 #include "api_action.h"
9 #include "stg/servconf.h"
10 #include "stg/servconf_types.h"
11 #include "stg/tariff_conf.h"
12 #include "stg/common.h"
26 std::string Indent(size_t level, bool dash = false)
30 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
33 std::string ChangePolicyToString(STG::Tariff::ChangePolicy changePolicy)
37 case STG::Tariff::ALLOW: return "allow";
38 case STG::Tariff::TO_CHEAP: return "to_cheap";
39 case STG::Tariff::TO_EXPENSIVE: return "to_expensive";
40 case STG::Tariff::DENY: return "deny";
45 std::string PeriodToString(STG::Tariff::Period period)
49 case STG::Tariff::DAY:
51 case STG::Tariff::MONTH:
57 std::string TraffTypeToString(STG::Tariff::TraffType traffType)
61 case STG::Tariff::TRAFF_UP:
63 case STG::Tariff::TRAFF_DOWN:
65 case STG::Tariff::TRAFF_UP_DOWN:
66 return "upload + download";
67 case STG::Tariff::TRAFF_MAX:
68 return "max(upload, download)";
73 void ConvPeriod(const std::string & value, std::optional<STG::Tariff::Period> & res)
75 std::string lowered = ToLower(value);
76 if (lowered == "daily")
77 res = STG::Tariff::DAY;
78 else if (lowered == "monthly")
79 res = STG::Tariff::MONTH;
81 throw SGCONF::ACTION::ERROR("Period should be 'daily' or 'monthly'. Got: '" + value + "'");
84 void ConvChangePolicy(const std::string & value, std::optional<STG::Tariff::ChangePolicy> & res)
86 std::string lowered = ToLower(value);
87 if (lowered == "allow")
88 res = STG::Tariff::ALLOW;
89 else if (lowered == "to_cheap")
90 res = STG::Tariff::TO_CHEAP;
91 else if (lowered == "to_expensive")
92 res = STG::Tariff::TO_EXPENSIVE;
93 else if (lowered == "deny")
94 res = STG::Tariff::DENY;
96 throw SGCONF::ACTION::ERROR("Change policy should be 'allow', 'to_cheap', 'to_expensive' or 'deny'. Got: '" + value + "'");
99 void ConvChangePolicyTimeout(const std::string & value, std::optional<time_t> & res)
101 struct tm brokenTime;
102 if (stg_strptime(value.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime) == NULL)
103 throw SGCONF::ACTION::ERROR("Credit expiration should be in format 'YYYY-MM-DD HH:MM:SS'. Got: '" + value + "'");
104 res = stg_timegm(&brokenTime);
107 void ConvTraffType(const std::string & value, std::optional<STG::Tariff::TraffType> & res)
109 std::string lowered = ToLower(value);
110 lowered.erase(std::remove(lowered.begin(), lowered.end(), ' '), lowered.end());
111 if (lowered == "upload")
112 res = STG::Tariff::TRAFF_UP;
113 else if (lowered == "download")
114 res = STG::Tariff::TRAFF_DOWN;
115 else if (lowered == "upload+download")
116 res = STG::Tariff::TRAFF_UP_DOWN;
117 else if (lowered.substr(0, 3) == "max")
118 res = STG::Tariff::TRAFF_MAX;
120 throw SGCONF::ACTION::ERROR("Traff type should be 'upload', 'download', 'upload + download' or 'max'. Got: '" + value + "'");
123 STG::DirPriceDataOpt ConvTimeSpan(const std::string & value)
125 size_t dashPos = value.find_first_of('-');
126 if (dashPos == std::string::npos)
127 throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
128 size_t fromColon = value.find_first_of(':');
129 if (fromColon == std::string::npos || fromColon > dashPos)
130 throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
131 size_t toColon = value.find_first_of(':', dashPos);
132 if (toColon == std::string::npos)
133 throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
134 STG::DirPriceDataOpt res;
135 res.hDay = FromString<int>(value.substr(0, fromColon));
136 if (res.hDay.value() < 0 || res.hDay.value() > 23)
137 throw SGCONF::ACTION::ERROR("Invalid 'from' hours. Got: '" + value.substr(0, fromColon) + "'");
138 res.mDay = FromString<int>(value.substr(fromColon + 1, dashPos - fromColon - 1));
139 if (res.mDay.value() < 0 || res.mDay.value() > 59)
140 throw SGCONF::ACTION::ERROR("Invalid 'from' minutes. Got: '" + value.substr(fromColon + 1, dashPos - fromColon - 1) + "'");
141 res.hNight = FromString<int>(value.substr(dashPos + 1, toColon - dashPos - 1));
142 if (res.hNight.value() < 0 || res.hNight.value() > 23)
143 throw SGCONF::ACTION::ERROR("Invalid 'to' hours. Got: '" + value.substr(dashPos + 1, toColon - dashPos - 1) + "'");
144 res.mNight = FromString<int>(value.substr(toColon + 1, value.length() - toColon));
145 if (res.mNight.value() < 0 || res.mNight.value() > 59)
146 throw SGCONF::ACTION::ERROR("Invalid 'to' minutes. Got: '" + value.substr(toColon + 1, value.length() - toColon) + "'");
150 void splice(std::vector<STG::DirPriceDataOpt> & lhs, const std::vector<STG::DirPriceDataOpt> & rhs)
152 for (size_t i = 0; i < lhs.size() && i < rhs.size(); ++i)
153 lhs[i].splice(rhs[i]);
156 void ConvTimes(std::string value, std::vector<STG::DirPriceDataOpt> & res)
158 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
159 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvTimeSpan));
164 using MemPtr = std::optional<double> (STG::DirPriceDataOpt::*);
165 ConvPrice(MemPtr before, MemPtr after)
166 : m_before(before), m_after(after)
169 STG::DirPriceDataOpt operator()(const std::string & value)
171 STG::DirPriceDataOpt res;
172 size_t slashPos = value.find_first_of('/');
173 if (slashPos == std::string::npos)
176 if (str2x(value, price) < 0)
177 throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value + "'");
178 (res.*m_before) = (res.*m_after) = price;
179 res.noDiscount = true;
184 if (str2x(value.substr(0, slashPos), price) < 0)
185 throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value.substr(0, slashPos) + "'");
186 (res.*m_before) = price;
187 if (str2x(value.substr(slashPos + 1, value.length() - slashPos), price) < 0)
188 throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value.substr(slashPos + 1, value.length() - slashPos) + "'");
189 (res.*m_after) = price;
190 res.noDiscount = false;
199 void ConvDayPrices(std::string value, std::vector<STG::DirPriceDataOpt> & res)
201 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
202 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvPrice(&STG::DirPriceDataOpt::priceDayA, &STG::DirPriceDataOpt::priceDayB)));
205 void ConvNightPrices(std::string value, std::vector<STG::DirPriceDataOpt> & res)
207 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
208 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvPrice(&STG::DirPriceDataOpt::priceNightA, &STG::DirPriceDataOpt::priceNightB)));
211 STG::DirPriceDataOpt ConvThreshold(std::string value)
213 STG::DirPriceDataOpt res;
214 double threshold = 0;
215 if (str2x(value, threshold) < 0)
216 throw SGCONF::ACTION::ERROR("Threshold should be a floating point value. Got: '" + value + "'");
217 res.threshold = threshold;
221 void ConvThresholds(std::string value, std::vector<STG::DirPriceDataOpt> & res)
223 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
224 splice(res, Split<std::vector<STG::DirPriceDataOpt> >(value, ',', ConvThreshold));
227 std::string TimeToString(int h, int m)
229 std::ostringstream stream;
230 stream << (h < 10 ? "0" : "") << h << ":"
231 << (m < 10 ? "0" : "") << m;
235 void PrintDirPriceData(size_t dir, const STG::DirPriceData & data, size_t level)
237 std::string night = TimeToString(data.hNight, data.mNight);
238 std::string day = TimeToString(data.hDay, data.mDay);
239 std::cout << Indent(level, true) << "dir: " << dir << "\n"
240 << Indent(level) << "'" << night << "' - '" << day << "': " << data.priceDayA << "/" << data.priceDayB << "\n"
241 << Indent(level) << "'" << day << "' - '" << night << "': " << data.priceNightA << "/" << data.priceNightB << "\n"
242 << Indent(level) << "threshold: " << data.threshold << "\n"
243 << Indent(level) << "single price: " << (data.singlePrice ? "yes" : "no") << "\n"
244 << Indent(level) << "discount: " << (data.noDiscount ? "no" : "yes") << "\n"; // Attention!
247 void PrintTariffConf(const STG::TariffConf & conf, size_t level)
249 std::cout << Indent(level, true) << "name: " << conf.name << "\n"
250 << Indent(level) << "fee: " << conf.fee << "\n"
251 << Indent(level) << "free mb: " << conf.free << "\n"
252 << Indent(level) << "passive cost: " << conf.passiveCost << "\n"
253 << Indent(level) << "traff type: " << TraffTypeToString(conf.traffType) << "\n"
254 << Indent(level) << "period: " << PeriodToString(conf.period) << "\n"
255 << Indent(level) << "change policy: " << ChangePolicyToString(conf.changePolicy) << "\n"
256 << Indent(level) << "change policy timeout: " << formatTime(conf.changePolicyTimeout) << "\n";
259 void PrintTariff(const STG::GetTariff::Info & info, size_t level = 0)
261 PrintTariffConf(info.tariffConf, level);
262 std::cout << Indent(level) << "dir prices:\n";
263 for (size_t i = 0; i < info.dirPrice.size(); ++i)
264 PrintDirPriceData(i, info.dirPrice[i], level + 1);
267 std::vector<SGCONF::API_ACTION::PARAM> GetTariffParams()
269 std::vector<SGCONF::API_ACTION::PARAM> params;
270 params.push_back(SGCONF::API_ACTION::PARAM("fee", "<fee>", "\t\ttariff fee"));
271 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traffic"));
272 params.push_back(SGCONF::API_ACTION::PARAM("passive-cost", "<cost>", "\tpassive cost"));
273 params.push_back(SGCONF::API_ACTION::PARAM("traff-type", "<type>", "\ttraffic type (up, down, up+down, max)"));
274 params.push_back(SGCONF::API_ACTION::PARAM("period", "<period>", "\ttarification period (daily, monthly)"));
275 params.push_back(SGCONF::API_ACTION::PARAM("change-policy", "<policy>", "tariff change policy (allow, to_cheap, to_expensive, deny)"));
276 params.push_back(SGCONF::API_ACTION::PARAM("change-policy-timeout", "<yyyy-mm-dd hh:mm:ss>", "tariff change policy timeout"));
277 params.push_back(SGCONF::API_ACTION::PARAM("times", "<hh:mm-hh:mm, ...>", "coma-separated day time-spans for each direction"));
278 params.push_back(SGCONF::API_ACTION::PARAM("day-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
279 params.push_back(SGCONF::API_ACTION::PARAM("night-prices", "<price/price, ...>", "coma-separated night prices for each direction"));
280 params.push_back(SGCONF::API_ACTION::PARAM("thresholds", "<threshold, ...>", "coma-separated thresholds for each direction"));
284 void SimpleCallback(bool result,
285 const std::string & reason,
290 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
293 std::cout << "Success.\n";
296 void GetTariffsCallback(bool result,
297 const std::string & reason,
298 const std::vector<STG::GetTariff::Info> & info,
303 std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
306 std::cout << "Tariffs:\n";
307 for (size_t i = 0; i < info.size(); ++i)
308 PrintTariff(info[i], 1);
311 void GetTariffCallback(bool result,
312 const std::string & reason,
313 const std::vector<STG::GetTariff::Info> & info,
316 assert(data != NULL && "Expecting pointer to std::string with the tariff's name.");
317 const std::string & name = *static_cast<const std::string *>(data);
320 std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
323 for (size_t i = 0; i < info.size(); ++i)
324 if (info[i].tariffConf.name == name)
325 PrintTariff(info[i]);
328 bool GetTariffsFunction(const SGCONF::CONFIG & config,
329 const std::string & /*arg*/,
330 const std::map<std::string, std::string> & /*options*/)
332 return makeProto(config).GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
335 bool GetTariffFunction(const SGCONF::CONFIG & config,
336 const std::string & arg,
337 const std::map<std::string, std::string> & /*options*/)
339 // STG currently doesn't support <GetTariff name="..."/>.
340 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
341 std::string name(arg);
342 return makeProto(config).GetTariffs(GetTariffCallback, &name) == STG::st_ok;
345 bool DelTariffFunction(const SGCONF::CONFIG & config,
346 const std::string & arg,
347 const std::map<std::string, std::string> & /*options*/)
349 return makeProto(config).DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
352 bool AddTariffFunction(const SGCONF::CONFIG & config,
353 const std::string & arg,
354 const std::map<std::string, std::string> & options)
356 STG::TariffDataOpt conf;
357 conf.tariffConf.name = arg;
358 SGCONF::MaybeSet(options, "fee", conf.tariffConf.fee);
359 SGCONF::MaybeSet(options, "free", conf.tariffConf.free);
360 SGCONF::MaybeSet(options, "passive-cost", conf.tariffConf.passiveCost);
361 SGCONF::MaybeSet(options, "traff-type", conf.tariffConf.traffType, ConvTraffType);
362 SGCONF::MaybeSet(options, "period", conf.tariffConf.period, ConvPeriod);
363 SGCONF::MaybeSet(options, "change-policy", conf.tariffConf.changePolicy, ConvChangePolicy);
364 SGCONF::MaybeSet(options, "change-policy-timeout", conf.tariffConf.changePolicyTimeout, ConvChangePolicyTimeout);
365 SGCONF::MaybeSet(options, "times", conf.dirPrice, ConvTimes);
366 SGCONF::MaybeSet(options, "day-prices", conf.dirPrice, ConvDayPrices);
367 SGCONF::MaybeSet(options, "night-prices", conf.dirPrice, ConvNightPrices);
368 SGCONF::MaybeSet(options, "thresholds", conf.dirPrice, ConvThresholds);
369 for (size_t i = 0; i < conf.dirPrice.size(); ++i)
371 if (conf.dirPrice[i].priceDayA ||
372 conf.dirPrice[i].priceNightA ||
373 conf.dirPrice[i].priceDayB ||
374 conf.dirPrice[i].priceNightB)
375 conf.dirPrice[i].singlePrice = conf.dirPrice[i].priceDayA.value() == conf.dirPrice[i].priceNightA.value() &&
376 conf.dirPrice[i].priceDayB.value() == conf.dirPrice[i].priceNightB.value();
378 return makeProto(config).AddTariff(arg, conf, SimpleCallback, NULL) == STG::st_ok;
381 bool ChgTariffFunction(const SGCONF::CONFIG & config,
382 const std::string & arg,
383 const std::map<std::string, std::string> & options)
385 STG::TariffDataOpt conf;
386 conf.tariffConf.name = arg;
387 SGCONF::MaybeSet(options, "fee", conf.tariffConf.fee);
388 SGCONF::MaybeSet(options, "free", conf.tariffConf.free);
389 SGCONF::MaybeSet(options, "passive-cost", conf.tariffConf.passiveCost);
390 SGCONF::MaybeSet(options, "traff-type", conf.tariffConf.traffType, ConvTraffType);
391 SGCONF::MaybeSet(options, "period", conf.tariffConf.period, ConvPeriod);
392 SGCONF::MaybeSet(options, "change-policy", conf.tariffConf.changePolicy, ConvChangePolicy);
393 SGCONF::MaybeSet(options, "change-policy-timeout", conf.tariffConf.changePolicyTimeout, ConvChangePolicyTimeout);
394 SGCONF::MaybeSet(options, "times", conf.dirPrice, ConvTimes);
395 SGCONF::MaybeSet(options, "day-prices", conf.dirPrice, ConvDayPrices);
396 SGCONF::MaybeSet(options, "night-prices", conf.dirPrice, ConvNightPrices);
397 SGCONF::MaybeSet(options, "thresholds", conf.dirPrice, ConvThresholds);
398 for (size_t i = 0; i < conf.dirPrice.size(); ++i)
400 if (conf.dirPrice[i].priceDayA ||
401 conf.dirPrice[i].priceNightA ||
402 conf.dirPrice[i].priceDayB ||
403 conf.dirPrice[i].priceNightB)
404 conf.dirPrice[i].singlePrice = conf.dirPrice[i].priceDayA.value() == conf.dirPrice[i].priceNightA.value() &&
405 conf.dirPrice[i].priceDayB.value() == conf.dirPrice[i].priceNightB.value();
407 return makeProto(config).ChgTariff(conf, SimpleCallback, NULL) == STG::st_ok;
410 } // namespace anonymous
412 void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
414 std::vector<API_ACTION::PARAM> params(GetTariffParams());
415 blocks.Add("Tariff management options")
416 .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list")
417 .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", GetTariffFunction), "get tariff")
418 .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, AddTariffFunction), "add tariff")
419 .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", DelTariffFunction), "delete tariff")
420 .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, ChgTariffFunction), "change tariff");