tdaq-develop-2025-02-12
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_ITERATOR_GROUP_FILE;
63 
64  static const uint8_t METADATA_COL_ALIASES;
65  static const uint8_t METADATA_COL_COMMENT;
66  static const uint8_t METADATA_COL_AUTHOR;
67  static const uint8_t METADATA_COL_TIMESTAMP;
68 
69  static const std::set<std::string> contextMemberNames_;
70  static const std::set<std::string> backboneMemberNames_;
71  static const std::set<std::string> iterateMemberNames_;
72  std::set<std::string>
74 
75  static const std::string CONTEXT_SUBSYSTEM_OPTIONAL_TABLE;
76  static const std::string UNKNOWN_INFO;
77  static const std::string UNKNOWN_TIME;
78 
79  enum class GroupType
80  {
81  UNKNOWN_TYPE,
82  CONTEXT_TYPE,
83  BACKBONE_TYPE,
84  ITERATE_TYPE,
85  CONFIGURATION_TYPE
86  };
87 
88  enum class LoadGroupType
89  {
90  ALL_TYPES,
91  ONLY_BACKBONE_OR_CONTEXT_TYPES,
92  ONLY_BACKBONE_TYPE
93  };
94 
95  // clang-format off
96 
97  static const std::set<std::string>& getContextMemberNames (void);
98  static const std::set<std::string>& getBackboneMemberNames (void);
99  static const std::set<std::string>& getIterateMemberNames (void);
100  const std::set<std::string>& getConfigurationMemberNames (void);
101 
102  static const std::string& convertGroupTypeToName (const ConfigurationManager::GroupType& groupTypeId);
103  static ConfigurationManager::GroupType getTypeOfGroup (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap);
104  static const std::string& getTypeNameOfGroup (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap);
105 
106  //==============================================================================
109  ConfigurationManager(bool initForWriteAccess = false,
110  bool initializeFromFhicl = false);
111  virtual ~ConfigurationManager(void);
112 
113 
114 
115  void init (std::string* accumulatedErrors = 0, bool initForWriteAccess = false, std::string* accumulatedWarnings = 0);
116  void destroy (void);
117  void destroyTableGroup (const std::string& theGroup = "", bool onlyDeactivate = false);
118 
119  //==============================================================================
122  std::chrono::steady_clock::time_point startClockTime_, deltaClockTime_;
123  std::string /* sinceStart deltaLastCheck */ runTimeSeconds() {
124 
125  //((double)(clock()-startClockTime_))/CLOCKS_PER_SEC;};
126  std::string retStr =
127  std::to_string(artdaq::TimeUtils::GetElapsedTime(startClockTime_))
128  + "s " +
129  std::to_string(artdaq::TimeUtils::GetElapsedTime(deltaClockTime_)) + "s";
130  deltaClockTime_ = std::chrono::steady_clock::now();
131  return retStr;
132  };
133 
134  void loadTableGroup (
135  const std::string& tableGroupName,
136  const TableGroupKey& tableGroupKey,
137  bool doActivate = false,
138  std::map<std::string, TableVersion>* groupMembers = 0,
139  ProgressBar* progressBar = 0,
140  std::string* accumulateWarnings = 0,
141  std::string* groupComment = 0,
142  std::string* groupAuthor = 0,
143  std::string* groupCreateTime = 0,
144  bool doNotLoadMember = false,
145  std::string* groupTypeString = 0,
146  std::map<std::string /*name*/, std::string /*alias*/>* groupAliases = 0,
147  ConfigurationManager::LoadGroupType onlyLoadIfBackboneOrContext = ConfigurationManager::LoadGroupType::ALL_TYPES,
148  bool ignoreVersionTracking = false);
150  const ConfigurationManager& cacheConfigMgr,
151  const std::map<std::string, TableVersion>& groupMembers,
152  const std::string& configGroupName = "",
153  const TableGroupKey& tableGroupKey = TableGroupKey(TableGroupKey::INVALID),
154  bool doActivate = false,
155  bool ignoreVersionTracking = false);
156  std::pair<std::string /* groupName */, TableGroupKey>
157  getGroupOfLoadedTable (const std::string& tableName) const;
158  void loadMemberMap (const std::map<std::string /*name*/, TableVersion /*version*/>& memberMap, std::string* accumulateWarnings = 0);
160 
161  //================
164  template<class T>
165  const T* getTable (const std::string& tableName) const {
166 const TableBase* srcPtr = getTableByName(tableName);
167 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;}
168  const TableBase* getTableByName (const std::string& configurationName) const;
169 
170  void dumpActiveConfiguration (const std::string& filePath, const std::string& dumpType, const std::string& configurationAlias, const std::string& logEntry, const std::string& activeUsers, std::ostream& altOut = std::cout);
171  void dumpMacroMakerModeFhicl (void);
172 
173  std::map<std::string /*groupAlias*/,
174  std::pair<std::string /*groupName*/,
178  std::map<std::string /*tableName*/,
179  std::map<std::string /*aliasName*/,
180  TableVersion>> getVersionAliases (void) const;
181  std::pair<std::string /*groupName*/,
182  TableGroupKey> getTableGroupFromAlias (std::string systemAlias, ProgressBar* progressBar = 0);
183  std::map<std::string /*groupType*/,
184  std::pair<std::string /*groupName*/,
185  TableGroupKey>> getActiveTableGroups (void) const;
186  const std::map<std::string /*groupType*/,
187  std::pair<std::string /*groupName*/,
188  TableGroupKey>>& getFailedTableGroups (void) const {return lastFailedGroupLoad_;}
189  const lastGroupLoad_t& getLastTableGroups (void) const {return lastGroupLoad_;}
190  const std::string& getActiveGroupName (const ConfigurationManager::GroupType& type = ConfigurationManager::GroupType::CONFIGURATION_TYPE) const;
191  TableGroupKey getActiveGroupKey (const ConfigurationManager::GroupType& type = ConfigurationManager::GroupType::CONFIGURATION_TYPE) const;
192 
193  ConfigurationTree getNode (const std::string& nodeString, bool doNotThrowOnBrokenUIDLinks = false) const;
194  std::map<std::string, ConfigurationTree>
195  getNodes (const std::string& nodeString) const;
196  ConfigurationTree getContextNode (const std::string& contextUID, const std::string& applicationUID) const;
197  ConfigurationTree getSupervisorNode (const std::string& contextUID, const std::string& applicationUID) const;
198  ConfigurationTree getSupervisorTableNode (const std::string& contextUID, const std::string& applicationUID) const;
199  ConfigurationTree getGatewaySupervisorNode (void) const;
200 
201  std::vector<std::pair<std::string /*childName*/,
202  ConfigurationTree>> getChildren (std::map<std::string, TableVersion>* memberMap = 0, std::string* accumulatedTreeErrors = 0) const;
203  std::map<std::string /*childName*/,
204  ConfigurationTree> getChildrenMap (std::map<std::string, TableVersion>* memberMap = 0, std::string* accumulatedTreeErrors = 0) const;
205  std::string getFirstPathToNode (const ConfigurationTree& node, const std::string& startPath = "/") const;
206 
207  std::map<std::string, TableVersion> getActiveVersions (void) const;
208 
209  const std::string& getOwnerContext (void) { return ownerContextUID_; }
210  const std::string& getOwnerApp (void) { return ownerAppUID_; }
211  bool isOwnerFirstAppInContext (void);
212 
213  std::map<std::string /*groupType*/,
214  std::pair<std::string /*groupName*/,
215  TableGroupKey>> getOtherSubsystemActiveTableGroups (const std::string& otherSubsystemUID, std::string* userDataPathPtr = nullptr, std::string* hostnamePtr = nullptr, std::string* usernamePtr = nullptr);
216  void getOtherSubsystemInstanceInfo (const std::string& otherSubsystemUID, std::string* userDataPathPtr = nullptr, std::string* hostnamePtr = nullptr, std::string* usernamePtr = nullptr, std::string* fullNamePtr = nullptr);
217  std::set<std::string /* configAlias */> getOtherSubsystemConfigAliases (const std::string& otherSubsystemUID);
218  std::set<std::string /* configAlias */> getOtherSubsystemFilteredConfigAliases (const std::string& otherSubsystemUID, const std::string& otherSubsystemFsmName );
219  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);
220 
221  //==============================================================================
223  std::shared_ptr<TableGroupKey> makeTheTableGroupKey (TableGroupKey key);
224  void restoreActiveTableGroups (bool throwErrors = false, const std::string& pathToActiveGroupsFile = "", ConfigurationManager::LoadGroupType onlyLoadIfBackboneOrContext = ConfigurationManager::LoadGroupType::ALL_TYPES, std::string* accumulatedWarnings = 0);
225 
226  void setOwnerContext (const std::string& contextUID) { ownerContextUID_ = contextUID; }
227  void setOwnerApp (const std::string& appUID) { ownerAppUID_ = appUID; }
228  static void saveGroupNameAndKey (const std::pair<std::string /*group name*/, TableGroupKey>& theGroup,const std::string& fileName);
229  static std::pair<
230  std::string /*group name*/,
231  TableGroupKey> loadGroupNameAndKey (const std::string& fileName, std::string& returnedTimeString);
232 
233 
234  private:
235  ConfigurationManager (const std::string& userName);
236 
237  TableBase* getDesktopIconTable (void);
238 
239  void initializeFromFhicl (const std::string& fhiclPath);
240  void recursiveInitFromFhiclPSet (const std::string& tableName, const fhicl::ParameterSet& pset, const std::string& recordName = "", const std::string& groupName = "", const std::string& groupLinkIndex = "");
241  void recursiveTreeToFhicl (ConfigurationTree node, std::ostream& out, std::string& tabStr, std::string& commentStr, unsigned int depth = -1);
242  static void initTableThread (ConfigurationManager* cfgMgr,
243  ots::TableBase* table,
244  std::string* accumulatedWarnings,
245  std::mutex* threadMutex,
246  std::shared_ptr<std::atomic<bool>> threadDone);
247  static void fillTableThread (ConfigurationInterface* theInterface,
248  std::map<std::string, ots::TableBase *>*nameToTableMap,
249  ots::TableBase* table,
250  std::string tableName,
251  ots::TableVersion version,
252  std::string* accumulatedWarnings,
253  std::mutex* threadMutex,
254  std::shared_ptr<std::atomic<bool>> threadDone);
255 
256 
257  protected:
258  std::string mfSubject_;
259  private:
260  std::string username_;
261  ConfigurationInterface* theInterface_;
262  std::shared_ptr<TableGroupKey> theConfigurationTableGroupKey_, theContextTableGroupKey_, theBackboneTableGroupKey_, theIterateTableGroupKey_;
263  std::string theConfigurationTableGroup_, theContextTableGroup_, theBackboneTableGroup_, theIterateTableGroup_;
264 
265  std::map<std::string,
266  std::pair<std::string, TableGroupKey>> lastFailedGroupLoad_;
267  lastGroupLoad_t lastGroupLoad_;
268 
269 
270 
271  std::map<std::string, TableBase*> nameToTableMap_;
272 
273  TableBase groupMetadataTable_;
274 
275  std::string ownerContextUID_;
276  std::string ownerAppUID_;
277 
278  std::mutex metaDataTableMutex_;
279 
280  // clang-format on
281 };
282 } // namespace ots
283 
284 #endif
std::map< std::string, std::map< std::string, TableVersion > > getVersionAliases(void) const
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 onlyLoadIfBackboneOrContext=ConfigurationManager::LoadGroupType::ALL_TYPES, bool ignoreVersionTracking=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_
std::set< std::string > configurationMemberNames_
list of 'active' configuration members
void loadMemberMap(const std::map< std::string, TableVersion > &memberMap, std::string *accumulateWarnings=0)
void dumpActiveConfiguration(const std::string &filePath, const std::string &dumpType, const std::string &configurationAlias, const std::string &logEntry, const std::string &activeUsers, std::ostream &altOut=std::cout)
std::map< std::string, TableVersion > getActiveVersions(void) const
getActiveVersions
ConfigurationManager(bool initForWriteAccess=false, bool initializeFromFhicl=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)
ConfigurationTree getNode(const std::string &nodeString, bool doNotThrowOnBrokenUIDLinks=false) const
"root/parent/parent/"
const T * getTable(const std::string &tableName) const
static const std::set< std::string > contextMemberNames_
list of context members
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)
static const std::set< std::string > backboneMemberNames_
list of backbone members
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.
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.
static const std::set< std::string > iterateMemberNames_
list of iterate members
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)