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 bool operator==(TransferMetadata const& o) const
53 {
54 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;
55 }
56
60 bool operator<(MetadataAbstract const& other) const override
61 {
62 auto o = dynamic_cast<const TransferMetadata&>(other);
63 return m_file_path.string().compare(o.m_file_path.string());
64 }
65
74 TransferMetadata(const std::filesystem::path& file_path,
75 uint64_t bytes_size,
76 const IPFormat& src,
77 const std::string& hash = "",
78 const IPFormat& dest = IPFormat(),
79 const std::string& group_id = "",
80 uint64_t bytes_transferred = 0,
82 : m_hash(hash)
83 , m_bytes_size(bytes_size)
84 , m_bytes_transferred(bytes_transferred)
85 , m_status(status)
86 , m_src(src)
87 , m_dest(dest)
88 , m_group_id(group_id)
89 , m_modified_fields({ { "hash", true },
90 { "size", true },
91 { "bytes_transferred", true },
92 { "transmission_speed", true },
93 { "status", true },
94 { "magnet_link", true },
95 { "error_code", true },
96 { "start_time", true },
97 { "end_time", true },
98 { "duration", true } })
99 {
100 // remove all occurences of ./ in the file path
101 std::string file_path_str = file_path.string();
102 std::string x = "./";
103
104 size_t pos = 0;
105 while (true) {
106 pos = file_path_str.find(x, pos);
107 if (pos == std::string::npos) {
108 break;
109 }
110
111 file_path_str.replace(pos, x.length(), "");
112 }
113
114 m_file_path = std::filesystem::absolute(file_path_str);
115 }
116
118 explicit TransferMetadata(const std::filesystem::path& src, bool is_path = true)
119 {
120 if (is_path) {
122 } else {
123 from_string(src.string());
124 }
125 }
126
127 virtual ~TransferMetadata() {}
128
129 // Setters
130 inline void set_file_path(const std::filesystem::path& file_path)
131 {
132 // remove all occurences of ./ in the file path
133 std::string file_path_str = file_path.string();
134 std::string x = "./";
135
136 size_t pos = 0;
137 while (true) {
138 pos = file_path_str.find(x, pos);
139 if (pos == std::string::npos) {
140 break;
141 }
142
143 file_path_str.replace(pos, x.length(), "");
144 }
145 m_file_path = std::filesystem::absolute(file_path_str);
146 }
147 inline void set_group_id(std::string group_id) { m_group_id = std::move(group_id); }
148 void set_src(const IPFormat& source)
149 {
150 // Check if the source is not equal to the dest
151 if (!(source == m_dest)) {
152 m_src = source;
153 } else {
154 throw std::invalid_argument("The source cannot be equal to the destination");
155 }
156 }
157
158 void set_dest(const IPFormat& dest)
159 {
160 // Check if the dest is not equal to the source
161 if (!(dest == m_src)) {
162 m_dest = dest;
163 } else {
164 throw std::invalid_argument("The destination cannot be equal to the source");
165 }
166 }
167
168 inline void set_hash(std::string hash)
169 {
170 m_hash = std::move(hash);
171 m_modified_fields["hash"] = true;
172 }
173
174 inline void set_size(uint64_t size)
175 {
177 m_modified_fields["size"] = true;
178 }
179
180 inline void set_bytes_transferred(uint64_t bytes_transferred)
181 {
182 m_bytes_transferred = bytes_transferred;
183 m_modified_fields["bytes_transferred"] = true;
184 }
185
186 void set_progress(int purcent)
187 {
188 if (purcent < 0 || purcent > 100) {
189 throw std::invalid_argument("The progress must be between 0 and 100");
190 }
191 set_bytes_transferred((purcent * m_bytes_size) / 100);
192 }
193
195 {
196 m_status = status;
199 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
200 .count();
201 }
203 m_end_time =
204 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
205 .count();
207 }
208 if (status == status_type::e_status::PAUSED) {
209 m_duration +=
210 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
211 .count() -
213 }
214
215 m_modified_fields["status"] = true;
216 }
217
218 inline void set_magnet_link(std::string magnet_link)
219 {
220 m_magnet_link = std::move(magnet_link);
221 m_modified_fields["magnet_link"] = true;
222 }
223
224 inline void set_error_code(std::string error_code)
225 {
226 m_error_code = std::move(error_code);
227 m_modified_fields["error_code"] = true;
228 }
229
230 inline void set_transmission_speed(int32_t transmission_speed)
231 {
232 m_transmission_speed = transmission_speed;
233 m_modified_fields["transmission_speed"] = true;
234 }
235
236 inline void set_duration(int64_t duration)
237 {
238 m_duration = duration;
239 m_modified_fields["duration"] = true;
240 }
241
242 inline void set_start_time(int64_t start_time)
243 {
245 m_modified_fields["start_time"] = true;
246 }
247
248 inline void set_end_time(int64_t end_time)
249 {
250 m_end_time = end_time;
251 m_modified_fields["end_time"] = true;
252 }
253
254 // Getters
255 inline std::filesystem::path get_file_path() const { return m_file_path; }
256 inline std::string get_file_name() const { return m_file_path.filename().string(); }
257 inline std::string get_hash() const { return m_hash; }
258 inline IPFormat get_src() const { return m_src; }
259 inline IPFormat get_dest() const { return m_dest; }
260 inline uint64_t get_size() const { return m_bytes_size; }
261 inline uint64_t get_bytes_transferred() const { return m_bytes_transferred; }
262 inline status_type::e_status get_status() const { return m_status; }
263 inline std::string get_magnet_link() const { return m_magnet_link; }
264 inline std::string get_group_id() const { return m_group_id; }
265 inline int get_progress() const
266 {
267 return m_bytes_size == 0 ? 0 : static_cast<int>(m_bytes_transferred * 100 / m_bytes_size);
268 }
269 inline std::string get_error_code() const { return m_error_code; }
270 inline int32_t get_transmission_speed() const { return m_transmission_speed; }
271 int64_t get_total_duration_ms() const
272 {
273 if (m_start_time == 0) {
274 return 0;
275 } else if (m_end_time == 0) {
276 return m_duration +
277 (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
278 .count() -
280 } else {
281 return m_end_time - m_start_time;
282 }
283 }
284 inline int64_t get_start_time() const { return m_start_time; }
285 inline int64_t get_end_time() const { return m_end_time; }
286 std::string get_start_time_str() const
287 {
288 if (m_start_time == 0) {
289 return "N/A";
290 }
291 std::time_t t = m_start_time / 1000;
292 std::tm tm = *std::localtime(&t);
293 std::stringstream ss;
294 ss << std::put_time(&tm, "%d-%m-%Y %H:%M:%S");
295 return ss.str();
296 }
297 std::string get_end_time_str() const
298 {
299 if (m_end_time == 0) {
300 return "N/A";
301 }
302 std::time_t t = m_end_time / 1000;
303 std::tm tm = *std::localtime(&t);
304 std::stringstream ss;
305 ss << std::put_time(&tm, "%d-%m-%Y %H:%M:%S");
306 return ss.str();
307 }
308
309private:
311 std::filesystem::path m_file_path = "";
312
314 std::string m_hash = "";
315
317 uint64_t m_bytes_size = 0;
318
321
324
327
330
333
335 std::string m_magnet_link = "";
336
338 std::string m_group_id = "";
339
341 std::string m_error_code = "";
342
344 int64_t m_start_time = 0;
345
347 int64_t m_end_time = 0;
348
350 int64_t m_duration = 0;
351
353 std::map<std::string, bool> m_modified_fields = {
354 { "file_path", false }, { "hash", false }, { "bytes_size", false },
355 { "bytes_transferred", false }, { "status", false }, { "magnet_link", false },
356 { "group_id", false }, { "error_code", false }, { "transmission_speed", false },
357 { "start_time", false }, { "end_time", false }, { "duration", false }
358 };
359};
360
361} // namespace dunedaq::snbmodules
362#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.
bool operator==(TransferMetadata const &o) const
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)