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