Line data Source code
1 : /**
2 : * @file check_action_plans.cxx
3 : *
4 : * Run the DAQModuleManager ActionPlan validation on each app in the given session
5 : *
6 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
7 : * Licensing/copyright details are in the COPYING file that you should have
8 : * received with this code.
9 : */
10 :
11 : #include "logging/Logging.hpp"
12 :
13 : #include "DAQModuleManager.hpp"
14 : #include "appfwk/ConfigurationManager.hpp"
15 : #include "appfwk/DAQModule.hpp"
16 : #include "appfwk/ValidationReport.hpp"
17 :
18 : #include "conffwk/Configuration.hpp"
19 : #include "confmodel/DaqModulesGroup.hpp"
20 : #include "confmodel/DaqModulesGroupById.hpp"
21 : #include "confmodel/DaqModulesGroupByType.hpp"
22 : #include "confmodel/Session.hpp"
23 : #include "opmonlib/TestOpMonManager.hpp"
24 :
25 : #include <iostream>
26 : #include <map>
27 : #include <memory>
28 : #include <set>
29 : #include <string>
30 : #include <vector>
31 :
32 : using namespace dunedaq;
33 :
34 : std::string red = ""; // NOLINT
35 : std::string green = ""; // NOLINT
36 : std::string yellow = ""; // NOLINT
37 : std::string blue = ""; // NOLINT
38 : std::string clear = ""; // NOLINT
39 :
40 : std::string
41 0 : severity_color(appfwk::ValidationReport report)
42 : {
43 0 : switch (report.get_severity()) {
44 0 : case appfwk::ValidationReport::Severity::Fatal:
45 0 : return red;
46 0 : case appfwk::ValidationReport::Severity::Error:
47 0 : return red;
48 0 : case appfwk::ValidationReport::Severity::Warning:
49 0 : return yellow;
50 0 : case appfwk::ValidationReport::Severity::Info:
51 0 : return blue;
52 0 : case appfwk::ValidationReport::Severity::Ignored:
53 0 : return green;
54 : }
55 0 : return clear;
56 : }
57 :
58 : int
59 0 : main(int argc, char* argv[])
60 : {
61 0 : if (argc < 3) {
62 0 : std::cout << "Usage: " << argv[0] << " <session> <database-file> [spacing=2] [nocolor]\n"; // NOLINT
63 0 : return 0;
64 : }
65 :
66 0 : std::string sessionName(argv[1]); // NOLINT
67 0 : std::string dbfile(argv[2]); // NOLINT
68 0 : size_t minimum_space = 2; // Minimum space between columns (must be >= 1)
69 0 : if (argc >= 4) {
70 0 : minimum_space = std::atoi(argv[3]); // NOLINT
71 0 : if (minimum_space < 1) {
72 : minimum_space = 1;
73 : }
74 0 : if (minimum_space > 10) {
75 : minimum_space = 10;
76 : }
77 : }
78 :
79 0 : if (argc < 5) {
80 0 : red = "\033[31m";
81 0 : green = "\033[32m";
82 0 : yellow = "\033[33m";
83 0 : blue = "\033[36m";
84 0 : clear = "\033[0m";
85 : }
86 :
87 0 : if (dbfile.find(":") == std::string::npos) {
88 0 : dbfile = "oksconflibs:" + dbfile;
89 : }
90 :
91 0 : logging::Logging::setup("test", "validate_plans");
92 :
93 0 : conffwk::Configuration* confdb = nullptr;
94 0 : try {
95 0 : confdb = new conffwk::Configuration(dbfile);
96 0 : } catch (conffwk::Generic& exc) {
97 0 : std::cout << "Failed to load OKS database: " << exc << std::endl; // NOLINT
98 0 : return 0;
99 0 : }
100 :
101 0 : auto session = confdb->get<confmodel::Session>(sessionName);
102 0 : if (session == nullptr) {
103 0 : std::cout << "Failed to get Session " << sessionName << " from database\n"; // NOLINT
104 : return 0;
105 : }
106 :
107 0 : auto apps = session->enabled_applications();
108 :
109 0 : std::vector<appfwk::ValidationReport> validation_errors;
110 :
111 0 : for (auto& app : apps) {
112 :
113 0 : TLOG() << app->UID() << ": Initializing ConfigurationManager to check for duplicate ActionPlans";
114 0 : auto cfgMgr = std::make_shared<appfwk::ConfigurationManager>(dbfile, app->UID(), sessionName);
115 0 : auto cfgMgr_reports = cfgMgr->initialize(false);
116 0 : validation_errors.insert(validation_errors.end(),
117 : std::make_move_iterator(cfgMgr_reports.begin()),
118 : std::make_move_iterator(cfgMgr_reports.end()));
119 :
120 : // Check module matching
121 0 : TLOG() << app->UID() << ": Constructing modules so they can register their commands";
122 0 : auto modules = cfgMgr->get_modules();
123 0 : appfwk::DAQModuleManager mmgr(sessionName);
124 0 : mmgr.set_config_mgr(cfgMgr);
125 0 : mmgr.construct_modules(modules);
126 :
127 :
128 0 : TLOG() << app->UID() << ": Validating Action Plans";
129 0 : auto ap_reports = mmgr.validate_action_plans(false);
130 0 : validation_errors.insert(validation_errors.end(),
131 : std::make_move_iterator(ap_reports.begin()), std::make_move_iterator(ap_reports.end()));
132 0 : }
133 :
134 0 : std::cout << std::endl << std::endl << "Summary:" << std::endl; // NOLINT
135 :
136 0 : if (validation_errors.size() > 0) {
137 0 : size_t longest_app = 11; // Application heading
138 0 : size_t longest_command = 7; // Command heading
139 0 : size_t longest_module = 6; // Module heading
140 0 : size_t longest_severity = 8; // Severity heading
141 :
142 0 : for (auto& report : validation_errors) {
143 0 : if (report.get_app().size() > longest_app)
144 0 : longest_app = report.get_app().size();
145 0 : if (report.get_command().size() > longest_command)
146 0 : longest_command = report.get_command().size();
147 0 : if (report.get_module().size() > longest_module)
148 0 : longest_module = report.get_module().size();
149 : }
150 :
151 0 : std::string app_heading_space(longest_app - 11 + minimum_space, ' ');
152 0 : std::string command_heading_space(longest_command - 7 + minimum_space, ' ');
153 0 : std::string module_heading_space(longest_module - 6 + minimum_space, ' ');
154 0 : std::string severity_heading_space(longest_severity - 8 + minimum_space, ' ');
155 : // NOLINTNEXTLINE
156 0 : std::cout << "Application" << app_heading_space << "Command" << command_heading_space << "Module"
157 0 : << module_heading_space << "Severity" << severity_heading_space << "Message" << std::endl;
158 :
159 0 : for (auto& report : validation_errors) {
160 :
161 0 : std::cout << severity_color(report); // NOLINT
162 0 : std::string app_space(longest_app - report.get_app().size() + minimum_space, ' ');
163 0 : std::cout << report.get_app() << app_space; // NOLINT
164 0 : std::string command_space(longest_command - report.get_command().size() + minimum_space, ' ');
165 0 : std::cout << report.get_command() << command_space; // NOLINT
166 0 : std::string module_space(longest_module - report.get_module().size() + minimum_space, ' ');
167 0 : std::cout << report.get_module() << module_space; // NOLINT
168 0 : std::string severity_space(longest_severity - report.severity_string().size() + minimum_space, ' ');
169 0 : std::cout << report.severity_string() << severity_space; // NOLINT
170 0 : std::cout << report.get_message() << clear << std::endl; // NOLINT
171 0 : }
172 0 : } else {
173 0 : std::cout << "No validation errors encountered!" << std::endl; // NOLINT
174 : }
175 0 : } // NOLINT
|