Skip to content

Instantly share code, notes, and snippets.

@Milerius
Created December 25, 2020 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Milerius/55c1ec5b275bef2f8113d94ba5e925f5 to your computer and use it in GitHub Desktop.
Save Milerius/55c1ec5b275bef2f8113d94ba5e925f5 to your computer and use it in GitHub Desktop.
const auto& events = j.at("events");
for (auto&& content: events)
{
const nlohmann::json& j_evt = content.at("event");
auto timestamp = content.at("timestamp").get<std::size_t>();
std::string human_date = atomic_dex::utils::to_human_date<std::chrono::seconds>(timestamp / 1000, "%F %H:%M:%S");
auto evt_type = j_evt.at("type").get<std::string>();
auto rate_bundler = [&event_timestamp_registry,
&total_time_in_ms](nlohmann::json& jf_evt, const std::string& event_type, const std::string& previous_event) {
if (event_timestamp_registry.count(previous_event) != 0)
{
std::int64_t ts = event_timestamp_registry.at(previous_event);
jf_evt["started_at"] = ts;
std::int64_t ts2 = jf_evt.at("timestamp").get<std::int64_t>();
date::sys_time<std::chrono::milliseconds> t1{std::chrono::milliseconds{ts}};
date::sys_time<std::chrono::milliseconds> t2{std::chrono::milliseconds{ts2}};
double res = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
jf_evt["time_diff"] = res;
event_timestamp_registry[event_type] = ts2; // Negotiated finished at this time
total_time_in_ms += res;
}
};
if (j_evt.count("data") == 0)
{
nlohmann::json jf_evt = {{"state", evt_type}, {"human_timestamp", human_date}, {"timestamp", timestamp}};
if (idx > 0)
{
rate_bundler(jf_evt, evt_type, events[idx - 1].at("event").at("type").get<std::string>());
}
events_array.push_back(jf_evt);
}
else
{
nlohmann::json jf_evt = {{"state", evt_type}, {"human_timestamp", human_date}, {"data", j_evt.at("data")}, {"timestamp", timestamp}};
if (evt_type == "Started")
{
std::int64_t ts = jf_evt.at("data").at("started_at").get<std::int64_t>() * 1000;
jf_evt["started_at"] = ts;
std::int64_t ts2 = jf_evt.at("timestamp").get<std::int64_t>();
date::sys_time<std::chrono::milliseconds> t1{std::chrono::milliseconds{ts}};
date::sys_time<std::chrono::milliseconds> t2{std::chrono::milliseconds{ts2}};
double res = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
jf_evt["time_diff"] = res;
event_timestamp_registry["Started"] = ts2; // Started finished at this time
total_time_in_ms += res;
}
if (idx > 0)
{
rate_bundler(jf_evt, evt_type, events[idx - 1].at("event").at("type").get<std::string>());
}
events_array.push_back(jf_evt);
}
idx += 1;
}
contents.events = nlohmann_json_array_to_qt_json_array(events_array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment