Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #include <string>
37 :
38 : #include <ert/tracing/Logger.hpp>
39 :
40 : #include <MockData.hpp>
41 :
42 :
43 : namespace h2agent
44 : {
45 : namespace model
46 : {
47 :
48 236 : std::shared_ptr<MockEventsHistory> MockData::getEvents(const DataKey &dataKey, bool &maiden) {
49 :
50 236 : auto it = get(dataKey.getKey());
51 236 : if (it != end()) {
52 41 : return it->second;
53 : }
54 195 : maiden = true;
55 195 : return std::make_shared<MockEventsHistory>(dataKey);
56 : }
57 :
58 22 : bool MockData::clear(bool &somethingDeleted, const EventKey &ekey)
59 : {
60 22 : bool result = true;
61 22 : somethingDeleted = false;
62 :
63 22 : if (ekey.empty()) {
64 2 : write_guard_t guard(rw_mutex_);
65 2 : somethingDeleted = (map_.size() > 0);
66 2 : map_.clear();
67 2 : return result;
68 2 : }
69 :
70 20 : if (!ekey.checkSelection())
71 7 : return false;
72 :
73 13 : write_guard_t guard(rw_mutex_);
74 :
75 13 : auto it = get(ekey.getKey());
76 13 : if (it == end())
77 5 : return true; // nothing found to be removed
78 :
79 : // Check event number:
80 8 : if (ekey.hasNumber()) {
81 6 : if (!ekey.validNumber())
82 3 : return false;
83 3 : somethingDeleted = it->second->removeEvent(ekey.getUNumber(), ekey.reverse());
84 3 : if (it->second->size() == 0) remove(it); // remove key when history is dropped (https://github.com/testillano/h2agent/issues/53).
85 : }
86 : else {
87 2 : somethingDeleted = true;
88 2 : remove(it); // remove key
89 : }
90 :
91 5 : return result;
92 13 : }
93 :
94 16 : std::string MockData::asJsonString(const EventLocationKey &elkey, bool &validQuery) const {
95 :
96 : //validQuery = false;
97 :
98 16 : if (elkey.empty()) {
99 2 : validQuery = true;
100 2 : return ((size() != 0) ? getJson().dump() : "[]"); // server data is shown as an array
101 : }
102 :
103 14 : if (!elkey.checkSelection()) {
104 2 : validQuery = false;
105 4 : return "[]";
106 : }
107 :
108 12 : validQuery = true;
109 :
110 12 : read_guard_t guard(rw_mutex_);
111 :
112 12 : auto it = get(elkey.getKey());
113 12 : if (it == end())
114 4 : return "[]"; // nothing found to be built
115 :
116 : // Check event number:
117 10 : if (elkey.hasNumber()) {
118 8 : if (!elkey.validNumber()) {
119 2 : validQuery = false;
120 4 : return "[]";
121 : }
122 :
123 6 : auto ptr = it->second->getEvent(elkey.getUNumber(), elkey.reverse());
124 6 : if (ptr) {
125 4 : return ptr->getJson(elkey.getPath()).dump();
126 : }
127 4 : else return "[]";
128 6 : }
129 : else {
130 2 : return it->second->getJson().dump();
131 : }
132 :
133 : return "[]";
134 12 : }
135 :
136 2 : std::string MockData::summary(const std::string &maxKeys) const {
137 2 : nlohmann::json result;
138 :
139 2 : result["totalKeys"] = (std::uint64_t)size();
140 :
141 2 : bool negative = false;
142 2 : std::uint64_t u_maxKeys = 0;
143 2 : if (!h2agent::model::string2uint64andSign(maxKeys, u_maxKeys, negative)) {
144 2 : u_maxKeys = std::numeric_limits<uint64_t>::max();
145 : }
146 :
147 2 : size_t totalEvents = 0;
148 2 : size_t historySize = 0;
149 2 : size_t displayedKeys = 0;
150 2 : nlohmann::json key;
151 :
152 2 : read_guard_t guard(rw_mutex_);
153 6 : for (auto it = begin(); it != end(); it++) {
154 4 : size_t historySize = it->second->size();
155 4 : totalEvents += historySize;
156 4 : if (displayedKeys >= u_maxKeys) continue;
157 :
158 4 : it->second->getKey().keyToJson(key);
159 4 : key["amount"] = (std::uint64_t)historySize;
160 4 : result["displayedKeys"]["list"].push_back(key);
161 4 : displayedKeys += 1;
162 : };
163 2 : if (displayedKeys > 0) result["displayedKeys"]["amount"] = (std::uint64_t)displayedKeys;
164 2 : result["totalEvents"] = (std::uint64_t)totalEvents;
165 :
166 4 : return result.dump();
167 2 : }
168 :
169 9 : std::shared_ptr<MockEvent> MockData::getEvent(const EventKey &ekey) const {
170 :
171 9 : if (!ekey.complete()) {
172 2 : LOGDEBUG(ert::tracing::Logger::debug("Must provide everything to address the event (data key and event number)", ERT_FILE_LOCATION));
173 2 : return nullptr;
174 : }
175 :
176 7 : LOGDEBUG(ert::tracing::Logger::debug(ekey.asString(), ERT_FILE_LOCATION));
177 :
178 7 : read_guard_t guard(rw_mutex_);
179 :
180 7 : auto it = get(ekey.getKey());
181 7 : if (it == end())
182 1 : return nullptr; // nothing found
183 :
184 6 : if (!ekey.validNumber())
185 2 : return nullptr;
186 :
187 4 : return (it->second->getEvent(ekey.getUNumber(), ekey.reverse()));
188 7 : }
189 :
190 4 : nlohmann::json MockData::getJson() const {
191 :
192 4 : nlohmann::json result;
193 :
194 4 : read_guard_t guard(rw_mutex_);
195 :
196 12 : for (auto it = begin(); it != end(); it++) {
197 8 : result.push_back(it->second->getJson());
198 : };
199 :
200 8 : return result;
201 4 : }
202 :
203 19 : bool MockData::findLastRegisteredRequestState(const DataKey &key, std::string &state) const {
204 :
205 19 : read_guard_t guard(rw_mutex_);
206 :
207 19 : auto it = get(key.getKey());
208 19 : if (it != end()) {
209 11 : state = it->second->getLastRegisteredRequestState(); // by design, a key always contains at least one history event (https://github.com/testillano/h2agent/issues/53).
210 11 : if (state.empty()) { // unprovisioned event must be understood as missing (ignore register)
211 5 : state = DEFAULT_ADMIN_PROVISION_STATE;
212 : }
213 :
214 11 : return true;
215 : }
216 :
217 8 : state = DEFAULT_ADMIN_PROVISION_STATE;
218 8 : return false;
219 19 : }
220 :
221 : }
222 : }
223 :
|