]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/tables.cpp
Another `stgTime` case.
[stg.git] / projects / stargazer / plugins / other / smux / tables.cpp
1 #include <cassert>
2 #include <utility>
3 #include <iterator>
4 #include <algorithm>
5
6 #include "stg/user_property.h"
7 #include "stg/tariffs.h"
8 #include "stg/tariff_conf.h"
9 #include "stg/users.h"
10
11 #include "tables.h"
12
13 std::pair<std::string, size_t> TD2Info(const STG::TariffData & td);
14
15 void TariffUsersTable::UpdateSensors(Sensors & sensors) const
16 {
17 std::map<std::string, size_t> data;
18
19 std::vector<STG::TariffData> tdl;
20 tariffs.GetTariffsData(&tdl);
21 std::transform(tdl.begin(),
22                tdl.end(),
23                std::inserter(data, data.begin()),
24                TD2Info);
25
26 int handle = users.OpenSearch();
27 assert(handle && "USERS::OpenSearch is always correct");
28
29 STG::User* user;
30 while (!users.SearchNext(handle, &user))
31     {
32     if (user->GetDeleted())
33         continue;
34     std::string tariffName(user->GetProperties().tariffName.ConstData());
35     std::map<std::string, size_t>::iterator it(data.lower_bound(tariffName));
36     if (it == data.end() ||
37         it->first != tariffName)
38         {
39         data.insert(it, std::make_pair(tariffName, 1));
40         }
41     else
42         {
43         ++it->second;
44         }
45     }
46
47 users.CloseSearch(handle);
48
49 size_t idx = 1;
50 OID prefixOid(prefix);
51
52 std::map<std::string, size_t>::const_iterator it(data.begin());
53 while (it != data.end())
54     {
55     sensors[prefixOid.copyWithSuffix(2, static_cast<unsigned int>(idx))] = new ConstSensor<std::string>(it->first);
56     sensors[prefixOid.copyWithSuffix(3, static_cast<unsigned int>(idx))] = new ConstSensor<unsigned long>(it->second);
57     ++idx;
58     ++it;
59     }
60 }
61
62 std::pair<std::string, size_t> TD2Info(const STG::TariffData & td)
63 {
64 return std::make_pair(td.tariffConf.name, 0);
65 }