SOMHunter Core
logger.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef LOGGER_H_
22 #define LOGGER_H_
23 
24 #include <filesystem>
25 #include <fstream>
26 #include <memory>
27 #include <mutex>
28 #include <string>
29 #include <thread>
30 #include <vector>
31 // ---
32 #include <nlohmann/json.hpp>
33 // ---
34 #include "canvas-query-ranker.h"
35 #include "common.h"
36 #include "dataset-frames.h"
37 #include "keyword-ranker.h"
38 #include "scores.h"
39 #include "utils.hpp"
40 
41 using namespace json11;
42 
43 namespace sh {
45 class EvalServerClient;
46 class UserContext;
47 
53 class Logger {
54  // *** METHODS ***
55 public:
56  Logger() = delete;
57  Logger(const EvalServerSettings& settings, const UserContext* p_user_ctx, EvalServerClient* p_eval_server);
58  ~Logger() noexcept;
59  // ---
60 
62  void poll();
63 
64  void log_search_context_switch(std::size_t dest_context_ID, size_t src_context_ID);
65 
67  void log_submit(const VideoFrame frame, bool submit_result);
68 
70  void log_query(const LogHash& hash, const Query& query) const;
71  void log_canvas_query(const std::vector<TemporalQuery>& temp_queries, const std::vector<VideoFrame>* p_targets);
72 
73  void log_rescore(const Query& prev_query, const Query& new_query);
74 
76  void log_results(const DatasetFrames& _dataset_frames, const ScoreModel& scores, const std::set<FrameId>& likes,
77  const UsedTools& used_tools, DisplayType disp_type, const std::vector<FrameId>& topn_imgs,
78  const std::string& sentence_query, const size_t topn_frames_per_video,
79  const size_t topn_frames_per_shot, const std::vector<bool>& dataset_parts_filter = { true, true });
80 
81  void log_text_query_change(const std::string& query_sentence);
82  void log_canvas_query_change();
83 
84  void log_like(FrameId frame_ID);
85  void log_unlike(FrameId frame_ID);
86 
87  void log_bookmark(FrameId frame_ID);
88  void log_unbookmark(FrameId frame_ID);
89 
90  void log_show_som_display(const DatasetFrames& _dataset_frames, const std::vector<FrameId>& imgs);
91 
92  void log_show_som_relocation_display(const DatasetFrames& _dataset_frames, const std::vector<FrameId>& imgs);
93 
94  void log_show_random_display(const DatasetFrames& _dataset_frames, const std::vector<FrameId>& imgs);
95 
96  void log_show_topn_display(const DatasetFrames& _dataset_frames, const std::vector<FrameId>& imgs);
97 
98  void log_show_topn_context_display(const DatasetFrames& _dataset_frames, const std::vector<FrameId>& imgs);
99 
100  void log_show_topknn_display(const DatasetFrames& _dataset_frames, FrameId frame_ID,
101  const std::vector<FrameId>& imgs);
102 
103  void log_show_detail_display(const DatasetFrames& _dataset_frames, FrameId frame_ID);
104 
105  void log_show_video_replay(const DatasetFrames& _dataset_frames, FrameId frame_ID, float delta);
106 
107  void log_scroll(const DatasetFrames& _dataset_frames, DisplayType from_disp_type, float dirY);
108 
109  void log_reset_search();
110 
112  void submit_interaction_logs_buffer();
113 
115  return DebugLogStreamPtrs{ _summary_streams.emplace_back(std::stringstream{}),
116  _actions_streams.emplace_back(std::stringstream{}),
117  _results_streams.emplace_back(std::stringstream{}) };
118  };
119 
121  _summary_streams.clear();
122  _actions_streams.clear();
123  _results_streams.clear();
124  }
125 
126 private:
127  void log_bothlike(FrameId frame_ID, const std::string& type);
128 
129  LogHash gen_action_hash(UnixTimestamp ts);
130  LogHash push_action(const std::string& action_name, const std::string& cat, const std::string& type,
131  const std::string& value, nlohmann::json&& our_log_JSON = {},
132  std::initializer_list<std::string> summary_keys = {});
133 
135  void write_result(const nlohmann::json& action_log) {
136  std::stringstream ss;
137 
138  // If first time output
139  if (_first_result) {
140  _first_result = false;
141  } else {
142  ss << "," << std::endl;
143  }
144  ss << action_log.dump(4);
145 
146  _results_log_stream << ss.str();
147  for (auto&& s : _results_streams) {
148  s << ss.str();
149  }
150  }
151 
153  void write_action(const nlohmann::json& action_log) {
154  std::stringstream ss;
155 
156  // If first time output
157  if (_first_actions) {
158  _first_actions = false;
159  } else {
160  ss << "," << std::endl;
161  }
162 
163  ss << action_log.dump(4);
164 
165  _actions_log_stream << ss.str();
166  for (auto&& s : _actions_streams) {
167  s << ss.str();
168  }
169  }
170 
172  void write_summary(const nlohmann::json& log, const std::string& action_name,
173  std::initializer_list<std::string> keys = {}) {
174  auto ts{ log["metadata"]["timestamp"].get<UnixTimestamp>() };
175  auto hash{ log["metadata"]["hash"].get<std::string>() };
176 
177  std::stringstream ss;
178 
179  ss << utils::get_formated_timestamp("%H:%M:%S", ts) << "\t" << hash << "\t" << action_name << "\t";
180 
181  for (auto& [key, value] : log.items()) {
182  if (std::find(keys.begin(), keys.end(), key) == keys.end()) {
183  continue;
184  }
185  ss << key << "=" << value << "\t";
186  }
187  ss << std::endl;
188 
189  // Write to actual stream
190  _summary_log_stream << ss.str();
191 
192  for (auto&& s : _summary_streams) {
193  s << ss.str();
194  }
195  }
196  std::string get_log_dir_queries() const;
197  std::string get_results_log_filepath() const;
198  std::string get_actions_log_filepath() const;
199  std::string get_summary_log_filepath() const;
200 
201  std::lock_guard<std::mutex> get_exclusive_actions_lock() const { return std::lock_guard{ _push_action_mtx }; };
202 
203  // *** MEMBER VARIABLES ***
204 private:
208 
209  std::vector<nlohmann::json> _interactions_buffer;
211 
212  std::ofstream _results_log_stream;
213  std::ofstream _summary_log_stream;
214  std::ofstream _actions_log_stream;
215 
216  std::vector<std::stringstream> _summary_streams;
217  std::vector<std::stringstream> _actions_streams;
218  std::vector<std::stringstream> _results_streams;
219 
220  bool _first_result{ true };
221  bool _first_actions{ true };
222 
223  mutable std::mutex _push_action_mtx;
224 };
225 
226 }; // namespace sh
227 
228 #endif // LOGGER_H_
Definition: dataset-frames.h:162
Definition: eval-server-client.h:37
Definition: keyword-ranker.h:52
Class responsible for all the logging for the given user (each user have it's own Logger....
Definition: logger.h:53
void write_result(const nlohmann::json &action_log)
Writes the log into the local file.
Definition: logger.h:135
std::ofstream _results_log_stream
Definition: logger.h:212
DebugLogStreamPtrs get_debug_streams()
Definition: logger.h:114
std::vector< nlohmann::json > _interactions_buffer
Definition: logger.h:209
std::mutex _push_action_mtx
Definition: logger.h:223
void write_action(const nlohmann::json &action_log)
Writes the log into the local file.
Definition: logger.h:153
Logger()=delete
std::vector< std::stringstream > _summary_streams
Definition: logger.h:216
void clear_debug_streams()
Definition: logger.h:120
std::ofstream _summary_log_stream
Definition: logger.h:213
std::vector< std::stringstream > _actions_streams
Definition: logger.h:217
void write_summary(const nlohmann::json &log, const std::string &action_name, std::initializer_list< std::string > keys={})
Writes the log into the local file.
Definition: logger.h:172
const EvalServerSettings _logger_settings
Definition: logger.h:201
const UserContext * _p_user_ctx
Definition: logger.h:206
EvalServerClient * _p_eval_server
Definition: logger.h:207
std::lock_guard< std::mutex > get_exclusive_actions_lock() const
Definition: logger.h:201
std::ofstream _actions_log_stream
Definition: logger.h:214
std::vector< std::stringstream > _results_streams
Definition: logger.h:218
UnixTimestamp _last_interactions_submit_ts
Definition: logger.h:210
Definition: scores.h:34
Represents exactly one state of ONE user that uses this core.
Definition: user-context.h:38
std::string get_formated_timestamp(const std::string &fmt, UnixTimestamp ts=timestamp())
Returns string representing current time and date in formated string based on provided format.
Definition: utils.hpp:105
Definition: common-types.h:33
std::string LogHash
Definition: common-types.h:59
DisplayType
Definition: common-types.h:102
std::int64_t UnixTimestamp
Definition: common-types.h:54
unsigned long FrameId
Definition: common-types.h:75
Definition: common-types.h:44
Definition: settings.h:59
The type representing the whole query.
Definition: query-types.h:470
Definition: query-types.h:404
Definition: common-types.h:178
Definition: dataset-frames.h:40