otsdaq  3.04.02
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 
44  static const std::string XDAQ_CONTEXT_TABLE_NAME;
45  static const std::string XDAQ_APPLICATION_TABLE_NAME;
46  static const std::string XDAQ_APP_PROPERTY_TABLE_NAME;
47  static const std::string GROUP_ALIASES_TABLE_NAME;
48  static const std::string VERSION_ALIASES_TABLE_NAME;
49  static const std::string ARTDAQ_TOP_TABLE_NAME;
50  static const std::string DESKTOP_ICON_TABLE_NAME;
51 
52  static const std::string GROUP_TYPE_NAME_CONTEXT;
53  static const std::string GROUP_TYPE_NAME_BACKBONE;
54  static const std::string GROUP_TYPE_NAME_ITERATE;
55  static const std::string GROUP_TYPE_NAME_CONFIGURATION;
56  static const std::string GROUP_TYPE_NAME_UNKNOWN;
57 
58  static const std::string LAST_TABLE_GROUP_SAVE_PATH;
59  static const std::string LAST_ACTIVATED_CONFIG_GROUP_FILE;
60  static const std::string LAST_ACTIVATED_CONTEXT_GROUP_FILE;
61  static const std::string LAST_ACTIVATED_BACKBONE_GROUP_FILE;
62  static const std::string LAST_ACTIVATED_ITERATE_GROUP_FILE;
63 
64  static const std::string ACTIVATED_CONFIGS_FILE;
65  static const std::string ACTIVATED_CONTEXTS_FILE;
66  static const std::string ACTIVATED_BACKBONES_FILE;
67  static const std::string ACTIVATED_ITERATES_FILE;
68 
69  static const std::string LAST_CONFIGURED_CONFIG_ALIAS_FILE;
70  static const std::string LAST_CONFIGURED_CONFIG_GROUP_FILE;
71  static const std::string LAST_CONFIGURED_CONTEXT_GROUP_FILE;
72  static const std::string LAST_CONFIGURED_BACKBONE_GROUP_FILE;
73  static const std::string LAST_CONFIGURED_ITERATE_GROUP_FILE;
74 
75  static const std::string CONFIGURED_CONFIG_ALIASES_FILE;
76  static const std::string CONFIGURED_CONFIGS_FILE;
77  static const std::string CONFIGURED_CONTEXTS_FILE;
78  static const std::string CONFIGURED_BACKBONES_FILE;
79  static const std::string CONFIGURED_ITERATES_FILE;
80 
81  static const std::string LAST_STARTED_CONFIG_ALIAS_FILE;
82  static const std::string LAST_STARTED_CONFIG_GROUP_FILE;
83  static const std::string LAST_STARTED_CONTEXT_GROUP_FILE;
84  static const std::string LAST_STARTED_BACKBONE_GROUP_FILE;
85  static const std::string LAST_STARTED_ITERATE_GROUP_FILE;
86 
87  static const std::string STARTED_CONFIG_ALIASES_FILE;
88  static const std::string STARTED_CONFIGS_FILE;
89  static const std::string STARTED_CONTEXTS_FILE;
90  static const std::string STARTED_BACKBONES_FILE;
91  static const std::string STARTED_ITERATES_FILE;
92 
93  static const std::string CONFIGURED_OR_STARTED_CONFIG_ALIASES_FILE;
94  static const std::string CONFIGURED_OR_STARTED_CONFIGS_FILE;
95  static const std::string CONFIGURED_OR_STARTED_CONTEXTS_FILE;
96  static const std::string CONFIGURED_OR_STARTED_BACKBONES_FILE;
97  static const std::string CONFIGURED_OR_STARTED_ITERATES_FILE;
98 
99  static const uint8_t METADATA_COL_ALIASES;
100  static const uint8_t METADATA_COL_COMMENT;
101  static const uint8_t METADATA_COL_AUTHOR;
102  static const uint8_t METADATA_COL_TIMESTAMP;
103 
104  static const std::string CONTEXT_SUBSYSTEM_OPTIONAL_TABLE;
105  static const std::string UNKNOWN_INFO;
106  static const std::string UNKNOWN_TIME;
107 
108  enum class GroupType
109  {
110  UNKNOWN_TYPE,
111  CONTEXT_TYPE,
112  BACKBONE_TYPE,
113  ITERATE_TYPE,
114  CONFIGURATION_TYPE
115  };
116 
117  enum class LoadGroupType
118  {
119  ALL_TYPES,
120  ONLY_BACKBONE_OR_CONTEXT_TYPES,
121  ONLY_BACKBONE_TYPE
122  };
123 
124  // clang-format off
125 
126  static const std::set<std::string>& getFixedContextMemberNames (void);
127  static const std::set<std::string>& getBackboneMemberNames (void);
128  static const std::set<std::string>& getIterateMemberNames (void);
129  const std::set<std::string>& getActiveContextMemberNames (void);
130  const std::set<std::string>& getConfigurationMemberNames (void);
131 
132  static const std::string& convertGroupTypeToName (const ConfigurationManager::GroupType& groupTypeId);
133  static ConfigurationManager::GroupType getTypeOfGroup (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap);
134  static const std::string& getTypeNameOfGroup (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap);
135 
136  //==============================================================================
139  ConfigurationManager(bool initForWriteAccess = false,
140  bool initializeFromFhicl = false,
141  bool forceNotFirstInContext = false);
142  virtual ~ConfigurationManager(void);
143 
144 
145 
146  void init (std::string* accumulatedErrors = 0, bool initForWriteAccess = false, std::string* accumulatedWarnings = 0);
147  void destroy (void);
148  void destroyTableGroup (const std::string& theGroup = "", bool onlyDeactivate = false);
149 
150  //==============================================================================
153  std::chrono::steady_clock::time_point startClockTime_, deltaClockTime_;
154  std::string /* sinceStart deltaLastCheck */ runTimeSeconds() {
155 
156  //((double)(clock()-startClockTime_))/CLOCKS_PER_SEC;};
157  std::string retStr =
158  std::to_string(artdaq::TimeUtils::GetElapsedTime(startClockTime_))
159  + "s " +
160  std::to_string(artdaq::TimeUtils::GetElapsedTime(deltaClockTime_)) + "s";
161  deltaClockTime_ = std::chrono::steady_clock::now();
162  return retStr;
163  };
164 
165  void loadTableGroup (
166  const std::string& tableGroupName,
167  const TableGroupKey& tableGroupKey,
168  bool doActivate = false,
169  std::map<std::string, TableVersion>* groupMembers = 0,
170  ProgressBar* progressBar = 0,
171  std::string* accumulateWarnings = 0,
172  std::string* groupComment = 0,
173  std::string* groupAuthor = 0,
174  std::string* groupCreateTime = 0,
175  bool doNotLoadMember = false,
176  std::string* groupTypeString = 0,
177  std::map<std::string /*name*/, std::string /*alias*/>* groupAliases = 0,
178  ConfigurationManager::LoadGroupType groupTypeToLoad = ConfigurationManager::LoadGroupType::ALL_TYPES,
179  bool ignoreVersionTracking = false);
181  const ConfigurationManager& cacheConfigMgr,
182  const std::map<std::string, TableVersion>& groupMembers,
183  const std::string& configGroupName = "",
184  const TableGroupKey& tableGroupKey = TableGroupKey(TableGroupKey::INVALID),
185  bool doActivate = false,
186  bool ignoreVersionTracking = false);
187  std::pair<std::string /* groupName */, TableGroupKey>
188  getGroupOfLoadedTable (const std::string& tableName) const;
189  void loadMemberMap (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap, std::string* accumulateWarnings = 0);
191 
192  //================
195  template<class T>
196  const T* getTable (const std::string& tableName) const
197  {
198  const TableBase* srcPtr = getTableByName(tableName);
199  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;
200  }
201  const TableBase* getTableByName (const std::string& configurationName) const;
202 
203  void dumpActiveConfiguration (const std::string& filePath, const std::string& dumpType, const std::string& configurationAlias, const std::string& logEntry, const std::string& activeUsers, const std::string& activeStateMachine, std::ostream& altOut = std::cout);
204  void dumpMacroMakerModeFhicl (void);
205 
206  std::map<std::string /*groupAlias*/,
207  std::pair<std::string /*groupName*/,
211  std::map<std::string /*tableName*/,
212  std::map<std::string /*aliasName*/,
213  TableVersion>> getVersionAliases (void) const;
214  std::pair<std::string /*groupName*/,
215  TableGroupKey> getTableGroupFromAlias (std::string systemAlias, ProgressBar* progressBar = 0);
216  std::map<std::string /*groupType*/,
217  std::pair<std::string /*groupName*/,
218  TableGroupKey>> getActiveTableGroups (void) const;
219  const std::map<std::string /*groupType*/,
220  std::pair<std::string /*groupName*/,
221  TableGroupKey>>& getFailedTableGroups (void) const {return lastFailedGroupLoad_;}
222  const lastGroupLoad_t& getLastTableGroups (void) const {return lastGroupLoad_;}
223  const std::string& getActiveGroupName (const ConfigurationManager::GroupType& type = ConfigurationManager::GroupType::CONFIGURATION_TYPE) const;
224  TableGroupKey getActiveGroupKey (const ConfigurationManager::GroupType& type = ConfigurationManager::GroupType::CONFIGURATION_TYPE) const;
225 
226  ConfigurationTree getNode (const std::string& nodeString, bool doNotThrowOnBrokenUIDLinks = false) const;
227  std::map<std::string, ConfigurationTree>
228  getNodes (const std::string& nodeString) const;
229  ConfigurationTree getContextNode (const std::string& contextUID, const std::string& applicationUID) const;
230  ConfigurationTree getSupervisorNode (const std::string& contextUID, const std::string& applicationUID) const;
231  ConfigurationTree getSupervisorTableNode (const std::string& contextUID, const std::string& applicationUID) const;
232  ConfigurationTree getGatewaySupervisorNode (void) const;
233 
234  std::vector<std::pair<std::string /*childName*/,
235  ConfigurationTree>> getChildren (std::map<std::string, TableVersion>* memberMap = 0, std::string* accumulatedTreeErrors = 0) const;
236  std::map<std::string /*childName*/,
237  ConfigurationTree> getChildrenMap (std::map<std::string, TableVersion>* memberMap = 0, std::string* accumulatedTreeErrors = 0) const;
238  std::string getFirstPathToNode (const ConfigurationTree& node, const std::string& startPath = "/") const;
239 
240  std::map<std::string, TableVersion> getActiveVersions (void) const;
241 
242  const std::string& getOwnerContext (void) { return ownerContextUID_; }
243  const std::string& getOwnerApp (void) { return ownerAppUID_; }
244  bool isOwnerFirstAppInContext (void);
245 
246  std::map<std::string /*groupType*/,
247  std::pair<std::string /*groupName*/,
248  TableGroupKey>> getOtherSubsystemActiveTableGroups (const std::string& otherSubsystemUID, std::string* userDataPathPtr = nullptr, std::string* hostnamePtr = nullptr, std::string* usernamePtr = nullptr);
249  void getOtherSubsystemInstanceInfo (const std::string& otherSubsystemUID, std::string* userDataPathPtr = nullptr, std::string* hostnamePtr = nullptr, std::string* usernamePtr = nullptr, std::string* fullNamePtr = nullptr);
250  std::set<std::string /* configAlias */> getOtherSubsystemConfigAliases (const std::string& otherSubsystemUID);
251  std::set<std::string /* configAlias */> getOtherSubsystemFilteredConfigAliases (const std::string& otherSubsystemUID, const std::string& otherSubsystemFsmName );
252  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);
253 
254  //==============================================================================
256  std::shared_ptr<TableGroupKey> makeTheTableGroupKey (TableGroupKey key);
257  void restoreActiveTableGroups (bool throwErrors = false, const std::string& pathToActiveGroupsFile = "", ConfigurationManager::LoadGroupType onlyLoadIfBackboneOrContext = ConfigurationManager::LoadGroupType::ALL_TYPES, std::string* accumulatedWarnings = 0);
258 
259  void setOwnerContext (const std::string& contextUID) { ownerContextUID_ = contextUID; }
260  void setOwnerApp (const std::string& appUID) { ownerAppUID_ = appUID; }
261  static void saveGroupNameAndKey (const std::pair<std::string /*group name*/, TableGroupKey>& theGroup,const std::string& fileName, bool appendMode = false, const std::string& associatedUser = "");
262  static std::pair<
263  std::string /*group name*/,
264  TableGroupKey> loadGroupNameAndKey (const std::string& fileName, std::string& returnedTimeString);
265  static std::vector<std::map<std::string /* group field key */,
266  std::string /* group field value */>>
267  loadGroupHistory (const std::string& groupAction, const std::string& groupType, bool formatTime = false);
268  static std::vector<std::map<std::string /* group field key */,
269  std::string /* group field value */>>
270  loadGroupHistory (const std::string& fileName, bool formatTime = false);
271 
272  bool forceNotFirstInContext_ = false;
273 
274  private:
275  ConfigurationManager (const std::string& userName);
276 
277  TableBase* getDesktopIconTable (void);
278 
279  void initializeFromFhicl (const std::string& fhiclPath);
280  void recursiveInitFromFhiclPSet (const std::string& tableName, const fhicl::ParameterSet& pset, const std::string& recordName = "", const std::string& groupName = "", const std::string& groupLinkIndex = "");
281  void recursiveTreeToFhicl (ConfigurationTree node, std::ostream& out, std::string& tabStr, std::string& commentStr, unsigned int depth = -1);
282  static void initTableThread (ConfigurationManager* cfgMgr,
283  ots::TableBase* table,
284  std::string* threadErrors,
285  std::mutex* threadMutex,
286  std::shared_ptr<std::atomic<bool>> threadDone);
287  static void fillTableThread (ConfigurationInterface* theInterface,
288  std::map<std::string, ots::TableBase *>*nameToTableMap,
289  ots::TableBase* table,
290  std::string tableName,
291  ots::TableVersion version,
292  std::string* threadErrors,
293  std::mutex* threadMutex,
294  std::shared_ptr<std::atomic<bool>> threadDone);
295 
296 
297  protected:
298  std::string mfSubject_;
299  private:
300  std::string username_;
301  ConfigurationInterface* theInterface_;
302  std::shared_ptr<TableGroupKey> theConfigurationTableGroupKey_, theContextTableGroupKey_, theBackboneTableGroupKey_, theIterateTableGroupKey_;
303  std::string theConfigurationTableGroup_, theContextTableGroup_, theBackboneTableGroup_, theIterateTableGroup_;
304 
305  std::map<std::string,
306  std::pair<std::string, TableGroupKey>> lastFailedGroupLoad_;
307  lastGroupLoad_t lastGroupLoad_;
308 
309 
310 
311  std::map<std::string, TableBase*> nameToTableMap_;
312 
313  TableBase groupMetadataTable_;
314 
315  std::string ownerContextUID_;
316  std::string ownerAppUID_;
317 
318  std::mutex metaDataTableMutex_;
319 
320  static const std::set<std::string>
321  fixedContextMemberNames_;
322  static const std::set<std::string>
323  backboneMemberNames_;
324  static const std::set<std::string> iterateMemberNames_;
325  std::set<std::string> contextMemberNames_,
326  configurationMemberNames_;
327 
328  // clang-format on
329 };
330 } // namespace ots
331 
332 #endif
std::map< std::string, std::map< std::string, TableVersion > > getVersionAliases(void) const
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 loadMemberMap(const std::map< std::string, TableVersion > &memberMap, std::string *accumulateWarnings=0)
std::map< std::string, TableVersion > getActiveVersions(void) const
getActiveVersions
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::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/"
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
void dumpActiveConfiguration(const std::string &filePath, const std::string &dumpType, const std::string &configurationAlias, const std::string &logEntry, const std::string &activeUsers, const std::string &activeStateMachine, std::ostream &altOut=std::cout)
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)
defines used also by OtsConfigurationWizardSupervisor