ws_workunitsHelpers.cpp 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065
  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. #include "jlib.hpp"
  14. #include "ws_workunitsHelpers.hpp"
  15. #include "exception_util.hpp"
  16. #include "daclient.hpp"
  17. #include "dalienv.hpp"
  18. #include "daaudit.hpp"
  19. #include "portlist.h"
  20. #include "dadfs.hpp"
  21. #include "fileview.hpp"
  22. #include "wuwebview.hpp"
  23. #include "dllserver.hpp"
  24. #include "wujobq.hpp"
  25. #include "hqlexpr.hpp"
  26. #include "rmtsmtp.hpp"
  27. #ifndef _NO_LDAP
  28. #include "ldapsecurity.ipp"
  29. #endif
  30. #ifdef _USE_ZLIB
  31. #include "zcrypt.hpp"
  32. #endif
  33. namespace ws_workunits {
  34. const char * const timerFilterText = "measure[time],source[global],depth[1,]"; // Does not include hthor subgraph timings
  35. const char* zipFolder = "tempzipfiles" PATHSEPSTR;
  36. SecAccessFlags chooseWuAccessFlagsByOwnership(const char *user, const char *owner, SecAccessFlags accessOwn, SecAccessFlags accessOthers)
  37. {
  38. return (isEmpty(owner) || (user && streq(user, owner))) ? accessOwn : accessOthers;
  39. }
  40. SecAccessFlags chooseWuAccessFlagsByOwnership(const char *user, IConstWorkUnitInfo& cw, SecAccessFlags accessOwn, SecAccessFlags accessOthers)
  41. {
  42. return chooseWuAccessFlagsByOwnership(user, cw.queryUser(), accessOwn, accessOthers);
  43. }
  44. const char *getWuAccessType(const char *owner, const char *user)
  45. {
  46. return (isEmpty(owner) || (user && streq(user, owner))) ? OWN_WU_ACCESS : OTHERS_WU_ACCESS;
  47. }
  48. const char *getWuAccessType(IConstWorkUnit& cw, const char *user)
  49. {
  50. return getWuAccessType(cw.queryUser(), user);
  51. }
  52. void getUserWuAccessFlags(IEspContext& context, SecAccessFlags& accessOwn, SecAccessFlags& accessOthers, bool except)
  53. {
  54. if (!context.authorizeFeature(OWN_WU_ACCESS, accessOwn))
  55. accessOwn = SecAccess_None;
  56. if (!context.authorizeFeature(OTHERS_WU_ACCESS, accessOthers))
  57. accessOthers = SecAccess_None;
  58. if (except && (accessOwn == SecAccess_None) && (accessOthers == SecAccess_None))
  59. {
  60. context.setAuthStatus(AUTH_STATUS_NOACCESS);
  61. AuditSystemAccess(context.queryUserId(), false, "Access Denied: User can't view any workunits");
  62. VStringBuffer msg("Access Denied: User %s does not have rights to access workunits.", context.queryUserId());
  63. throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "%s", msg.str());
  64. }
  65. }
  66. SecAccessFlags getWsWorkunitAccess(IEspContext& ctx, IConstWorkUnit& cw)
  67. {
  68. SecAccessFlags accessFlag = SecAccess_None;
  69. ctx.authorizeFeature(getWuAccessType(cw, ctx.queryUserId()), accessFlag);
  70. return accessFlag;
  71. }
  72. bool validateWsWorkunitAccess(IEspContext& ctx, const char* wuid, SecAccessFlags minAccess)
  73. {
  74. Owned<IWorkUnitFactory> wf = getWorkUnitFactory(ctx.querySecManager(), ctx.queryUser());
  75. Owned<IConstWorkUnit> cw = wf->openWorkUnit(wuid);
  76. if (!cw)
  77. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Failed to open workunit %s when validating workunit access", wuid);
  78. return ctx.validateFeatureAccess(getWuAccessType(*cw, ctx.queryUserId()), minAccess, false);
  79. }
  80. void ensureWsWorkunitAccessByOwnerId(IEspContext& ctx, const char* owner, SecAccessFlags minAccess)
  81. {
  82. if (!ctx.validateFeatureAccess(getWuAccessType(owner, ctx.queryUserId()), minAccess, false))
  83. {
  84. ctx.setAuthStatus(AUTH_STATUS_NOACCESS);
  85. throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to access workunit. Permission denied.");
  86. }
  87. }
  88. void ensureWsWorkunitAccess(IEspContext& ctx, IConstWorkUnit& cw, SecAccessFlags minAccess)
  89. {
  90. if (!ctx.validateFeatureAccess(getWuAccessType(cw, ctx.queryUserId()), minAccess, false))
  91. {
  92. ctx.setAuthStatus(AUTH_STATUS_NOACCESS);
  93. throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to access workunit. Permission denied.");
  94. }
  95. }
  96. void ensureWsWorkunitAccess(IEspContext& context, const char* wuid, SecAccessFlags minAccess)
  97. {
  98. Owned<IWorkUnitFactory> wf = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  99. Owned<IConstWorkUnit> cw = wf->openWorkUnit(wuid);
  100. if (!cw)
  101. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Failed to open workunit %s when ensuring workunit access", wuid);
  102. ensureWsWorkunitAccess(context, *cw, minAccess);
  103. }
  104. void ensureWsCreateWorkunitAccess(IEspContext& ctx)
  105. {
  106. if (!ctx.validateFeatureAccess(OWN_WU_ACCESS, SecAccess_Write, false))
  107. {
  108. ctx.setAuthStatus(AUTH_STATUS_NOACCESS);
  109. throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to create workunit. Permission denied.");
  110. }
  111. }
  112. StringBuffer &getWuidFromLogicalFileName(IEspContext &context, const char *logicalName, StringBuffer &wuid)
  113. {
  114. Owned<IUserDescriptor> userdesc = createUserDescriptor();
  115. userdesc->set(context.queryUserId(), context.queryPassword(), context.querySignature());
  116. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName, userdesc, false, false, false, nullptr, defaultPrivilegedUser);
  117. if (!df)
  118. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",logicalName);
  119. return wuid.append(df->queryAttributes().queryProp("@workunit"));
  120. }
  121. void formatDuration(StringBuffer &s, unsigned ms)
  122. {
  123. unsigned days = ms / (1000*60*60*24);
  124. ms %= (1000*60*60*24);
  125. unsigned hours = ms / (1000*60*60);
  126. ms %= (1000*60*60);
  127. unsigned mins = ms / (1000*60);
  128. ms %= (1000*60);
  129. unsigned secs = ms / 1000;
  130. ms %= 1000;
  131. if (days)
  132. s.appendf("%d days ", days);
  133. if (hours || s.length())
  134. s.appendf("%d:", hours);
  135. if (mins || s.length())
  136. s.appendf("%d:", mins);
  137. if (s.length())
  138. s.appendf("%02d.%03d", secs, ms);
  139. else
  140. s.appendf("%d.%03d", secs, ms);
  141. }
  142. WsWUExceptions::WsWUExceptions(IConstWorkUnit& wu): numerr(0), numwrn(0), numinf(0), numalert(0)
  143. {
  144. Owned<IConstWUExceptionIterator> it = &wu.getExceptions();
  145. ForEach(*it)
  146. {
  147. IConstWUException & cur = it->query();
  148. SCMStringBuffer src, msg, file;
  149. Owned<IEspECLException> e= createECLException("","");
  150. e->setCode(cur.getExceptionCode());
  151. e->setSource(cur.getExceptionSource(src).str());
  152. e->setMessage(cur.getExceptionMessage(msg).str());
  153. e->setFileName(cur.getExceptionFileName(file).str());
  154. e->setLineNo(cur.getExceptionLineNo());
  155. e->setColumn(cur.getExceptionColumn());
  156. if (cur.getActivityId())
  157. e->setActivity(cur.getActivityId());
  158. if (cur.getPriority())
  159. e->setPriority(cur.getPriority());
  160. e->setScope(cur.queryScope());
  161. const char * label = "";
  162. switch (cur.getSeverity())
  163. {
  164. default:
  165. case SeverityError: label = "Error"; numerr++; break;
  166. case SeverityWarning: label = "Warning"; numwrn++; break;
  167. case SeverityInformation: label = "Info"; numinf++; break;
  168. case SeverityAlert: label = "Alert"; numalert++; break;
  169. }
  170. e->setSeverity(label);
  171. errors.append(*e.getLink());
  172. }
  173. }
  174. #define SDS_LOCK_TIMEOUT 30000
  175. void getSashaNode(SocketEndpoint &ep)
  176. {
  177. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  178. Owned<IConstEnvironment> env = factory->openEnvironment();
  179. Owned<IPropertyTree> root = &env->getPTree();
  180. IPropertyTree *pt = root->queryPropTree("Software/SashaServerProcess[1]/Instance[1]");
  181. if (!pt)
  182. throw MakeStringException(ECLWATCH_ARCHIVE_SERVER_NOT_FOUND, "Archive Server not found.");
  183. ep.set(pt->queryProp("@netAddress"), pt->getPropInt("@port",DEFAULT_SASHA_PORT));
  184. }
  185. void WsWuInfo::getSourceFiles(IEspECLWorkunit &info, unsigned long flags)
  186. {
  187. if (!(flags & WUINFO_IncludeSourceFiles))
  188. return;
  189. try
  190. {
  191. Owned<IUserDescriptor> userdesc;
  192. StringBuffer username;
  193. context.getUserID(username);
  194. const char* passwd = context.queryPassword();
  195. userdesc.setown(createUserDescriptor());
  196. userdesc->set(username.str(), passwd, context.querySignature());
  197. IArrayOf<IEspECLSourceFile> files;
  198. if (version < 1.27)
  199. {
  200. Owned<IPropertyTreeIterator> f=&cw->getFilesReadIterator();
  201. ForEach(*f)
  202. {
  203. IPropertyTree &query = f->query();
  204. const char *clusterName = query.queryProp("@cluster");
  205. const char *fileName = query.queryProp("@name");
  206. int fileCount = query.getPropInt("@useCount");
  207. Owned<IEspECLSourceFile> file= createECLSourceFile("","");
  208. if(clusterName && *clusterName)
  209. {
  210. file->setFileCluster(clusterName);
  211. }
  212. if (version > 1.11)
  213. {
  214. Owned<IPropertyTreeIterator> filetrees= query.getElements("Subfile");
  215. if (filetrees->first())
  216. file->setIsSuperFile(true);
  217. }
  218. if (fileName && *fileName)
  219. {
  220. file->setName(fileName);
  221. }
  222. file->setCount(fileCount);
  223. files.append(*file.getLink());
  224. }
  225. }
  226. else
  227. {
  228. StringArray fileNames;
  229. Owned<IPropertyTreeIterator> f=&cw->getFilesReadIterator();
  230. ForEach(*f)
  231. {
  232. IPropertyTree &query = f->query();
  233. const char *clusterName = query.queryProp("@cluster");
  234. const char *fileName = query.queryProp("@name");
  235. int fileCount = query.getPropInt("@useCount");
  236. bool bFound = false;
  237. if (fileName && *fileName && (fileNames.length() > 0))
  238. {
  239. for (unsigned i = 0; i < fileNames.length(); i++ ) // MORE - unnecessary n^2 process
  240. {
  241. const char *fileName0 = fileNames.item(i);
  242. if (!stricmp(fileName, fileName0))
  243. {
  244. bFound = true;
  245. break;
  246. }
  247. }
  248. }
  249. if (bFound)
  250. continue;
  251. Owned<IEspECLSourceFile> file= createECLSourceFile("","");
  252. if(clusterName && *clusterName)
  253. {
  254. file->setFileCluster(clusterName);
  255. }
  256. if (fileName && *fileName)
  257. {
  258. file->setName(fileName);
  259. }
  260. file->setCount(fileCount);
  261. Owned<IPropertyTreeIterator> filetrees= query.getElements("Subfile");
  262. if (filetrees->first())
  263. {
  264. file->setIsSuperFile(true);
  265. getSubFiles(filetrees, file, fileNames);
  266. }
  267. files.append(*file.getLink());
  268. }
  269. }
  270. info.setSourceFiles(files);
  271. }
  272. catch(IException* e)
  273. {
  274. StringBuffer eMsg;
  275. IERRLOG("%s", e->errorMessage(eMsg).str());
  276. info.setSourceFilesDesc(eMsg.str());
  277. e->Release();
  278. }
  279. }
  280. void WsWuInfo::getExceptions(IEspECLWorkunit &info, unsigned long flags)
  281. {
  282. if ((flags & WUINFO_IncludeExceptions) || version > 1.16)
  283. {
  284. WsWUExceptions errors(*cw);
  285. if (version > 1.16)
  286. {
  287. info.setErrorCount(errors.ErrCount());
  288. info.setWarningCount(errors.WrnCount());
  289. info.setInfoCount(errors.InfCount());
  290. info.setAlertCount(errors.AlertCount());
  291. }
  292. if ((flags & WUINFO_IncludeExceptions))
  293. info.setExceptions(errors);
  294. }
  295. }
  296. void WsWuInfo::getVariables(IEspECLWorkunit &info, unsigned long flags)
  297. {
  298. if (!(flags & WUINFO_IncludeVariables))
  299. return;
  300. try
  301. {
  302. IArrayOf<IEspECLResult> results;
  303. Owned<IConstWUResultIterator> vars = &cw->getVariables();
  304. ForEach(*vars)
  305. getResult(vars->query(), results, flags);
  306. info.setVariables(results);
  307. results.kill();
  308. }
  309. catch(IException* e)
  310. {
  311. StringBuffer eMsg;
  312. IERRLOG("%s", e->errorMessage(eMsg).str());
  313. info.setVariablesDesc(eMsg.str());
  314. e->Release();
  315. }
  316. }
  317. void WsWuInfo::addTimerToList(SCMStringBuffer& name, const char * scope, IConstWUStatistic & stat, IArrayOf<IEspECLTimer>& timers)
  318. {
  319. StringBuffer fd;
  320. formatStatistic(fd, stat.getValue(), stat.getMeasure());
  321. Owned<IEspECLTimer> t= createECLTimer("","");
  322. name.s.replace('_', ' '); // yuk!
  323. t->setName(name.str());
  324. t->setValue(fd.str());
  325. //Theoretically this could overflow, in practice it is unlikely - fix in the new stats interface when implemented
  326. t->setCount((unsigned)stat.getCount());
  327. if (version > 1.19)
  328. {
  329. StringAttr graphName;
  330. unsigned graphNum;
  331. unsigned subGraphNum = 0;
  332. unsigned subId = 0;
  333. if (parseGraphScope(scope, graphName, graphNum, subId) ||
  334. parseGraphTimerLabel(name.str(), graphName, graphNum, subGraphNum, subId)) // leacy
  335. {
  336. if (graphName.length() > 0)
  337. t->setGraphName(graphName);
  338. if (subId > 0)
  339. t->setSubGraphId((int)subId);
  340. }
  341. }
  342. if (version >= 1.72)
  343. {
  344. StringBuffer tsText;
  345. unsigned __int64 ts = stat.getTimestamp();
  346. formatStatistic(tsText, ts, SMeasureTimestampUs);
  347. t->setTimestamp(ts);
  348. t->setWhen(tsText.str());
  349. }
  350. timers.append(*t.getLink());
  351. }
  352. void WsWuInfo::doGetTimers(IArrayOf<IEspECLTimer>& timers)
  353. {
  354. class TimingVisitor : public WuScopeVisitorBase
  355. {
  356. public:
  357. TimingVisitor(WsWuInfo & _wuInfo, IArrayOf<IEspECLTimer>& _timers) : wuInfo(_wuInfo), timers(_timers) {}
  358. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra) override
  359. {
  360. SCMStringBuffer name;
  361. extra.getDescription(name, true);
  362. const char * scope = extra.queryScope();
  363. wuInfo.addTimerToList(name, scope, extra, timers);
  364. //Aggregate all the times spent executing graphs
  365. if ((kind == StTimeElapsed) && (extra.getScopeType() == SSTgraph))
  366. totalGraphTime.noteValue(value);
  367. }
  368. void addSummary()
  369. {
  370. if (totalGraphTime.getCount())
  371. {
  372. StringBuffer totalThorTimeText;
  373. formatStatistic(totalThorTimeText, totalGraphTime.getSum(), SMeasureTimeNs);
  374. Owned<IEspECLTimer> t= createECLTimer("","");
  375. if (wuInfo.version > 1.52)
  376. t->setName(TOTALCLUSTERTIME);
  377. else
  378. t->setName(TOTALTHORTIME);
  379. t->setValue(totalThorTimeText.str());
  380. t->setCount((unsigned)totalGraphTime.getCount());
  381. timers.append(*t.getClear());
  382. }
  383. }
  384. protected:
  385. WsWuInfo & wuInfo;
  386. IArrayOf<IEspECLTimer>& timers;
  387. StatsAggregation totalGraphTime;
  388. } visitor(*this, timers);
  389. WuScopeFilter filter(timerFilterText);
  390. Owned<IConstWUScopeIterator> it = &cw->getScopeIterator(filter);
  391. ForEach(*it)
  392. it->playProperties(visitor);
  393. visitor.addSummary();
  394. }
  395. void WsWuInfo::getTimers(IEspECLWorkunit &info, unsigned long flags)
  396. {
  397. if (!(flags & WUINFO_IncludeTimers))
  398. return;
  399. try
  400. {
  401. IArrayOf<IEspECLTimer> timers;
  402. doGetTimers(timers);
  403. info.setTimers(timers);
  404. }
  405. catch(IException* e)
  406. {
  407. StringBuffer eMsg;
  408. IERRLOG("%s", e->errorMessage(eMsg).str());
  409. info.setTimersDesc(eMsg.str());
  410. e->Release();
  411. }
  412. }
  413. class TimingCounter : public WuScopeVisitorBase
  414. {
  415. public:
  416. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & extra) override
  417. {
  418. numTimers++;
  419. if ((kind == StTimeElapsed) && (extra.getScopeType() == SSTgraph))
  420. hasGraphTiming = true;
  421. }
  422. unsigned getNumTimers() const
  423. {
  424. return numTimers + (hasGraphTiming ? 1 : 0);
  425. }
  426. protected:
  427. bool hasGraphTiming = false;
  428. unsigned numTimers = 0;
  429. };
  430. unsigned WsWuInfo::getTimerCount()
  431. {
  432. TimingCounter visitor;
  433. try
  434. {
  435. WuScopeFilter filter(timerFilterText);
  436. Owned<IConstWUScopeIterator> it = &cw->getScopeIterator(filter);
  437. ForEach(*it)
  438. it->playProperties(visitor);
  439. }
  440. catch(IException* e)
  441. {
  442. StringBuffer eMsg;
  443. IERRLOG("%s", e->errorMessage(eMsg).str());
  444. e->Release();
  445. }
  446. return visitor.getNumTimers();
  447. }
  448. EnumMapping queryFileTypes[] = {
  449. { FileTypeCpp, "cpp" },
  450. { FileTypeDll, "dll" },
  451. { FileTypeResText, "res" },
  452. { FileTypeHintXml, "hint" },
  453. { FileTypeXml, "xml" },
  454. { FileTypeLog, "log" },
  455. { FileTypeSize, NULL },
  456. };
  457. void WsWuInfo::getHelpers(IEspECLWorkunit &info, unsigned long flags)
  458. {
  459. try
  460. {
  461. IArrayOf<IEspECLHelpFile> helpers;
  462. unsigned helpersCount = 2; // ECL + Workunit XML are also helpers...
  463. Owned <IConstWUQuery> query = cw->getQuery();
  464. if(!query)
  465. {
  466. IERRLOG("Cannot get Query for this workunit.");
  467. info.setHelpersDesc("Cannot get Query for this workunit.");
  468. }
  469. else
  470. {
  471. if (flags & WUINFO_IncludeECL)
  472. {
  473. SCMStringBuffer queryText;
  474. query->getQueryShortText(queryText);
  475. if (queryText.length())
  476. {
  477. if((flags & WUINFO_TruncateEclTo64k) && (queryText.length() > 64000))
  478. queryText.setLen(queryText.str(), 64000);
  479. IEspECLQuery* q=&info.updateQuery();
  480. q->setText(queryText.str());
  481. }
  482. }
  483. if (version > 1.34)
  484. {
  485. SCMStringBuffer mainDefinition;
  486. query->getQueryMainDefinition(mainDefinition);
  487. if(mainDefinition.length())
  488. {
  489. IEspECLQuery* q=&info.updateQuery();
  490. q->setQueryMainDefinition(mainDefinition.str());
  491. }
  492. }
  493. if (version > 1.30)
  494. {
  495. info.setHasArchiveQuery(query->hasArchive());
  496. }
  497. for (unsigned i = 0; i < FileTypeSize; i++)
  498. getHelpFiles(query, (WUFileType) i, helpers, flags, helpersCount);
  499. }
  500. getWorkunitThorLogInfo(helpers, info, flags, helpersCount);
  501. if (cw->getWuidVersion() > 0)
  502. {
  503. Owned<IPropertyTreeIterator> eclAgents = cw->getProcesses("EclAgent", NULL);
  504. ForEach (*eclAgents)
  505. {
  506. StringBuffer logName;
  507. IPropertyTree& eclAgent = eclAgents->query();
  508. eclAgent.getProp("@log",logName);
  509. if (!logName.length())
  510. continue;
  511. helpersCount++;
  512. if (!(flags & WUINFO_IncludeHelpers))
  513. continue;
  514. Owned<IEspECLHelpFile> h= createECLHelpFile("","");
  515. h->setName(logName.str());
  516. h->setType(File_EclAgentLog);
  517. if (version >= 1.43)
  518. {
  519. offset_t fileSize;
  520. if (getFileSize(logName.str(), NULL, fileSize))
  521. h->setFileSize(fileSize);
  522. if (version >= 1.44)
  523. {
  524. if (eclAgent.hasProp("@pid"))
  525. h->setPID(eclAgent.getPropInt("@pid"));
  526. else
  527. h->setPID(cw->getAgentPID());
  528. }
  529. }
  530. helpers.append(*h.getLink());
  531. }
  532. }
  533. else // legacy wuid
  534. {
  535. Owned<IStringIterator> eclAgentLogs = cw->getLogs("EclAgent");
  536. ForEach (*eclAgentLogs)
  537. {
  538. SCMStringBuffer name;
  539. eclAgentLogs->str(name);
  540. if (name.length() < 1)
  541. continue;
  542. helpersCount++;
  543. if (!(flags & WUINFO_IncludeHelpers))
  544. break;
  545. Owned<IEspECLHelpFile> h= createECLHelpFile("","");
  546. h->setName(name.str());
  547. h->setType(File_EclAgentLog);
  548. if (version >= 1.43)
  549. {
  550. offset_t fileSize;
  551. if (getFileSize(name.str(), NULL, fileSize))
  552. h->setFileSize(fileSize);
  553. }
  554. helpers.append(*h.getLink());
  555. break;
  556. }
  557. }
  558. info.setHelpers(helpers);
  559. info.setHelpersCount(helpersCount);
  560. }
  561. catch(IException* e)
  562. {
  563. StringBuffer eMsg;
  564. IERRLOG("%s", e->errorMessage(eMsg).str());
  565. info.setHelpersDesc(eMsg.str());
  566. e->Release();
  567. }
  568. }
  569. void WsWuInfo::getApplicationValues(IEspECLWorkunit &info, unsigned long flags)
  570. {
  571. if (!(flags & WUINFO_IncludeApplicationValues))
  572. return;
  573. try
  574. {
  575. IArrayOf<IEspApplicationValue> av;
  576. Owned<IConstWUAppValueIterator> app(&cw->getApplicationValues());
  577. ForEach(*app)
  578. {
  579. IConstWUAppValue& val=app->query();
  580. Owned<IEspApplicationValue> t= createApplicationValue("","");
  581. t->setApplication(val.queryApplication());
  582. t->setName(val.queryName());
  583. t->setValue(val.queryValue());
  584. av.append(*t.getLink());
  585. }
  586. info.setApplicationValues(av);
  587. }
  588. catch(IException* e)
  589. {
  590. StringBuffer eMsg;
  591. IERRLOG("%s", e->errorMessage(eMsg).str());
  592. info.setApplicationValuesDesc(eMsg.str());
  593. e->Release();
  594. }
  595. }
  596. void WsWuInfo::getDebugValues(IEspECLWorkunit &info, unsigned long flags)
  597. {
  598. if (!(flags & WUINFO_IncludeDebugValues))
  599. {
  600. if (version >= 1.50)
  601. {
  602. unsigned debugValueCount = 0;
  603. Owned<IStringIterator> debugs(&cw->getDebugValues());
  604. ForEach(*debugs)
  605. debugValueCount++;
  606. info.setDebugValueCount(debugValueCount);
  607. }
  608. return;
  609. }
  610. try
  611. {
  612. IArrayOf<IEspDebugValue> dv;
  613. Owned<IStringIterator> debugs(&cw->getDebugValues());
  614. ForEach(*debugs)
  615. {
  616. SCMStringBuffer name, val;
  617. debugs->str(name);
  618. cw->getDebugValue(name.str(),val);
  619. Owned<IEspDebugValue> t= createDebugValue("","");
  620. t->setName(name.str());
  621. t->setValue(val.str());
  622. dv.append(*t.getLink());
  623. }
  624. if (version >= 1.50)
  625. info.setDebugValueCount(dv.length());
  626. info.setDebugValues(dv);
  627. }
  628. catch(IException* e)
  629. {
  630. StringBuffer eMsg;
  631. IERRLOG("%s", e->errorMessage(eMsg).str());
  632. info.setDebugValuesDesc(eMsg.str());
  633. e->Release();
  634. }
  635. }
  636. const char *getGraphNum(const char *s,unsigned &num)
  637. {
  638. while (*s && !isdigit(*s))
  639. s++;
  640. num = 0;
  641. while (isdigit(*s))
  642. {
  643. num = num*10+*s-'0';
  644. s++;
  645. }
  646. return s;
  647. }
  648. bool WsWuInfo::hasSubGraphTimings()
  649. {
  650. try
  651. {
  652. WuScopeFilter filter("depth[3],stype[subgraph],stat[TimeElapsed],nested[0]");
  653. Owned<IConstWUScopeIterator> it = &cw->getScopeIterator(filter);
  654. ForEach(*it)
  655. {
  656. stat_type value;
  657. if (it->getStat(StTimeElapsed, value))
  658. return true;
  659. }
  660. }
  661. catch(IException* e)
  662. {
  663. StringBuffer eMsg;
  664. IERRLOG("%s", e->errorMessage(eMsg).str());
  665. e->Release();
  666. }
  667. return false;
  668. }
  669. void WsWuInfo::doGetGraphs(IArrayOf<IEspECLGraph>& graphs)
  670. {
  671. SCMStringBuffer runningGraph;
  672. WUGraphIDType id;
  673. WUState st = cw->getState();
  674. bool running = (!(st==WUStateFailed || st==WUStateAborted || st==WUStateCompleted) && cw->getRunningGraph(runningGraph,id));
  675. Owned<IConstWUGraphMetaIterator> it = &cw->getGraphsMeta(GraphTypeAny);
  676. ForEach(*it)
  677. {
  678. IConstWUGraphMeta &graph = it->query();
  679. SCMStringBuffer name, label, type;
  680. graph.getName(name);
  681. graph.getLabel(label);
  682. graph.getTypeName(type);
  683. WUGraphState graphState = graph.getState();
  684. Owned<IEspECLGraph> g= createECLGraph();
  685. g->setName(name.str());
  686. g->setLabel(label.str());
  687. g->setType(type.str());
  688. if (WUGraphComplete == graphState)
  689. g->setComplete(true);
  690. else if (running && (WUGraphRunning == graphState))
  691. {
  692. g->setRunning(true);
  693. g->setRunningId(id);
  694. }
  695. else if (WUGraphFailed == graphState)
  696. g->setFailed(true);
  697. if (version >= 1.53)
  698. {
  699. //MORE: Will need to be prefixed with the wfid
  700. StringBuffer scope;
  701. scope.append(name);
  702. StringBuffer s;
  703. stat_type timeStamp;
  704. if (cw->getStatistic(timeStamp, scope.str(), StWhenStarted) ||
  705. cw->getStatistic(timeStamp, name.str(), StWhenGraphStarted))
  706. {
  707. g->setWhenStarted(formatStatistic(s.clear(), timeStamp, SMeasureTimestampUs));
  708. }
  709. if (cw->getStatistic(timeStamp, scope.str(), StWhenFinished) ||
  710. cw->getStatistic(timeStamp, name.str(), StWhenGraphFinished))
  711. {
  712. g->setWhenFinished(formatStatistic(s.clear(), timeStamp, SMeasureTimestampUs));
  713. }
  714. }
  715. graphs.append(*g.getLink());
  716. }
  717. }
  718. void WsWuInfo::getGraphInfo(IEspECLWorkunit &info, unsigned long flags)
  719. {
  720. if ((version > 1.01) && (version < 1.71))
  721. {
  722. info.setHaveSubGraphTimings(false);
  723. if (hasSubGraphTimings())
  724. info.setHaveSubGraphTimings(true);
  725. }
  726. if (!(flags & WUINFO_IncludeGraphs))
  727. return;
  728. try
  729. {
  730. IArrayOf<IEspECLGraph> graphs;
  731. doGetGraphs(graphs);
  732. info.setGraphs(graphs);
  733. }
  734. catch(IException* e)
  735. {
  736. StringBuffer eMsg;
  737. IERRLOG("%s", e->errorMessage(eMsg).str());
  738. info.setGraphsDesc(eMsg.str());
  739. e->Release();
  740. }
  741. }
  742. void WsWuInfo::getWUGraphNameAndTypes(WUGraphType graphType, IArrayOf<IEspNameAndType>& graphNameAndTypes)
  743. {
  744. Owned<IConstWUGraphMetaIterator> it = &cw->getGraphsMeta(graphType);
  745. ForEach(*it)
  746. {
  747. SCMStringBuffer name, type;
  748. IConstWUGraphMeta &graph = it->query();
  749. Owned<IEspNameAndType> nameAndType = createNameAndType();
  750. nameAndType->setName(graph.getName(name).str());
  751. nameAndType->setType(graph.getTypeName(type).str());
  752. graphNameAndTypes.append(*nameAndType.getLink());
  753. }
  754. }
  755. void WsWuInfo::getGraphTimingData(IArrayOf<IConstECLTimingData> &timingData)
  756. {
  757. class TimingVisitor : public WuScopeVisitorBase
  758. {
  759. public:
  760. TimingVisitor(WsWuInfo & _wuInfo, IArrayOf<IConstECLTimingData> & _timingData) : wuInfo(_wuInfo), timingData(_timingData) {}
  761. virtual void noteStatistic(StatisticKind kind, unsigned __int64 value, IConstWUStatistic & cur) override
  762. {
  763. const char * scope = cur.queryScope();
  764. StringAttr graphName;
  765. unsigned graphNum;
  766. unsigned subGraphId;
  767. if (parseGraphScope(scope, graphName, graphNum, subGraphId))
  768. {
  769. unsigned time = (unsigned)nanoToMilli(value);
  770. SCMStringBuffer name;
  771. cur.getDescription(name, true);
  772. Owned<IEspECLTimingData> g = createECLTimingData();
  773. g->setName(name.str());
  774. g->setGraphNum(graphNum);
  775. g->setSubGraphNum(subGraphId); // Use the Id - the number is not known
  776. g->setGID(subGraphId);
  777. g->setMS(time);
  778. g->setMin(time/60000);
  779. timingData.append(*g.getClear());
  780. }
  781. }
  782. protected:
  783. WsWuInfo & wuInfo;
  784. IArrayOf<IConstECLTimingData> & timingData;
  785. } visitor(*this, timingData);
  786. WuScopeFilter filter("stype[subgraph],stat[TimeElapsed],nested[0]");
  787. Owned<IConstWUScopeIterator> it = &cw->getScopeIterator(filter);
  788. ForEach(*it)
  789. it->playProperties(visitor);
  790. }
  791. void WsWuInfo::getServiceNames(IEspECLWorkunit &info, unsigned long flags)
  792. {
  793. if (!(flags & WUINFO_IncludeServiceNames))
  794. return;
  795. StringArray serviceNames;
  796. WuScopeFilter filter;
  797. filter.addScopeType("activity");
  798. filter.addOutputAttribute(WaServiceName);
  799. filter.addRequiredAttr(WaServiceName);
  800. filter.finishedFilter();
  801. Owned<IConstWUScopeIterator> it = &cw->getScopeIterator(filter);
  802. ForEach(*it)
  803. {
  804. StringBuffer serviceName;
  805. const char *value = it->queryAttribute(WaServiceName, serviceName);
  806. if (!isEmptyString(value))
  807. serviceNames.append(value);
  808. }
  809. info.setServiceNames(serviceNames);
  810. }
  811. void WsWuInfo::getEventScheduleFlag(IEspECLWorkunit &info)
  812. {
  813. info.setEventSchedule(0);
  814. if (info.getState() && !stricmp(info.getState(), "wait"))
  815. {
  816. info.setEventSchedule(2); //Can deschedule
  817. }
  818. else
  819. {
  820. Owned<IConstWorkflowItemIterator> it = cw->getWorkflowItems();
  821. if (it)
  822. {
  823. ForEach(*it)
  824. {
  825. IConstWorkflowItem *r = it->query();
  826. if (!r)
  827. continue;
  828. Owned<IWorkflowEvent> wfevent = r->getScheduleEvent();
  829. if (!wfevent)
  830. continue;
  831. if ((!r->hasScheduleCount() || (r->queryScheduleCountRemaining() > 0))
  832. && info.getState() && !strieq(info.getState(), "scheduled")
  833. && !strieq(info.getState(), "aborting") && !strieq(info.getState(), "aborted")
  834. && !strieq(info.getState(), "failed") && !strieq(info.getState(), "archived"))
  835. {
  836. info.setEventSchedule(1); //Can reschedule
  837. break;
  838. }
  839. }
  840. }
  841. }
  842. }
  843. unsigned WsWuInfo::getTotalThorTime()
  844. {
  845. const WuScopeFilter filter("stype[graph],nested[0],stat[TimeElapsed]");
  846. StatsAggregation summary;
  847. aggregateStatistic(summary, cw, filter, StTimeElapsed);
  848. return nanoToMilli(summary.getSum());
  849. }
  850. void WsWuInfo::getCommon(IEspECLWorkunit &info, unsigned long flags)
  851. {
  852. info.setWuid(cw->queryWuid());
  853. info.setProtected(cw->isProtected() ? 1 : 0);
  854. info.setJobname(cw->queryJobName());
  855. info.setOwner(cw->queryUser());
  856. clusterName.set(cw->queryClusterName());
  857. info.setCluster(clusterName.str());
  858. SCMStringBuffer s;
  859. info.setSnapshot(cw->getSnapshot(s).str());
  860. if ((cw->getState() == WUStateScheduled) && cw->aborting())
  861. {
  862. info.setStateID(WUStateAborting);
  863. info.setState("aborting");
  864. }
  865. else
  866. {
  867. info.setStateID(cw->getState());
  868. info.setState(cw->queryStateDesc());
  869. }
  870. if (cw->isPausing())
  871. info.setIsPausing(true);
  872. getEventScheduleFlag(info);
  873. //The TotalClusterTime should always be returned between versions 1.27 and 1.73.
  874. //After version 1.73, it should be returned only if IncludeTotalClusterTime is true.
  875. if ((version > 1.27) && ((version < 1.73) || (flags & WUINFO_IncludeTotalClusterTime)))
  876. {
  877. unsigned totalThorTimeMS = getTotalThorTime();
  878. if (totalThorTimeMS)
  879. {
  880. StringBuffer totalThorTimeStr;
  881. formatDuration(totalThorTimeStr, totalThorTimeMS);
  882. if (version > 1.52)
  883. info.setTotalClusterTime(totalThorTimeStr.str());
  884. else
  885. info.setTotalThorTime(totalThorTimeStr.str());
  886. }
  887. }
  888. WsWuDateTime dt;
  889. cw->getTimeScheduled(dt);
  890. if(dt.isValid())
  891. info.setDateTimeScheduled(dt.getString(s).str());
  892. }
  893. void WsWuInfo::setWUAbortTime(IEspECLWorkunit &info, unsigned __int64 abortTS)
  894. {
  895. StringBuffer abortTimeStr;
  896. formatStatistic(abortTimeStr, abortTS, SMeasureTimestampUs);
  897. if ((abortTimeStr.length() > 19) && (abortTimeStr.charAt(10) == 'T') && (abortTimeStr.charAt(19) == '.'))
  898. {
  899. abortTimeStr.setCharAt(10, ' ');
  900. abortTimeStr.setLength(19);
  901. }
  902. info.setAbortTime(abortTimeStr.str());
  903. }
  904. void WsWuInfo::getInfo(IEspECLWorkunit &info, unsigned long flags)
  905. {
  906. getCommon(info, flags);
  907. SecAccessFlags accessFlag = getWsWorkunitAccess(context, *cw);
  908. info.setAccessFlag(accessFlag);
  909. SCMStringBuffer s;
  910. info.setStateEx(cw->getStateEx(s).str());
  911. WUState state = cw->getState();
  912. if ((state == WUStateAborting) || (state == WUStateAborted))
  913. {
  914. unsigned __int64 abortTS = cw->getAbortTimeStamp();
  915. if (abortTS > 0) //AbortTimeStamp may not be set in old wu
  916. {
  917. setWUAbortTime(info, abortTS);
  918. cw->getAbortBy(s);
  919. if (s.length())
  920. info.setAbortBy(s.str());
  921. }
  922. }
  923. info.setPriorityClass(cw->getPriority());
  924. info.setPriorityLevel(cw->getPriorityLevel());
  925. if (context.querySecManager())
  926. info.setScope(cw->queryWuScope());
  927. info.setActionEx(cw->queryActionDesc());
  928. info.setDescription(cw->getDebugValue("description", s).str());
  929. if (version > 1.21)
  930. info.setXmlParams(cw->getXmlParams(s, true).str());
  931. info.setResultLimit(cw->getResultLimit());
  932. info.setArchived(false);
  933. info.setGraphCount(cw->getGraphCount());
  934. info.setSourceFileCount(cw->getSourceFileCount());
  935. info.setResultCount(cw->getResultCount());
  936. info.setWorkflowCount(cw->queryEventScheduledCount());
  937. info.setVariableCount(cw->getVariableCount());
  938. info.setTimerCount(getTimerCount());
  939. info.setSourceFileCount(cw->getSourceFileCount());
  940. info.setApplicationValueCount(cw->getApplicationValueCount());
  941. info.setHasDebugValue(cw->hasDebugValue("__calculated__complexity__"));
  942. getClusterInfo(info, flags);
  943. getExceptions(info, flags);
  944. getHelpers(info, flags);
  945. getGraphInfo(info, flags);
  946. getSourceFiles(info, flags);
  947. getResults(info, flags);
  948. getVariables(info, flags);
  949. getTimers(info, flags);
  950. getDebugValues(info, flags);
  951. getApplicationValues(info, flags);
  952. getWorkflow(info, flags);
  953. getServiceNames(info, flags);
  954. }
  955. unsigned WsWuInfo::getWorkunitThorLogInfo(IArrayOf<IEspECLHelpFile>& helpers, IEspECLWorkunit &info, unsigned long flags, unsigned& helpersCount)
  956. {
  957. unsigned countThorLog = 0;
  958. IArrayOf<IConstThorLogInfo> thorLogList;
  959. if (cw->getWuidVersion() > 0)
  960. {
  961. StringAttr clusterName(cw->queryClusterName());
  962. if (!clusterName.length()) //Cluster name may not be set yet
  963. return countThorLog;
  964. Owned<IConstWUClusterInfo> clusterInfo = getTargetClusterInfo(clusterName.str());
  965. if (!clusterInfo)
  966. {
  967. IWARNLOG("Cannot find TargetClusterInfo for workunit %s", cw->queryWuid());
  968. return countThorLog;
  969. }
  970. unsigned numberOfSlaveLogs = clusterInfo->getNumberOfSlaveLogs();
  971. BoolHash uniqueProcesses;
  972. Owned<IStringIterator> thorInstances = cw->getProcesses("Thor");
  973. ForEach (*thorInstances)
  974. {
  975. SCMStringBuffer processName;
  976. thorInstances->str(processName);
  977. if (processName.length() < 1)
  978. continue;
  979. bool* found = uniqueProcesses.getValue(processName.str());
  980. if (found && *found)
  981. continue;
  982. uniqueProcesses.setValue(processName.str(), true);
  983. StringBuffer groupName;
  984. getClusterThorGroupName(groupName, processName.str());
  985. Owned<IStringIterator> thorLogs = cw->getLogs("Thor", processName.str());
  986. ForEach (*thorLogs)
  987. {
  988. SCMStringBuffer logName;
  989. thorLogs->str(logName);
  990. if (logName.length() < 1)
  991. continue;
  992. countThorLog++;
  993. StringBuffer fileType;
  994. if (countThorLog < 2)
  995. fileType.append(File_ThorLog);
  996. else
  997. fileType.appendf("%s%d", File_ThorLog, countThorLog);
  998. helpersCount++;
  999. if (flags & WUINFO_IncludeHelpers)
  1000. {
  1001. Owned<IEspECLHelpFile> h= createECLHelpFile("","");
  1002. h->setName(logName.str());
  1003. h->setDescription(processName.str());
  1004. h->setType(fileType.str());
  1005. if (version >= 1.43)
  1006. {
  1007. offset_t fileSize;
  1008. if (getFileSize(logName.str(), NULL, fileSize))
  1009. h->setFileSize(fileSize);
  1010. }
  1011. helpers.append(*h.getLink());
  1012. }
  1013. if (version < 1.38)
  1014. continue;
  1015. const char* pStr = logName.str();
  1016. const char* ppStr = strstr(pStr, "/thormaster.");
  1017. if (!ppStr)
  1018. {
  1019. IWARNLOG("Invalid thorlog entry in workunit xml: %s", logName.str());
  1020. continue;
  1021. }
  1022. ppStr += 12;
  1023. StringBuffer logDate(ppStr);
  1024. logDate.setLength(10);
  1025. Owned<IEspThorLogInfo> thorLog = createThorLogInfo("","");
  1026. thorLog->setProcessName(processName.str());
  1027. thorLog->setClusterGroup(groupName.str());
  1028. thorLog->setLogDate(logDate.str());
  1029. thorLog->setNumberSlaves(numberOfSlaveLogs);
  1030. thorLogList.append(*thorLog.getLink());
  1031. }
  1032. }
  1033. }
  1034. else //legacy wuid
  1035. {
  1036. Owned<IStringIterator> thorLogs = cw->getLogs("Thor");
  1037. ForEach (*thorLogs)
  1038. {
  1039. SCMStringBuffer name;
  1040. thorLogs->str(name);
  1041. if (name.length() < 1)
  1042. continue;
  1043. countThorLog++;
  1044. StringBuffer fileType;
  1045. if (countThorLog < 2)
  1046. fileType.append(File_ThorLog);
  1047. else
  1048. fileType.appendf("%s%d", File_ThorLog, countThorLog);
  1049. helpersCount++;
  1050. if (flags & WUINFO_IncludeHelpers)
  1051. {
  1052. Owned<IEspECLHelpFile> h= createECLHelpFile("","");
  1053. h->setName(name.str());
  1054. h->setType(fileType.str());
  1055. if (version >= 1.43)
  1056. {
  1057. offset_t fileSize;
  1058. if (getFileSize(name.str(), NULL, fileSize))
  1059. h->setFileSize(fileSize);
  1060. }
  1061. helpers.append(*h.getLink());
  1062. }
  1063. }
  1064. StringBuffer logDir;
  1065. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  1066. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  1067. Owned<IPropertyTree> logTree = &constEnv->getPTree();
  1068. if (logTree)
  1069. logTree->getProp("EnvSettings/log", logDir);
  1070. if (logDir.length() > 0)
  1071. {
  1072. Owned<IStringIterator> debugs = cw->getLogs("Thor");
  1073. ForEach(*debugs)
  1074. {
  1075. SCMStringBuffer val;
  1076. debugs->str(val);
  1077. if (val.length() < 1)
  1078. continue;
  1079. const char* pStr = val.str();
  1080. const char* ppStr = strstr(pStr, logDir.str());
  1081. if (!ppStr)
  1082. {
  1083. IWARNLOG("Invalid thorlog entry in workunit xml: %s", val.str());
  1084. continue;
  1085. }
  1086. const char* pProcessName = ppStr + logDir.length();
  1087. char sep = pProcessName[0];
  1088. StringBuffer processName(pProcessName + 1);
  1089. ppStr = strchr(pProcessName + 1, sep);
  1090. if (!ppStr)
  1091. {
  1092. IWARNLOG("Invalid thorlog entry in workunit xml: %s", val.str());
  1093. continue;
  1094. }
  1095. processName.setLength(ppStr - pProcessName - 1);
  1096. StringBuffer groupName;
  1097. getClusterThorGroupName(groupName, processName.str());
  1098. StringBuffer logDate(ppStr + 12);
  1099. logDate.setLength(10);
  1100. Owned<IEspThorLogInfo> thorLog = createThorLogInfo("","");
  1101. thorLog->setProcessName(processName.str());
  1102. thorLog->setClusterGroup(groupName.str());
  1103. thorLog->setLogDate(logDate.str());
  1104. //for legacy wuid, the log name does not contain slaveNum. So, a user may not specify
  1105. //a slaveNum and we only display the first slave log if > 1 per IP.
  1106. thorLog->setNumberSlaves(0);
  1107. thorLogList.append(*thorLog.getLink());
  1108. }
  1109. }
  1110. }
  1111. if (thorLogList.length() > 0)
  1112. info.setThorLogList(thorLogList);
  1113. thorLogList.kill();
  1114. return countThorLog;
  1115. }
  1116. bool WsWuInfo::getClusterInfo(IEspECLWorkunit &info, unsigned long flags)
  1117. {
  1118. if ((flags & WUINFO_IncludeAllowedClusters) && (version > 1.04))
  1119. {
  1120. StringArray allowedClusters;
  1121. SCMStringBuffer val;
  1122. cw->getAllowedClusters(val);
  1123. if (val.length() > 0)
  1124. {
  1125. const char* ptr = val.str();
  1126. while(*ptr != '\0')
  1127. {
  1128. StringBuffer onesub;
  1129. while(*ptr != '\0' && *ptr != ',')
  1130. {
  1131. onesub.append((char)(*ptr));
  1132. ptr++;
  1133. }
  1134. if(onesub.length() > 0)
  1135. allowedClusters.append(onesub.str());
  1136. if(*ptr != '\0')
  1137. ptr++;
  1138. }
  1139. }
  1140. if (allowedClusters.length() > 0)
  1141. info.setAllowedClusters(allowedClusters);
  1142. }
  1143. if (version > 1.23 && clusterName.length())
  1144. {
  1145. int clusterTypeFlag = 0;
  1146. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  1147. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  1148. Owned<IPropertyTree> root = &constEnv->getPTree();
  1149. Owned<IConstWUClusterInfo> clusterInfo = getTargetClusterInfo(clusterName.str());
  1150. if (clusterInfo.get())
  1151. {//Set thor flag or roxie flag in order to display some options for thor or roxie
  1152. ClusterType platform = clusterInfo->getPlatform();
  1153. if (isThorCluster(platform))
  1154. {
  1155. clusterTypeFlag=1;
  1156. if (version > 1.29)
  1157. info.setThorLCR(ThorLCRCluster == platform);
  1158. }
  1159. else if (RoxieCluster == platform)
  1160. clusterTypeFlag=2;
  1161. }
  1162. info.setClusterFlag(clusterTypeFlag);
  1163. }
  1164. return true;
  1165. }
  1166. void WsWuInfo::getWorkflow(IEspECLWorkunit &info, unsigned long flags)
  1167. {
  1168. if (!(flags & WUINFO_IncludeWorkflows))
  1169. return;
  1170. try
  1171. {
  1172. Owned<IConstWorkflowItemIterator> it = cw->getWorkflowItems();
  1173. if (!it)
  1174. return;
  1175. IArrayOf<IConstECLWorkflow> workflows;
  1176. ForEach(*it)
  1177. {
  1178. IConstWorkflowItem* r = it->query();
  1179. if (!r)
  1180. continue;
  1181. IWorkflowEvent* wfevent = r->getScheduleEvent();
  1182. if (!wfevent)
  1183. continue;
  1184. StringBuffer id;
  1185. Owned<IEspECLWorkflow> g = createECLWorkflow();
  1186. g->setWFID(id.appendf("%d", r->queryWfid()).str());
  1187. g->setEventName(wfevent->queryName());
  1188. g->setEventText(wfevent->queryText());
  1189. if (r->hasScheduleCount())
  1190. {
  1191. g->setCount(r->queryScheduleCount());
  1192. g->setCountRemaining(r->queryScheduleCountRemaining());
  1193. }
  1194. workflows.append(*g.getLink());
  1195. }
  1196. if (workflows.length() > 0)
  1197. info.setWorkflows(workflows);
  1198. }
  1199. catch(IException* e)
  1200. {
  1201. StringBuffer eMsg;
  1202. IERRLOG("%s", e->errorMessage(eMsg).str());
  1203. info.setWorkflowsDesc(eMsg.str());
  1204. e->Release();
  1205. }
  1206. }
  1207. IDistributedFile* WsWuInfo::getLogicalFileData(IEspContext& context, const char* logicalName, bool& showFileContent)
  1208. {
  1209. StringBuffer username;
  1210. context.getUserID(username);
  1211. Owned<IUserDescriptor> userdesc(createUserDescriptor());
  1212. userdesc->set(username.str(), context.queryPassword(), context.querySignature());
  1213. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName, userdesc, false, false, false, nullptr, defaultPrivilegedUser);
  1214. if (!df)
  1215. return NULL;
  1216. bool blocked;
  1217. if (df->isCompressed(&blocked) && !blocked)
  1218. return df.getClear();
  1219. IPropertyTree& properties = df->queryAttributes();
  1220. const char * format = properties.queryProp("@format");
  1221. if (format && (stricmp(format,"csv")==0 || memicmp(format, "utf", 3) == 0))
  1222. {
  1223. showFileContent = true;
  1224. return df.getClear();
  1225. }
  1226. const char * recordEcl = properties.queryProp("ECL");
  1227. if (!recordEcl)
  1228. return df.getClear();
  1229. MultiErrorReceiver errs;
  1230. Owned<IHqlExpression> ret = ::parseQuery(recordEcl, &errs);
  1231. showFileContent = errs.errCount() == 0;
  1232. return df.getClear();
  1233. }
  1234. void WsWuInfo::getEclSchemaChildFields(IArrayOf<IEspECLSchemaItem>& schemas, IHqlExpression * expr, bool isConditional)
  1235. {
  1236. if(!expr)
  1237. return;
  1238. ForEachChild(idx, expr)
  1239. getEclSchemaFields(schemas, expr->queryChild(idx), isConditional);
  1240. }
  1241. void WsWuInfo::getEclSchemaFields(IArrayOf<IEspECLSchemaItem>& schemas, IHqlExpression * expr, bool isConditional)
  1242. {
  1243. if(!expr)
  1244. return;
  1245. int ret = expr->getOperator();
  1246. switch (ret)
  1247. {
  1248. case no_record:
  1249. getEclSchemaChildFields(schemas, expr, isConditional);
  1250. break;
  1251. case no_ifblock:
  1252. {
  1253. getEclSchemaChildFields(schemas, expr->queryChild(1), true);
  1254. break;
  1255. }
  1256. case no_field:
  1257. {
  1258. if (expr->hasAttribute(__ifblockAtom))
  1259. break;
  1260. ITypeInfo * type = expr->queryType();
  1261. IAtom * name = expr->queryName();
  1262. IHqlExpression * nameAttr = expr->queryAttribute(namedAtom);
  1263. StringBuffer outname;
  1264. if (nameAttr && nameAttr->queryChild(0) && nameAttr->queryChild(0)->queryValue())
  1265. nameAttr->queryChild(0)->queryValue()->getStringValue(outname);
  1266. else
  1267. outname.append(name).toLowerCase();
  1268. if(type)
  1269. {
  1270. type_t tc = type->getTypeCode();
  1271. if (tc == type_row)
  1272. {
  1273. getEclSchemaChildFields(schemas, expr->queryRecord(), isConditional);
  1274. }
  1275. else
  1276. {
  1277. if (type->getTypeCode() == type_alien)
  1278. {
  1279. IHqlAlienTypeInfo * alien = queryAlienType(type);
  1280. type = alien->queryPhysicalType();
  1281. }
  1282. Owned<IEspECLSchemaItem> schema = createECLSchemaItem("","");
  1283. StringBuffer eclType;
  1284. type->getECLType(eclType);
  1285. schema->setColumnName(outname);
  1286. schema->setColumnType(eclType.str());
  1287. schema->setColumnTypeCode(tc);
  1288. schema->setIsConditional(isConditional);
  1289. schemas.append(*schema.getClear());
  1290. }
  1291. }
  1292. break;
  1293. }
  1294. }
  1295. }
  1296. bool WsWuInfo::getResultEclSchemas(IConstWUResult &r, IArrayOf<IEspECLSchemaItem>& schemas)
  1297. {
  1298. SCMStringBuffer schema;
  1299. r.getResultEclSchema(schema);
  1300. if (!schema.length())
  1301. return false;
  1302. MultiErrorReceiver errs;
  1303. Owned<IHqlExpression> expr = ::parseQuery(schema.str(), &errs);
  1304. if (errs.errCount() != 0)
  1305. return false;
  1306. getEclSchemaFields(schemas, expr, false);
  1307. return true;
  1308. }
  1309. void WsWuInfo::getResult(IConstWUResult &r, IArrayOf<IEspECLResult>& results, unsigned long flags)
  1310. {
  1311. SCMStringBuffer name;
  1312. r.getResultName(name);
  1313. SCMStringBuffer filename;
  1314. r.getResultLogicalName(filename);
  1315. bool showFileContent = false;
  1316. Owned<IDistributedFile> df = NULL;
  1317. if (filename.length())
  1318. df.setown(getLogicalFileData(context, filename.str(), showFileContent));
  1319. StringBuffer value, link;
  1320. if (r.getResultStatus() == ResultStatusUndefined)
  1321. value.set("[undefined]");
  1322. else if (r.isResultScalar())
  1323. {
  1324. try
  1325. {
  1326. SCMStringBuffer xml;
  1327. r.getResultXml(xml, true);
  1328. Owned<IPropertyTree> props = createPTreeFromXMLString(xml.str(), ipt_caseInsensitive);
  1329. IPropertyTree *val = props->queryPropTree("Row/*");
  1330. if(val)
  1331. value.set(val->queryProp(NULL));
  1332. else
  1333. {
  1334. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
  1335. Owned<INewResultSet> result;
  1336. result.setown(resultSetFactory->createNewResultSet(&r, wuid.str()));
  1337. Owned<IResultSetCursor> cursor(result->createCursor());
  1338. cursor->first();
  1339. if (r.getResultIsAll())
  1340. {
  1341. value.set("<All/>");
  1342. }
  1343. else
  1344. {
  1345. Owned<IResultSetCursor> childCursor = cursor->getChildren(0);
  1346. if (childCursor)
  1347. {
  1348. ForEach(*childCursor)
  1349. {
  1350. StringBuffer out;
  1351. StringBufferAdaptor adaptor(out);
  1352. childCursor->getDisplayText(adaptor, 0);
  1353. if (!value.length())
  1354. value.append('[');
  1355. else
  1356. value.append(", ");
  1357. value.append('\'').append(out.str()).append('\'');
  1358. }
  1359. if (value.length())
  1360. value.append(']');
  1361. }
  1362. }
  1363. }
  1364. }
  1365. catch(...)
  1366. {
  1367. value.append("[value not available]");
  1368. }
  1369. }
  1370. else
  1371. {
  1372. value.append('[').append(r.getResultTotalRowCount()).append(" rows]");
  1373. if((r.getResultSequence()>=0) && (!filename.length() || (df && df->queryAttributes().hasProp("ECL"))))
  1374. link.append(r.getResultSequence());
  1375. }
  1376. Owned<IEspECLResult> result= createECLResult("","");
  1377. if (flags & WUINFO_IncludeEclSchemas)
  1378. {
  1379. IArrayOf<IEspECLSchemaItem> schemas;
  1380. if (getResultEclSchemas(r, schemas))
  1381. result->setECLSchemas(schemas);
  1382. }
  1383. if (flags & WUINFO_IncludeXmlSchema)
  1384. {
  1385. Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
  1386. Owned<INewResultSet> rs = resultSetFactory->createNewResultSet(&r, wuid.str());
  1387. Owned<IResultSetCursor> cursor(rs->createCursor());
  1388. SCMStringBuffer xsd;
  1389. const IResultSetMetaData & meta = cursor->queryResultSet()->getMetaData();
  1390. meta.getXmlXPathSchema(xsd, false);
  1391. result->setXmlSchema(xsd.str());
  1392. }
  1393. if (filename.length())
  1394. result->setShowFileContent(showFileContent);
  1395. result->setName(name.str());
  1396. result->setLink(link.str());
  1397. result->setSequence(r.getResultSequence());
  1398. result->setValue(value.str());
  1399. result->setFileName(filename.str());
  1400. result->setIsSupplied(r.getResultStatus() == ResultStatusSupplied);
  1401. result->setTotal(r.getResultTotalRowCount());
  1402. results.append(*result.getLink());
  1403. }
  1404. void WsWuInfo::getResults(IEspECLWorkunit &info, unsigned long flags)
  1405. {
  1406. if (!(flags & WUINFO_IncludeResults))
  1407. return;
  1408. try
  1409. {
  1410. IArrayOf<IEspECLResult> results;
  1411. Owned<IConstWUResultIterator> it = &(cw->getResults());
  1412. ForEach(*it)
  1413. {
  1414. IConstWUResult &r = it->query();
  1415. if(r.getResultSequence()>=0)
  1416. getResult(r, results, flags);
  1417. }
  1418. if (results.length())
  1419. info.setResults(results);
  1420. results.kill();
  1421. }
  1422. catch(IException* e)
  1423. {
  1424. StringBuffer eMsg;
  1425. IERRLOG("%s", e->errorMessage(eMsg).str());
  1426. info.setResultsDesc(eMsg.str());
  1427. e->Release();
  1428. }
  1429. }
  1430. class FilteredStatisticsVisitor : public WuScopeVisitorBase
  1431. {
  1432. public:
  1433. FilteredStatisticsVisitor(WsWuInfo & _wuInfo, bool _createDescriptions, IArrayOf<IEspWUStatisticItem>& _statistics, const StatisticsFilter& _statsFilter)
  1434. : wuInfo(_wuInfo), statistics(_statistics), statsFilter(_statsFilter), createDescriptions(_createDescriptions) {}
  1435. virtual void noteStatistic(StatisticKind curKind, unsigned __int64 value, IConstWUStatistic & cur) override
  1436. {
  1437. StringBuffer xmlBuf, tsValue;
  1438. SCMStringBuffer curCreator, curDescription, curFormattedValue;
  1439. StatisticCreatorType curCreatorType = cur.getCreatorType();
  1440. StatisticScopeType curScopeType = cur.getScopeType();
  1441. StatisticMeasure curMeasure = cur.getMeasure();
  1442. unsigned __int64 count = cur.getCount();
  1443. unsigned __int64 max = cur.getMax();
  1444. unsigned __int64 ts = cur.getTimestamp();
  1445. const char * curScope = cur.queryScope();
  1446. cur.getCreator(curCreator);
  1447. cur.getDescription(curDescription, createDescriptions);
  1448. cur.getFormattedValue(curFormattedValue);
  1449. Owned<IEspWUStatisticItem> wuStatistic = createWUStatisticItem();
  1450. if (!statsFilter.matches(curCreatorType, curCreator.str(), curScopeType, curScope, curMeasure, curKind, value))
  1451. return;
  1452. if (wuInfo.version > 1.61)
  1453. wuStatistic->setWuid(wuInfo.wuid);
  1454. if (curCreatorType != SCTnone)
  1455. wuStatistic->setCreatorType(queryCreatorTypeName(curCreatorType));
  1456. if (curCreator.length())
  1457. wuStatistic->setCreator(curCreator.str());
  1458. if (curScopeType != SSTnone)
  1459. wuStatistic->setScopeType(queryScopeTypeName(curScopeType));
  1460. if (!isEmpty(curScope))
  1461. wuStatistic->setScope(curScope);
  1462. if (curMeasure != SMeasureNone)
  1463. wuStatistic->setMeasure(queryMeasureName(curMeasure));
  1464. if (curKind != StKindNone)
  1465. wuStatistic->setKind(queryStatisticName(curKind));
  1466. wuStatistic->setRawValue(value);
  1467. wuStatistic->setValue(curFormattedValue.str());
  1468. if (count != 1)
  1469. wuStatistic->setCount(count);
  1470. if (max)
  1471. wuStatistic->setMax(max);
  1472. if (ts)
  1473. {
  1474. formatStatistic(tsValue, ts, SMeasureTimestampUs);
  1475. wuStatistic->setTimeStamp(tsValue.str());
  1476. }
  1477. if (curDescription.length())
  1478. wuStatistic->setDescription(curDescription.str());
  1479. statistics.append(*wuStatistic.getClear());
  1480. }
  1481. protected:
  1482. WsWuInfo & wuInfo;
  1483. const StatisticsFilter& statsFilter;
  1484. IArrayOf<IEspWUStatisticItem>& statistics;
  1485. bool createDescriptions;
  1486. };
  1487. void WsWuInfo::getStats(const WuScopeFilter & filter, const StatisticsFilter& statsFilter, bool createDescriptions, IArrayOf<IEspWUStatisticItem>& statistics)
  1488. {
  1489. FilteredStatisticsVisitor visitor(*this, createDescriptions, statistics, statsFilter);
  1490. Owned<IConstWUScopeIterator> it = &cw->getScopeIterator(filter);
  1491. ForEach(*it)
  1492. it->playProperties(visitor);
  1493. }
  1494. bool WsWuInfo::getFileSize(const char* fileName, const char* IPAddress, offset_t& fileSize)
  1495. {
  1496. if (!fileName || !*fileName)
  1497. return false;
  1498. Owned<IFile> aFile;
  1499. if (!IPAddress || !*IPAddress)
  1500. {
  1501. aFile.setown(createIFile(fileName));
  1502. }
  1503. else
  1504. {
  1505. RemoteFilename rfn;
  1506. rfn.setRemotePath(fileName);
  1507. SocketEndpoint ep(IPAddress);
  1508. rfn.setIp(ep);
  1509. aFile.setown(createIFile(rfn));
  1510. }
  1511. if (!aFile)
  1512. return false;
  1513. bool isDir;
  1514. CDateTime modtime;
  1515. if (!aFile->getInfo(isDir, fileSize, modtime) || isDir)
  1516. return false;
  1517. return true;
  1518. }
  1519. void WsWuInfo::getHelpFiles(IConstWUQuery* query, WUFileType type, IArrayOf<IEspECLHelpFile>& helpers, unsigned long flags, unsigned& helpersCount)
  1520. {
  1521. if (!query)
  1522. return;
  1523. Owned<IConstWUAssociatedFileIterator> iter = &query->getAssociatedFiles();
  1524. ForEach(*iter)
  1525. {
  1526. SCMStringBuffer name, Ip, description;
  1527. IConstWUAssociatedFile & cur = iter->query();
  1528. if (cur.getType() != type)
  1529. continue;
  1530. helpersCount++;
  1531. if (!(flags & WUINFO_IncludeHelpers))
  1532. continue;
  1533. cur.getName(name);
  1534. Owned<IEspECLHelpFile> h= createECLHelpFile("","");
  1535. h->setName(name.str());
  1536. h->setType(getEnumText(type, queryFileTypes));
  1537. if (version > 1.31)
  1538. {
  1539. cur.getIp(Ip);
  1540. h->setIPAddress(Ip.str());
  1541. cur.getDescription(description);
  1542. if ((description.length() < 1) && (name.length() > 0))
  1543. {
  1544. const char* desc = pathTail(name.str());
  1545. if (desc && *desc)
  1546. description.set(desc);
  1547. }
  1548. if (description.length() < 1)
  1549. description.set("Help File");
  1550. h->setDescription(description.str());
  1551. if (version >= 1.43)
  1552. {
  1553. offset_t fileSize;
  1554. if (getFileSize(name.str(), Ip.str(), fileSize))
  1555. h->setFileSize(fileSize);
  1556. }
  1557. if (version >= 1.58)
  1558. {
  1559. h->setMinActivityId(cur.getMinActivityId());
  1560. h->setMaxActivityId(cur.getMaxActivityId());
  1561. }
  1562. }
  1563. helpers.append(*h.getLink());
  1564. }
  1565. }
  1566. void WsWuInfo::getSubFiles(IPropertyTreeIterator* f, IEspECLSourceFile* eclSuperFile, StringArray& fileNames)
  1567. {
  1568. IArrayOf<IEspECLSourceFile> files;
  1569. ForEach(*f)
  1570. {
  1571. IPropertyTree &query = f->query();
  1572. const char *clusterName = query.queryProp("@cluster");
  1573. const char *fileName = query.queryProp("@name");
  1574. int fileCount = query.getPropInt("@useCount");
  1575. bool bFound = false;
  1576. if (fileName && *fileName && (fileNames.length() > 0)) // MORE - this is an n^2 process and as far as I can tell unnecessary as there will be no dups
  1577. {
  1578. for (unsigned i = 0; i < fileNames.length(); i++ )
  1579. {
  1580. const char *fileName0 = fileNames.item(i);
  1581. if (!stricmp(fileName, fileName0))
  1582. {
  1583. bFound = true;
  1584. break;
  1585. }
  1586. }
  1587. }
  1588. if (bFound)
  1589. continue;
  1590. Owned<IEspECLSourceFile> file= createECLSourceFile("","");
  1591. if(clusterName && *clusterName)
  1592. {
  1593. file->setFileCluster(clusterName);
  1594. }
  1595. if (fileName && *fileName)
  1596. {
  1597. file->setName(fileName);
  1598. fileNames.append(fileName);
  1599. }
  1600. file->setCount(fileCount);
  1601. Owned<IPropertyTreeIterator> filetrees= query.getElements("Subfile"); // We do not store subfiles of subfiles like this - so this code will never be triggered
  1602. if (filetrees->first())
  1603. {
  1604. file->setIsSuperFile(true);
  1605. getSubFiles(filetrees, file, fileNames);
  1606. }
  1607. files.append(*file.getLink());
  1608. }
  1609. eclSuperFile->setECLSourceFiles(files);
  1610. return;
  1611. }
  1612. bool WsWuInfo::getResourceInfo(StringArray &viewnames, StringArray &urls, unsigned long flags)
  1613. {
  1614. if (!(flags & (WUINFO_IncludeResultsViewNames | WUINFO_IncludeResourceURLs)))
  1615. return true;
  1616. try
  1617. {
  1618. Owned<IWuWebView> wv = createWuWebView(*cw, NULL, NULL, NULL, false, nullptr);
  1619. if (wv)
  1620. {
  1621. if (flags & WUINFO_IncludeResultsViewNames)
  1622. wv->getResultViewNames(viewnames);
  1623. if (flags & WUINFO_IncludeResourceURLs)
  1624. wv->getResourceURLs(urls, NULL);
  1625. }
  1626. return true;
  1627. }
  1628. catch(IException* e)
  1629. {
  1630. StringBuffer eMsg;
  1631. IERRLOG("%s", e->errorMessage(eMsg).str());
  1632. e->Release();
  1633. }
  1634. return false;
  1635. }
  1636. unsigned WsWuInfo::getResourceURLCount()
  1637. {
  1638. try
  1639. {
  1640. Owned<IWuWebView> wv = createWuWebView(*cw, NULL, NULL, NULL, false, nullptr);
  1641. if (wv)
  1642. return wv->getResourceURLCount();
  1643. }
  1644. catch(IException* e)
  1645. {
  1646. StringBuffer eMsg;
  1647. IERRLOG("%s", e->errorMessage(eMsg).str());
  1648. e->Release();
  1649. }
  1650. return 0;
  1651. }
  1652. void WsWuInfo::copyContentFromRemoteFile(const char* sourceFileName, const char* sourceIPAddress,
  1653. const char* sourceAlias, const char *outFileName)
  1654. {
  1655. RemoteFilename rfn;
  1656. rfn.setRemotePath(sourceFileName);
  1657. SocketEndpoint ep(sourceIPAddress);
  1658. rfn.setIp(ep);
  1659. OwnedIFile source = createIFile(rfn);
  1660. if (!source)
  1661. throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Cannot open %s.", sourceAlias);
  1662. OwnedIFile target = createIFile(outFileName);
  1663. if (!target)
  1664. throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Cannot open %s.", outFileName);
  1665. copyFile(target, source);
  1666. }
  1667. void WsWuInfo::readFileContent(const char* sourceFileName, const char* sourceIPAddress,
  1668. const char* sourceAlias, MemoryBuffer &mb, bool forDownload)
  1669. {
  1670. RemoteFilename rfn;
  1671. rfn.setRemotePath(sourceFileName);
  1672. SocketEndpoint ep(sourceIPAddress);
  1673. rfn.setIp(ep);
  1674. OwnedIFile source = createIFile(rfn);
  1675. if (!source)
  1676. throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Cannot open %s.", sourceAlias);
  1677. OwnedIFileIO sourceIO = source->openShared(IFOread,IFSHfull);
  1678. if (!sourceIO)
  1679. throw MakeStringException(ECLWATCH_CANNOT_READ_FILE, "Cannot open %s.", sourceAlias);
  1680. offset_t len = source->size();
  1681. if (!forDownload && (len > 640000))
  1682. len = 640000;
  1683. if (read(sourceIO, 0, len, mb) != len)
  1684. throw MakeStringException(ECLWATCH_CANNOT_READ_FILE, "Cannot read %s.", sourceAlias);
  1685. }
  1686. void WsWuInfo::getWorkunitEclAgentLog(const char* fileName, const char* agentPid, MemoryBuffer& buf, const char* outFile)
  1687. {
  1688. if(!fileName || !*fileName)
  1689. throw MakeStringException(ECLWATCH_ECLAGENT_LOG_NOT_FOUND,"Log file not specified");
  1690. Owned<IFile> rFile = createIFile(fileName);
  1691. if(!rFile)
  1692. throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Cannot open file %s.", fileName);
  1693. OwnedIFileIO rIO = rFile->openShared(IFOread,IFSHfull);
  1694. if(!rIO)
  1695. throw MakeStringException(ECLWATCH_CANNOT_READ_FILE, "Cannot read file %s.", fileName);
  1696. OwnedIFileIOStream ios = createIOStream(rIO);
  1697. Owned<IStreamLineReader> lineReader = createLineReader(ios, true);
  1698. Owned<IFileIOStream> outIOS;
  1699. if (!isEmptyString(outFile))
  1700. {
  1701. CWsWuFileHelper helper(nullptr);
  1702. outIOS.setown(helper.createIOStreamWithFileName(outFile, IFOcreate));
  1703. }
  1704. StringBuffer line;
  1705. bool wuidFound = false;
  1706. StringBuffer pidstr;
  1707. if (agentPid && *agentPid)
  1708. pidstr.appendf(" %s ", agentPid);
  1709. else
  1710. pidstr.appendf(" %5d ", cw->getAgentPID());
  1711. /*
  1712. Scan the master daily logfile for given PID/WUID. We make the following assumptions
  1713. Column ordering (time, date, pid) is unknown, but we must assume it is constant throughout the logfile.
  1714. It is assumed that the first column is the 8 digit workunit logfile line number.
  1715. Rows from concurrent workunits are intermixed.
  1716. Logfiles are searched via PID and WUID. You are not assured of a match until you have both.
  1717. PIDS and TIDS can and are reused. Beware that a TID could match the search PID.
  1718. Once you have both, you know the offset of the PID column. It is assumed this offset remains constant.
  1719. Search stops at EOF, or early exit if the search PID reappears on different WUID.
  1720. */
  1721. char const * pidchars = pidstr.str();
  1722. size32_t pidLen = pidstr.length();
  1723. unsigned pidOffset = 0;//offset of PID in logfile entry
  1724. while(!lineReader->readLine(line.clear()))
  1725. {
  1726. if (pidOffset > line.length())
  1727. continue;
  1728. //Retain all rows that match a unique program instance - by retaining all rows that match a pid
  1729. const char * pPid = strstr(line.str() + pidOffset, pidchars);
  1730. if (pPid)
  1731. {
  1732. //Check if this is a new instance using line sequence number (PIDs are often reused)
  1733. if (strncmp(line.str(), "00000000", 8) == 0)
  1734. {
  1735. if (wuidFound) //If the correct instance has been found, return that instance before the next instance.
  1736. break;
  1737. //The last instance is not a correct instance. Clean the buf in order to start a new instance.
  1738. if (isEmptyString(outFile))
  1739. buf.clear();
  1740. }
  1741. //If we spot the workunit id anywhere in the tracing for this pid then assume it is the correct instance.
  1742. if(!wuidFound && strstr(line.str(), wuid.str()))
  1743. {
  1744. pidOffset = pPid - line.str();//remember offset of PID within line
  1745. wuidFound = true;
  1746. }
  1747. if (pidOffset && 0 == strncmp(line.str() + pidOffset, pidchars, pidLen))//this makes sure the match was the PID and not the TID or something else
  1748. outputALine(line.length(), line.str(), buf, outIOS);
  1749. }
  1750. }
  1751. if (!wuidFound)
  1752. {
  1753. const char * msg = "(No logfile entries found for this workunit)";
  1754. outputALine(strlen(msg), msg, buf, outIOS);
  1755. }
  1756. }
  1757. void WsWuInfo::getWorkunitThorLog(const char* fileName, MemoryBuffer& buf, const char* outFile)
  1758. {
  1759. if(!fileName || !*fileName)
  1760. throw MakeStringException(ECLWATCH_ECLAGENT_LOG_NOT_FOUND,"Log file not specified");
  1761. Owned<IFile> rFile = createIFile(fileName);
  1762. if (!rFile)
  1763. throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE,"Cannot open file %s.",fileName);
  1764. readWorkunitLog(rFile, buf, outFile);
  1765. }
  1766. void WsWuInfo::getWorkunitThorSlaveLog(IGroup *nodeGroup, const char *ipAddress, const char* logDate,
  1767. const char* logDir, int slaveNum, MemoryBuffer& buf, const char* outFile, bool forDownload)
  1768. {
  1769. if (isEmpty(logDir))
  1770. throw MakeStringException(ECLWATCH_INVALID_INPUT,"ThorSlave log path not specified.");
  1771. if (isEmpty(logDate))
  1772. throw MakeStringException(ECLWATCH_INVALID_INPUT,"ThorSlave log date not specified.");
  1773. StringBuffer slaveIPAddress, logName;
  1774. logName.append(logDir);
  1775. addPathSepChar(logName);
  1776. if (slaveNum > 0)
  1777. {
  1778. nodeGroup->queryNode(slaveNum-1).endpoint().getIpText(slaveIPAddress);
  1779. if (slaveIPAddress.length() < 1)
  1780. throw MakeStringException(ECLWATCH_INVALID_INPUT,"ThorSlave log network address not found.");
  1781. logName.appendf("thorslave.%d.%s.log", slaveNum, logDate);
  1782. }
  1783. else
  1784. {//legacy wuid: a user types in an IP address for a thor slave
  1785. if (isEmpty(ipAddress))
  1786. throw MakeStringException(ECLWATCH_INVALID_INPUT,"ThorSlave address not specified.");
  1787. //thorslave.10.239.219.6_20100.2012_05_23.log
  1788. logName.appendf("thorslave.%s*.%s.log", ipAddress, logDate);
  1789. const char* portPtr = strchr(ipAddress, '_');
  1790. if (!portPtr)
  1791. slaveIPAddress.append(ipAddress);
  1792. else
  1793. {
  1794. StringBuffer ipAddressStr(ipAddress);
  1795. ipAddressStr.setLength(portPtr - ipAddress);
  1796. slaveIPAddress.append(ipAddressStr.str());
  1797. }
  1798. }
  1799. if (slaveNum > 0)
  1800. {
  1801. RemoteFilename rfn;
  1802. rfn.setRemotePath(logName);
  1803. SocketEndpoint ep(slaveIPAddress.str());
  1804. rfn.setIp(ep);
  1805. Owned<IFile> logfile = createIFile(rfn);
  1806. if (!logfile)
  1807. throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Cannot open %s.", logName.str());
  1808. readWorkunitLog(logfile, buf, outFile);
  1809. }
  1810. else
  1811. {//legacy wuid
  1812. readFileContent(logName, slaveIPAddress.str(), logName, buf, forDownload);
  1813. }
  1814. }
  1815. void WsWuInfo::getWorkunitThorSlaveLog(IPropertyTree* directories, const char *process,
  1816. const char* instanceName, const char *ipAddress, const char* logDate, int slaveNum,
  1817. MemoryBuffer& buf, const char* outFile, bool forDownload)
  1818. {
  1819. StringBuffer logDir, groupName;
  1820. getConfigurationDirectory(directories, "log", "thor", process, logDir);
  1821. getClusterThorGroupName(groupName, instanceName);
  1822. if (groupName.isEmpty())
  1823. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Failed to get Thor Group Name for %s", instanceName);
  1824. Owned<IGroup> nodeGroup = queryNamedGroupStore().lookup(groupName);
  1825. if (!nodeGroup || (nodeGroup->ordinality() == 0))
  1826. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Node group %s not found", groupName.str());
  1827. getWorkunitThorSlaveLog(nodeGroup, ipAddress, logDate, logDir.str(), slaveNum, buf, outFile, forDownload);
  1828. }
  1829. void WsWuInfo::readWorkunitLog(IFile* sourceFile, MemoryBuffer& buf, const char* outFile)
  1830. {
  1831. OwnedIFileIO sourceIO = sourceFile->openShared(IFOread,IFSHfull);
  1832. if (!sourceIO)
  1833. throw MakeStringException(ECLWATCH_CANNOT_READ_FILE,"Cannot read file %s.", sourceFile->queryFilename());
  1834. Owned<IFileIOStream> ios = createIOStream(sourceIO);
  1835. VStringBuffer startwuid("Started wuid=%s", wuid.str());
  1836. VStringBuffer endwuid("Finished wuid=%s", wuid.str());
  1837. StringBuffer line;
  1838. Owned<IFileIOStream> outIOS;
  1839. if (!isEmptyString(outFile))
  1840. {
  1841. CWsWuFileHelper helper(nullptr);
  1842. outIOS.setown(helper.createIOStreamWithFileName(outFile, IFOcreate));
  1843. }
  1844. Owned<IStreamLineReader> lineReader = createLineReader(ios, true);
  1845. bool eof = lineReader->readLine(line.clear());
  1846. if (eof)
  1847. return;
  1848. // Process header for log file format
  1849. unsigned logfields = getMessageFieldsFromHeader(line);
  1850. if (logfields==0) // No header line, so must be in legacy format
  1851. logfields = MSGFIELD_LEGACY;
  1852. else
  1853. eof = lineReader->readLine(line.clear());
  1854. const unsigned columnNumPID = getPositionOfField(logfields, MSGFIELD_process);
  1855. bool outputThisLine = false;
  1856. unsigned processID = 0;
  1857. bool foundEndWUID = false;
  1858. while (!eof)
  1859. {
  1860. if (outputThisLine)
  1861. {
  1862. //If the slave is restarted before WU is finished, we cannot find out the "Finished wuid=...".
  1863. //So, we should check whether the slave is restarting or not.
  1864. unsigned pID = 0;
  1865. foundEndWUID = parseLogLine(line.str(), endwuid, pID, columnNumPID);
  1866. if ((pID > 0) && (pID != processID))
  1867. break;
  1868. outputALine(line.length(), line.str(), buf, outIOS);
  1869. if (foundEndWUID)
  1870. outputThisLine = false;
  1871. }
  1872. else if (strstr(line.str(), startwuid))
  1873. {
  1874. outputThisLine = true;
  1875. foundEndWUID = false;
  1876. outputALine(line.length(), line.str(), buf, outIOS);
  1877. if (processID == 0)
  1878. parseLogLine(line.str(), nullptr, processID, columnNumPID);
  1879. }
  1880. eof = lineReader->readLine(line.clear());
  1881. }
  1882. }
  1883. bool WsWuInfo::parseLogLine(const char* line, const char* endWUID, unsigned& processID, const unsigned columnNumPID)
  1884. {
  1885. const char* bptr = line;
  1886. for (unsigned cur=0; cur < columnNumPID && *bptr; ++cur)
  1887. {
  1888. while(*bptr && *bptr!=' ') ++bptr; // Skip field
  1889. while(*bptr && *bptr==' ') ++bptr; // Skip spaces
  1890. }
  1891. if (!*bptr) return false;
  1892. const char* eptr = bptr + 1;
  1893. while (*eptr && isdigit(*eptr)) //Read ProcessID
  1894. eptr++;
  1895. if (*eptr != ' ')
  1896. return false;
  1897. processID = (unsigned) atoi_l(bptr, eptr - bptr);
  1898. return (endWUID && strstr(eptr+1, endWUID));
  1899. }
  1900. void WsWuInfo::getWorkunitResTxt(MemoryBuffer& buf)
  1901. {
  1902. Owned<IConstWUQuery> query = cw->getQuery();
  1903. if(!query)
  1904. throw MakeStringException(ECLWATCH_QUERY_NOT_FOUND_FOR_WU,"No query for workunit %s.",wuid.str());
  1905. SCMStringBuffer resname;
  1906. queryDllServer().getDll(query->getQueryResTxtName(resname).str(), buf);
  1907. }
  1908. IConstWUQuery* WsWuInfo::getEmbeddedQuery()
  1909. {
  1910. Owned<IWuWebView> wv = createWuWebView(*cw, NULL, NULL, NULL, false, nullptr);
  1911. if (wv)
  1912. return wv->getEmbeddedQuery();
  1913. return NULL;
  1914. }
  1915. void WsWuInfo::getWorkunitArchiveQuery(IStringVal& str)
  1916. {
  1917. Owned<IConstWUQuery> query = cw->getQuery();
  1918. if(!query)
  1919. throw MakeStringException(ECLWATCH_QUERY_NOT_FOUND_FOR_WU,"No query for workunit %s.",wuid.str());
  1920. query->getQueryText(str);
  1921. if ((str.length() < 1) || !isArchiveQuery(str.str()))
  1922. {
  1923. if (!query->hasArchive())
  1924. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Archive query not found for workunit %s.", wuid.str());
  1925. Owned<IConstWUQuery> embeddedQuery = getEmbeddedQuery();
  1926. if (!embeddedQuery)
  1927. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Embedded query not found for workunit %s.", wuid.str());
  1928. embeddedQuery->getQueryText(str);
  1929. if ((str.length() < 1) || !isArchiveQuery(str.str()))
  1930. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Archive query not found for workunit %s.", wuid.str());
  1931. }
  1932. }
  1933. void WsWuInfo::getWorkunitArchiveQuery(StringBuffer& buf)
  1934. {
  1935. StringBufferAdaptor queryText(buf);
  1936. getWorkunitArchiveQuery(queryText);
  1937. }
  1938. void WsWuInfo::getWorkunitArchiveQuery(MemoryBuffer& buf)
  1939. {
  1940. SCMStringBuffer queryText;
  1941. getWorkunitArchiveQuery(queryText);
  1942. buf.append(queryText.length(), queryText.str());
  1943. }
  1944. void WsWuInfo::getWorkunitQueryShortText(MemoryBuffer& buf, const char* outFile)
  1945. {
  1946. Owned<IConstWUQuery> query = cw->getQuery();
  1947. if(!query)
  1948. throw MakeStringException(ECLWATCH_QUERY_NOT_FOUND_FOR_WU,"No query for workunit %s.",wuid.str());
  1949. SCMStringBuffer queryText;
  1950. query->getQueryShortText(queryText);
  1951. if (queryText.length() < 1)
  1952. throw MakeStringException(ECLWATCH_QUERY_NOT_FOUND_FOR_WU, "No query for workunit %s.",wuid.str());
  1953. if (isEmptyString(outFile))
  1954. buf.append(queryText.length(), queryText.str());
  1955. else
  1956. {
  1957. CWsWuFileHelper helper(nullptr);
  1958. Owned<IFileIOStream> outIOS = helper.createIOStreamWithFileName(outFile, IFOcreate);
  1959. outIOS->write(queryText.length(), queryText.str());
  1960. }
  1961. }
  1962. void WsWuInfo::getWorkunitDll(StringBuffer &dllname, MemoryBuffer& buf)
  1963. {
  1964. Owned<IConstWUQuery> query = cw->getQuery();
  1965. if(!query)
  1966. throw MakeStringException(ECLWATCH_QUERY_NOT_FOUND_FOR_WU,"No query for workunit %s.",wuid.str());
  1967. StringBufferAdaptor isvName(dllname);
  1968. query->getQueryDllName(isvName);
  1969. queryDllServer().getDll(dllname.str(), buf);
  1970. }
  1971. void WsWuInfo::getWorkunitXml(const char* plainText, MemoryBuffer& buf)
  1972. {
  1973. const char* header;
  1974. if (plainText && (!stricmp(plainText, "yes")))
  1975. header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  1976. else
  1977. header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet href=\"../esp/xslt/xmlformatter.xsl\" type=\"text/xsl\"?>";
  1978. StringBuffer xml;
  1979. exportWorkUnitToXML(cw, xml, true, false, true);
  1980. buf.append(strlen(header), header);
  1981. buf.append(xml.length(), xml.str());
  1982. }
  1983. void WsWuInfo::getWorkunitCpp(const char* cppname, const char* description, const char* ipAddress, MemoryBuffer& buf, bool forDownload, const char* outFile)
  1984. {
  1985. if (isEmpty(description))
  1986. throw MakeStringException(ECLWATCH_INVALID_INPUT, "File not specified.");
  1987. if (isEmpty(ipAddress))
  1988. throw MakeStringException(ECLWATCH_INVALID_INPUT, "File location not specified.");
  1989. if (isEmpty(cppname))
  1990. throw MakeStringException(ECLWATCH_INVALID_FILE_NAME, "File path not specified.");
  1991. if (isEmpty(outFile))
  1992. readFileContent(cppname, ipAddress, description, buf, forDownload);
  1993. else
  1994. copyContentFromRemoteFile(cppname, ipAddress, description, outFile);
  1995. }
  1996. void WsWuInfo::getWorkunitAssociatedXml(const char* name, const char* ipAddress, const char* plainText,
  1997. const char* description, bool forDownload, bool addXMLDeclaration, MemoryBuffer& buf, const char* outFile)
  1998. {
  1999. if (isEmpty(description)) //'File Name' as shown in WU Details page
  2000. throw MakeStringException(ECLWATCH_INVALID_INPUT, "File not specified.");
  2001. if (isEmpty(ipAddress))
  2002. throw MakeStringException(ECLWATCH_INVALID_INPUT, "File location not specified.");
  2003. if (isEmpty(name)) //file name with full path
  2004. throw MakeStringException(ECLWATCH_INVALID_FILE_NAME, "File path not specified.");
  2005. if (addXMLDeclaration)
  2006. {
  2007. const char* header;
  2008. if (plainText && (!stricmp(plainText, "yes")))
  2009. header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  2010. else
  2011. header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet href=\"../esp/xslt/xmlformatter.xsl\" type=\"text/xsl\"?>";
  2012. buf.append(strlen(header), header);
  2013. }
  2014. if (isEmpty(outFile))
  2015. readFileContent(name, ipAddress, description, buf, forDownload);
  2016. else
  2017. copyContentFromRemoteFile(name, ipAddress, description, outFile);
  2018. }
  2019. IPropertyTree* WsWuInfo::getWorkunitArchive()
  2020. {
  2021. Owned <IConstWUQuery> query = cw->getQuery();
  2022. if(!query)
  2023. return NULL;
  2024. SCMStringBuffer name, ip;
  2025. Owned<IConstWUAssociatedFileIterator> iter = &query->getAssociatedFiles();
  2026. ForEach(*iter)
  2027. {
  2028. IConstWUAssociatedFile& cur = iter->query();
  2029. if (cur.getType() != FileTypeXml)
  2030. continue;
  2031. cur.getName(name);
  2032. if (name.length() < 15)
  2033. continue;
  2034. const char* pStr = name.str() + name.length() - 15;
  2035. if (strieq(pStr, ".archive.eclxml"))
  2036. {
  2037. cur.getIp(ip);
  2038. break;
  2039. }
  2040. }
  2041. if (!ip.length())
  2042. return NULL;
  2043. MemoryBuffer content;
  2044. getWorkunitAssociatedXml(name.str(), ip.str(), "", "WU archive eclxml", true, false, content, nullptr);
  2045. if (!content.length())
  2046. return NULL;
  2047. return createPTreeFromXMLString(content.length(), content.toByteArray());
  2048. }
  2049. IEspWUArchiveFile* WsWuInfo::readArchiveFileAttr(IPropertyTree& fileTree, const char* path)
  2050. {
  2051. const char* fileName = fileTree.queryProp("@name");
  2052. if (isEmpty(fileName))
  2053. return NULL;
  2054. Owned<IEspWUArchiveFile> file= createWUArchiveFile();
  2055. file->setName(fileName);
  2056. if (!isEmpty(path))
  2057. file->setPath(path);
  2058. if (fileTree.hasProp("@key"))
  2059. file->setKey(fileTree.queryProp("@key"));
  2060. if (fileTree.hasProp("@sourcePath"))
  2061. file->setSourcePath(fileTree.queryProp("@sourcePath"));
  2062. return file.getClear();
  2063. }
  2064. IEspWUArchiveModule* WsWuInfo::readArchiveModuleAttr(IPropertyTree& moduleTree, const char* path)
  2065. {
  2066. const char* moduleName = moduleTree.queryProp("@name");
  2067. if (isEmpty(moduleName))
  2068. return NULL;
  2069. Owned<IEspWUArchiveModule> module= createWUArchiveModule();
  2070. module->setName(moduleName);
  2071. if (!isEmpty(path))
  2072. module->setPath(path);
  2073. if (moduleTree.hasProp("@fullName"))
  2074. module->setFullName(moduleTree.queryProp("@fullName"));
  2075. if (moduleTree.hasProp("@key"))
  2076. module->setKey(moduleTree.queryProp("@key"));
  2077. if (moduleTree.hasProp("@plugin"))
  2078. module->setPlugin(moduleTree.queryProp("@plugin"));
  2079. if (moduleTree.hasProp("@version"))
  2080. module->setVersion(moduleTree.queryProp("@version"));
  2081. if (moduleTree.hasProp("@sourcePath"))
  2082. module->setSourcePath(moduleTree.queryProp("@sourcePath"));
  2083. if (moduleTree.hasProp("@flags"))
  2084. module->setFlags(moduleTree.getPropInt("@flags", 0));
  2085. return module.getClear();
  2086. }
  2087. void WsWuInfo::readArchiveFiles(IPropertyTree* archiveTree, const char* path, IArrayOf<IEspWUArchiveFile>& files)
  2088. {
  2089. Owned<IPropertyTreeIterator> iter = archiveTree->getElements("Attribute");
  2090. ForEach(*iter)
  2091. {
  2092. IPropertyTree& item = iter->query();
  2093. Owned<IEspWUArchiveFile> file = readArchiveFileAttr(item, path);
  2094. if (file)
  2095. files.append(*file.getClear());
  2096. }
  2097. }
  2098. void WsWuInfo::listArchiveFiles(IPropertyTree* archiveTree, const char* path, IArrayOf<IEspWUArchiveModule>& modules, IArrayOf<IEspWUArchiveFile>& files)
  2099. {
  2100. if (!archiveTree)
  2101. return;
  2102. Owned<IPropertyTreeIterator> iter = archiveTree->getElements("Module");
  2103. ForEach(*iter)
  2104. {
  2105. IPropertyTree& item = iter->query();
  2106. Owned<IEspWUArchiveModule> module = readArchiveModuleAttr(item, path);
  2107. if (!module)
  2108. continue;
  2109. StringBuffer newPath;
  2110. if (isEmpty(path))
  2111. newPath.set(module->getName());
  2112. else
  2113. newPath.setf("%s/%s", path, module->getName());
  2114. IArrayOf<IEspWUArchiveModule> modulesInModule;
  2115. IArrayOf<IEspWUArchiveFile> filesInModule;
  2116. listArchiveFiles(&item, newPath.str(), modulesInModule, filesInModule);
  2117. if (modulesInModule.length())
  2118. module->setArchiveModules(modulesInModule);
  2119. if (filesInModule.length())
  2120. module->setFiles(filesInModule);
  2121. modules.append(*module.getClear());
  2122. }
  2123. readArchiveFiles(archiveTree, path, files);
  2124. }
  2125. void WsWuInfo::getArchiveFile(IPropertyTree* archive, const char* moduleName, const char* attrName, const char* path, StringBuffer& file)
  2126. {
  2127. StringBuffer xPath;
  2128. if (!isEmpty(path))
  2129. {
  2130. StringArray list;
  2131. list.appendListUniq(path, "/");
  2132. ForEachItemIn(m, list)
  2133. {
  2134. const char* module = list.item(m);
  2135. if (!isEmpty(module))
  2136. xPath.appendf("Module[@name=\"%s\"]/", module);
  2137. }
  2138. }
  2139. if (isEmpty(moduleName))
  2140. xPath.appendf("Attribute[@name=\"%s\"]", attrName);
  2141. else
  2142. xPath.appendf("Module[@name=\"%s\"]/Text", moduleName);
  2143. file.set(archive->queryProp(xPath.str()));
  2144. }
  2145. void WsWuInfo::outputALine(size32_t length, const char* content, MemoryBuffer& outputBuf, IFileIOStream* outIOS)
  2146. {
  2147. if (outIOS)
  2148. outIOS->write(length, content);
  2149. else
  2150. outputBuf.append(length, content);
  2151. }
  2152. WsWuSearch::WsWuSearch(IEspContext& context,const char* owner,const char* state,const char* cluster,const char* startDate,const char* endDate,const char* jobname)
  2153. {
  2154. SecAccessFlags accessOwn;
  2155. SecAccessFlags accessOthers;
  2156. getUserWuAccessFlags(context, accessOwn, accessOthers, true);
  2157. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2158. Owned<IConstWorkUnitIterator> it(factory->getWorkUnitsByOwner(owner)); // null owner means fetch all
  2159. StringBuffer wuFrom, wuTo, jobPattern;
  2160. if (startDate && *startDate)
  2161. createWuidFromDate(startDate, wuFrom);
  2162. if (endDate && *endDate)
  2163. createWuidFromDate(endDate, wuTo);
  2164. if (jobname && *jobname)
  2165. jobPattern.appendf("*%s*", jobname);
  2166. ForEach(*it)
  2167. {
  2168. IConstWorkUnitInfo &cw = it->query();
  2169. if (chooseWuAccessFlagsByOwnership(context.queryUserId(), cw, accessOwn, accessOthers) < SecAccess_Read)
  2170. continue;
  2171. if (state && *state && !strieq(cw.queryStateDesc(), state))
  2172. continue;
  2173. if (cluster && *cluster && !strieq(cw.queryClusterName(), cluster))
  2174. continue;
  2175. if (jobPattern.length() && !WildMatch(cw.queryJobName(), jobPattern, true))
  2176. continue;
  2177. const char *wuid = cw.queryWuid();
  2178. if (wuFrom.length() && strcmp(wuid,wuFrom.str())<0)
  2179. continue;
  2180. if (wuTo.length() && strcmp(wuid, wuTo.str())>0)
  2181. continue;
  2182. wuids.push_back(wuid);
  2183. }
  2184. std::sort(wuids.begin(), wuids.end(),std::greater<std::string>());
  2185. }
  2186. StringBuffer& WsWuSearch::createWuidFromDate(const char* timestamp,StringBuffer& s)
  2187. {
  2188. CDateTime wuTime;
  2189. wuTime.setString(timestamp,NULL,true);
  2190. unsigned year, month, day, hour, minute, second, nano;
  2191. wuTime.getDate(year, month, day, true);
  2192. wuTime.getTime(hour, minute, second, nano, true);
  2193. s.appendf("W%4d%02d%02d-%02d%02d%02d",year,month,day,hour,minute,second);
  2194. return s;
  2195. }
  2196. struct CompareData
  2197. {
  2198. CompareData(const char* _filter): filter(_filter) {}
  2199. bool operator()(const Linked<DataCacheElement>& e) const
  2200. {
  2201. return stricmp(e->m_filter.c_str(),filter)==0;
  2202. }
  2203. const char* filter;
  2204. };
  2205. DataCacheElement* DataCache::lookup(IEspContext &context, const char* filter, unsigned timeOutMin)
  2206. {
  2207. CriticalBlock block(crit);
  2208. if (cache.size() < 1)
  2209. return NULL;
  2210. //erase data if it should be
  2211. CDateTime timeNow;
  2212. int timeout = timeOutMin;
  2213. timeNow.setNow();
  2214. timeNow.adjustTime(-timeout);
  2215. while (true)
  2216. {
  2217. std::list<Linked<DataCacheElement> >::iterator list_iter = cache.begin();
  2218. if (list_iter == cache.end())
  2219. break;
  2220. DataCacheElement* awu = list_iter->get();
  2221. if (!awu || (awu->m_timeCached > timeNow))
  2222. break;
  2223. cache.pop_front();
  2224. }
  2225. if (cache.size() < 1)
  2226. return NULL;
  2227. //Check whether we have the data cache for this cluster. If yes, get the version
  2228. std::list<Linked<DataCacheElement> >::iterator it = std::find_if(cache.begin(),cache.end(),CompareData(filter));
  2229. if(it!=cache.end())
  2230. {
  2231. return it->getLink();
  2232. }
  2233. return NULL;
  2234. }
  2235. void DataCache::add(const char* filter, const char* data, const char* name, const char* localName, const char* wuid,
  2236. const char* resultName, unsigned seq, __int64 start, unsigned count, __int64 requested, __int64 total)
  2237. {
  2238. CriticalBlock block(crit);
  2239. //Save new data
  2240. Owned<DataCacheElement> e=new DataCacheElement(filter, data, name, localName, wuid, resultName, seq, start, count, requested, total);
  2241. if (cacheSize > 0)
  2242. {
  2243. if (cache.size() >= cacheSize)
  2244. cache.pop_front();
  2245. cache.push_back(e.get());
  2246. }
  2247. return;
  2248. }
  2249. struct CompareArchivedWUs
  2250. {
  2251. CompareArchivedWUs(const char* _filter): filter(_filter) {}
  2252. bool operator()(const Linked<ArchivedWuCacheElement>& e) const
  2253. {
  2254. return stricmp(e->m_filter.c_str(),filter)==0;
  2255. }
  2256. const char* filter;
  2257. };
  2258. ArchivedWuCacheElement* ArchivedWuCache::lookup(IEspContext &context, const char* filter, const char* sashaUpdatedWhen, unsigned timeOutMin)
  2259. {
  2260. CriticalBlock block(crit);
  2261. if (cache.size() < 1)
  2262. return NULL;
  2263. //erase data if it should be
  2264. CDateTime timeNow;
  2265. int timeout = timeOutMin;
  2266. timeNow.setNow();
  2267. timeNow.adjustTime(-timeout);
  2268. while (true)
  2269. {
  2270. std::list<Linked<ArchivedWuCacheElement> >::iterator list_iter = cache.begin();
  2271. if (list_iter == cache.end())
  2272. break;
  2273. ArchivedWuCacheElement* awu = list_iter->get();
  2274. if (awu && !stricmp(sashaUpdatedWhen, awu->m_sashaUpdatedWhen.c_str()) && (awu->m_timeCached > timeNow))
  2275. break;
  2276. cache.pop_front();
  2277. }
  2278. if (cache.size() < 1)
  2279. return NULL;
  2280. //Check whether we have the data cache for this cluster. If yes, get the version
  2281. std::list<Linked<ArchivedWuCacheElement> >::iterator it = std::find_if(cache.begin(),cache.end(),CompareArchivedWUs(filter));
  2282. if(it!=cache.end())
  2283. return it->getLink();
  2284. return NULL;
  2285. }
  2286. void ArchivedWuCache::add(const char* filter, const char* sashaUpdatedWhen, bool hasNextPage, unsigned numWUsReturned, IArrayOf<IEspECLWorkunit>& wus, IArrayOf<IEspECLWorkunitLW>& lwwus)
  2287. {
  2288. CriticalBlock block(crit);
  2289. //Save new data
  2290. Owned<ArchivedWuCacheElement> e=new ArchivedWuCacheElement(filter, sashaUpdatedWhen, hasNextPage, numWUsReturned, wus, lwwus);
  2291. if (cacheSize > 0)
  2292. {
  2293. if (cache.size() >= cacheSize)
  2294. cache.pop_front();
  2295. cache.push_back(e.get());
  2296. }
  2297. return;
  2298. }
  2299. WsWuJobQueueAuditInfo::WsWuJobQueueAuditInfo(IEspContext &context, const char *cluster, const char *from , const char *to, CHttpResponse* response, const char *xls)
  2300. {
  2301. if(!response)
  2302. return;
  2303. unsigned maxDisplay = 125;
  2304. IArrayOf<IEspThorQueue> items;
  2305. CDateTime fromTime;
  2306. CDateTime toTime;
  2307. StringBuffer fromstr;
  2308. StringBuffer tostr;
  2309. if(from && *from)
  2310. {
  2311. fromTime.setString(from,NULL,false);
  2312. fromTime.getString(fromstr, false);
  2313. }
  2314. if(to && *to)
  2315. {
  2316. toTime.setString(to,NULL,false);
  2317. toTime.getString(tostr, false);
  2318. }
  2319. StringBuffer filter("ThorQueueMonitor");
  2320. if(notEmpty(cluster))
  2321. filter.appendf(",%s", cluster);
  2322. StringAttrArray lines;
  2323. queryAuditLogs(fromTime, toTime, filter.str(), lines);
  2324. unsigned countLines = 0;
  2325. unsigned maxConnected = 0;
  2326. unsigned longestQueue = 0;
  2327. ForEachItemIn(idx, lines)
  2328. {
  2329. const char* line = lines.item(idx).text;
  2330. if(!line || !*line)
  2331. continue;
  2332. if (idx < (lines.length() - 1))
  2333. getAuditLineInfo(line, longestQueue, maxConnected, maxDisplay, 1, items);
  2334. else
  2335. getAuditLineInfo(line, longestQueue, maxConnected, maxDisplay, 2, items);
  2336. countLines++;
  2337. }
  2338. StringBuffer responsebuf;
  2339. if (items.length() < 1)
  2340. {
  2341. responsebuf.append("<script language=\"javascript\">\r\nparent.displayQEnd(\'No data found\')</script>\r\n");
  2342. response->sendChunk(responsebuf.str());
  2343. return;
  2344. }
  2345. unsigned itemCount = items.length();
  2346. if (itemCount > maxDisplay)
  2347. itemCount = maxDisplay;
  2348. responsebuf.append("<script language=\"javascript\">parent.displayQLegend()</script>\r\n");
  2349. response->sendChunk(responsebuf.str());
  2350. responsebuf.clear();
  2351. responsebuf.append("<script language=\"javascript\">parent.displayQBegin(").append(longestQueue).append(",").append(maxConnected).append(",").append(itemCount).append(")</script>\r\n");
  2352. response->sendChunk(responsebuf.str());
  2353. responsebuf.clear();
  2354. responsebuf.append("<script language=\"javascript\">\r\n");
  2355. //bool displayDT = false;
  2356. unsigned count = 0;
  2357. unsigned jobpending=0;
  2358. ForEachItemIn(i,items)
  2359. {
  2360. IEspThorQueue& tq = items.item(i);
  2361. //displayDT = !displayDT;
  2362. count++;
  2363. if (count > maxDisplay)
  2364. break;
  2365. StringBuffer countStr, dtStr;
  2366. countStr.appendulong(count);
  2367. //if (displayDT)
  2368. dtStr = tq.getDT();
  2369. responsebuf.append("parent.displayQueue(\'").append(count).append("\',\'").append(dtStr.str()).append("\',\'").append(tq.getRunningWUs()).append("\',");
  2370. responsebuf.append("\'").append(tq.getQueuedWUs()).append("\',\'").append(tq.getWaitingThors()).append("\',");
  2371. responsebuf.append("\'").append(tq.getConnectedThors()).append("\',\'").append(tq.getIdledThors()).append("\',");
  2372. responsebuf.append("\'").append(tq.getRunningWU1()).append("\',\'").append(tq.getRunningWU2()).append("\')\r\n");
  2373. if(++jobpending>=50)
  2374. {
  2375. responsebuf.append("</script>\r\n");
  2376. response->sendChunk(responsebuf.str());
  2377. responsebuf.clear();
  2378. responsebuf.append("<script language=\"javascript\">\r\n");
  2379. jobpending=0;
  2380. }
  2381. }
  2382. StringBuffer countStr;
  2383. countStr.appendulong(count);
  2384. StringBuffer msg("<table><tr><td>");
  2385. msg.append("Total Records in the Time Period: ").append(items.length()).append(" (<a href=\"/WsWorkunits/WUClusterJobQueueLOG?").append(xls).append("\">txt</a>...<a href=\"/WsWorkunits/WUClusterJobQueueXLS?").append(xls).append("\">xls</a>).");
  2386. msg.append("</td></tr><tr><td>");
  2387. if (count > maxDisplay)
  2388. msg.append("Displayed: First ").append(maxDisplay).append(". ");
  2389. msg.append("Max. Queue Length: ").append(longestQueue).append(".");
  2390. msg.append("</td></tr></table>");
  2391. responsebuf.append("parent.displayQEnd(\'").append(msg).append("\')</script>\r\n");
  2392. response->sendChunk(responsebuf.str());
  2393. }
  2394. void WsWuJobQueueAuditInfo::getAuditLineInfo(const char* line, unsigned& longestQueue, unsigned& maxConnected, unsigned maxDisplay, unsigned showAll, IArrayOf<IEspThorQueue>& items)
  2395. {
  2396. //2009-08-12 02:44:12 ,ThorQueueMonitor,thor400_88_dev,0,0,1,1,114,---,---
  2397. if(!line || !*line)
  2398. return;
  2399. Owned<IEspThorQueue> tq = createThorQueue();
  2400. StringBuffer dt, runningWUs, queuedWUs, waitingThors, connectedThors, idledThors, runningWU1, runningWU2;
  2401. // date/time
  2402. const char* bptr = line;
  2403. const char* eptr = strchr(bptr, ',');
  2404. if(eptr)
  2405. dt.append(eptr - bptr, bptr);
  2406. else
  2407. dt.append(bptr);
  2408. tq->setDT(dt.str());
  2409. if(!eptr)
  2410. {
  2411. if (checkNewThorQueueItem(tq, showAll, items))
  2412. items.append(*tq.getClear());
  2413. return;
  2414. }
  2415. //skip title
  2416. bptr = eptr + 1;
  2417. eptr = strchr(bptr, ',');
  2418. if(!eptr)
  2419. {
  2420. if (checkNewThorQueueItem(tq, showAll, items))
  2421. items.append(*tq.getClear());
  2422. return;
  2423. }
  2424. //skip queue name
  2425. bptr = eptr + 1;
  2426. eptr = strchr(bptr, ',');
  2427. if(!eptr)
  2428. {
  2429. if (checkNewThorQueueItem(tq, showAll, items))
  2430. items.append(*tq.getClear());
  2431. return;
  2432. }
  2433. //running
  2434. bptr = eptr + 1;
  2435. eptr = strchr(bptr, ',');
  2436. if(eptr)
  2437. runningWUs.append(eptr - bptr, bptr);
  2438. else
  2439. runningWUs.append(bptr);
  2440. tq->setRunningWUs(runningWUs.str());
  2441. if(!eptr)
  2442. {
  2443. if (checkNewThorQueueItem(tq, showAll, items))
  2444. items.append(*tq.getClear());
  2445. return;
  2446. }
  2447. //queued
  2448. bptr = eptr + 1;
  2449. eptr = strchr(bptr, ',');
  2450. if(eptr)
  2451. queuedWUs.append(eptr - bptr, bptr);
  2452. else
  2453. queuedWUs.append(bptr);
  2454. if (maxDisplay > items.length())
  2455. {
  2456. unsigned queueLen = atoi(queuedWUs.str());
  2457. if (queueLen > longestQueue)
  2458. longestQueue = queueLen;
  2459. }
  2460. tq->setQueuedWUs(queuedWUs.str());
  2461. if(!eptr)
  2462. {
  2463. if (checkNewThorQueueItem(tq, showAll, items))
  2464. items.append(*tq.getClear());
  2465. return;
  2466. }
  2467. //waiting
  2468. bptr = eptr + 1;
  2469. eptr = strchr(bptr, ',');
  2470. if(eptr)
  2471. waitingThors.append(eptr - bptr, bptr);
  2472. else
  2473. waitingThors.append(bptr);
  2474. tq->setWaitingThors(waitingThors.str());
  2475. if(!eptr)
  2476. {
  2477. if (checkNewThorQueueItem(tq, showAll, items))
  2478. items.append(*tq.getClear());
  2479. return;
  2480. }
  2481. //connected
  2482. bptr = eptr + 1;
  2483. eptr = strchr(bptr, ',');
  2484. if(eptr)
  2485. connectedThors.append(eptr - bptr, bptr);
  2486. else
  2487. connectedThors.append(bptr);
  2488. if (maxDisplay > items.length())
  2489. {
  2490. unsigned connnectedLen = atoi(connectedThors.str());
  2491. if (connnectedLen > maxConnected)
  2492. maxConnected = connnectedLen;
  2493. }
  2494. tq->setConnectedThors(connectedThors.str());
  2495. if(!eptr)
  2496. {
  2497. if (checkNewThorQueueItem(tq, showAll, items))
  2498. items.append(*tq.getClear());
  2499. return;
  2500. }
  2501. //idled
  2502. bptr = eptr + 1;
  2503. eptr = strchr(bptr, ',');
  2504. if(eptr)
  2505. idledThors.append(eptr - bptr, bptr);
  2506. else
  2507. idledThors.append(bptr);
  2508. tq->setIdledThors(idledThors.str());
  2509. if(!eptr)
  2510. {
  2511. items.append(*tq.getClear());
  2512. return;
  2513. }
  2514. //runningWU1
  2515. bptr = eptr + 1;
  2516. eptr = strchr(bptr, ',');
  2517. if(eptr)
  2518. runningWU1.append(eptr - bptr, bptr);
  2519. else
  2520. {
  2521. runningWU1.append(bptr);
  2522. }
  2523. if (!strcmp(runningWU1.str(), "---"))
  2524. runningWU1.clear();
  2525. if (runningWU1.length() > 0)
  2526. tq->setRunningWU1(runningWU1.str());
  2527. if(!eptr)
  2528. {
  2529. if (checkNewThorQueueItem(tq, showAll, items))
  2530. items.append(*tq.getClear());
  2531. return;
  2532. }
  2533. //runningWU2
  2534. bptr = eptr + 1;
  2535. eptr = strchr(bptr, ',');
  2536. if(eptr)
  2537. runningWU2.append(eptr - bptr, bptr);
  2538. else
  2539. {
  2540. runningWU2.append(bptr);
  2541. }
  2542. if (!strcmp(runningWU2.str(), "---"))
  2543. runningWU2.clear();
  2544. if (runningWU2.length() > 0)
  2545. tq->setRunningWU2(runningWU2.str());
  2546. if (checkNewThorQueueItem(tq, showAll, items))
  2547. items.append(*tq.getClear());
  2548. }
  2549. bool WsWuJobQueueAuditInfo::checkSameStrings(const char* s1, const char* s2)
  2550. {
  2551. if (s1)
  2552. {
  2553. if (!s2)
  2554. return false;
  2555. if (strcmp(s1, s2))
  2556. return false;
  2557. }
  2558. else if (s2)
  2559. {
  2560. if (!s1)
  2561. return false;
  2562. }
  2563. return true;
  2564. }
  2565. bool WsWuJobQueueAuditInfo::checkNewThorQueueItem(IEspThorQueue* tq, unsigned showAll, IArrayOf<IEspThorQueue>& items)
  2566. {
  2567. bool bAdd = false;
  2568. if (showAll < 1) //show every lines
  2569. bAdd = true;
  2570. else if (items.length() < 1)
  2571. bAdd = true;
  2572. else if (showAll > 1) //last line now
  2573. {
  2574. IEspThorQueue& tq0 = items.item(items.length()-1);
  2575. if (!checkSameStrings(tq->getDT(), tq0.getDT()))
  2576. bAdd = true;
  2577. }
  2578. else
  2579. {
  2580. IEspThorQueue& tq0 = items.item(items.length()-1);
  2581. if (!checkSameStrings(tq->getRunningWUs(), tq0.getRunningWUs()))
  2582. bAdd = true;
  2583. if (!checkSameStrings(tq->getQueuedWUs(), tq0.getQueuedWUs()))
  2584. bAdd = true;
  2585. if (!checkSameStrings(tq->getConnectedThors(), tq0.getConnectedThors()))
  2586. bAdd = true;
  2587. if (!checkSameStrings(tq->getWaitingThors(), tq0.getWaitingThors()))
  2588. bAdd = true;
  2589. if (!checkSameStrings(tq->getRunningWU1(), tq0.getRunningWU1()))
  2590. bAdd = true;
  2591. if (!checkSameStrings(tq->getRunningWU2(), tq0.getRunningWU2()))
  2592. bAdd = true;
  2593. }
  2594. return bAdd;
  2595. }
  2596. void xsltTransform(const char* xml, const char* sheet, IProperties *params, StringBuffer& ret)
  2597. {
  2598. StringBuffer xsl;
  2599. if(!checkFileExists(sheet))
  2600. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST, "Could not find stylesheet %s.",sheet);
  2601. Owned<IXslProcessor> proc = getXslProcessor();
  2602. Owned<IXslTransform> trans = proc->createXslTransform();
  2603. trans->setXmlSource(xml, strlen(xml));
  2604. trans->loadXslFromFile(sheet);
  2605. trans->copyParameters(params);
  2606. trans->transform(ret);
  2607. }
  2608. bool addToQueryString(StringBuffer &queryString, const char *name, const char *value, const char delim)
  2609. {
  2610. if (isEmpty(name) || isEmpty(value))
  2611. return false;
  2612. if (queryString.length() > 0)
  2613. queryString.append(delim);
  2614. queryString.append(name).append("=").append(value);
  2615. return true;
  2616. }
  2617. int WUSchedule::run()
  2618. {
  2619. PROGLOG("ECLWorkunit WUSchedule Thread started.");
  2620. unsigned int waitTimeMillies = 1000*60;
  2621. while(!stopping)
  2622. {
  2623. if (!m_container)
  2624. {
  2625. DBGLOG("ECLWorkunit WUSchedule Thread is waiting for container to be set.");
  2626. }
  2627. else if (!detached)
  2628. {
  2629. try
  2630. {
  2631. if (waitTimeMillies == (unsigned)-1)
  2632. {
  2633. PROGLOG("ECLWorkunit WUSchedule Thread Re-started.");
  2634. waitTimeMillies = 1000*60;
  2635. }
  2636. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  2637. Owned<IConstWorkUnitIterator> itr = factory->getScheduledWorkUnits();
  2638. if (itr)
  2639. {
  2640. ForEach(*itr)
  2641. {
  2642. try
  2643. {
  2644. IConstWorkUnitInfo & cw = itr->query();
  2645. if (factory->isAborting(cw.queryWuid()))
  2646. {
  2647. WorkunitUpdate wu(factory->updateWorkUnit(cw.queryWuid()));
  2648. wu->setState(WUStateAborted);
  2649. continue;
  2650. }
  2651. WsWuDateTime dt, now;
  2652. now.setNow();
  2653. cw.getTimeScheduled(dt);
  2654. if (now.compare(dt)>=0)
  2655. {
  2656. runWorkUnit(cw.queryWuid(), cw.queryClusterName());
  2657. if (m_container->hasCacheClient())
  2658. {
  2659. StringArray errorMsgs;
  2660. m_container->clearCacheByGroupID("ESPWsWUs", errorMsgs);
  2661. if (errorMsgs.length() > 0)
  2662. {
  2663. ForEachItemIn(i, errorMsgs)
  2664. DBGLOG("%s", errorMsgs.item(i));
  2665. }
  2666. }
  2667. }
  2668. }
  2669. catch(IException *e)
  2670. {
  2671. StringBuffer msg;
  2672. IERRLOG("Exception %d:%s in WsWorkunits Schedule::run while processing WU", e->errorCode(), e->errorMessage(msg).str());
  2673. e->Release();
  2674. }
  2675. }
  2676. }
  2677. }
  2678. catch(IException *e)
  2679. {
  2680. StringBuffer msg;
  2681. IERRLOG("Exception %d:%s in WsWorkunits Schedule::run while fetching scheduled WUs from DALI", e->errorCode(), e->errorMessage(msg).str());
  2682. e->Release();
  2683. }
  2684. catch(...)
  2685. {
  2686. IERRLOG("Unknown exception in WsWorkunits Schedule::run while fetching scheduled WUs from DALI");
  2687. }
  2688. }
  2689. else
  2690. {
  2691. OWARNLOG("Detached from DALI, WSWorkunits schedule interrupted");
  2692. waitTimeMillies = (unsigned)-1;
  2693. }
  2694. semSchedule.wait(waitTimeMillies);
  2695. }
  2696. return 0;
  2697. }
  2698. void WsWuHelpers::setXmlParameters(IWorkUnit *wu, const char *xml, bool setJobname)
  2699. {
  2700. if (!xml || !*xml)
  2701. return;
  2702. Owned<IPropertyTree> tree = createPTreeFromXMLString(xml, ipt_none, (PTreeReaderOptions)(ptr_ignoreWhiteSpace | ptr_ignoreNameSpaces));
  2703. IPropertyTree *root = tree.get();
  2704. if (strieq(root->queryName(), "Envelope"))
  2705. root = root->queryPropTree("Body/*[1]");
  2706. if (!root)
  2707. return;
  2708. if (setJobname)
  2709. {
  2710. const char *name = wu->queryJobName();
  2711. if (!name || !*name)
  2712. wu->setJobName(root->queryName());
  2713. }
  2714. wu->setXmlParams(LINK(root));
  2715. }
  2716. void WsWuHelpers::setXmlParameters(IWorkUnit *wu, const char *xml, IArrayOf<IConstNamedValue> *variables, bool setJobname)
  2717. {
  2718. StringBuffer extParamXml;
  2719. if (variables && variables->length())
  2720. {
  2721. Owned<IPropertyTree> paramTree = (xml && *xml) ? createPTreeFromXMLString(xml) : createPTree("input");
  2722. ForEachItemIn(i, *variables)
  2723. {
  2724. IConstNamedValue &item = variables->item(i);
  2725. const char *name = item.getName();
  2726. const char *value = item.getValue();
  2727. if (!name || !*name)
  2728. continue;
  2729. if (!value)
  2730. {
  2731. size_t len = strlen(name);
  2732. char last = name[len-1];
  2733. if (last == '-' || last == '+')
  2734. {
  2735. StringAttr s(name, len-1);
  2736. paramTree->setPropInt(s.get(), last == '+' ? 1 : 0);
  2737. }
  2738. else
  2739. paramTree->setPropInt(name, 1);
  2740. continue;
  2741. }
  2742. paramTree->setProp(name, value);
  2743. }
  2744. toXML(paramTree, extParamXml);
  2745. xml=extParamXml.str();
  2746. }
  2747. setXmlParameters(wu, xml, setJobname);
  2748. }
  2749. void WsWuHelpers::submitWsWorkunit(IEspContext& context, IConstWorkUnit* cw, const char* cluster, const char* snapshot, int maxruntime, bool compile, bool resetWorkflow, bool resetVariables,
  2750. const char *paramXml, IArrayOf<IConstNamedValue> *variables, IArrayOf<IConstNamedValue> *debugs, IArrayOf<IConstApplicationValue> *applications)
  2751. {
  2752. ensureWsWorkunitAccess(context, *cw, SecAccess_Write);
  2753. #ifndef _NO_LDAP
  2754. CLdapSecManager* secmgr = dynamic_cast<CLdapSecManager*>(context.querySecManager());
  2755. // View Scope is checked only when LDAP secmgr is available AND checkViewPermissions config is also enabled.
  2756. // Otherwise, the view permission check is skipped, and WU is submitted as normal.
  2757. if (secmgr && secmgr->getCheckViewPermissions())
  2758. {
  2759. StringArray filenames, columnnames;
  2760. if (cw->getFieldUsageArray(filenames, columnnames, cluster)) // check view permission only for a query with fieldUsage information
  2761. {
  2762. if (!secmgr->authorizeViewScope(*context.queryUser(), filenames, columnnames))
  2763. throw MakeStringException(ECLWATCH_VIEW_ACCESS_DENIED, "View Access denied for a WU: %s", cw->queryWuid());
  2764. }
  2765. }
  2766. #endif
  2767. switch(cw->getState())
  2768. {
  2769. case WUStateRunning:
  2770. case WUStateDebugPaused:
  2771. case WUStateDebugRunning:
  2772. case WUStateCompiling:
  2773. case WUStateAborting:
  2774. case WUStateBlocked:
  2775. throw MakeStringException(ECLWATCH_CANNOT_SUBMIT_WORKUNIT, "Cannot submit the workunit. Workunit state is '%s'.", cw->queryStateDesc());
  2776. }
  2777. StringAttr wuid(cw->queryWuid());
  2778. WorkunitUpdate wu(&cw->lock());
  2779. if(!wu.get())
  2780. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Cannot update workunit %s.", wuid.str());
  2781. wu->clearExceptions();
  2782. if(notEmpty(cluster))
  2783. wu->setClusterName(cluster);
  2784. if(notEmpty(snapshot))
  2785. wu->setSnapshot(snapshot);
  2786. wu->setState(WUStateSubmitted);
  2787. if (maxruntime)
  2788. wu->setDebugValueInt("maxRunTime",maxruntime,true);
  2789. if (debugs && debugs->length())
  2790. {
  2791. ForEachItemIn(i, *debugs)
  2792. {
  2793. IConstNamedValue &item = debugs->item(i);
  2794. const char *name = item.getName();
  2795. const char *value = item.getValue();
  2796. if (!name || !*name)
  2797. continue;
  2798. StringBuffer expanded;
  2799. if (*name=='-')
  2800. name=expanded.append("eclcc").append(name).str();
  2801. if (!value)
  2802. {
  2803. size_t len = strlen(name);
  2804. char last = name[len-1];
  2805. if (last == '-' || last == '+')
  2806. {
  2807. StringAttr s(name, len-1);
  2808. wu->setDebugValueInt(s.get(), last == '+' ? 1 : 0, true);
  2809. }
  2810. else
  2811. wu->setDebugValueInt(name, 1, true);
  2812. continue;
  2813. }
  2814. wu->setDebugValue(name, value, true);
  2815. }
  2816. }
  2817. if (applications)
  2818. {
  2819. ForEachItemIn(ii, *applications)
  2820. {
  2821. IConstApplicationValue& item = applications->item(ii);
  2822. if(notEmpty(item.getApplication()) && notEmpty(item.getName()))
  2823. wu->setApplicationValue(item.getApplication(), item.getName(), item.getValue(), true);
  2824. }
  2825. }
  2826. if (resetWorkflow)
  2827. wu->resetWorkflow();
  2828. if (!compile)
  2829. wu->schedule();
  2830. if (resetVariables)
  2831. {
  2832. SCMStringBuffer varname;
  2833. Owned<IConstWUResultIterator> vars = &wu->getVariables();
  2834. ForEach (*vars)
  2835. {
  2836. vars->query().getResultName(varname);
  2837. Owned<IWUResult> v = wu->updateVariableByName(varname.str());
  2838. if (v)
  2839. v->setResultStatus(ResultStatusUndefined);
  2840. }
  2841. }
  2842. setXmlParameters(wu, paramXml, variables, (wu->getAction()==WUActionExecuteExisting));
  2843. wu->commit();
  2844. wu.clear();
  2845. if (!compile)
  2846. runWorkUnit(wuid.str());
  2847. else if (context.querySecManager())
  2848. secSubmitWorkUnit(wuid.str(), *context.querySecManager(), *context.queryUser());
  2849. else
  2850. submitWorkUnit(wuid.str(), context.queryUserId(), context.queryPassword());
  2851. AuditSystemAccess(context.queryUserId(), true, "Submitted %s", wuid.str());
  2852. }
  2853. void WsWuHelpers::submitWsWorkunit(IEspContext& context, const char *wuid, const char* cluster, const char* snapshot, int maxruntime, bool compile, bool resetWorkflow, bool resetVariables,
  2854. const char *paramXml, IArrayOf<IConstNamedValue> *variables, IArrayOf<IConstNamedValue> *debugs, IArrayOf<IConstApplicationValue> *applications)
  2855. {
  2856. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2857. Owned<IConstWorkUnit> cw = factory->openWorkUnit(wuid);
  2858. if(!cw)
  2859. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT,"Cannot open workunit %s.",wuid);
  2860. submitWsWorkunit(context, cw, cluster, snapshot, maxruntime, compile, resetWorkflow, resetVariables, paramXml, variables, debugs, applications);
  2861. }
  2862. void WsWuHelpers::copyWsWorkunit(IEspContext &context, IWorkUnit &wu, const char *srcWuid)
  2863. {
  2864. Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
  2865. Owned<IConstWorkUnit> src(factory->openWorkUnit(srcWuid));
  2866. queryExtendedWU(&wu)->copyWorkUnit(src, false, false);
  2867. wu.commit();
  2868. }
  2869. void WsWuHelpers::runWsWorkunit(IEspContext &context, StringBuffer &wuid, const char *srcWuid, const char *cluster, const char *paramXml,
  2870. IArrayOf<IConstNamedValue> *variables, IArrayOf<IConstNamedValue> *debugs, IArrayOf<IConstApplicationValue> *applications)
  2871. {
  2872. NewWsWorkunit wu(context);
  2873. wuid.set(wu->queryWuid());
  2874. copyWsWorkunit(context, *wu, srcWuid);
  2875. wu.clear();
  2876. submitWsWorkunit(context, wuid.str(), cluster, NULL, 0, false, true, true, paramXml, variables, debugs, applications);
  2877. }
  2878. void WsWuHelpers::runWsWorkunit(IEspContext &context, IConstWorkUnit *cw, const char *srcWuid, const char *cluster, const char *paramXml,
  2879. IArrayOf<IConstNamedValue> *variables, IArrayOf<IConstNamedValue> *debugs, IArrayOf<IConstApplicationValue> *applications)
  2880. {
  2881. WorkunitUpdate wu(&cw->lock());
  2882. copyWsWorkunit(context, *wu, srcWuid);
  2883. wu.clear();
  2884. submitWsWorkunit(context, cw, cluster, NULL, 0, false, true, true, paramXml, variables, debugs, applications);
  2885. }
  2886. IException * WsWuHelpers::noteException(IWorkUnit *wu, IException *e, ErrorSeverity level)
  2887. {
  2888. if (wu)
  2889. {
  2890. Owned<IWUException> we = wu->createException();
  2891. StringBuffer s;
  2892. we->setExceptionMessage(e->errorMessage(s).str());
  2893. we->setExceptionSource("WsWorkunits");
  2894. we->setSeverity(level);
  2895. if (level==SeverityError)
  2896. wu->setState(WUStateFailed);
  2897. }
  2898. return e;
  2899. }
  2900. StringBuffer & WsWuHelpers::resolveQueryWuid(StringBuffer &wuid, const char *queryset, const char *query, bool notSuspended, IWorkUnit *wu)
  2901. {
  2902. Owned<IPropertyTree> qs = getQueryRegistry(queryset, true);
  2903. if (!qs)
  2904. throw noteException(wu, MakeStringException(ECLWATCH_QUERYSET_NOT_FOUND, "QuerySet '%s' not found", queryset));
  2905. Owned<IPropertyTree> q = resolveQueryAlias(qs, query);
  2906. if (!q)
  2907. throw noteException(wu, MakeStringException(ECLWATCH_QUERYID_NOT_FOUND, "Query '%s/%s' not found", queryset, query));
  2908. if (notSuspended && q->getPropBool("@suspended"))
  2909. throw noteException(wu, MakeStringException(ECLWATCH_QUERY_SUSPENDED, "Query '%s/%s' is suspended", queryset, query));
  2910. return wuid.append(q->queryProp("@wuid"));
  2911. }
  2912. void WsWuHelpers::runWsWuQuery(IEspContext &context, IConstWorkUnit *cw, const char *queryset, const char *query,
  2913. const char *cluster, const char *paramXml, IArrayOf<IConstApplicationValue> *applications)
  2914. {
  2915. StringBuffer srcWuid;
  2916. WorkunitUpdate wu(&cw->lock());
  2917. resolveQueryWuid(srcWuid, queryset, query, true, wu);
  2918. copyWsWorkunit(context, *wu, srcWuid);
  2919. wu.clear();
  2920. submitWsWorkunit(context, cw, cluster, NULL, 0, false, true, true, paramXml, NULL, NULL, applications);
  2921. }
  2922. void WsWuHelpers::runWsWuQuery(IEspContext &context, StringBuffer &wuid, const char *queryset, const char *query,
  2923. const char *cluster, const char *paramXml, IArrayOf<IConstApplicationValue> *applications)
  2924. {
  2925. StringBuffer srcWuid;
  2926. NewWsWorkunit wu(context);
  2927. wuid.set(wu->queryWuid());
  2928. resolveQueryWuid(srcWuid, queryset, query, true, wu);
  2929. copyWsWorkunit(context, *wu, srcWuid);
  2930. wu.clear();
  2931. submitWsWorkunit(context, wuid.str(), cluster, NULL, 0, false, true, true, paramXml, NULL, NULL, applications);
  2932. }
  2933. void WsWuHelpers::checkAndTrimWorkunit(const char* methodName, StringBuffer& input)
  2934. {
  2935. const char* trimmedInput = input.trim().str();
  2936. if (isEmpty(trimmedInput))
  2937. throw MakeStringException(ECLWATCH_INVALID_INPUT, "%s: Workunit ID not set", methodName);
  2938. if (!looksLikeAWuid(trimmedInput, 'W'))
  2939. throw MakeStringException(ECLWATCH_INVALID_INPUT, "%s: Invalid Workunit ID: %s", methodName, trimmedInput);
  2940. return;
  2941. }
  2942. IFileIOStream* CWsWuFileHelper::createIOStreamWithFileName(const char* fileNameWithPath, IFOmode mode)
  2943. {
  2944. if (isEmptyString(fileNameWithPath))
  2945. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA, "File name not specified.");
  2946. Owned<IFile> wuInfoIFile = createIFile(fileNameWithPath);
  2947. Owned<IFileIO> wuInfoIO = wuInfoIFile->open(mode);
  2948. if (!wuInfoIO)
  2949. throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Failed to open %s.", fileNameWithPath);
  2950. return createIOStream(wuInfoIO);
  2951. }
  2952. void CWsWuFileHelper::writeToFile(const char* fileName, size32_t contentLength, const void* content)
  2953. {
  2954. if (isEmptyString(fileName))
  2955. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA, "File name not specified.");
  2956. Owned<IFile> wuInfoIFile = createIFile(fileName);
  2957. Owned<IFileIO> wuInfoIO = wuInfoIFile->open(IFOcreate);
  2958. if (wuInfoIO)
  2959. wuInfoIO->write(0, contentLength, content);
  2960. else
  2961. PROGLOG("Failed to open %s.", fileName);
  2962. }
  2963. void CWsWuFileHelper::writeToFileIOStream(const char* folder, const char* file, MemoryBuffer& mb)
  2964. {
  2965. if (isEmptyString(folder))
  2966. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty folder name is not allowed to create FileIOStream.");
  2967. if (isEmptyString(file))
  2968. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Empty file name is not allowed to create FileIOStream.");
  2969. VStringBuffer fileNameWithPath("%s%c%s", folder, PATHSEPCHAR, file);
  2970. CWsWuFileHelper helper(nullptr);
  2971. Owned<IFileIOStream> outIOS = helper.createIOStreamWithFileName(fileNameWithPath.str(), IFOcreate);
  2972. if (outIOS)
  2973. outIOS->write(mb.length(), mb.toByteArray());
  2974. else
  2975. PROGLOG("Failed to open FileIOStream for %s.", fileNameWithPath.str());
  2976. }
  2977. void CWsWuFileHelper::cleanFolder(IFile* folder, bool removeFolder)
  2978. {
  2979. if (!folder)
  2980. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA,"Invalid file interface for the zip folder.");
  2981. Owned<IDirectoryIterator> iter = folder->directoryFiles(NULL,false,false);
  2982. ForEach(*iter)
  2983. {
  2984. OwnedIFile thisFile = createIFile(iter->query().queryFilename());
  2985. if (thisFile->isFile() == fileBool::foundYes)
  2986. thisFile->remove();
  2987. }
  2988. if (removeFolder)
  2989. folder->remove();
  2990. }
  2991. void CWsWuFileHelper::createProcessLogfile(IConstWorkUnit* cwu, WsWuInfo& winfo, const char* process, const char* path)
  2992. {
  2993. Owned<IPropertyTreeIterator> procs = cwu->getProcesses(process, NULL);
  2994. ForEach (*procs)
  2995. {
  2996. StringBuffer logSpec;
  2997. IPropertyTree& proc = procs->query();
  2998. proc.getProp("@log", logSpec);
  2999. if (!logSpec.length())
  3000. continue;
  3001. const char* processName = proc.queryName();
  3002. if (isEmpty(processName))
  3003. continue;
  3004. MemoryBuffer mb;
  3005. VStringBuffer fileName("%s%c%s_%s", path, PATHSEPCHAR, processName, pathTail(logSpec.str()));
  3006. try
  3007. {
  3008. if (strieq(process, "EclAgent"))
  3009. {
  3010. StringBuffer pid;
  3011. pid.appendf("%d", proc.getPropInt("@pid"));
  3012. winfo.getWorkunitEclAgentLog(logSpec.str(), pid.str(), mb, fileName.str());
  3013. }
  3014. else if (strieq(process, "Thor"))
  3015. winfo.getWorkunitThorLog(logSpec.str(), mb, fileName.str());
  3016. }
  3017. catch(IException* e)
  3018. {
  3019. StringBuffer s;
  3020. e->errorMessage(s);
  3021. IERRLOG("Error accessing Process Log file %s: %s", logSpec.str(), s.str());
  3022. writeToFile(fileName.str(), s.length(), s.str());
  3023. e->Release();
  3024. }
  3025. }
  3026. }
  3027. void CWsWuFileHelper::createThorSlaveLogfile(IConstWorkUnit* cwu, WsWuInfo& winfo, const char* path)
  3028. {
  3029. if (cwu->getWuidVersion() == 0)
  3030. return;
  3031. const char* clusterName = cwu->queryClusterName();
  3032. if (isEmptyString(clusterName)) //Cluster name may not be set yet
  3033. return;
  3034. Owned<IConstWUClusterInfo> clusterInfo = getTargetClusterInfo(clusterName);
  3035. if (!clusterInfo)
  3036. {
  3037. OWARNLOG("Cannot find TargetClusterInfo for workunit %s", cwu->queryWuid());
  3038. return;
  3039. }
  3040. Owned<IThreadFactory> threadFactory = new CGetThorSlaveLogToFileThreadFactory();
  3041. Owned<IThreadPool> threadPool = createThreadPool("WsWuFileHelper GetThorSlaveLogToFile Thread Pool",
  3042. threadFactory, NULL, thorSlaveLogThreadPoolSize, INFINITE);
  3043. unsigned numberOfSlaveLogs = clusterInfo->getNumberOfSlaveLogs();
  3044. BoolHash uniqueProcesses;
  3045. Owned<IStringIterator> thorInstances = cwu->getProcesses("Thor");
  3046. ForEach (*thorInstances)
  3047. {
  3048. SCMStringBuffer processName;
  3049. thorInstances->str(processName);
  3050. if (processName.length() == 0)
  3051. continue;
  3052. bool* found = uniqueProcesses.getValue(processName.str());
  3053. if (found && *found)
  3054. continue;
  3055. uniqueProcesses.setValue(processName.str(), true);
  3056. StringBuffer groupName, logDir;
  3057. getClusterThorGroupName(groupName, processName.str());
  3058. if (groupName.isEmpty())
  3059. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Failed to get Thor Group Name for %s", processName.str());
  3060. Owned<IGroup> nodeGroup = queryNamedGroupStore().lookup(groupName);
  3061. if (!nodeGroup || (nodeGroup->ordinality() == 0))
  3062. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Node group %s not found", groupName.str());
  3063. getConfigurationDirectory(directories, "log", "thor", processName.str(), logDir);
  3064. Owned<IStringIterator> thorLogs = cwu->getLogs("Thor", processName.str());
  3065. ForEach (*thorLogs)
  3066. {
  3067. SCMStringBuffer logName;
  3068. thorLogs->str(logName);
  3069. if (logName.length() == 0)
  3070. continue;
  3071. const char* pStr = logName.str();
  3072. const char* ppStr = strstr(pStr, "/thormaster.");
  3073. if (!ppStr)
  3074. {
  3075. IWARNLOG("Invalid thorlog entry in workunit xml: %s", logName.str());
  3076. continue;
  3077. }
  3078. ppStr += 12;
  3079. StringBuffer logDate(ppStr);
  3080. logDate.setLength(10);
  3081. for (unsigned i = 0; i < numberOfSlaveLogs; i++)
  3082. {
  3083. VStringBuffer fileName("%s%c%s_thorslave.%u.%s.log", path, PATHSEPCHAR, processName.str(), i+1, logDate.str());
  3084. Owned<CGetThorSlaveLogToFileThreadParam> threadParam = new CGetThorSlaveLogToFileThreadParam(
  3085. &winfo, nodeGroup, logDate, logDir, i+1, fileName);
  3086. threadPool->start(threadParam.getClear());
  3087. }
  3088. }
  3089. }
  3090. threadPool->joinAll();
  3091. }
  3092. void CWsWuFileHelper::createZAPInfoFile(const char* url, const char* espIP, const char* thorIP, const char* problemDesc,
  3093. const char* whatChanged, const char* timing, IConstWorkUnit* cwu, const char* pathNameStr)
  3094. {
  3095. VStringBuffer fileName("%s.txt", pathNameStr);
  3096. Owned<IFileIOStream> outFile = createIOStreamWithFileName(fileName.str(), IFOcreate);
  3097. StringBuffer sb;
  3098. sb.set("Workunit: ").append(cwu->queryWuid()).append("\r\n");
  3099. sb.append("User: ").append(cwu->queryUser()).append("\r\n");
  3100. sb.append("Build Version:").append(getBuildVersion()).append("\r\n");
  3101. sb.append("Cluster: ").append(cwu->queryClusterName()).append("\r\n");
  3102. if (!isEmptyString(espIP))
  3103. sb.append("ESP: ").append(espIP).append("\r\n");
  3104. else
  3105. {
  3106. StringBuffer espIPAddr;
  3107. IpAddress ipaddr = queryHostIP();
  3108. ipaddr.getIpText(espIPAddr);
  3109. sb.append("ESP: ").append(espIPAddr.str()).append("\r\n");
  3110. }
  3111. if (!isEmptyString(url))
  3112. sb.append("URL: ").append(url).append("\r\n");
  3113. if (!isEmptyString(thorIP))
  3114. sb.append("Thor: ").append(thorIP).append("\r\n");
  3115. outFile->write(sb.length(), sb.str());
  3116. //Exceptions/Warnings/Info
  3117. Owned<IConstWUExceptionIterator> exceptions = &cwu->getExceptions();
  3118. StringBuffer info, warn, err, alert;
  3119. ForEach(*exceptions)
  3120. {
  3121. const char* severityString = nullptr;
  3122. switch (exceptions->query().getSeverity())
  3123. {
  3124. case SeverityInformation:
  3125. severityString = "Information: ";
  3126. break;
  3127. case SeverityWarning:
  3128. severityString = "Warnings: ";
  3129. break;
  3130. case SeverityError:
  3131. severityString = "Exceptions: ";
  3132. break;
  3133. case SeverityAlert:
  3134. severityString = "Alert: ";
  3135. break;
  3136. }
  3137. if (!severityString)
  3138. continue;
  3139. SCMStringBuffer temp;
  3140. exceptions->query().getExceptionMessage(temp);
  3141. writeZAPWUInfoToIOStream(outFile, severityString, temp);
  3142. }
  3143. //User provided Information
  3144. writeZAPWUInfoToIOStream(outFile, "Problem: ", problemDesc);
  3145. writeZAPWUInfoToIOStream(outFile, "What Changed: ", whatChanged);
  3146. writeZAPWUInfoToIOStream(outFile, "Timing: ", timing);
  3147. }
  3148. void CWsWuFileHelper::writeZAPWUInfoToIOStream(IFileIOStream* outFile, const char* name, SCMStringBuffer& value)
  3149. {
  3150. outFile->write(strlen(name), name);
  3151. outFile->write(3, "\r\n\t");
  3152. outFile->write(value.length(), value.str());
  3153. outFile->write(4, "\r\n\r\n");
  3154. }
  3155. void CWsWuFileHelper::writeZAPWUInfoToIOStream(IFileIOStream* outFile, const char* name, const char* value)
  3156. {
  3157. outFile->write(strlen(name), name);
  3158. outFile->write(strlen(value), value);
  3159. outFile->write(4, "\r\n\r\n");
  3160. }
  3161. void CWsWuFileHelper::createZAPWUXMLFile(WsWuInfo& winfo, const char* pathNameStr)
  3162. {
  3163. MemoryBuffer mb;
  3164. winfo.getWorkunitXml(NULL, mb);
  3165. VStringBuffer fileName("%s.xml", pathNameStr);
  3166. writeToFile(fileName.str(), mb.length(), mb.bufferBase());
  3167. }
  3168. void CWsWuFileHelper::createZAPECLQueryArchiveFiles(IConstWorkUnit* cwu, const char* pathNameStr)
  3169. {
  3170. Owned<IConstWUQuery> query = cwu->getQuery();
  3171. if(!query)
  3172. return;
  3173. //Add archive if present
  3174. Owned<IConstWUAssociatedFileIterator> iter = &query->getAssociatedFiles();
  3175. ForEach(*iter)
  3176. {
  3177. IConstWUAssociatedFile& cur = iter->query();
  3178. SCMStringBuffer ssb, ip;
  3179. cur.getDescription(ssb);
  3180. if (!strieq(ssb.str(), "archive"))
  3181. continue;
  3182. cur.getName(ssb);
  3183. cur.getIp(ip);
  3184. if (!ssb.length() || !ip.length())
  3185. continue;
  3186. StringBuffer fileName, archiveContents;
  3187. try
  3188. {
  3189. SocketEndpoint ep(ip.str());
  3190. RemoteFilename rfn;
  3191. rfn.setRemotePath(ssb.str());
  3192. rfn.setIp(ep);
  3193. Owned<IFile> rFile = createIFile(rfn);
  3194. if (!rFile)
  3195. {
  3196. OERRLOG("Cannot open %s on %s", ssb.str(), ip.str());
  3197. continue;
  3198. }
  3199. archiveContents.loadFile(rFile);
  3200. }
  3201. catch (IException *e)
  3202. {
  3203. StringBuffer s;
  3204. e->errorMessage(s);
  3205. OERRLOG("Error accessing archive file %s: %s", ssb.str(), s.str());
  3206. archiveContents.insert(0, "Error accessing archive file ").appendf("%s: %s\r\n\r\n", ssb.str(), s.str());
  3207. e->Release();
  3208. }
  3209. fileName.setf("%s.archive", pathNameStr);
  3210. writeToFile(fileName.str(), archiveContents.length(), archiveContents.str());
  3211. break;
  3212. }
  3213. //Add Query
  3214. SCMStringBuffer temp;
  3215. query->getQueryText(temp);
  3216. if (temp.length())
  3217. {
  3218. VStringBuffer fileName("%s.ecl", pathNameStr);
  3219. writeToFile(fileName.str(), temp.length(), temp.str());
  3220. }
  3221. }
  3222. void CWsWuFileHelper::createZAPWUGraphProgressFile(const char* wuid, const char* pathNameStr)
  3223. {
  3224. Owned<IPropertyTree> graphProgress = getWUGraphProgress(wuid, true);
  3225. if (!graphProgress)
  3226. return;
  3227. StringBuffer graphProgressXML;
  3228. toXML(graphProgress, graphProgressXML, 1, XML_Format);
  3229. VStringBuffer fileName("%s.graphprogress", pathNameStr);
  3230. writeToFile(fileName.str(), graphProgressXML.length(), graphProgressXML.str());
  3231. }
  3232. int CWsWuFileHelper::zipAFolder(const char* folder, const char* passwordReq, const char* zipFileNameWithPath)
  3233. {
  3234. VStringBuffer archiveInPath("%s%c*", folder, PATHSEPCHAR);
  3235. StringBuffer zipCommand;
  3236. if (!isEmptyString(passwordReq))
  3237. zipCommand.setf("zip -j --password %s %s %s", passwordReq, zipFileNameWithPath, archiveInPath.str());
  3238. else
  3239. zipCommand.setf("zip -j %s %s", zipFileNameWithPath, archiveInPath.str());
  3240. return (system(zipCommand.str()));
  3241. }
  3242. int CWsWuFileHelper::zipAFolder(const char* folder, bool gzip, const char* zipFileNameWithPath)
  3243. {
  3244. StringBuffer zipCommand;
  3245. VStringBuffer archiveInPath("%s%c*", folder, PATHSEPCHAR);
  3246. if (!gzip)
  3247. zipCommand.appendf("zip -j %s %s", zipFileNameWithPath, archiveInPath.str());
  3248. else
  3249. zipCommand.appendf("tar -czf %s %s", zipFileNameWithPath, archiveInPath.str());
  3250. return (system(zipCommand.str()));
  3251. }
  3252. void CWsWuFileHelper::createWUZAPFile(IEspContext& context, IConstWorkUnit* cwu, CWsWuZAPInfoReq& request,
  3253. StringBuffer& zipFileName, StringBuffer& zipFileNameWithPath, unsigned _thorSlaveLogThreadPoolSize)
  3254. {
  3255. StringBuffer zapReportNameStr, folderToZIP, inFileNamePrefixWithPath;
  3256. Owned<IFile> zipDir = createWorkingFolder(context, request.wuid.str(), "ZAPReport_", zapReportNameStr, folderToZIP);
  3257. setZAPFile(request.zapFileName.str(), zapReportNameStr.str(), zipFileName, zipFileNameWithPath);
  3258. thorSlaveLogThreadPoolSize = _thorSlaveLogThreadPoolSize;
  3259. //create WU ZAP files
  3260. inFileNamePrefixWithPath.set(folderToZIP.str()).append(PATHSEPCHAR).append(zapReportNameStr.str());
  3261. createZAPInfoFile(request.url.str(), request.espIP.str(), request.thorIP.str(), request.problemDesc.str(), request.whatChanged.str(),
  3262. request.whereSlow.str(), cwu, inFileNamePrefixWithPath.str());
  3263. createZAPECLQueryArchiveFiles(cwu, inFileNamePrefixWithPath.str());
  3264. WsWuInfo winfo(context, cwu);
  3265. createZAPWUXMLFile(winfo, inFileNamePrefixWithPath.str());
  3266. createZAPWUGraphProgressFile(request.wuid.str(), inFileNamePrefixWithPath.str());
  3267. createZAPWUQueryAssociatedFiles(cwu, folderToZIP);
  3268. createProcessLogfile(cwu, winfo, "EclAgent", folderToZIP.str());
  3269. createProcessLogfile(cwu, winfo, "Thor", folderToZIP.str());
  3270. if (request.includeThorSlaveLog.isEmpty() || strieq(request.includeThorSlaveLog.str(), "on"))
  3271. createThorSlaveLogfile(cwu, winfo, folderToZIP.str());
  3272. //Write out to ZIP file
  3273. int zipRet = zipAFolder(folderToZIP.str(), request.password.str(), zipFileNameWithPath);
  3274. //Remove the temporary files and the folder
  3275. cleanFolder(zipDir, true);
  3276. if (zipRet != 0)
  3277. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA,"Failed to execute system command 'zip'. Please make sure that zip utility is installed.");
  3278. }
  3279. void CWsWuFileHelper::createZAPWUQueryAssociatedFiles(IConstWorkUnit* cwu, const char* pathToCreate)
  3280. {
  3281. Owned<IConstWUQuery> query = cwu->getQuery();
  3282. if (!query)
  3283. {
  3284. IERRLOG("Cannot get Query for workunit %s.", cwu->queryWuid());
  3285. return;
  3286. }
  3287. Owned<IConstWUAssociatedFileIterator> iter = &query->getAssociatedFiles();
  3288. ForEach(*iter)
  3289. {
  3290. SCMStringBuffer name, ip;
  3291. IConstWUAssociatedFile& cur = iter->query();
  3292. cur.getName(name);
  3293. cur.getIp(ip);
  3294. RemoteFilename rfn;
  3295. SocketEndpoint ep(ip.str());
  3296. rfn.setPath(ep, name.str());
  3297. OwnedIFile sourceFile = createIFile(rfn);
  3298. if (!sourceFile)
  3299. {
  3300. IERRLOG("Cannot open %s on %s.", name.str(), ip.str());
  3301. continue;
  3302. }
  3303. StringBuffer fileName(name.str());
  3304. getFileNameOnly(fileName, false);
  3305. StringBuffer outFileName(pathToCreate);
  3306. outFileName.append(PATHSEPCHAR).append(fileName);
  3307. OwnedIFile outFile = createIFile(outFileName);
  3308. if (!outFile)
  3309. {
  3310. IERRLOG("Cannot create %s.", outFileName.str());
  3311. continue;
  3312. }
  3313. copyFile(outFile, sourceFile);
  3314. }
  3315. }
  3316. void CWsWuFileHelper::setZAPFile(const char* zipFileNameReq, const char* zipFileNamePrefix,
  3317. StringBuffer& zipFileName, StringBuffer& zipFileNameWithPath)
  3318. {
  3319. StringBuffer outFileNameReq(zipFileNameReq);
  3320. //Clean zipFileNameReq. The zipFileNameReq should not end with PATHSEPCHAR.
  3321. while (!outFileNameReq.isEmpty() && (outFileNameReq.charAt(outFileNameReq.length() - 1) == PATHSEPCHAR))
  3322. outFileNameReq.setLength(outFileNameReq.length() - 1);
  3323. if (outFileNameReq.isEmpty())
  3324. zipFileName.set(zipFileNamePrefix).append(".zip");
  3325. else
  3326. {
  3327. zipFileName.set(outFileNameReq.str());
  3328. const char* ext = pathExtension(zipFileName.str());
  3329. if (!ext || !strieq(ext, ".zip"))
  3330. zipFileName.append(".zip");
  3331. }
  3332. zipFileNameWithPath.set(zipFolder);
  3333. Owned<IFile> workingDir = createIFile(zipFileNameWithPath.str());
  3334. if (!workingDir->exists())
  3335. workingDir->createDirectory();
  3336. zipFileNameWithPath.append(PATHSEPCHAR).append(zipFileName);
  3337. OwnedIFile thisFile = createIFile(zipFileNameWithPath.str());
  3338. if (thisFile->isFile() == fileBool::foundYes)
  3339. thisFile->remove();
  3340. }
  3341. IFile* CWsWuFileHelper::createWorkingFolder(IEspContext& context, const char* wuid, const char* namePrefix,
  3342. StringBuffer& namePrefixStr, StringBuffer& folderName)
  3343. {
  3344. StringBuffer userName;
  3345. if (context.queryUser())
  3346. userName.append(context.queryUser()->getName());
  3347. namePrefixStr.set(namePrefix).append(wuid).append('_').append(userName.str());
  3348. folderName.append(zipFolder).append(namePrefixStr.str());
  3349. Owned<IFile> workingDir = createIFile(folderName.str());
  3350. if (!workingDir->exists())
  3351. workingDir->createDirectory();
  3352. else
  3353. cleanFolder(workingDir, false);
  3354. return workingDir.getClear();
  3355. }
  3356. IFileIOStream* CWsWuFileHelper::createWUZAPFileIOStream(IEspContext& context, IConstWorkUnit* cwu,
  3357. CWsWuZAPInfoReq& request, unsigned thorSlaveLogThreadPoolSize)
  3358. {
  3359. StringBuffer zapFileName, zapFileNameWithPath;
  3360. createWUZAPFile(context, cwu, request, zapFileName, zapFileNameWithPath, thorSlaveLogThreadPoolSize);
  3361. if (request.sendEmail)
  3362. {
  3363. CWsWuEmailHelper emailHelper(request.emailFrom.str(), request.emailTo.str(), request.emailServer.str(), request.port);
  3364. StringBuffer subject(request.emailSubject.str());
  3365. if (subject.isEmpty())
  3366. subject.append(request.wuid.str()).append(" ZAP Report");
  3367. emailHelper.setSubject(subject.str());
  3368. PROGLOG("Sending WU ZAP email (%s): from %s to %s", request.emailServer.str(), request.emailFrom.str(), request.emailTo.str());
  3369. StringArray warnings;
  3370. if (!request.attachZAPReportToEmail)
  3371. emailHelper.send(request.emailBody.str(), "", 0, warnings);
  3372. else
  3373. {
  3374. Owned<IFile> f = createIFile(zapFileNameWithPath.str());
  3375. Owned<IFileIO> io = f->open(IFOread);
  3376. unsigned zapFileSize = (unsigned) io->size();
  3377. if (zapFileSize > request.maxAttachmentSize)
  3378. {
  3379. request.emailBody.appendf("\n\n(Failed to attach the ZAP report. The size limit is %u bytes.)", request.maxAttachmentSize);
  3380. emailHelper.send(request.emailBody.str(), "", 0, warnings);
  3381. }
  3382. else
  3383. {
  3384. MemoryBuffer mb;
  3385. void * data = mb.reserve(zapFileSize);
  3386. size32_t read = io->read(0, zapFileSize, data);
  3387. mb.setLength(read);
  3388. emailHelper.setAttachmentName(zapFileName.str());
  3389. emailHelper.setMimeType("application/zip, application/octet-stream");
  3390. emailHelper.send(request.emailBody.str(), mb.toByteArray(), mb.length(), warnings);
  3391. }
  3392. }
  3393. }
  3394. VStringBuffer headerStr("attachment;filename=%s", zapFileName.str());
  3395. context.addCustomerHeader("Content-disposition", headerStr.str());
  3396. return createIOStreamWithFileName(zapFileNameWithPath.str(), IFOread);
  3397. }
  3398. IFileIOStream* CWsWuFileHelper::createWUFileIOStream(IEspContext& context, const char* wuid, IArrayOf<IConstWUFileOption>& wuFileOptions,
  3399. CWUFileDownloadOption& downloadOptions, StringBuffer& contentType)
  3400. {
  3401. StringBuffer fileName, fileNameStr, workingFolder, zipFileNameWithPath;
  3402. Owned<IFile> zipDir;
  3403. bool doZIP = (downloadOptions == CWUFileDownloadOption_ZIP) || (downloadOptions == CWUFileDownloadOption_GZIP);
  3404. if (doZIP)
  3405. {
  3406. zipDir.setown(createWorkingFolder(context, wuid, "WUFiles_", fileNameStr, workingFolder));
  3407. }
  3408. else
  3409. {
  3410. StringBuffer userName;
  3411. if (context.queryUser())
  3412. userName.append(context.queryUser()->getName());
  3413. fileName.set("WUFiles_").append(wuid).append('_').append(userName.str());
  3414. }
  3415. WsWuInfo winfo(context, wuid);
  3416. ForEachItemIn(i, wuFileOptions)
  3417. {
  3418. if (!doZIP)
  3419. {//If no zip, only return one file. If > 1 files, the caller throws exception.
  3420. readWUFile(wuid, zipFolder, winfo, wuFileOptions.item(i), fileName, contentType);
  3421. break;
  3422. }
  3423. StringBuffer aFileName, aFileMimeType;//Not used
  3424. readWUFile(wuid, workingFolder.str(), winfo, wuFileOptions.item(i), aFileName, aFileMimeType);
  3425. }
  3426. if (!doZIP)
  3427. {
  3428. if (downloadOptions != CWUFileDownloadOption_OriginalText)
  3429. {
  3430. VStringBuffer headerStr("attachment;filename=%s", fileName.str());
  3431. context.addCustomerHeader("Content-disposition", headerStr.str());
  3432. }
  3433. zipFileNameWithPath.set(zipFolder).append(fileName.str());
  3434. return createIOStreamWithFileName(zipFileNameWithPath.str(), IFOread);
  3435. }
  3436. if (downloadOptions == CWUFileDownloadOption_ZIP)
  3437. fileName.set(fileNameStr).append(".zip");
  3438. else
  3439. fileName.set(fileNameStr).append(".gzip");
  3440. zipFileNameWithPath.set(zipFolder).append(fileName.str());
  3441. {
  3442. OwnedIFile oldFile = createIFile(zipFileNameWithPath.str());
  3443. if (oldFile->isFile() == fileBool::foundYes)
  3444. oldFile->remove();
  3445. }
  3446. int zipRet = zipAFolder(workingFolder.str(), downloadOptions == CWUFileDownloadOption_GZIP, zipFileNameWithPath);
  3447. //Remove the temporary files and the folder
  3448. cleanFolder(zipDir, true);
  3449. if (zipRet != 0)
  3450. throw MakeStringException(ECLWATCH_CANNOT_COMPRESS_DATA, "Failed to execute system command 'zip'. Please make sure that zip utility is installed.");
  3451. contentType.set(HTTP_TYPE_OCTET_STREAM);
  3452. VStringBuffer headerStr("attachment;filename=%s", fileName.str());
  3453. context.addCustomerHeader("Content-disposition", headerStr.str());
  3454. return createIOStreamWithFileName(zipFileNameWithPath.str(), IFOread);
  3455. }
  3456. void CWsWuFileHelper::readWUFile(const char* wuid, const char* workingFolder, WsWuInfo& winfo, IConstWUFileOption& item,
  3457. StringBuffer& fileName, StringBuffer& fileMimeType)
  3458. {
  3459. MemoryBuffer mb;
  3460. StringBuffer fileNameWithPath;
  3461. CWUFileType fileType = item.getFileType();
  3462. switch (fileType)
  3463. {
  3464. case CWUFileType_ArchiveQuery:
  3465. winfo.getWorkunitArchiveQuery(mb);
  3466. fileName.set("ArchiveQuery.xml");
  3467. fileMimeType.set(HTTP_TYPE_APPLICATION_XML);
  3468. writeToFileIOStream(workingFolder, "ArchiveQuery.xml", mb);
  3469. break;
  3470. case CWUFileType_CPP:
  3471. case CWUFileType_LOG:
  3472. {
  3473. const char *tail=pathTail(item.getName());
  3474. fileName.set(tail ? tail : item.getName());
  3475. fileMimeType.set(HTTP_TYPE_TEXT_PLAIN);
  3476. fileNameWithPath.set(workingFolder).append(PATHSEPCHAR).append(fileName.str());
  3477. winfo.getWorkunitCpp(item.getName(), item.getDescription(), item.getIPAddress(), mb, true, fileNameWithPath.str());
  3478. break;
  3479. }
  3480. case CWUFileType_DLL:
  3481. {
  3482. const char *tail=pathTail(item.getName());
  3483. fileName.set(tail ? tail : item.getName());
  3484. fileMimeType.set(HTTP_TYPE_OCTET_STREAM);
  3485. StringBuffer name;
  3486. winfo.getWorkunitDll(name, mb);
  3487. writeToFileIOStream(workingFolder, fileName.str(), mb);
  3488. break;
  3489. }
  3490. case CWUFileType_Res:
  3491. fileName.set("res.txt");
  3492. fileMimeType.set(HTTP_TYPE_TEXT_PLAIN);
  3493. winfo.getWorkunitResTxt(mb);
  3494. writeToFileIOStream(workingFolder, fileName.str(), mb);
  3495. break;
  3496. case CWUFileType_ThorLog:
  3497. fileName.set("thormaster.log");
  3498. fileMimeType.set(HTTP_TYPE_TEXT_PLAIN);
  3499. fileNameWithPath.set(workingFolder).append(PATHSEPCHAR).append(fileName.str());
  3500. winfo.getWorkunitThorLog(item.getName(), mb, fileNameWithPath.str());
  3501. break;
  3502. case CWUFileType_ThorSlaveLog:
  3503. {
  3504. fileName.set("ThorSlave.log");
  3505. fileMimeType.set(HTTP_TYPE_TEXT_PLAIN);
  3506. fileNameWithPath.set(workingFolder).append(PATHSEPCHAR).append(fileName.str());
  3507. winfo.getWorkunitThorSlaveLog(directories, item.getProcess(), item.getClusterGroup(), item.getIPAddress(),
  3508. item.getLogDate(), item.getSlaveNumber(), mb, fileNameWithPath.str(), false);
  3509. break;
  3510. }
  3511. case CWUFileType_EclAgentLog:
  3512. fileName.set("eclagent.log");
  3513. fileMimeType.set(HTTP_TYPE_TEXT_PLAIN);
  3514. fileNameWithPath.set(workingFolder).append(PATHSEPCHAR).append(fileName.str());
  3515. winfo.getWorkunitEclAgentLog(item.getName(), item.getProcess(), mb, fileNameWithPath.str());
  3516. break;
  3517. case CWUFileType_XML:
  3518. {
  3519. StringBuffer name(item.getName());
  3520. if (!name.isEmpty())
  3521. {
  3522. const char *tail=pathTail(name.str());
  3523. fileName.set(tail ? tail : name.str());
  3524. fileNameWithPath.set(workingFolder).append(PATHSEPCHAR).append(fileName.str());
  3525. winfo.getWorkunitAssociatedXml(fileName.str(), item.getIPAddress(), item.getPlainText(), item.getDescription(), true, true, mb, fileNameWithPath.str());
  3526. }
  3527. else
  3528. {
  3529. fileName.setf("%s.xml", wuid);
  3530. winfo.getWorkunitXml(item.getPlainText(), mb);
  3531. writeToFileIOStream(workingFolder, fileName.str(), mb);
  3532. }
  3533. const char* plainText = item.getPlainText();
  3534. if (plainText && strieq(plainText, "yes"))
  3535. fileMimeType.set(HTTP_TYPE_TEXT_PLAIN);
  3536. else
  3537. fileMimeType.set(HTTP_TYPE_APPLICATION_XML);
  3538. break;
  3539. }
  3540. case CWUFileType_WUECL:
  3541. fileName.setf("%s.ecl", wuid);
  3542. fileMimeType.set(HTTP_TYPE_TEXT_PLAIN);
  3543. fileNameWithPath.set(workingFolder).append(PATHSEPCHAR).append(fileName.str());
  3544. winfo.getWorkunitQueryShortText(mb, fileNameWithPath.str());
  3545. break;
  3546. default:
  3547. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Unsupported file type %d.", fileType);
  3548. }
  3549. }
  3550. void CWsWuEmailHelper::send(const char* body, const void* attachment, size32_t lenAttachment, StringArray& warnings)
  3551. {
  3552. if (lenAttachment == 0)
  3553. sendEmail(to.get(), subject.get(), body, mailServer.get(), port, sender.get(), &warnings);
  3554. else
  3555. sendEmailAttachData(to.get(), subject.get(), body, lenAttachment, attachment, mimeType.get(),
  3556. attachmentName.get(), mailServer.get(), port, sender.get(), &warnings);
  3557. }
  3558. }