otsdaq  3.06.00
ConfigurationManager.h
1 #ifndef _ots_ConfigurationManager_h_
2 #define _ots_ConfigurationManager_h_
3 
4 #include <map>
5 #include <mutex>
6 #include <set>
7 #include <string>
8 #include <thread>
9 
10 #include "artdaq-core/Utilities/TimeUtils.hh"
11 #include "otsdaq/ConfigurationInterface/ConfigurationInterface.h"
12 #include "otsdaq/ConfigurationInterface/ConfigurationTree.h"
13 #include "otsdaq/TableCore/TableVersion.h"
14 
15 namespace ots
16 {
17 class ProgressBar;
18 
19 #define __GET_CONFIG__(X) getTable<X>(QUOTE(X))
20 
22 {
25  friend class ConfigurationManagerRW;
26  friend class GatewaySupervisor;
27 
28  public:
29  typedef std::map<std::string,
30  std::pair<std::pair<std::string, TableGroupKey>,
31  std::map<std::string, TableVersion> /* memberMap */
32  >>
33  lastGroupLoad_t;
34 
35  //==============================================================================
37  static const unsigned int PROCESSOR_COUNT;
38 
39  static const std::string READONLY_USER;
40  static const std::string ACTIVE_GROUPS_FILENAME;
41  static const std::string ALIAS_VERSION_PREAMBLE;
42  static const std::string SCRATCH_VERSION_ALIAS;
43  static const std::string SUBSYSTEM_COMMON_VERSION_ALIAS;
44  static const std::string SUBSYSTEM_COMMON_OVERRIDE_VERSION_ALIAS;
45 
46  static const std::string XDAQ_CONTEXT_TABLE_NAME;
47  static const std::string XDAQ_APPLICATION_TABLE_NAME;
48  static const std::string XDAQ_APP_PROPERTY_TABLE_NAME;
49  static const std::string GROUP_ALIASES_TABLE_NAME;
50  static const std::string VERSION_ALIASES_TABLE_NAME;
51  static const std::string ARTDAQ_TOP_TABLE_NAME;
52  static const std::string DESKTOP_ICON_TABLE_NAME;
53 
54  static const std::string GROUP_TYPE_NAME_CONTEXT;
55  static const std::string GROUP_TYPE_NAME_BACKBONE;
56  static const std::string GROUP_TYPE_NAME_ITERATE;
57  static const std::string GROUP_TYPE_NAME_CONFIGURATION;
58  static const std::string GROUP_TYPE_NAME_UNKNOWN;
59 
60  static const std::string LAST_TABLE_GROUP_SAVE_PATH;
61  static const std::string LAST_ACTIVATED_CONFIG_GROUP_FILE;
62  static const std::string LAST_ACTIVATED_CONTEXT_GROUP_FILE;
63  static const std::string LAST_ACTIVATED_BACKBONE_GROUP_FILE;
64  static const std::string LAST_ACTIVATED_ITERATE_GROUP_FILE;
65 
66  static const std::string ACTIVATED_CONFIGS_FILE;
67  static const std::string ACTIVATED_CONTEXTS_FILE;
68  static const std::string ACTIVATED_BACKBONES_FILE;
69  static const std::string ACTIVATED_ITERATES_FILE;
70 
71  static const std::string LAST_ATTEMPTED_CONFIGURE_CONFIG_ALIAS_FILE;
72  static const std::string LAST_ATTEMPTED_CONFIGURE_CONFIG_GROUP_FILE;
73 
74  static const std::string ATTEMPTED_CONFIGURE_CONFIG_ALIASES_FILE;
75  static const std::string ATTEMPTED_CONFIGURE_CONFIGS_FILE;
76 
77  static const std::string LAST_CONFIGURED_CONFIG_ALIAS_FILE;
78  static const std::string LAST_CONFIGURED_CONFIG_GROUP_FILE;
79  static const std::string LAST_CONFIGURED_CONTEXT_GROUP_FILE;
80  static const std::string LAST_CONFIGURED_BACKBONE_GROUP_FILE;
81  static const std::string LAST_CONFIGURED_ITERATE_GROUP_FILE;
82 
83  static const std::string CONFIGURED_CONFIG_ALIASES_FILE;
84  static const std::string CONFIGURED_CONFIGS_FILE;
85  static const std::string CONFIGURED_CONTEXTS_FILE;
86  static const std::string CONFIGURED_BACKBONES_FILE;
87  static const std::string CONFIGURED_ITERATES_FILE;
88 
89  static const std::string LAST_STARTED_CONFIG_ALIAS_FILE;
90  static const std::string LAST_STARTED_CONFIG_GROUP_FILE;
91  static const std::string LAST_STARTED_CONTEXT_GROUP_FILE;
92  static const std::string LAST_STARTED_BACKBONE_GROUP_FILE;
93  static const std::string LAST_STARTED_ITERATE_GROUP_FILE;
94 
95  static const std::string STARTED_CONFIG_ALIASES_FILE;
96  static const std::string STARTED_CONFIGS_FILE;
97  static const std::string STARTED_CONTEXTS_FILE;
98  static const std::string STARTED_BACKBONES_FILE;
99  static const std::string STARTED_ITERATES_FILE;
100 
101  static const std::string CONFIGURED_OR_STARTED_CONFIG_ALIASES_FILE;
102  static const std::string CONFIGURED_OR_STARTED_CONFIGS_FILE;
103  static const std::string CONFIGURED_OR_STARTED_CONTEXTS_FILE;
104  static const std::string CONFIGURED_OR_STARTED_BACKBONES_FILE;
105  static const std::string CONFIGURED_OR_STARTED_ITERATES_FILE;
106 
107  static const uint8_t METADATA_COL_ALIASES;
108  static const uint8_t METADATA_COL_COMMENT;
109  static const uint8_t METADATA_COL_AUTHOR;
110  static const uint8_t METADATA_COL_TIMESTAMP;
111 
112  static const std::string CONTEXT_SUBSYSTEM_OPTIONAL_TABLE;
113  static const std::string UNKNOWN_INFO;
114  static const std::string UNKNOWN_TIME;
115 
116  enum class GroupType
117  {
118  UNKNOWN_TYPE,
119  CONTEXT_TYPE,
120  BACKBONE_TYPE,
121  ITERATE_TYPE,
122  CONFIGURATION_TYPE
123  };
124 
125  enum class LoadGroupType
126  {
127  ALL_TYPES,
128  ONLY_BACKBONE_OR_CONTEXT_TYPES,
129  ONLY_BACKBONE_TYPE
130  };
131 
132  // clang-format off
133 
134  static const std::set<std::string>& getFixedContextMemberNames (void);
135  static const std::set<std::string>& getBackboneMemberNames (void);
136  static const std::set<std::string>& getIterateMemberNames (void);
137  const std::set<std::string>& getActiveContextMemberNames (void);
138  const std::set<std::string>& getConfigurationMemberNames (void);
139 
140  static const std::string& convertGroupTypeToName (const ConfigurationManager::GroupType& groupTypeId);
141  static ConfigurationManager::GroupType getTypeOfGroup (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap);
142  static const std::string& getTypeNameOfGroup (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap);
143 
144  //==============================================================================
147  ConfigurationManager(bool initForWriteAccess = false,
148  bool initializeFromFhicl = false,
149  bool forceNotFirstInContext = false);
150  virtual ~ConfigurationManager(void);
151 
152 
153 
154  void init (std::string* accumulatedErrors = 0, bool initForWriteAccess = false, std::string* accumulatedWarnings = 0);
155  void destroy (void);
156  void destroyTableGroup (const std::string& theGroup = "", bool onlyDeactivate = false);
157 
158  //==============================================================================
161  std::chrono::steady_clock::time_point startClockTime_, deltaClockTime_;
162  std::string /* sinceStart deltaLastCheck */ runTimeSeconds() {
163 
164  //((double)(clock()-startClockTime_))/CLOCKS_PER_SEC;};
165  std::string retStr =
166  std::to_string(artdaq::TimeUtils::GetElapsedTime(startClockTime_))
167  + "s " +
168  std::to_string(artdaq::TimeUtils::GetElapsedTime(deltaClockTime_)) + "s";
169  deltaClockTime_ = std::chrono::steady_clock::now();
170  return retStr;
171  };
172 
173  void loadTableGroup (
174  const std::string& tableGroupName,
175  const TableGroupKey& tableGroupKey,
176  bool doActivate = false,
177  std::map<std::string, TableVersion>* groupMembers = 0,
178  ProgressBar* progressBar = 0,
179  std::string* accumulateWarnings = 0,
180  std::string* groupComment = 0,
181  std::string* groupAuthor = 0,
182  std::string* groupCreateTime = 0,
183  bool doNotLoadMember = false,
184  std::string* groupTypeString = 0,
185  std::map<std::string /*name*/, std::string /*alias*/>* groupAliases = 0,
186  ConfigurationManager::LoadGroupType groupTypeToLoad = ConfigurationManager::LoadGroupType::ALL_TYPES,
187  bool ignoreVersionTracking = false,
188  std::map<std::string /* tableName */,
189  TableVersion> mergeInTables = {},
190  std::map<std::string /* tableName */,
191  TableVersion> overrideTables = {});
193  const ConfigurationManager& cacheConfigMgr,
194  const std::map<std::string, TableVersion>& groupMembers,
195  const std::string& configGroupName = "",
196  const TableGroupKey& tableGroupKey = TableGroupKey(TableGroupKey::INVALID),
197  bool doActivate = false,
198  bool ignoreVersionTracking = false);
199  std::pair<std::string /* groupName */, TableGroupKey>
200  getGroupOfLoadedTable (const std::string& tableName) const;
201  void loadMemberMap (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap, std::string* accumulateWarnings = 0);
202  TableGroupKey loadConfigurationBackbone (void);
203 
204  //================
207  template<class T>
208  const T* getTable (const std::string& tableName) const
209  {
210  const TableBase* srcPtr = getTableByName(tableName);
211  const T* retPtr = dynamic_cast<const T*>(srcPtr); if(retPtr == nullptr) { __SS__ << "Illegal cast of '" << tableName << "' to type " << StringMacros::getTypeName<T>() << " (s=" << static_cast<const void*>(srcPtr) <<", t=" << typeid(srcPtr).name() << ")"<< __E__; __SS_THROW__ } return retPtr;
212  }
213  const TableBase* getTableByName (const std::string& configurationName) const;
214 
215  void dumpActiveConfiguration (const std::string& filePath, const std::string& dumpType, const std::string& configurationAlias, const std::string& subsystemCommonList, const std::string& subsystemCommonOverrideList, const std::string& logEntry, const std::string& activeUsers, const std::string& activeStateMachine, std::ostream& altOut = std::cout);
216  void dumpMacroMakerModeFhicl (void);
217 
218  std::map<std::string /*groupAlias*/,
219  std::pair<std::string /*groupName*/,
223  std::map<std::string /*tableName*/,
224  std::map<std::string /*aliasName*/,
225  TableVersion>> getVersionAliases (void) const;
226  std::set<std::pair<std::string /*tableName*/,
227  TableVersion>> getVersionAliases (const std::string& tableAliasNeedle) const;
228  std::pair<std::string /*groupName*/,
229  TableGroupKey> getTableGroupFromAlias (std::string systemAlias, ProgressBar* progressBar = 0);
230  std::map<std::string /*groupType*/,
231  std::pair<std::string /*groupName*/,
232  TableGroupKey>> getActiveTableGroups (void) const;
233  const std::map<std::string /*groupType*/,
234  std::pair<std::string /*groupName*/,
235  TableGroupKey>>& getFailedTableGroups (void) const {return lastFailedGroupLoad_;}
236  const lastGroupLoad_t& getLastTableGroups (void) const {return lastGroupLoad_;}
237  const std::string& getActiveGroupName (const ConfigurationManager::GroupType& type = ConfigurationManager::GroupType::CONFIGURATION_TYPE) const;
238  TableGroupKey getActiveGroupKey (const ConfigurationManager::GroupType& type = ConfigurationManager::GroupType::CONFIGURATION_TYPE) const;
239 
240  ConfigurationTree getNode (const std::string& nodeString, bool doNotThrowOnBrokenUIDLinks = false) const;
241  std::map<std::string, ConfigurationTree>
242  getNodes (const std::string& nodeString) const;
243  ConfigurationTree getContextNode (const std::string& contextUID, const std::string& applicationUID) const;
244  ConfigurationTree getSupervisorNode (const std::string& contextUID, const std::string& applicationUID) const;
245  ConfigurationTree getSupervisorTableNode (const std::string& contextUID, const std::string& applicationUID) const;
246  ConfigurationTree getGatewaySupervisorNode (void) const;
247 
248  std::vector<std::pair<std::string /*childName*/,
249  ConfigurationTree>> getChildren (std::map<std::string, TableVersion>* memberMap = 0, std::string* accumulatedTreeErrors = 0) const;
250  std::map<std::string /*childName*/,
251  ConfigurationTree> getChildrenMap (std::map<std::string, TableVersion>* memberMap = 0, std::string* accumulatedTreeErrors = 0) const;
252  std::string getFirstPathToNode (const ConfigurationTree& node, const std::string& startPath = "/") const;
253 
254  std::map<std::string, TableVersion> getActiveVersions (void) const;
255 
256  const std::string& getOwnerContext (void) { return ownerContextUID_; }
257  const std::string& getOwnerApp (void) { return ownerAppUID_; }
258  bool isOwnerFirstAppInContext (void);
259 
260  std::map<std::string /*groupType*/,
261  std::pair<std::string /*groupName*/,
262  TableGroupKey>> getOtherSubsystemActiveTableGroups (const std::string& otherSubsystemUID, std::string* userDataPathPtr = nullptr, std::string* hostnamePtr = nullptr, std::string* usernamePtr = nullptr);
263  void getOtherSubsystemInstanceInfo (const std::string& otherSubsystemUID, std::string* userDataPathPtr = nullptr, std::string* hostnamePtr = nullptr, std::string* usernamePtr = nullptr, std::string* fullNamePtr = nullptr);
264  std::set<std::string /* configAlias */> getOtherSubsystemConfigAliases (const std::string& otherSubsystemUID);
265  std::set<std::string /* configAlias */> getOtherSubsystemFilteredConfigAliases (const std::string& otherSubsystemUID, const std::string& otherSubsystemFsmName );
266  void getOtherSubsystemConfigAliasInfo (const std::string& otherSubsystemUID, const std::string& configAlias, std::pair<std::string, TableGroupKey>& groupTranslation, std::string& groupComment, std::string& groupAuthor, std::string& groupCreationTime);
267 
268  //==============================================================================
270  std::shared_ptr<TableGroupKey> makeTheTableGroupKey (TableGroupKey key);
271  void restoreActiveTableGroups (bool throwErrors = false, const std::string& pathToActiveGroupsFile = "", ConfigurationManager::LoadGroupType onlyLoadIfBackboneOrContext = ConfigurationManager::LoadGroupType::ALL_TYPES, std::string* accumulatedWarnings = 0);
272 
273  void setOwnerContext (const std::string& contextUID) { ownerContextUID_ = contextUID; }
274  void setOwnerApp (const std::string& appUID) { ownerAppUID_ = appUID; }
275  static void saveGroupNameAndKey (const std::pair<std::string /*group name*/, TableGroupKey>& theGroup,const std::string& fileName, bool appendMode = false, const std::string& associatedUser = "");
276  static std::pair<
277  std::string /*group name*/,
278  TableGroupKey> loadGroupNameAndKey (const std::string& fileName, std::string& returnedTimeString);
279  static std::vector<std::map<std::string /* group field key */,
280  std::string /* group field value */>>
281  loadGroupHistory (const std::string& groupAction, const std::string& groupType, bool formatTime = false);
282  static std::vector<std::map<std::string /* group field key */,
283  std::string /* group field value */>>
284  loadGroupHistory (const std::string& fileName, bool formatTime = false);
285 
286  void initPrereqsForARTDAQ (void);
287 
288  bool forceNotFirstInContext_ = false;
289 
290  private:
291  ConfigurationManager (const std::string& userName);
292 
293  TableBase* getDesktopIconTable (void);
294 
295  void initializeFromFhicl (const std::string& fhiclPath);
296  void recursiveInitFromFhiclPSet (const std::string& tableName, const fhicl::ParameterSet& pset, const std::string& recordName = "", const std::string& groupName = "", const std::string& groupLinkIndex = "");
297  void recursiveTreeToFhicl (ConfigurationTree node, std::ostream& out, std::string& tabStr, std::string& commentStr, unsigned int depth = -1);
298  static void initTableThread (ConfigurationManager* cfgMgr,
299  ots::TableBase* table,
300  std::string* threadErrors,
301  std::mutex* threadMutex,
302  std::shared_ptr<std::atomic<bool>> threadDone);
303  static void fillTableThread (ConfigurationInterface* theInterface,
304  std::map<std::string, ots::TableBase *>*nameToTableMap,
305  ots::TableBase* table,
306  std::string tableName,
307  ots::TableVersion version,
308  std::string* threadErrors,
309  std::mutex* threadMutex,
310  std::shared_ptr<std::atomic<bool>> threadDone);
311 
312 
313  protected:
314  TableBase* getVersionedTableByName (const std::string& tableName, TableVersion version, bool looseColumnMatching = false, std::string* accumulatedErrors = 0, bool getRawData = false);
315 
316  std::string mfSubject_;
317  private:
318  std::string username_;
319  ConfigurationInterface* theInterface_;
320  std::shared_ptr<TableGroupKey> theConfigurationTableGroupKey_, theContextTableGroupKey_, theBackboneTableGroupKey_, theIterateTableGroupKey_;
321  std::string theConfigurationTableGroup_, theContextTableGroup_, theBackboneTableGroup_, theIterateTableGroup_;
322 
323  std::map<std::string,
324  std::pair<std::string, TableGroupKey>> lastFailedGroupLoad_;
325  lastGroupLoad_t lastGroupLoad_;
326 
327 
328 
329  std::map<std::string, TableBase*> nameToTableMap_;
330 
331  TableBase groupMetadataTable_;
332 
333  std::string ownerContextUID_;
334  std::string ownerAppUID_;
335 
336  std::mutex metaDataTableMutex_;
337 
338  static const std::set<std::string>
339  fixedContextMemberNames_;
340  static const std::set<std::string>
341  backboneMemberNames_;
342  static const std::set<std::string> iterateMemberNames_;
343  std::set<std::string> contextMemberNames_,
344  configurationMemberNames_;
345 
346  // clang-format on
347 };
348 } // namespace ots
349 
350 #endif
std::map< std::string, std::map< std::string, TableVersion > > getVersionAliases(void) const
TableBase * getVersionedTableByName(const std::string &tableName, TableVersion version, bool looseColumnMatching=false, std::string *accumulatedErrors=0, bool getRawData=false)
static const unsigned int PROCESSOR_COUNT
Static members.
static const std::string & convertGroupTypeToName(const ConfigurationManager::GroupType &groupTypeId)
void restoreActiveTableGroups(bool throwErrors=false, const std::string &pathToActiveGroupsFile="", ConfigurationManager::LoadGroupType onlyLoadIfBackboneOrContext=ConfigurationManager::LoadGroupType::ALL_TYPES, std::string *accumulatedWarnings=0)
std::map< std::string, std::pair< std::string, TableGroupKey > > getActiveTableGroups(void) const
std::set< std::string > getOtherSubsystemConfigAliases(const std::string &otherSubsystemUID)
Ignore any System Aliases with "Context" or "Iterat" in the name.
std::chrono::steady_clock::time_point startClockTime_
void dumpActiveConfiguration(const std::string &filePath, const std::string &dumpType, const std::string &configurationAlias, const std::string &subsystemCommonList, const std::string &subsystemCommonOverrideList, const std::string &logEntry, const std::string &activeUsers, const std::string &activeStateMachine, std::ostream &altOut=std::cout)
void loadMemberMap(const std::map< std::string, TableVersion > &memberMap, std::string *accumulateWarnings=0)
std::map< std::string, TableVersion > getActiveVersions(void) const
getActiveVersions
std::shared_ptr< TableGroupKey > makeTheTableGroupKey(TableGroupKey key)
Setters/Modifiers.
void copyTableGroupFromCache(const ConfigurationManager &cacheConfigMgr, const std::map< std::string, TableVersion > &groupMembers, const std::string &configGroupName="", const TableGroupKey &tableGroupKey=TableGroupKey(TableGroupKey::INVALID), bool doActivate=false, bool ignoreVersionTracking=false)
void getOtherSubsystemInstanceInfo(const std::string &otherSubsystemUID, std::string *userDataPathPtr=nullptr, std::string *hostnamePtr=nullptr, std::string *usernamePtr=nullptr, std::string *fullNamePtr=nullptr)
ConfigurationManager(bool initForWriteAccess=false, bool initializeFromFhicl=false, bool forceNotFirstInContext=false)
ConfigurationTree getNode(const std::string &nodeString, bool doNotThrowOnBrokenUIDLinks=false) const
"root/parent/parent/"
void loadTableGroup(const std::string &tableGroupName, const TableGroupKey &tableGroupKey, bool doActivate=false, std::map< std::string, TableVersion > *groupMembers=0, ProgressBar *progressBar=0, std::string *accumulateWarnings=0, std::string *groupComment=0, std::string *groupAuthor=0, std::string *groupCreateTime=0, bool doNotLoadMember=false, std::string *groupTypeString=0, std::map< std::string, std::string > *groupAliases=0, ConfigurationManager::LoadGroupType groupTypeToLoad=ConfigurationManager::LoadGroupType::ALL_TYPES, bool ignoreVersionTracking=false, std::map< std::string, TableVersion > mergeInTables={}, std::map< std::string, TableVersion > overrideTables={})
const T * getTable(const std::string &tableName) const
void init(std::string *accumulatedErrors=0, bool initForWriteAccess=false, std::string *accumulatedWarnings=0)
std::string getFirstPathToNode(const ConfigurationTree &node, const std::string &startPath="/") const
getFirstPathToNode
static ConfigurationManager::GroupType getTypeOfGroup(const std::map< std::string, TableVersion > &memberMap)
static const std::string & getTypeNameOfGroup(const std::map< std::string, TableVersion > &memberMap)
void destroyTableGroup(const std::string &theGroup="", bool onlyDeactivate=false)
std::vector< std::pair< std::string, ConfigurationTree > > getChildren(std::map< std::string, TableVersion > *memberMap=0, std::string *accumulatedTreeErrors=0) const
std::map< std::string, ConfigurationTree > getChildrenMap(std::map< std::string, TableVersion > *memberMap=0, std::string *accumulatedTreeErrors=0) const
std::pair< std::string, TableGroupKey > getTableGroupFromAlias(std::string systemAlias, ProgressBar *progressBar=0)
Getters.
static std::vector< std::map< std::string, std::string > > loadGroupHistory(const std::string &groupAction, const std::string &groupType, bool formatTime=false)
loadGroupHistory static
void getOtherSubsystemConfigAliasInfo(const std::string &otherSubsystemUID, const std::string &configAlias, std::pair< std::string, TableGroupKey > &groupTranslation, std::string &groupComment, std::string &groupAuthor, std::string &groupCreationTime)
returns configAlias translation group info by reference
ConfigurationTree getGatewaySupervisorNode(void) const
There can only be one active Gateway Superivsor app, so find it.
static const std::string ACTIVE_GROUPS_FILENAME
added env check for otsdaq_flatten_active_to_version to function
std::set< std::string > getOtherSubsystemFilteredConfigAliases(const std::string &otherSubsystemUID, const std::string &otherSubsystemFsmName)
Ignore any System Aliases with "Context" or "Iterat" in the name.
TableGroupKey loadConfigurationBackbone(void)
const TableBase * getTableByName(const std::string &configurationName) const
static std::pair< std::string, TableGroupKey > loadGroupNameAndKey(const std::string &fileName, std::string &returnedTimeString)
std::map< std::string, std::pair< std::string, TableGroupKey > > getActiveGroupAliases(void)
void initPrereqsForARTDAQ(void)
loadGroupHistory
defines used also by OtsConfigurationWizardSupervisor