SRS-control 0.1.4
Loading...
Searching...
No Matches
JsonWriter.cpp
Go to the documentation of this file.
1#include "JsonWriter.hpp"
6#include <cassert>
7#include <cstddef>
8#include <cstdint>
9#include <glaze/core/opts.hpp>
10#include <glaze/core/write.hpp>
11#include <ios>
12#include <ranges>
13#include <spdlog/spdlog.h>
14#include <stdexcept>
15#include <string>
16#include <vector>
17
18namespace srs::writer
19{
21 {
22 header = data_struct.header;
23 fill_hit_data(data_struct.hit_data);
24 fill_marker_data(data_struct.marker_data);
25 }
26
27 void CompactExportData::fill_hit_data(const std::vector<HitData>& hits)
28 {
29 hit_size = hits.size();
30 for (auto& [key, value] : hit_data)
31 {
32 value.clear();
33 value.reserve(hit_size);
34 }
35 for (const auto& hit : hits)
36 {
37 hit_data.at("is_over_threshold").push_back(static_cast<uint16_t>(hit.is_over_threshold));
38 hit_data.at("channel_num").push_back(hit.channel_num);
39 hit_data.at("tdc").push_back(hit.tdc);
40 hit_data.at("offset").push_back(hit.offset);
41 hit_data.at("vmm_id").push_back(hit.vmm_id);
42 hit_data.at("adc").push_back(hit.adc);
43 hit_data.at("bc_id").push_back(hit.bc_id);
44 }
45 }
46
47 void CompactExportData::fill_marker_data(const std::vector<MarkerData>& markers)
48 {
49 marker_size = markers.size();
50 for (auto& [key, value] : marker_data)
51 {
52 value.clear();
53 value.reserve(marker_size);
54 }
55 for (const auto& marker : markers)
56 {
57 marker_data.at("vmm_id").push_back(marker.vmm_id);
58 marker_data.at("srs_timestamp").push_back(marker.srs_timestamp);
59 }
60 }
61
62 Json::Json(const std::string& filename, process::DataConvertOptions convert_mode, std::size_t n_lines)
63 : WriterTask{ "JSONWriter", convert_mode, n_lines }
64 , filename_{ filename }
65 {
66 assert(n_lines > 0);
67 is_first_item_.resize(n_lines);
68 output_data_.resize(n_lines);
69 data_buffers_.resize(n_lines);
70 string_buffers_.resize(n_lines);
71 file_streams_.reserve(n_lines);
72
73 for (auto idx : std::views::iota(0, static_cast<int>(n_lines)))
74 {
75 auto full_filename = (n_lines == 1) ? filename : common::insert_index_to_filename(filename, idx);
76 file_streams_.emplace_back(full_filename, std::ios::out | std::ios::trunc);
77 }
78
79 for (auto& file_stream : file_streams_)
80 {
81 if (not file_stream.is_open())
82 {
83 spdlog::critical("JsonWriter: cannot open the file with filename {:?}", filename);
84 throw std::runtime_error("Error occurred with JsonWriter");
85 }
86 file_stream << "[\n";
87 }
88 }
90 {
91 for (auto [idx, file_stream] : std::views::zip(std::views::iota(0), file_streams_))
92 {
93 file_stream << "]\n";
94 file_stream.close();
95 spdlog::debug("JSON file {} with index {} is closed successfully", filename_, idx);
96 }
97 spdlog::info("Writer: JSON file writer with the base name {:?} is closed successfully.", filename_);
98 }
99
100 void Json::write_json(const StructData& data_struct, std::size_t line_num)
101 {
102 if (not is_first_item_[line_num].value)
103 {
104 file_streams_[line_num] << ", ";
105 }
106 else
107 {
108 is_first_item_[line_num].value = false;
109 }
110
111 data_buffers_[line_num].set_value(data_struct);
112 auto error_code = glz::write<glz::opts{ .prettify = true, .new_lines_in_arrays = false }>(
113 data_buffers_[line_num], string_buffers_[line_num]);
114 if (error_code)
115 {
116 spdlog::critical("JsonWriter: cannot interpret data struct to json. Error: {}",
117 error_code.custom_error_message);
118 throw std::runtime_error("Error occurred with JsonWriter");
119 }
120 output_data_[line_num] = string_buffers_[line_num].size();
121 file_streams_[line_num] << string_buffers_[line_num];
122 string_buffers_[line_num].clear();
123 }
124} // namespace srs::writer
WriterTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
std::vector< Bool > is_first_item_
std::vector< std::string > string_buffers_
Json(const std::string &filename, process::DataConvertOptions convert_mode, std::size_t n_lines=1)
std::vector< CompactExportData > data_buffers_
std::vector< std::fstream > file_streams_
std::string filename_
void write_json(const StructData &data_struct, std::size_t line_num)
std::vector< OutputType > output_data_
constexpr auto insert_index_to_filename(std::string_view native_name, int idx) -> std::string
ReceiveDataHeader header
Header data.
std::vector< MarkerData > marker_data
Marker data.
std::vector< HitData > hit_data
Hit data.
void fill_hit_data(const std::vector< HitData > &hits)
std ::map< std::string, std::vector< uint64_t > > marker_data
std ::map< std::string, std::vector< uint16_t > > hit_data
void fill_marker_data(const std::vector< MarkerData > &markers)
void set_value(const StructData &data_struct)