srs-control 0.2.2
Loading...
Searching...
No Matches
BaseTask.hpp
Go to the documentation of this file.
1#pragma once
2
7#include <asio/experimental/coro.hpp>
8#include <asio/use_awaitable.hpp>
9#include <cassert>
10#include <chrono>
11#include <cstddef>
12#include <expected>
13#include <fmt/ranges.h>
14#include <gsl/gsl-lite.hpp>
15#include <spdlog/spdlog.h>
16#include <string>
17#include <string_view>
18#include <vector>
19
20namespace srs::process
21{
22
23 template <typename Input, typename Output>
25 {
26 public:
27 using InputType = Input;
28 using OutputType = Output;
29
30 using enum DataConvertOptions;
31
32 using RunResult = std::expected<OutputType, std::string_view>;
33
34 explicit BaseTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines = 1)
35 : previous_conversion_{ prev_convert }
36 , name_{ name }
37 , n_lines_{ n_lines }
38 {
39 spdlog::trace("Task: Creating {} task with {} pipelines", name_, n_lines);
41 stats_.resize(n_lines_);
42 }
43
45 {
46 if (task_report_ != nullptr)
47 {
48 task_report_->register_task_result(get_name(), stats_);
49 }
50 }
51
52 [[nodiscard]] auto get_n_lines() const -> std::size_t { return n_lines_; }
53 [[nodiscard]] auto get_required_conversion() const -> DataConvertOptions { return previous_conversion_; }
54 [[nodiscard]] auto get_name() const -> std::string_view { return name_; };
55 [[nodiscard]] auto get_name_str() const -> std::string { return std::string{ name_ }; };
56
57 void set_report(AppReport* task_report)
58 {
59 assert(task_report != nullptr);
60 task_report_ = task_report;
61 }
62
63 auto run_once(this auto&& self,
64 const OutputTo<InputType> auto& prev_data_converter,
65 std::size_t line_number = 0) -> RunResult
66 {
67 assert(line_number < self.get_n_lines());
68
69 self.last_time_points_[line_number] = self.clock_.now();
70
71 auto res = self.run(prev_data_converter, line_number);
72
73 auto time_duration = self.clock_.now() - self.last_time_points_[line_number];
74 self.stats_[line_number].total_time_ms += static_cast<double>(time_duration.count()) / 1e6;
75 ++(self.stats_[line_number].total_sample_size);
76
77 return res;
78 }
79
80 protected:
81 auto get_report() -> AppReport* { return task_report_; }
82
83 private:
86 std::vector<AppReport::TaskStat> stats_;
87 std::chrono::steady_clock clock_;
88 std::vector<std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds>> last_time_points_;
89 std::string name_ = "empty";
90 std::size_t n_lines_ = 1;
91 };
92
93 template <DataConvertOptions Conversion, typename Input, typename Output>
94 class ConverterTask : public BaseTask<Input, Output>
95 {
96 public:
97 constexpr static auto converter_type = Conversion;
98 explicit ConverterTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines = 1)
99 : BaseTask<Input, Output>{ name, prev_convert, n_lines }
100 {
101 }
102 };
103
104 template <DataWriterOption writer, typename Input, typename Output>
105 class SinkTask : public BaseTask<Input, Output>
106 {
107 public:
108 constexpr static auto writer_type = writer;
109 explicit SinkTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines = 1)
110 : BaseTask<Input, Output>{ name, prev_convert, n_lines }
111 {
112 }
113 };
114} // namespace srs::process
BaseTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
Definition BaseTask.hpp:34
auto get_n_lines() const -> std::size_t
Definition BaseTask.hpp:52
auto run_once(this auto &&self, const OutputTo< InputType > auto &prev_data_converter, std::size_t line_number=0) -> RunResult
Definition BaseTask.hpp:63
std::chrono::steady_clock clock_
Definition BaseTask.hpp:87
auto get_name_str() const -> std::string
Definition BaseTask.hpp:55
std::vector< AppReport::TaskStat > stats_
Definition BaseTask.hpp:86
process::DataConvertOptions previous_conversion_
Definition BaseTask.hpp:84
void set_report(AppReport *task_report)
Definition BaseTask.hpp:57
std::expected< OutputType, std::string_view > RunResult
Definition BaseTask.hpp:32
AppReport * task_report_
Definition BaseTask.hpp:85
auto get_required_conversion() const -> DataConvertOptions
Definition BaseTask.hpp:53
auto get_report() -> AppReport *
Definition BaseTask.hpp:81
std::vector< std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > > last_time_points_
Definition BaseTask.hpp:88
auto get_name() const -> std::string_view
Definition BaseTask.hpp:54
ConverterTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
Definition BaseTask.hpp:98
SinkTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
Definition BaseTask.hpp:109
static constexpr auto writer_type
Definition BaseTask.hpp:108