DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
transfer_metadata.hpp
Go to the documentation of this file.
1
10#ifndef SNBMODULES_INCLUDE_SNBMODULES_TRANSFER_METADATA_HPP_
11#define SNBMODULES_INCLUDE_SNBMODULES_TRANSFER_METADATA_HPP_
12
17
18#include <chrono>
19#include <ctime>
20#include <filesystem>
21#include <iostream>
22#include <map>
23#include <stdexcept>
24#include <string>
25#include <utility>
26
27namespace dunedaq::snbmodules {
29{
30
31public:
32 static const std::string m_file_extension;
33
37 virtual std::string export_to_string_partial(bool force_all);
38
39 // Overriden methods
40 std::string export_to_string() override { return export_to_string_partial(true); }
41 void from_string(const std::string&) override;
42
43 // Used to export the metadata to a json file
44 void generate_metadata_file(std::filesystem::path dest) override;
45 void load_metadata_from_meta_file(std::filesystem::path src) override;
46
47 bool operator==(MetadataAbstract const& other) const override
48 {
49 auto o = dynamic_cast<const TransferMetadata&>(other);
50 return m_file_path == o.m_file_path && m_src == o.m_src && m_dest == o.m_dest && m_group_id == o.m_group_id;
51 }
52
56 bool operator<(MetadataAbstract const& other) const override
57 {
58 auto o = dynamic_cast<const TransferMetadata&>(other);
59 return m_file_path.string().compare(o.m_file_path.string());
60 }
61
70 TransferMetadata(const std::filesystem::path& file_path,
71 uint64_t bytes_size,
72 const IPFormat& src,
73 const std::string& hash = "",
74 const IPFormat& dest = IPFormat(),
75 const std::string& group_id = "",
76 uint64_t bytes_transferred = 0,
78 : m_hash(hash)
79 , m_bytes_size(bytes_size)
80 , m_bytes_transferred(bytes_transferred)
81 , m_status(status)
82 , m_src(src)
83 , m_dest(dest)
84 , m_group_id(group_id)
85 , m_modified_fields({ { "hash", true },
86 { "size", true },
87 { "bytes_transferred", true },
88 { "transmission_speed", true },
89 { "status", true },
90 { "magnet_link", true },
91 { "error_code", true },
92 { "start_time", true },
93 { "end_time", true },
94 { "duration", true } })
95 {
96 // remove all occurences of ./ in the file path
97 std::string file_path_str = file_path.string();
98 std::string x = "./";
99
100 size_t pos = 0;
101 while (true) {
102 pos = file_path_str.find(x, pos);
103 if (pos == std::string::npos) {
104 break;
105 }
106
107 file_path_str.replace(pos, x.length(), "");
108 }
109
110 m_file_path = std::filesystem::absolute(file_path_str);
111 }
112
114 explicit TransferMetadata(const std::filesystem::path& src, bool is_path = true)
115 {
116 if (is_path) {
118 } else {
119 from_string(src.string());
120 }
121 }
122
123 virtual ~TransferMetadata() {}
124
125 // Setters
126 inline void set_file_path(const std::filesystem::path& file_path)
127 {
128 // remove all occurences of ./ in the file path
129 std::string file_path_str = file_path.string();
130 std::string x = "./";
131
132 size_t pos = 0;
133 while (true) {
134 pos = file_path_str.find(x, pos);
135 if (pos == std::string::npos) {
136 break;
137 }
138
139 file_path_str.replace(pos, x.length(), "");
140 }
141 m_file_path = std::filesystem::absolute(file_path_str);
142 }
143 inline void set_group_id(std::string group_id) { m_group_id = std::move(group_id); }
144 void set_src(const IPFormat& source)
145 {
146 // Check if the source is not equal to the dest
147 if (!(source == m_dest)) {
148 m_src = source;
149 } else {
150 throw std::invalid_argument("The source cannot be equal to the destination");
151 }
152 }
153
154 void set_dest(const IPFormat& dest)
155 {
156 // Check if the dest is not equal to the source
157 if (!(dest == m_src)) {
158 m_dest = dest;
159 } else {
160 throw std::invalid_argument("The destination cannot be equal to the source");
161 }
162 }
163
164 inline void set_hash(std::string hash)
165 {
166 m_hash = std::move(hash);
167 m_modified_fields["hash"] = true;
168 }
169
170 inline void set_size(uint64_t size)
171 {
173 m_modified_fields["size"] = true;
174 }
175
176 inline void set_bytes_transferred(uint64_t bytes_transferred)
177 {
178 m_bytes_transferred = bytes_transferred;
179 m_modified_fields["bytes_transferred"] = true;
180 }
181
182 void set_progress(int purcent)
183 {
184 if (purcent < 0 || purcent > 100) {
185 throw std::invalid_argument("The progress must be between 0 and 100");
186 }
187 set_bytes_transferred((purcent * m_bytes_size) / 100);
188 }
189
191 {
192 m_status = status;
195 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
196 .count();
197 }
199 m_end_time =
200 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
201 .count();
203 }
204 if (status == status_type::e_status::PAUSED) {
205 m_duration +=
206 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
207 .count() -
209 }
210
211 m_modified_fields["status"] = true;
212 }
213
214 inline void set_magnet_link(std::string magnet_link)
215 {
216 m_magnet_link = std::move(magnet_link);
217 m_modified_fields["magnet_link"] = true;
218 }
219
220 inline void set_error_code(std::string error_code)
221 {
222 m_error_code = std::move(error_code);
223 m_modified_fields["error_code"] = true;
224 }
225
226 inline void set_transmission_speed(int32_t transmission_speed)
227 {
228 m_transmission_speed = transmission_speed;
229 m_modified_fields["transmission_speed"] = true;
230 }
231
232 inline void set_duration(int64_t duration)
233 {
234 m_duration = duration;
235 m_modified_fields["duration"] = true;
236 }
237
238 inline void set_start_time(int64_t start_time)
239 {
241 m_modified_fields["start_time"] = true;
242 }
243
244 inline void set_end_time(int64_t end_time)
245 {
246 m_end_time = end_time;
247 m_modified_fields["end_time"] = true;
248 }
249
250 // Getters
251 inline std::filesystem::path get_file_path() const { return m_file_path; }
252 inline std::string get_file_name() const { return m_file_path.filename().string(); }
253 inline std::string get_hash() const { return m_hash; }
254 inline IPFormat get_src() const { return m_src; }
255 inline IPFormat get_dest() const { return m_dest; }
256 inline uint64_t get_size() const { return m_bytes_size; }
257 inline uint64_t get_bytes_transferred() const { return m_bytes_transferred; }
258 inline status_type::e_status get_status() const { return m_status; }
259 inline std::string get_magnet_link() const { return m_magnet_link; }
260 inline std::string get_group_id() const { return m_group_id; }
261 inline int get_progress() const
262 {
263 return m_bytes_size == 0 ? 0 : static_cast<int>(m_bytes_transferred * 100 / m_bytes_size);
264 }
265 inline std::string get_error_code() const { return m_error_code; }
266 inline int32_t get_transmission_speed() const { return m_transmission_speed; }
267 int64_t get_total_duration_ms() const
268 {
269 if (m_start_time == 0) {
270 return 0;
271 } else if (m_end_time == 0) {
272 return m_duration +
273 (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
274 .count() -
276 } else {
277 return m_end_time - m_start_time;
278 }
279 }
280 inline int64_t get_start_time() const { return m_start_time; }
281 inline int64_t get_end_time() const { return m_end_time; }
282 std::string get_start_time_str() const
283 {
284 if (m_start_time == 0) {
285 return "N/A";
286 }
287 std::time_t t = m_start_time / 1000;
288 std::tm tm = *std::localtime(&t);
289 std::stringstream ss;
290 ss << std::put_time(&tm, "%d-%m-%Y %H:%M:%S");
291 return ss.str();
292 }
293 std::string get_end_time_str() const
294 {
295 if (m_end_time == 0) {
296 return "N/A";
297 }
298 std::time_t t = m_end_time / 1000;
299 std::tm tm = *std::localtime(&t);
300 std::stringstream ss;
301 ss << std::put_time(&tm, "%d-%m-%Y %H:%M:%S");
302 return ss.str();
303 }
304
305private:
307 std::filesystem::path m_file_path = "";
308
310 std::string m_hash = "";
311
313 uint64_t m_bytes_size = 0;
314
317
320
323
326
329
331 std::string m_magnet_link = "";
332
334 std::string m_group_id = "";
335
337 std::string m_error_code = "";
338
340 int64_t m_start_time = 0;
341
343 int64_t m_end_time = 0;
344
346 int64_t m_duration = 0;
347
349 std::map<std::string, bool> m_modified_fields = {
350 { "file_path", false }, { "hash", false }, { "bytes_size", false },
351 { "bytes_transferred", false }, { "status", false }, { "magnet_link", false },
352 { "group_id", false }, { "error_code", false }, { "transmission_speed", false },
353 { "start_time", false }, { "end_time", false }, { "duration", false }
354 };
355};
356
357} // namespace dunedaq::snbmodules
358#endif // SNBMODULES_INCLUDE_SNBMODULES_TRANSFER_METADATA_HPP_
Class that represents an IP address and a port TODO: should be replaced by something better ?
Definition ip_format.hpp:26
Abstract class for metadata classes, they must be able to generate and load metadata files.
void set_error_code(std::string error_code)
TransferMetadata(const std::filesystem::path &file_path, uint64_t bytes_size, const IPFormat &src, const std::string &hash="", const IPFormat &dest=IPFormat(), const std::string &group_id="", uint64_t bytes_transferred=0, status_type::e_status status=status_type::e_status::WAITING)
Constructor.
status_type::e_status get_status() const
IPFormat m_dest
Destination of the file transfer, not always initialized.
std::map< std::string, bool > m_modified_fields
Vector of modified fields in order : to send only the modified fields.
std::string m_magnet_link
Magnet link of the file only for bit torrent.
int64_t m_duration
Duration of the transfer.
virtual std::string export_to_string_partial(bool force_all)
Export only the modified fields to a string.
void from_string(const std::string &) override
Import metadata from string (json format)
bool operator==(MetadataAbstract const &other) const override
Operator ==.
std::filesystem::path get_file_path() const
status_type::e_status m_status
Status of the file transfer.
void set_transmission_speed(int32_t transmission_speed)
bool operator<(MetadataAbstract const &other) const override
Operator < overload.
void generate_metadata_file(std::filesystem::path dest) override
Generaete metadata file to dest.
std::string m_hash
TODO : Hash of the file sha1.
uint64_t m_bytes_size
Total size of the file in bytes.
void set_bytes_transferred(uint64_t bytes_transferred)
std::string m_error_code
show the error exception in case of error
TransferMetadata(const std::filesystem::path &src, bool is_path=true)
Load from file constructor.
void set_magnet_link(std::string magnet_link)
uint64_t m_bytes_transferred
Number of bytes transferred or received.
int32_t m_transmission_speed
Transmission speed in bytes/s.
int64_t m_start_time
Start time of the transfer, 0 if not started, reset if resumed.
std::string m_group_id
Group id of the file, used to group files together.
std::string export_to_string() override
Export metadata to string (json format)
void set_file_path(const std::filesystem::path &file_path)
void set_status(status_type::e_status status)
int64_t m_end_time
End time of the transfer, 0 if not finished.
IPFormat m_src
Source of the file transfer.
std::filesystem::path m_file_path
Path of the file on the src filesystem.
void load_metadata_from_meta_file(std::filesystem::path src) override
Load metadata file from src.
FELIX Initialization std::string initerror FELIX queue timed std::string queuename Unexpected chunk size
Cannot add TPSet with start_time
e_status
Different type of session status Need to be sorted by priority (highest last)