ws_workunitsHelpers.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef _WSWU_HELPERS_HPP__
  14. #define _WSWU_HELPERS_HPP__
  15. #include "ws_workunits_esp.ipp"
  16. #include "exception_util.hpp"
  17. #include "jtime.ipp"
  18. #include "workunit.hpp"
  19. #include "hqlerror.hpp"
  20. #include "dllserver.hpp"
  21. #include <list>
  22. #include <vector>
  23. #include <functional>
  24. namespace ws_workunits {
  25. #define OWN_WU_ACCESS "OwnWorkunitsAccess"
  26. #define OTHERS_WU_ACCESS "OthersWorkunitsAccess"
  27. #define File_Cpp "cpp"
  28. #define File_ThorLog "ThorLog"
  29. #define File_ThorSlaveLog "ThorSlaveLog"
  30. #define File_EclAgentLog "EclAgentLog"
  31. #define File_XML "XML"
  32. #define File_Res "res"
  33. #define File_DLL "dll"
  34. #define File_WUECL "WUECL"
  35. #define File_ArchiveQuery "ArchiveQuery"
  36. #define TOTALTHORTIME "Total thor time"
  37. #define TOTALCLUSTERTIME "Total cluster time" //for roxie and hthor
  38. #define TEMPZIPDIR "tempzipfiles"
  39. static const long MAXXLSTRANSFER = 5000000;
  40. const unsigned DATA_SIZE = 16;
  41. const unsigned WUARCHIVE_CACHE_SIZE = 8;
  42. const unsigned WUARCHIVE_CACHE_MINITES = 5;
  43. const unsigned AWUS_CACHE_SIZE = 16;
  44. const unsigned AWUS_CACHE_MIN_DEFAULT = 15;
  45. const unsigned WUDEFAULT_ZAPEMAILSERVER_PORT = 25;
  46. inline bool notEmpty(const char *val){return (val && *val);}
  47. inline bool isEmpty(const char *val){return (!val || !*val);}
  48. const char *getWuAccessType(IConstWorkUnit& cw, const char *user);
  49. SecAccessFlags chooseWuAccessFlagsByOwnership(const char *user, const char *owner, SecAccessFlags accessOwn, SecAccessFlags accessOthers);
  50. SecAccessFlags chooseWuAccessFlagsByOwnership(const char *user, IConstWorkUnitInfo& cw, SecAccessFlags accessOwn, SecAccessFlags accessOthers);
  51. SecAccessFlags getWsWorkunitAccess(IEspContext& cxt, IConstWorkUnit& cw);
  52. void getUserWuAccessFlags(IEspContext& context, SecAccessFlags& accessOwn, SecAccessFlags& accessOthers, bool except);
  53. void ensureWsWorkunitAccess(IEspContext& cxt, IConstWorkUnit& cw, SecAccessFlags minAccess);
  54. void ensureWsWorkunitAccess(IEspContext& context, const char* wuid, SecAccessFlags minAccess);
  55. void ensureWsWorkunitAccessByOwnerId(IEspContext& context, const char* owner, SecAccessFlags minAccess);
  56. void ensureWsCreateWorkunitAccess(IEspContext& cxt);
  57. const char *getGraphNum(const char *s,unsigned &num);
  58. class WsWuDateTime : public CScmDateTime
  59. {
  60. public:
  61. IMPLEMENT_IINTERFACE;
  62. WsWuDateTime()
  63. {
  64. setSimpleLocal(0);
  65. }
  66. bool isValid()
  67. {
  68. unsigned year, month, day;
  69. cdt.getDate(year, month, day, true);
  70. return year>1969;
  71. }
  72. };
  73. void formatDuration(StringBuffer &s, unsigned ms);
  74. struct WsWUExceptions
  75. {
  76. WsWUExceptions(IConstWorkUnit& wu);
  77. operator IArrayOf<IEspECLException>&() { return errors; }
  78. int ErrCount() { return numerr; }
  79. int WrnCount() { return numwrn; }
  80. int InfCount() { return numinf; }
  81. int AlertCount() { return numalert; }
  82. private:
  83. IArrayOf<IEspECLException> errors;
  84. int numerr;
  85. int numwrn;
  86. int numinf;
  87. int numalert;
  88. };
  89. #define WUINFO_TruncateEclTo64k 0x0001
  90. #define WUINFO_IncludeExceptions 0x0002
  91. #define WUINFO_IncludeGraphs 0x0004
  92. #define WUINFO_IncludeResults 0x0008
  93. #define WUINFO_IncludeVariables 0x0010
  94. #define WUINFO_IncludeTimers 0x0020
  95. #define WUINFO_IncludeDebugValues 0x0040
  96. #define WUINFO_IncludeApplicationValues 0x0080
  97. #define WUINFO_IncludeWorkflows 0x0100
  98. #define WUINFO_IncludeEclSchemas 0x0200
  99. #define WUINFO_IncludeSourceFiles 0x0400
  100. #define WUINFO_IncludeResultsViewNames 0x0800
  101. #define WUINFO_IncludeXmlSchema 0x1000
  102. #define WUINFO_IncludeResourceURLs 0x2000
  103. #define WUINFO_IncludeECL 0x4000
  104. #define WUINFO_IncludeHelpers 0x8000
  105. #define WUINFO_IncludeAllowedClusters 0x10000
  106. #define WUINFO_IncludeTotalClusterTime 0x20000
  107. #define WUINFO_All 0xFFFFFFFF
  108. class WsWuInfo
  109. {
  110. IEspWUArchiveFile* readArchiveFileAttr(IPropertyTree& fileTree, const char* path);
  111. IEspWUArchiveModule* readArchiveModuleAttr(IPropertyTree& moduleTree, const char* path);
  112. void readArchiveFiles(IPropertyTree* archiveTree, const char* path, IArrayOf<IEspWUArchiveFile>& files);
  113. void outputALine(size32_t len, const char* content, MemoryBuffer& outputBuf, IFileIOStream* outIOS);
  114. public:
  115. WsWuInfo(IEspContext &ctx, IConstWorkUnit *cw_) :
  116. context(ctx), cw(cw_)
  117. {
  118. version = context.getClientVersion();
  119. wuid.set(cw->queryWuid());
  120. }
  121. WsWuInfo(IEspContext &ctx, const char *wuid_) :
  122. context(ctx)
  123. {
  124. wuid.set(wuid_);
  125. version = context.getClientVersion();
  126. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(ctx.querySecManager(), ctx.queryUser());
  127. cw.setown(factory->openWorkUnit(wuid_));
  128. if(!cw)
  129. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.", wuid_);
  130. }
  131. bool getResourceInfo(StringArray &viewnames, StringArray &urls, unsigned long flags);
  132. unsigned getResourceURLCount();
  133. void getCommon(IEspECLWorkunit &info, unsigned long flags);
  134. void getInfo(IEspECLWorkunit &info, unsigned long flags);
  135. void getResults(IEspECLWorkunit &info, unsigned long flags);
  136. void getVariables(IEspECLWorkunit &info, unsigned long flags);
  137. void getDebugValues(IEspECLWorkunit &info, unsigned long flags);
  138. bool getClusterInfo(IEspECLWorkunit &info, unsigned long flags);
  139. void getApplicationValues(IEspECLWorkunit &info, unsigned long flags);
  140. void getExceptions(IEspECLWorkunit &info, unsigned long flags);
  141. void getSourceFiles(IEspECLWorkunit &info, unsigned long flags);
  142. unsigned getTimerCount();
  143. void getTimers(IEspECLWorkunit &info, unsigned long flags);
  144. void doGetTimers(IArrayOf<IEspECLTimer>& timers);
  145. void getHelpers(IEspECLWorkunit &info, unsigned long flags);
  146. void getGraphInfo(IEspECLWorkunit &info, unsigned long flags);
  147. void doGetGraphs(IArrayOf<IEspECLGraph>& graphs);
  148. void getWUGraphNameAndTypes(WUGraphType graphType, IArrayOf<IEspNameAndType>& graphNameAndTypes);
  149. void getGraphTimingData(IArrayOf<IConstECLTimingData> &timingData);
  150. bool getFileSize(const char* fileName, const char* IPAddress, offset_t& fileSize);
  151. void getWorkflow(IEspECLWorkunit &info, unsigned long flags);
  152. void getHelpFiles(IConstWUQuery* query, WUFileType type, IArrayOf<IEspECLHelpFile>& helpers, unsigned long flags, unsigned& helpersCount);
  153. void getSubFiles(IPropertyTreeIterator* f, IEspECLSourceFile* eclSuperFile, StringArray& fileNames);
  154. void getEclSchemaChildFields(IArrayOf<IEspECLSchemaItem>& schemas, IHqlExpression * expr, bool isConditional);
  155. void getEclSchemaFields(IArrayOf<IEspECLSchemaItem>& schemas, IHqlExpression * expr, bool isConditional);
  156. bool getResultEclSchemas(IConstWUResult &r, IArrayOf<IEspECLSchemaItem>& schemas);
  157. void getResult(IConstWUResult &r, IArrayOf<IEspECLResult>& results, unsigned long flags);
  158. void getStats(const WuScopeFilter & filter, const StatisticsFilter& statsFilter, bool createDescriptions, IArrayOf<IEspWUStatisticItem>& statistics);
  159. void getWorkunitEclAgentLog(const char* eclAgentInstance, const char* agentPid, MemoryBuffer& buf, const char* outFile);
  160. void getWorkunitThorLog(const char *processName, MemoryBuffer& buf, const char* outFile);
  161. void getWorkunitThorSlaveLog(const char *groupName, const char *ipAddress, const char* logDate, const char* logDir, int slaveNum, MemoryBuffer& buf, const char* outIOS, bool forDownload);
  162. void getWorkunitResTxt(MemoryBuffer& buf);
  163. void getWorkunitArchiveQuery(IStringVal& str);
  164. void getWorkunitArchiveQuery(StringBuffer& str);
  165. void getWorkunitArchiveQuery(MemoryBuffer& mb);
  166. void getWorkunitDll(StringBuffer &name, MemoryBuffer& buf);
  167. void getWorkunitXml(const char* plainText, MemoryBuffer& buf);
  168. void getWorkunitQueryShortText(MemoryBuffer& buf, const char* outFile);
  169. void getWorkunitAssociatedXml(const char* name, const char* IPAddress, const char* plainText, const char* description,
  170. bool forDownload, bool addXMLDeclaration, MemoryBuffer& buf, const char* outFile);
  171. void getWorkunitCpp(const char* cppname, const char* description, const char* ipAddress, MemoryBuffer& buf, bool forDownload, const char* outFile);
  172. void getEventScheduleFlag(IEspECLWorkunit &info);
  173. unsigned getWorkunitThorLogInfo(IArrayOf<IEspECLHelpFile>& helpers, IEspECLWorkunit &info, unsigned long flags, unsigned& helpersCount);
  174. IDistributedFile* getLogicalFileData(IEspContext& context, const char* logicalName, bool& showFileContent);
  175. IPropertyTree* getWorkunitArchive();
  176. void listArchiveFiles(IPropertyTree* archive, const char* path, IArrayOf<IEspWUArchiveModule>& modules, IArrayOf<IEspWUArchiveFile>& files);
  177. void getArchiveFile(IPropertyTree* archive, const char* moduleName, const char* attrName, const char* path, StringBuffer& file);
  178. void setWUAbortTime(IEspECLWorkunit &info, unsigned __int64 abortTS);
  179. IConstWUQuery* getEmbeddedQuery();
  180. void addTimerToList(SCMStringBuffer& name, const char * scope, IConstWUStatistic & stat, IArrayOf<IEspECLTimer>& timers);
  181. protected:
  182. unsigned getTotalThorTime();
  183. bool hasSubGraphTimings();
  184. public:
  185. IEspContext &context;
  186. Linked<IConstWorkUnit> cw;
  187. double version;
  188. SCMStringBuffer clusterName;
  189. StringAttr wuid;
  190. };
  191. void getSashaNode(SocketEndpoint &ep);
  192. struct WsWuSearch
  193. {
  194. WsWuSearch(IEspContext& context,const char* owner=NULL,const char* state=NULL,const char* cluster=NULL,const char* startDate=NULL,const char* endDate=NULL,const char* jobname=NULL);
  195. typedef std::vector<std::string>::iterator iterator;
  196. iterator begin() { return wuids.begin(); }
  197. iterator end() { return wuids.end(); }
  198. iterator locate(const char* wuid)
  199. {
  200. if(wuids.size() && *wuids.begin()>wuid)
  201. return std::lower_bound(wuids.begin(),wuids.end(),wuid,std::greater<std::string>());
  202. return wuids.begin();
  203. }
  204. __int64 getSize() { return wuids.size(); }
  205. private:
  206. StringBuffer& createWuidFromDate(const char* timestamp,StringBuffer& s);
  207. std::vector<std::string> wuids;
  208. };
  209. struct DataCacheElement: public CInterface, implements IInterface
  210. {
  211. IMPLEMENT_IINTERFACE;
  212. DataCacheElement(const char* filter, const char* data, const char* name, const char* logicalName, const char* wuid,
  213. const char* resultName, unsigned seq, __int64 start, unsigned count, __int64 requested, __int64 total):m_filter(filter),
  214. m_data(data), m_name(name), m_logicalName(logicalName), m_wuid(wuid), m_resultName(resultName),
  215. m_seq(seq), m_start(start), m_rowcount(count), m_requested(requested), m_total(total)
  216. {
  217. m_timeCached.setNow();
  218. }
  219. CDateTime m_timeCached;
  220. std::string m_filter;
  221. std::string m_data;
  222. std::string m_name;
  223. std::string m_logicalName;
  224. std::string m_wuid;
  225. std::string m_resultName;
  226. unsigned m_seq;
  227. __int64 m_start;
  228. unsigned m_rowcount;
  229. __int64 m_requested;
  230. __int64 m_total;
  231. };
  232. struct DataCache: public CInterface, implements IInterface
  233. {
  234. IMPLEMENT_IINTERFACE;
  235. DataCache(size32_t _cacheSize=0): cacheSize(_cacheSize){}
  236. DataCacheElement* lookup(IEspContext &context, const char* filter, unsigned timeOutMin);
  237. void add(const char* filter, const char* data, const char* name, const char* localName, const char* wuid,
  238. const char* resultName, unsigned seq, __int64 start, unsigned count, __int64 requested, __int64 total);
  239. std::list<Linked<DataCacheElement> > cache;
  240. CriticalSection crit;
  241. size32_t cacheSize;
  242. };
  243. interface IArchivedWUsReader : extends IInterface
  244. {
  245. virtual void getArchivedWUs(bool lightWeight, IEspWUQueryRequest& req, IEspWULightWeightQueryRequest& reqLW, IArrayOf<IEspECLWorkunit>& results, IArrayOf<IEspECLWorkunitLW>& resultsLW) = 0;
  246. virtual bool getHasMoreWU() = 0;
  247. virtual unsigned getNumberOfWUsReturned() = 0;
  248. };
  249. struct ArchivedWuCacheElement: public CInterface, implements IInterface
  250. {
  251. IMPLEMENT_IINTERFACE;
  252. ArchivedWuCacheElement(const char* filter, const char* sashaUpdatedWhen, bool hasNextPage, unsigned _numWUsReturned, IArrayOf<IEspECLWorkunit>& wus, IArrayOf<IEspECLWorkunitLW>& lwwus):m_filter(filter),
  253. m_sashaUpdatedWhen(sashaUpdatedWhen), m_hasNextPage(hasNextPage), numWUsReturned(_numWUsReturned)
  254. {
  255. m_timeCached.setNow();
  256. ForEachItemIn(i, wus)
  257. {
  258. Owned<IEspECLWorkunit> info= createECLWorkunit("","");
  259. IEspECLWorkunit& info0 = wus.item(i);
  260. info->copy(info0);
  261. m_results.append(*info.getClear());
  262. }
  263. ForEachItemIn(ii, lwwus)
  264. {
  265. Owned<IEspECLWorkunitLW> info= createECLWorkunitLW("","");
  266. IEspECLWorkunitLW& info0 = lwwus.item(ii);
  267. info->copy(info0);
  268. resultsLW.append(*info.getClear());
  269. }
  270. }
  271. std::string m_filter;
  272. std::string m_sashaUpdatedWhen;
  273. bool m_hasNextPage;
  274. unsigned numWUsReturned;
  275. CDateTime m_timeCached;
  276. IArrayOf<IEspECLWorkunit> m_results;
  277. IArrayOf<IEspECLWorkunitLW> resultsLW;
  278. };
  279. struct ArchivedWuCache: public CInterface, implements IInterface
  280. {
  281. IMPLEMENT_IINTERFACE;
  282. ArchivedWuCache(size32_t _cacheSize=0): cacheSize(_cacheSize){}
  283. ArchivedWuCacheElement* lookup(IEspContext &context, const char* filter, const char* sashaUpdatedWhen, unsigned timeOutMin);
  284. void add(const char* filter, const char* sashaUpdatedWhen, bool hasNextPage, unsigned numWUsReturned, IArrayOf<IEspECLWorkunit>& wus, IArrayOf<IEspECLWorkunitLW>& lwwus);
  285. std::list<Linked<ArchivedWuCacheElement> > cache;
  286. CriticalSection crit;
  287. size32_t cacheSize;
  288. };
  289. struct WUArchiveCacheElement: public CInterface, implements IInterface
  290. {
  291. IMPLEMENT_IINTERFACE;
  292. WUArchiveCacheElement(const char* _wuid, IPropertyTree* _archive) : wuid(_wuid)
  293. {
  294. archive.setown(_archive);
  295. timeCached.setNow();
  296. }
  297. CDateTime timeCached;
  298. std::string wuid;
  299. Owned<IPropertyTree> archive;
  300. };
  301. struct CompareWUArchive
  302. {
  303. CompareWUArchive(const char* _wuid): wuid(_wuid) {}
  304. bool operator()(const Linked<WUArchiveCacheElement>& e) const
  305. {
  306. return streq(e->wuid.c_str(), wuid.c_str());
  307. }
  308. std::string wuid;
  309. };
  310. struct WUArchiveCache: public CInterface, implements IInterface
  311. {
  312. IMPLEMENT_IINTERFACE;
  313. WUArchiveCache(size32_t _cacheSize=0): cacheSize(_cacheSize){}
  314. WUArchiveCacheElement* lookup(IEspContext &context, const char* wuid, unsigned timeOutMin)
  315. {
  316. CriticalBlock block(crit);
  317. if (cache.size() < 1)
  318. return NULL;
  319. //erase data if it should be
  320. CDateTime timeNow;
  321. int timeout = timeOutMin;
  322. timeNow.setNow();
  323. timeNow.adjustTime(-timeout);
  324. while (true)
  325. {
  326. std::list<Linked<WUArchiveCacheElement> >::iterator list_iter = cache.begin();
  327. if (list_iter == cache.end())
  328. break;
  329. WUArchiveCacheElement* wuArchive = list_iter->get();
  330. if (!wuArchive || (wuArchive->timeCached > timeNow))
  331. break;
  332. cache.pop_front();
  333. }
  334. if (cache.size() < 1)
  335. return NULL;
  336. //Check whether we have the WUArchive cache for this WU.
  337. std::list<Linked<WUArchiveCacheElement> >::iterator it = std::find_if(cache.begin(), cache.end(), CompareWUArchive(wuid));
  338. if(it!=cache.end())
  339. {
  340. return it->getLink();
  341. }
  342. return NULL;
  343. }
  344. void add(const char* _wuid, IPropertyTree* _archive)
  345. {
  346. Owned<IPropertyTree> archive = _archive;
  347. CriticalBlock block(crit);
  348. //Save new data
  349. Owned<WUArchiveCacheElement> e = new WUArchiveCacheElement(_wuid, archive.getClear());
  350. if (cacheSize > 0)
  351. {
  352. if (cache.size() >= cacheSize)
  353. cache.pop_front();
  354. cache.push_back(e.get());
  355. }
  356. return;
  357. }
  358. std::list<Linked<WUArchiveCacheElement> > cache;
  359. CriticalSection crit;
  360. size32_t cacheSize;
  361. };
  362. class WsWuJobQueueAuditInfo
  363. {
  364. public:
  365. WsWuJobQueueAuditInfo() { };
  366. WsWuJobQueueAuditInfo(IEspContext &context, const char *cluster, const char *from , const char *to, CHttpResponse* response, const char *xls);
  367. void getAuditLineInfo(const char* line, unsigned& longestQueue, unsigned& maxConnected, unsigned maxDisplay, unsigned showAll, IArrayOf<IEspThorQueue>& items);
  368. bool checkSameStrings(const char* s1, const char* s2);
  369. bool checkNewThorQueueItem(IEspThorQueue* tq, unsigned showAll, IArrayOf<IEspThorQueue>& items);
  370. };
  371. StringBuffer &getWuidFromLogicalFileName(IEspContext &context, const char *logicalName, StringBuffer &wuid);
  372. bool addToQueryString(StringBuffer &queryString, const char *name, const char *value, const char delim = '&');
  373. void xsltTransform(const char* xml, const char* sheet, IProperties *params, StringBuffer& ret);
  374. class WUSchedule : public Thread
  375. {
  376. bool stopping;
  377. Semaphore semSchedule;
  378. IEspContainer* m_container;
  379. bool detached;
  380. public:
  381. WUSchedule()
  382. {
  383. stopping = false;
  384. detached = false;
  385. m_container = nullptr;
  386. }
  387. ~WUSchedule()
  388. {
  389. stopping = true;
  390. semSchedule.signal();
  391. join();
  392. }
  393. virtual int run();
  394. virtual void setContainer(IEspContainer * container)
  395. {
  396. m_container = container;
  397. if (m_container)
  398. setDetachedState(!m_container->isAttachedToDali());
  399. }
  400. void setDetachedState(bool detached_)
  401. {
  402. if (detached != detached_)
  403. {
  404. detached = detached_;
  405. semSchedule.signal();
  406. }
  407. }
  408. };
  409. namespace WsWuHelpers
  410. {
  411. void setXmlParameters(IWorkUnit *wu, const char *xml, bool setJobname=false);
  412. void submitWsWorkunit(IEspContext& context, IConstWorkUnit* cw, const char* cluster, const char* snapshot, int maxruntime, bool compile, bool resetWorkflow, bool resetVariables,
  413. const char *paramXml=NULL, IArrayOf<IConstNamedValue> *variables=NULL, IArrayOf<IConstNamedValue> *debugs=NULL, IArrayOf<IConstApplicationValue> *applications=NULL);
  414. void setXmlParameters(IWorkUnit *wu, const char *xml, IArrayOf<IConstNamedValue> *variables, bool setJobname=false);
  415. void submitWsWorkunit(IEspContext& context, const char *wuid, const char* cluster, const char* snapshot, int maxruntime, bool compile, bool resetWorkflow, bool resetVariables,
  416. const char *paramXml=NULL, IArrayOf<IConstNamedValue> *variables=NULL, IArrayOf<IConstNamedValue> *debugs=NULL, IArrayOf<IConstApplicationValue> *applications=NULL);
  417. void copyWsWorkunit(IEspContext &context, IWorkUnit &wu, const char *srcWuid);
  418. void runWsWorkunit(IEspContext &context, StringBuffer &wuid, const char *srcWuid, const char *cluster, const char *paramXml=NULL,
  419. IArrayOf<IConstNamedValue> *variables=NULL, IArrayOf<IConstNamedValue> *debugs=NULL, IArrayOf<IConstApplicationValue> *applications=NULL);
  420. void runWsWorkunit(IEspContext &context, IConstWorkUnit *cw, const char *srcWuid, const char *cluster, const char *paramXml=NULL,
  421. IArrayOf<IConstNamedValue> *variables=NULL, IArrayOf<IConstNamedValue> *debugs=NULL, IArrayOf<IConstApplicationValue> *applications=NULL);
  422. IException * noteException(IWorkUnit *wu, IException *e, ErrorSeverity level=SeverityError);
  423. StringBuffer & resolveQueryWuid(StringBuffer &wuid, const char *queryset, const char *query, bool notSuspended=true, IWorkUnit *wu=NULL);
  424. void runWsWuQuery(IEspContext &context, IConstWorkUnit *cw, const char *queryset, const char *query, const char *cluster, const char *paramXml=NULL,
  425. IArrayOf<IConstApplicationValue> *applications=NULL);
  426. void runWsWuQuery(IEspContext &context, StringBuffer &wuid, const char *queryset, const char *query, const char *cluster, const char *paramXml=NULL,
  427. IArrayOf<IConstApplicationValue> *applications=NULL);
  428. void checkAndTrimWorkunit(const char* methodName, StringBuffer& input);
  429. };
  430. class NewWsWorkunit : public Owned<IWorkUnit>
  431. {
  432. public:
  433. NewWsWorkunit(IWorkUnitFactory *factory, IEspContext &context)
  434. {
  435. create(factory, context, NULL);
  436. }
  437. NewWsWorkunit(IEspContext &context)
  438. {
  439. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  440. create(factory, context, NULL);
  441. }
  442. NewWsWorkunit(IEspContext &context, const char *wuid)
  443. {
  444. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  445. create(factory, context, wuid);
  446. }
  447. ~NewWsWorkunit() { if (get()) get()->commit(); }
  448. void create(IWorkUnitFactory *factory, IEspContext &context, const char *wuid)
  449. {
  450. if (wuid && *wuid)
  451. setown(factory->createNamedWorkUnit(wuid, "ws_workunits", context.queryUserId()));
  452. else
  453. setown(factory->createWorkUnit("ws_workunits", context.queryUserId()));
  454. if(!get())
  455. throw MakeStringException(ECLWATCH_CANNOT_CREATE_WORKUNIT,"Could not create workunit.");
  456. get()->setUser(context.queryUserId());
  457. }
  458. void associateDll(const char *dllpath, const char *dllname)
  459. {
  460. Owned<IWUQuery> query = get()->updateQuery();
  461. StringBuffer dllurl;
  462. createUNCFilename(dllpath, dllurl);
  463. unsigned crc = crc_file(dllpath);
  464. associateLocalFile(query, FileTypeDll, dllpath, "Workunit DLL", crc);
  465. queryDllServer().registerDll(dllname, "Workunit DLL", dllurl.str());
  466. }
  467. void setQueryText(const char *text)
  468. {
  469. if (!text || !*text)
  470. return;
  471. Owned<IWUQuery> query=get()->updateQuery();
  472. query->setQueryText(text);
  473. }
  474. void setQueryMain(const char *s)
  475. {
  476. if (!s || !*s)
  477. return;
  478. Owned<IWUQuery> query=get()->updateQuery();
  479. query->setQueryMainDefinition(s);
  480. }
  481. };
  482. struct CWsWuZAPInfoReq
  483. {
  484. StringBuffer wuid, espIP, url, thorIP, problemDesc, whatChanged, whereSlow, includeThorSlaveLog, zapFileName, password;
  485. StringBuffer emailFrom, emailTo, emailServer, emailSubject, emailBody;
  486. bool sendEmail, attachZAPReportToEmail;
  487. unsigned maxAttachmentSize, port;
  488. };
  489. class CWsWuFileHelper
  490. {
  491. IPropertyTree* directories;
  492. void cleanFolder(IFile *folder, bool removeFolder);
  493. int zipAFolder(const char *folder, const char *passwordReq, const char *zipFileNameWithPath);
  494. int zipAFolder(const char *folder, bool gzip, const char *zipFileNameWithPath);
  495. void setZAPFile(const char *zipFileNameReq, const char *zipFileNamePrefix, StringBuffer &zipFileName,
  496. StringBuffer &zipFileNameWithPath);
  497. void writeToFile(const char *fileName, size32_t contentLength, const void *content);
  498. void writeToFileIOStream(const char *folder, const char *file, MemoryBuffer &mb);
  499. void readWUFile(const char *wuid, const char *zipFolder, WsWuInfo &winfo, IConstWUFileOption &item,
  500. StringBuffer &fileName, StringBuffer &fileMimeType);
  501. IFile *createWorkingFolder(IEspContext &context, const char *wuid, const char *namePrefix,
  502. StringBuffer &namePrefixStr, StringBuffer &folderName);
  503. void createZAPInfoFile(const char *url, const char *espIP, const char *thorIP, const char *problemDesc,
  504. const char *whatChanged, const char *timing, Owned<IConstWorkUnit> &cwu, const char *pathNameStr);
  505. void createZAPWUXMLFile(WsWuInfo &winfo, const char *pathNameStr);
  506. void createZAPECLQueryArchiveFiles(Owned<IConstWorkUnit> &cwu, const char *pathNameStr);
  507. void createZAPWUGraphProgressFile(const char *wuid, const char *pathNameStr);
  508. void createProcessLogfile(Owned<IConstWorkUnit> &cwu, WsWuInfo &winfo, const char *process, const char *path);
  509. void createThorSlaveLogfile(Owned<IConstWorkUnit> &cwu, WsWuInfo &winfo, const char *path);
  510. void writeZAPWUInfoToIOStream(IFileIOStream *outFile, const char *name, SCMStringBuffer &value);
  511. void writeZAPWUInfoToIOStream(IFileIOStream *outFile, const char *name, const char *value);
  512. public:
  513. CWsWuFileHelper(IPropertyTree *_directories) : directories(_directories) {};
  514. void createWUZAPFile(IEspContext &context, Owned<IConstWorkUnit> &cwu, CWsWuZAPInfoReq &request,
  515. StringBuffer &zipFileName, StringBuffer &zipFileNameWithPath);
  516. IFileIOStream* createWUZAPFileIOStream(IEspContext &context, Owned<IConstWorkUnit> &cwu, CWsWuZAPInfoReq &request);
  517. IFileIOStream* createWUFileIOStream(IEspContext &context, const char *wuid, IArrayOf<IConstWUFileOption> &wuFileOptions,
  518. CWUFileDownloadOption &downloadOptions, StringBuffer &contentType);
  519. IFileIOStream* createIOStreamWithFileName(const char *fileNameWithPath, IFOmode mode);
  520. };
  521. class CWsWuEmailHelper
  522. {
  523. StringAttr mailServer, sender, to, subject, body;
  524. StringAttr attachmentName, mimeType;
  525. unsigned port;
  526. public:
  527. CWsWuEmailHelper(const char *_sender, const char *_to, const char *_mailServer, unsigned _port)
  528. : sender(_sender), to(_to), mailServer(_mailServer), port(_port) {};
  529. void setSubject(const char *_subject) { subject.set(_subject); };
  530. void setMimeType(const char *_mimeType) { mimeType.set(_mimeType); };
  531. void setAttachmentName(const char *_attachmentName) { attachmentName.set(_attachmentName); };
  532. void send(const char *body, const void *attachment, size32_t lenAttachment, StringArray &warnings);
  533. };
  534. }
  535. #endif