12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- #include "jlib.hpp"
- #include "ws_workunitsHelpers.hpp"
- #include "exception_util.hpp"
- #include "daclient.hpp"
- #include "dalienv.hpp"
- #include "daaudit.hpp"
- #include "portlist.h"
- #include "dadfs.hpp"
- #include "fileview.hpp"
- #include "wuwebview.hpp"
- #include "dllserver.hpp"
- #include "wujobq.hpp"
- #ifdef _USE_ZLIB
- #include "zcrypt.hpp"
- #endif
- //#include "workunit.hpp"
- //#include "daclient.hpp"
- //#include "dalienv.hpp"
- //#include "exception_util.hpp"
- //#include "wujobq.hpp"
- //#include "eventqueue.hpp"
- //#include "hqlerror.hpp"
- //#include "sacmd.hpp"
- //#include "portlist.h"
- //#define OWN_WU_ACCESS "OwnWorkunitsAccess"
- //#define OTHERS_WU_ACCESS "OthersWorkunitsAccess"
- //const unsigned MAXTHORS = 1024;
- //#define File_Cpp "cpp"
- //#define File_ThorLog "ThorLog"
- //#define File_ThorSlaveLog "ThorSlaveLog"
- //#define File_EclAgentLog "EclAgentLog"
- //#define File_XML "XML"
- //#define File_Res "res"
- //#define File_DLL "dll"
- //#define File_ArchiveQuery "ArchiveQuery"
- namespace ws_workunits {
- SecAccessFlags chooseWuAccessFlagsByOwnership(const char *user, const char *owner, SecAccessFlags accessOwn, SecAccessFlags accessOthers)
- {
- return (isEmpty(owner) || (user && streq(user, owner))) ? accessOwn : accessOthers;
- }
- SecAccessFlags chooseWuAccessFlagsByOwnership(const char *user, IConstWorkUnit& cw, SecAccessFlags accessOwn, SecAccessFlags accessOthers)
- {
- SCMStringBuffer owner;
- return chooseWuAccessFlagsByOwnership(user, cw.getUser(owner).str(), accessOwn, accessOthers);
- }
- const char *getWuAccessType(const char *owner, const char *user)
- {
- return (isEmpty(owner) || (user && streq(user, owner))) ? OWN_WU_ACCESS : OTHERS_WU_ACCESS;
- }
- const char *getWuAccessType(IConstWorkUnit& cw, const char *user)
- {
- SCMStringBuffer owner;
- return getWuAccessType(cw.getUser(owner).str(), user);
- }
- void getUserWuAccessFlags(IEspContext& context, SecAccessFlags& accessOwn, SecAccessFlags& accessOthers, bool except)
- {
- if (!context.authorizeFeature(OWN_WU_ACCESS, accessOwn))
- accessOwn = SecAccess_None;
- if (!context.authorizeFeature(OTHERS_WU_ACCESS, accessOthers))
- accessOthers = SecAccess_None;
- if (except && (accessOwn == SecAccess_None) && (accessOthers == SecAccess_None))
- {
- AuditSystemAccess(context.queryUserId(), false, "Access Denied: User can't view any workunits");
- VStringBuffer msg("Access Denied: User %s does not have rights to access workunits.", context.queryUserId());
- throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "%s", msg.str());
- }
- }
- SecAccessFlags getWsWorkunitAccess(IEspContext& cxt, IConstWorkUnit& cw)
- {
- SecAccessFlags accessFlag = SecAccess_None;
- cxt.authorizeFeature(getWuAccessType(cw, cxt.queryUserId()), accessFlag);
- return accessFlag;
- }
- void ensureWsWorkunitAccessByOwnerId(IEspContext& cxt, const char* owner, SecAccessFlags minAccess)
- {
- if (!cxt.validateFeatureAccess(getWuAccessType(owner, cxt.queryUserId()), minAccess, false))
- throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to access workunit. Permission denied.");
- }
- void ensureWsWorkunitAccess(IEspContext& cxt, IConstWorkUnit& cw, SecAccessFlags minAccess)
- {
- if (!cxt.validateFeatureAccess(getWuAccessType(cw, cxt.queryUserId()), minAccess, false))
- throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to access workunit. Permission denied.");
- }
- void ensureWsWorkunitAccess(IEspContext& context, const char* wuid, SecAccessFlags minAccess)
- {
- Owned<IWorkUnitFactory> wf = getWorkUnitFactory(context.querySecManager(), context.queryUser());
- Owned<IConstWorkUnit> cw = wf->openWorkUnit(wuid, false);
- if (!cw)
- throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Failed to open workunit %s when ensuring workunit access", wuid);
- ensureWsWorkunitAccess(context, *cw, minAccess);
- }
- void ensureWsCreateWorkunitAccess(IEspContext& cxt)
- {
- if (!cxt.validateFeatureAccess(OWN_WU_ACCESS, SecAccess_Write, false))
- throw MakeStringException(ECLWATCH_ECL_WU_ACCESS_DENIED, "Failed to create workunit. Permission denied.");
- }
- StringBuffer &getWuidFromLogicalFileName(IEspContext &context, const char *logicalName, StringBuffer &wuid)
- {
- Owned<IUserDescriptor> userdesc = createUserDescriptor();
- userdesc->set(context.queryUserId(), context.queryPassword());
- Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName, userdesc);
- if (!df)
- throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find file %s.",logicalName);
- return wuid.append(df->queryAttributes().queryProp("@workunit"));
- }
- void formatDuration(StringBuffer &s, unsigned ms)
- {
- unsigned days = ms / (1000*60*60*24);
- ms %= (1000*60*60*24);
- unsigned hours = ms / (1000*60*60);
- ms %= (1000*60*60);
- unsigned mins = ms / (1000*60);
- ms %= (1000*60);
- unsigned secs = ms / 1000;
- ms %= 1000;
- if (days)
- s.appendf("%d days ", days);
- if (hours || s.length())
- s.appendf("%d:", hours);
- if (mins || s.length())
- s.appendf("%d:", mins);
- if (s.length())
- s.appendf("%02d.%03d", secs, ms);
- else
- s.appendf("%d.%03d", secs, ms);
- }
- WsWUExceptions::WsWUExceptions(IConstWorkUnit& wu): numerr(0), numwrn(0), numinf(0)
- {
- Owned<IConstWUExceptionIterator> it = &wu.getExceptions();
- ForEach(*it)
- {
- SCMStringBuffer src, msg, file;
- Owned<IEspECLException> e= createECLException("","");
- e->setCode(it->query().getExceptionCode());
- e->setSource(it->query().getExceptionSource(src).str());
- e->setMessage(it->query().getExceptionMessage(msg).str());
- e->setFileName(it->query().getExceptionFileName(file).str());
- e->setLineNo(it->query().getExceptionLineNo());
- e->setColumn(it->query().getExceptionColumn());
- const char * label = "";
- switch (it->query().getSeverity())
- {
- default:
- case ExceptionSeverityError: label = "Error"; numerr++; break;
- case ExceptionSeverityWarning: label = "Warning"; numwrn++; break;
- case ExceptionSeverityInformation: label = "Info"; numinf++; break;
- }
- e->setSeverity(label);
- errors.append(*e.getLink());
- }
- }
- #define SDS_LOCK_TIMEOUT 30000
- void getSashaNode(SocketEndpoint &ep)
- {
- Owned<IRemoteConnection> econn = querySDS().connect("/Environment", myProcessSession(), 0, SDS_LOCK_TIMEOUT);
- if (!econn)
- throw MakeStringException(ECLWATCH_CANNOT_CONNECT_DALI,"Cannot connect to DALI server.");
- IPropertyTree *root = econn->queryRoot();
- if (!root)
- throw MakeStringException(ECLWATCH_CANNOT_GET_ENV_INFO,"Cannot get environment information.");
- IPropertyTree *pt = root->queryPropTree("Software/SashaServerProcess[1]/Instance[1]");
- if (!pt)
- throw MakeStringException(ECLWATCH_ARCHIVE_SERVER_NOT_FOUND, "Archive Server not found.");
- ep.set(pt->queryProp("@netAddress"), pt->getPropInt("@port",DEFAULT_SASHA_PORT));
- }
- void WsWuInfo::getSourceFiles(IEspECLWorkunit &info, unsigned flags)
- {
- if (!(flags & WUINFO_IncludeSourceFiles))
- return;
- try
- {
- Owned<IUserDescriptor> userdesc;
- StringBuffer username;
- context.getUserID(username);
- const char* passwd = context.queryPassword();
- userdesc.setown(createUserDescriptor());
- userdesc->set(username.str(), passwd);
- IArrayOf<IEspECLSourceFile> files;
- if (version < 1.27)
- {
- Owned<IPropertyTreeIterator> f=&cw->getFilesReadIterator();
- ForEach(*f)
- {
- IPropertyTree &query = f->query();
- const char *clusterName = query.queryProp("@cluster");
- const char *fileName = query.queryProp("@name");
- int fileCount = query.getPropInt("@useCount");
- Owned<IEspECLSourceFile> file= createECLSourceFile("","");
- if(clusterName && *clusterName)
- {
- file->setFileCluster(clusterName);
- }
- if (version > 1.11)
- {
- Owned<IPropertyTreeIterator> filetrees= query.getElements("Subfile");
- if (filetrees->first())
- file->setIsSuperFile(true);
- }
- if (fileName && *fileName)
- {
- file->setName(fileName);
- }
- file->setCount(fileCount);
- files.append(*file.getLink());
- }
- }
- else
- {
- StringArray fileNames;
- Owned<IPropertyTreeIterator> f=&cw->getFilesReadIterator();
- ForEach(*f)
- {
- IPropertyTree &query = f->query();
- const char *clusterName = query.queryProp("@cluster");
- const char *fileName = query.queryProp("@name");
- int fileCount = query.getPropInt("@useCount");
- bool bFound = false;
- if (fileName && *fileName && (fileNames.length() > 0))
- {
- for (unsigned i = 0; i < fileNames.length(); i++ )
- {
- const char *fileName0 = fileNames.item(i);
- if (!stricmp(fileName, fileName0))
- {
- bFound = true;
- break;
- }
- }
- }
- if (bFound)
- continue;
- Owned<IEspECLSourceFile> file= createECLSourceFile("","");
- if(clusterName && *clusterName)
- {
- file->setFileCluster(clusterName);
- }
- if (fileName && *fileName)
- {
- file->setName(fileName);
- }
- file->setCount(fileCount);
- Owned<IPropertyTreeIterator> filetrees= query.getElements("Subfile");
- if (filetrees->first())
- {
- file->setIsSuperFile(true);
- getSubFiles(filetrees, file, fileNames);
- }
- files.append(*file.getLink());
- }
- }
- info.setSourceFiles(files);
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setSourceFilesDesc(eMsg.str());
- e->Release();
- }
- }
- void WsWuInfo::getExceptions(IEspECLWorkunit &info, unsigned flags)
- {
- if ((flags & WUINFO_IncludeExceptions) || version > 1.16)
- {
- WsWUExceptions errors(*cw);
- if (version > 1.16)
- {
- info.setErrorCount(errors.ErrCount());
- info.setWarningCount(errors.WrnCount());
- info.setInfoCount(errors.InfCount());
- }
- if ((flags & WUINFO_IncludeExceptions))
- info.setExceptions(errors);
- }
- }
- void WsWuInfo::getVariables(IEspECLWorkunit &info, unsigned flags)
- {
- if (!(flags & WUINFO_IncludeVariables))
- return;
- try
- {
- IArrayOf<IEspECLResult> results;
- Owned<IConstWUResultIterator> vars = &cw->getVariables();
- ForEach(*vars)
- getResult(vars->query(), results, flags);
- info.setVariables(results);
- results.kill();
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setVariablesDesc(eMsg.str());
- e->Release();
- }
- }
- void WsWuInfo::getTimers(IEspECLWorkunit &info, unsigned flags)
- {
- if (!(flags & WUINFO_IncludeTimers))
- return;
- try
- {
- StringBuffer totalThorTimeValue;
- unsigned totalThorTimerCount = 0; //Do we need this?
- //This should be switched to using getStatistics() once backward compatibility is no longer required. (5.0?)
- //The code inside the #if 0 is equivalent.
- IArrayOf<IEspECLTimer> timers;
- #if 0
- Owned<IConstWUStatisticIterator> it = &cw->getStatistics();
- ForEach(*it)
- {
- IConstWUStatistic & cur = it->query();
- //Only interested in timings...
- if (cur.getKind() != SMEASURE_TIME_NS)
- continue;
- SCMStringBuffer name;
- cur.getDescription(name);
- name.s.replace('_', ' ');
- unsigned __int64 count = cur.getCount();
- unsigned __int64 duration = nanoToMilli(cur.getValue());
- StringBuffer fd;
- formatDuration(fd, duration);
- if (strieq(name.str(), TOTALTHORTIME))
- {
- totalThorTimeValue = fd;
- totalThorTimerCount = count;
- continue;
- }
- Owned<IEspECLTimer> t= createECLTimer("","");
- t->setName(name.str());
- t->setValue(fd.str());
- t->setCount(count);
- if (version > 1.19)
- {
- StringAttr graphName;
- unsigned graphNum;
- unsigned subGraphNum;
- unsigned subId;
- if (parseGraphTimerLabel(name.str(), graphName, graphNum, subGraphNum, subId))
- {
- if (graphName.length() > 0)
- {
- t->setGraphName(graphName);
- }
- if (subId > 0)
- {
- t->setSubGraphId((int)subId);
- }
- }
- }
- timers.append(*t.getLink());
- }
- #else
- Owned<IStringIterator> it = &cw->getTimers();
- ForEach(*it)
- {
- SCMStringBuffer name;
- it->str(name);
- unsigned count = cw->getTimerCount(name.str());
- unsigned duration = cw->getTimerDuration(name.str());
- StringBuffer fd;
- formatDuration(fd, duration);
- name.s.replace('_', ' ');
- if (strieq(name.str(), TOTALTHORTIME))
- {
- totalThorTimeValue = fd;
- totalThorTimerCount = count;
- continue;
- }
- Owned<IEspECLTimer> t= createECLTimer("","");
- t->setName(name.str());
- t->setValue(fd.str());
- t->setCount(count);
- if (version > 1.19)
- {
- StringAttr graphName;
- unsigned graphNum;
- unsigned subGraphNum;
- unsigned subId;
- if (parseGraphTimerLabel(name.str(), graphName, graphNum, subGraphNum, subId))
- {
- if (graphName.length() > 0)
- {
- t->setGraphName(graphName);
- }
- if (subId > 0)
- {
- t->setSubGraphId((int)subId);
- }
- }
- }
- timers.append(*t.getLink());
- }
- #endif
- if (totalThorTimeValue.length() > 0)
- {
- Owned<IEspECLTimer> t= createECLTimer("","");
- t->setName(TOTALTHORTIME);
- t->setValue(totalThorTimeValue.str());
- t->setCount(totalThorTimerCount);
- timers.append(*t.getLink());
- }
- info.setTimers(timers);
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setTimersDesc(eMsg.str());
- e->Release();
- }
- }
- struct mapEnums { int val; const char *str; };
- mapEnums queryFileTypes[] = {
- { FileTypeCpp, "cpp" },
- { FileTypeDll, "dll" },
- { FileTypeResText, "res" },
- { FileTypeHintXml, "hint" },
- { FileTypeXml, "xml" },
- { FileTypeSize, NULL },
- };
- const char *getEnumText(int value, mapEnums *map)
- {
- const char *defval = map->str;
- while (map->str)
- {
- if (value==map->val)
- return map->str;
- map++;
- }
- assertex(!"Unexpected value in setEnum");
- return defval;
- }
- void WsWuInfo::getHelpers(IEspECLWorkunit &info, unsigned flags)
- {
- try
- {
- IArrayOf<IEspECLHelpFile> helpers;
- Owned <IConstWUQuery> query = cw->getQuery();
- if(!query)
- {
- ERRLOG("Cannot get Query for this workunit.");
- info.setHelpersDesc("Cannot get Query for this workunit.");
- }
- else
- {
- SCMStringBuffer qname;
- query->getQueryShortText(qname);
- if(qname.length())
- {
- if((flags & WUINFO_TruncateEclTo64k) && (qname.length() > 64000))
- qname.setLen(qname.str(), 64000);
- IEspECLQuery* q=&info.updateQuery();
- q->setText(qname.str());
- }
- if (version > 1.34)
- {
- SCMStringBuffer mainDefinition;
- query->getQueryMainDefinition(mainDefinition);
- if(mainDefinition.length())
- {
- IEspECLQuery* q=&info.updateQuery();
- q->setQueryMainDefinition(mainDefinition.str());
- }
- }
- if (version > 1.30)
- {
- SCMStringBuffer qText;
- query->getQueryText(qText);
- if ((qText.length() > 0) && isArchiveQuery(qText.str()))
- info.setHasArchiveQuery(true);
- }
- for (unsigned i = 0; i < FileTypeSize; i++)
- getHelpFiles(query, (WUFileType) i, helpers);
- }
- getWorkunitThorLogInfo(helpers, info);
- if (cw->getWuidVersion() > 0)
- {
- Owned<IPropertyTreeIterator> eclAgents = cw->getProcesses("EclAgent", NULL);
- ForEach (*eclAgents)
- {
- StringBuffer logName;
- IPropertyTree& eclAgent = eclAgents->query();
- eclAgent.getProp("@log",logName);
- if (!logName.length())
- continue;
- Owned<IEspECLHelpFile> h= createECLHelpFile("","");
- h->setName(logName.str());
- h->setType(File_EclAgentLog);
- if (version >= 1.43)
- {
- offset_t fileSize;
- if (getFileSize(logName.str(), NULL, fileSize))
- h->setFileSize(fileSize);
- if (version >= 1.44)
- {
- if (eclAgent.hasProp("@pid"))
- h->setPID(eclAgent.getPropInt("@pid"));
- else
- h->setPID(cw->getAgentPID());
- }
- }
- helpers.append(*h.getLink());
- }
- }
- else // legacy wuid
- {
- Owned<IStringIterator> eclAgentLogs = cw->getLogs("EclAgent");
- ForEach (*eclAgentLogs)
- {
- SCMStringBuffer name;
- eclAgentLogs->str(name);
- if (name.length() < 1)
- continue;
- Owned<IEspECLHelpFile> h= createECLHelpFile("","");
- h->setName(name.str());
- h->setType(File_EclAgentLog);
- if (version >= 1.43)
- {
- offset_t fileSize;
- if (getFileSize(name.str(), NULL, fileSize))
- h->setFileSize(fileSize);
- }
- helpers.append(*h.getLink());
- break;
- }
- }
- info.setHelpers(helpers);
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setHelpersDesc(eMsg.str());
- e->Release();
- }
- }
- void WsWuInfo::getApplicationValues(IEspECLWorkunit &info, unsigned flags)
- {
- if (!(flags & WUINFO_IncludeApplicationValues))
- return;
- try
- {
- IArrayOf<IEspApplicationValue> av;
- Owned<IConstWUAppValueIterator> app(&cw->getApplicationValues());
- ForEach(*app)
- {
- IConstWUAppValue& val=app->query();
- SCMStringBuffer buf;
- Owned<IEspApplicationValue> t= createApplicationValue("","");
- t->setApplication(val.getApplication(buf).str());
- t->setValue(val.getValue(buf).str());
- t->setName(val.getName(buf).str());
- t->setValue(val.getValue(buf).str());
- av.append(*t.getLink());
- }
- info.setApplicationValues(av);
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setApplicationValuesDesc(eMsg.str());
- e->Release();
- }
- }
- void WsWuInfo::getDebugValues(IEspECLWorkunit &info, unsigned flags)
- {
- if (!(flags & WUINFO_IncludeDebugValues))
- return;
- try
- {
- IArrayOf<IEspDebugValue> dv;
- Owned<IStringIterator> debugs(&cw->getDebugValues());
- ForEach(*debugs)
- {
- SCMStringBuffer name, val;
- debugs->str(name);
- cw->getDebugValue(name.str(),val);
- Owned<IEspDebugValue> t= createDebugValue("","");
- t->setName(name.str());
- t->setValue(val.str());
- dv.append(*t.getLink());
- }
- info.setDebugValues(dv);
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setDebugValuesDesc(eMsg.str());
- e->Release();
- }
- }
- const char *getGraphNum(const char *s,unsigned &num)
- {
- while (*s && !isdigit(*s))
- s++;
- num = 0;
- while (isdigit(*s))
- {
- num = num*10+*s-'0';
- s++;
- }
- return s;
- }
- void WsWuInfo::getGraphInfo(IEspECLWorkunit &info, unsigned flags)
- {
- if (version > 1.01)
- {
- info.setHaveSubGraphTimings(false);
- Owned<IStringIterator> times = &cw->getTimers();
- ForEach(*times)
- {
- SCMStringBuffer name;
- times->str(name);
- StringAttr graphName;
- unsigned graphNum;
- unsigned subGraphNum;
- unsigned subId;
- if (parseGraphTimerLabel(name.str(), graphName, graphNum, subGraphNum, subId))
- {
- info.setHaveSubGraphTimings(true);
- break;
- }
- }
- }
- if (!(flags & WUINFO_IncludeGraphs))
- return;
- try
- {
- SCMStringBuffer runningGraph;
- WUGraphIDType id;
- WUState st = cw->getState();
- bool running = (!(st==WUStateFailed || st==WUStateAborted || st==WUStateCompleted) && cw->getRunningGraph(runningGraph,id));
- IArrayOf<IEspECLGraph> graphs;
- Owned<IConstWUGraphIterator> it = &cw->getGraphs(GraphTypeAny);
- ForEach(*it)
- {
- IConstWUGraph &graph = it->query();
- if(!graph.isValid())
- continue;
- SCMStringBuffer name, label, type;
- graph.getName(name);
- graph.getLabel(label);
- graph.getTypeName(type);
- Owned<IEspECLGraph> g= createECLGraph("","");
- g->setName(name.str());
- g->setLabel(label.str());
- g->setType(type.str());
- if(running && strcmp(name.str(),runningGraph.str())==0)
- {
- g->setRunning(true);
- g->setRunningId(id);
- }
- Owned<IConstWUGraphProgress> progress = cw->getGraphProgress(name.str());
- if (progress)
- {
- WUGraphState graphstate= progress->queryGraphState();
- if (graphstate == WUGraphComplete)
- g->setComplete(true);
- if (version > 1.13 && graphstate == WUGraphFailed)
- {
- g->setFailed(true);
- }
- }
- graphs.append(*g.getLink());
- }
- info.setGraphs(graphs);
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setGraphsDesc(eMsg.str());
- e->Release();
- }
- }
- void WsWuInfo::getGraphTimingData(IArrayOf<IConstECLTimingData> &timingData, unsigned flags)
- {
- Owned<IStringIterator> times = &cw->getTimers();
- ForEach(*times)
- {
- SCMStringBuffer name;
- times->str(name);
- StringAttr graphName;
- unsigned graphNum;
- unsigned subGraphNum;
- unsigned subId;
- if (parseGraphTimerLabel(name.str(), graphName, graphNum, subGraphNum, subId))
- {
- unsigned time = cw->getTimerDuration(name.str());
- Owned<IEspECLTimingData> g = createECLTimingData();
- g->setName(name.str());
- g->setGraphNum(graphNum);
- g->setSubGraphNum(subGraphNum);
- g->setGID(subId);
- g->setMS(time);
- g->setMin(time/60000);
- timingData.append(*g.getClear());
- }
- }
- }
- void WsWuInfo::getRoxieCluster(IEspECLWorkunit &info, unsigned flags)
- {
- if (version > 1.06)
- {
- Owned<IConstWURoxieQueryInfo> roxieQueryInfo = cw->getRoxieQueryInfo();
- if (roxieQueryInfo)
- {
- SCMStringBuffer roxieClusterName;
- roxieQueryInfo->getRoxieClusterName(roxieClusterName);
- info.setRoxieCluster(roxieClusterName.str());
- }
- }
- }
- void WsWuInfo::getEventScheduleFlag(IEspECLWorkunit &info)
- {
- info.setEventSchedule(0);
- if (info.getState() && !stricmp(info.getState(), "wait"))
- {
- info.setEventSchedule(2); //Can deschedule
- }
- else
- {
- Owned<IConstWorkflowItemIterator> it = cw->getWorkflowItems();
- if (it)
- {
- ForEach(*it)
- {
- IConstWorkflowItem *r = it->query();
- if (!r)
- continue;
- IWorkflowEvent *wfevent = r->getScheduleEvent();
- if (!wfevent)
- continue;
- if (!r->hasScheduleCount() || (r->queryScheduleCountRemaining() > 0))
- {
- info.setEventSchedule(1); //Can reschedule
- break;
- }
- }
- }
- }
- }
- void WsWuInfo::getCommon(IEspECLWorkunit &info, unsigned flags)
- {
- SCMStringBuffer s;
- info.setWuid(cw->getWuid(s).str());
- info.setProtected(cw->isProtected() ? 1 : 0);
- info.setJobname(cw->getJobName(s).str());
- info.setOwner(cw->getUser(s).str());
- info.setCluster(cw->getClusterName(clusterName).str());
- info.setSnapshot(cw->getSnapshot(s).str());
- if ((cw->getState() == WUStateScheduled) && cw->aborting())
- {
- info.setStateID(WUStateAborting);
- info.setState("aborting");
- }
- else
- {
- info.setStateID(cw->getState());
- info.setState(cw->getStateDesc(s).str());
- }
- if (cw->isPausing())
- info.setIsPausing(true);
- getEventScheduleFlag(info);
- if (version > 1.27)
- {
- StringBuffer totalThorTimeStr;
- unsigned totalThorTimeMS = cw->getTimerDuration(TOTALTHORTIME);
- formatDuration(totalThorTimeStr, totalThorTimeMS);
- info.setTotalThorTime(totalThorTimeStr.str());
- }
- WsWuDateTime dt;
- cw->getTimeScheduled(dt);
- if(dt.isValid())
- info.setDateTimeScheduled(dt.getString(s).str());
- getRoxieCluster(info, flags);
- }
- void WsWuInfo::getInfo(IEspECLWorkunit &info, unsigned flags)
- {
- getCommon(info, flags);
- SecAccessFlags accessFlag = getWsWorkunitAccess(context, *cw);
- info.setAccessFlag(accessFlag);
- SCMStringBuffer s;
- info.setStateEx(cw->getStateEx(s).str());
- info.setPriorityClass(cw->getPriority());
- info.setPriorityLevel(cw->getPriorityLevel());
- info.setScope(cw->getWuScope(s).str());
- info.setActionEx(cw->getActionEx(s).str());
- info.setDescription(cw->getDebugValue("description", s).str());
- if (version > 1.21)
- info.setXmlParams(cw->getXmlParams(s).str());
- info.setResultLimit(cw->getResultLimit());
- info.setArchived(false);
- info.setGraphCount(cw->getGraphCount());
- info.setSourceFileCount(cw->getSourceFileCount());
- info.setVariableCount(cw->getVariableCount());
- info.setTimerCount(cw->getTimerCount());
- info.setSourceFileCount(cw->getSourceFileCount());
- info.setApplicationValueCount(cw->getApplicationValueCount());
- info.setHasDebugValue(cw->hasDebugValue("__calculated__complexity__"));
- getClusterInfo(info, flags);
- getExceptions(info, flags);
- getHelpers(info, flags);
- getGraphInfo(info, flags);
- getSourceFiles(info, flags);
- getResults(info, flags);
- getVariables(info, flags);
- getTimers(info, flags);
- getDebugValues(info, flags);
- getApplicationValues(info, flags);
- getWorkflow(info, flags);
- }
- unsigned WsWuInfo::getWorkunitThorLogInfo(IArrayOf<IEspECLHelpFile>& helpers, IEspECLWorkunit &info)
- {
- unsigned countThorLog = 0;
- IArrayOf<IConstThorLogInfo> thorLogList;
- if (cw->getWuidVersion() > 0)
- {
- SCMStringBuffer clusterName;
- cw->getClusterName(clusterName);
- if (!clusterName.length()) //Cluster name may not be set yet
- return countThorLog;
- Owned<IConstWUClusterInfo> clusterInfo = getTargetClusterInfo(clusterName.str());
- if (!clusterInfo)
- {
- SCMStringBuffer wuid;
- WARNLOG("Cannot find TargetClusterInfo for workunit %s", cw->getWuid(wuid).str());
- return countThorLog;
- }
- unsigned numberOfSlaves = clusterInfo->getSize();
- BoolHash uniqueProcesses;
- Owned<IStringIterator> thorInstances = cw->getProcesses("Thor");
- ForEach (*thorInstances)
- {
- SCMStringBuffer processName;
- thorInstances->str(processName);
- if ((processName.length() < 1) || uniqueProcesses.getValue(processName.str()))
- continue;
- uniqueProcesses.setValue(processName.str(), true);
- StringBuffer groupName;
- getClusterThorGroupName(groupName, processName.str());
- Owned<IStringIterator> thorLogs = cw->getLogs("Thor", processName.str());
- ForEach (*thorLogs)
- {
- SCMStringBuffer logName;
- thorLogs->str(logName);
- if (logName.length() < 1)
- continue;
- countThorLog++;
- StringBuffer fileType;
- if (countThorLog < 2)
- fileType.append(File_ThorLog);
- else
- fileType.appendf("%s%d", File_ThorLog, countThorLog);
- Owned<IEspECLHelpFile> h= createECLHelpFile("","");
- h->setName(logName.str());
- h->setDescription(processName.str());
- h->setType(fileType.str());
- if (version >= 1.43)
- {
- offset_t fileSize;
- if (getFileSize(logName.str(), NULL, fileSize))
- h->setFileSize(fileSize);
- }
- helpers.append(*h.getLink());
- if (version < 1.38)
- continue;
- const char* pStr = logName.str();
- const char* ppStr = strstr(pStr, "/thormaster.");
- if (!ppStr)
- {
- WARNLOG("Invalid thorlog entry in workunit xml: %s", logName.str());
- continue;
- }
- ppStr += 12;
- StringBuffer logDate = ppStr;
- logDate.setLength(10);
- Owned<IEspThorLogInfo> thorLog = createThorLogInfo("","");
- thorLog->setProcessName(processName.str());
- thorLog->setClusterGroup(groupName.str());
- thorLog->setLogDate(logDate.str());
- thorLog->setNumberSlaves(numberOfSlaves);
- thorLogList.append(*thorLog.getLink());
- }
- }
- }
- else //legacy wuid
- {
- Owned<IStringIterator> thorLogs = cw->getLogs("Thor");
- ForEach (*thorLogs)
- {
- SCMStringBuffer name;
- thorLogs->str(name);
- if (name.length() < 1)
- continue;
- countThorLog++;
- StringBuffer fileType;
- if (countThorLog < 2)
- fileType.append(File_ThorLog);
- else
- fileType.appendf("%s%d", File_ThorLog, countThorLog);
- Owned<IEspECLHelpFile> h= createECLHelpFile("","");
- h->setName(name.str());
- h->setType(fileType.str());
- if (version >= 1.43)
- {
- offset_t fileSize;
- if (getFileSize(name.str(), NULL, fileSize))
- h->setFileSize(fileSize);
- }
- helpers.append(*h.getLink());
- }
- StringBuffer logDir;
- Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory();
- Owned<IConstEnvironment> constEnv = envFactory->openEnvironmentByFile();
- Owned<IPropertyTree> logTree = &constEnv->getPTree();
- if (logTree)
- logTree->getProp("EnvSettings/log", logDir);
- if (logDir.length() > 0)
- {
- Owned<IStringIterator> debugs = cw->getLogs("Thor");
- ForEach(*debugs)
- {
- SCMStringBuffer val;
- debugs->str(val);
- if (val.length() < 1)
- continue;
- const char* pStr = val.str();
- const char* ppStr = strstr(pStr, logDir.str());
- if (!ppStr)
- {
- WARNLOG("Invalid thorlog entry in workunit xml: %s", val.str());
- continue;
- }
- const char* pProcessName = ppStr + logDir.length();
- char sep = pProcessName[0];
- StringBuffer processName = pProcessName + 1;
- ppStr = strchr(pProcessName + 1, sep);
- if (!ppStr)
- {
- WARNLOG("Invalid thorlog entry in workunit xml: %s", val.str());
- continue;
- }
- processName.setLength(ppStr - pProcessName - 1);
- StringBuffer groupName;
- getClusterThorGroupName(groupName, processName.str());
- StringBuffer logDate = ppStr + 12;
- logDate.setLength(10);
- Owned<IEspThorLogInfo> thorLog = createThorLogInfo("","");
- thorLog->setProcessName(processName.str());
- thorLog->setClusterGroup(groupName.str());
- thorLog->setLogDate(logDate.str());
- //for legacy wuid, the log name does not contain slaveNum. So, a user may not specify
- //a slaveNum and we only display the first slave log if > 1 per IP.
- thorLog->setNumberSlaves(0);
- thorLogList.append(*thorLog.getLink());
- }
- }
- }
- if (thorLogList.length() > 0)
- info.setThorLogList(thorLogList);
- thorLogList.kill();
- return countThorLog;
- }
- bool WsWuInfo::getClusterInfo(IEspECLWorkunit &info, unsigned flags)
- {
- if (version > 1.04)
- {
- StringArray allowedClusters;
- SCMStringBuffer val;
- cw->getAllowedClusters(val);
- if (val.length() > 0)
- {
- const char* ptr = val.str();
- while(*ptr != '\0')
- {
- StringBuffer onesub;
- while(*ptr != '\0' && *ptr != ',')
- {
- onesub.append((char)(*ptr));
- ptr++;
- }
- if(onesub.length() > 0)
- allowedClusters.append(onesub.str());
- if(*ptr != '\0')
- ptr++;
- }
- }
- if (allowedClusters.length() > 0)
- info.setAllowedClusters(allowedClusters);
- }
- if (version > 1.23 && clusterName.length())
- {
- int clusterTypeFlag = 0;
- Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory();
- Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
- Owned<IPropertyTree> root = &constEnv->getPTree();
- if (!root)
- throw MakeStringException(ECLWATCH_CANNOT_CONNECT_DALI,"Cannot connect to DALI server.");
- Owned<IConstWUClusterInfo> clusterInfo = getTargetClusterInfo(clusterName.str());
- if (clusterInfo.get())
- {//Set thor flag or roxie flag in order to display some options for thor or roxie
- ClusterType platform = clusterInfo->getPlatform();
- if (isThorCluster(platform))
- {
- clusterTypeFlag=1;
- if (version > 1.29)
- info.setThorLCR(ThorLCRCluster == platform);
- }
- else if (RoxieCluster == platform)
- clusterTypeFlag=2;
- }
- info.setClusterFlag(clusterTypeFlag);
- }
- return true;
- }
- void WsWuInfo::getWorkflow(IEspECLWorkunit &info, unsigned flags)
- {
- bool eventCountRemaining = false;
- bool eventCountUnlimited = false;
- try
- {
- info.setEventSchedule(0);
- IArrayOf<IConstECLWorkflow> workflows;
- Owned<IConstWorkflowItemIterator> it = cw->getWorkflowItems();
- if (it)
- {
- ForEach(*it)
- {
- IConstWorkflowItem *r = it->query();
- if (r)
- {
- IWorkflowEvent *wfevent = r->getScheduleEvent();
- if (wfevent)
- {
- Owned<IEspECLWorkflow> g;
- if (flags & WUINFO_IncludeWorkflows)
- {
- StringBuffer id;
- g.setown(createECLWorkflow("",""));
- g->setWFID(id.appendf("%d", r->queryWfid()).str());
- g->setEventName(wfevent->queryName());
- g->setEventText(wfevent->queryText());
- }
- if (r->hasScheduleCount())
- {
- if (r->queryScheduleCountRemaining() > 0)
- eventCountRemaining = true;
- if (flags & WUINFO_IncludeWorkflows)
- {
- g->setCount(r->queryScheduleCount());
- g->setCountRemaining(r->queryScheduleCountRemaining());
- }
- }
- else
- {
- eventCountUnlimited = true;
- }
- if (flags & WUINFO_IncludeWorkflows)
- workflows.append(*g.getLink());
- }
- }
- }
- if (workflows.length() > 0)
- info.setWorkflows(workflows);
- workflows.kill();
- }
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setWorkflowsDesc(eMsg.str());
- e->Release();
- }
- if (info.getState() && !stricmp(info.getState(), "wait"))
- info.setEventSchedule(2); //Can deschedule
- else if (eventCountUnlimited || eventCountRemaining)
- info.setEventSchedule(1); //Can reschedule
- }
- bool shouldFileContentBeShown(IEspContext &context, const char * logicalName)
- {
- StringBuffer username;
- context.getUserID(username);
- Owned<IUserDescriptor> userdesc(createUserDescriptor());
- userdesc->set(username.str(), context.queryPassword());
- Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(logicalName, userdesc);
- if (!df)
- return false;
- bool blocked;
- if (df->isCompressed(&blocked) && !blocked)
- return false;
- IPropertyTree & properties = df->queryAttributes();
- const char * format = properties.queryProp("@format");
- if (format && (stricmp(format,"csv")==0 || memicmp(format, "utf", 3) == 0))
- {
- return true;
- }
- const char * recordEcl = properties.queryProp("ECL");
- if (!recordEcl)
- return false;
- MultiErrorReceiver errs;
- Owned<IHqlExpression> ret = ::parseQuery(recordEcl, &errs);
- return errs.errCount() == 0;
- }
- void WsWuInfo::getEclSchemaChildFields(IArrayOf<IEspECLSchemaItem>& schemas, IHqlExpression * expr, bool isConditional)
- {
- if(!expr)
- return;
- ForEachChild(idx, expr)
- getEclSchemaFields(schemas, expr->queryChild(idx), isConditional);
- }
- void WsWuInfo::getEclSchemaFields(IArrayOf<IEspECLSchemaItem>& schemas, IHqlExpression * expr, bool isConditional)
- {
- if(!expr)
- return;
- int ret = expr->getOperator();
- switch (ret)
- {
- case no_record:
- getEclSchemaChildFields(schemas, expr, isConditional);
- break;
- case no_ifblock:
- {
- getEclSchemaChildFields(schemas, expr->queryChild(1), true);
- break;
- }
- case no_field:
- {
- if (expr->hasAttribute(__ifblockAtom))
- break;
- ITypeInfo * type = expr->queryType();
- IAtom * name = expr->queryName();
- IHqlExpression * nameAttr = expr->queryAttribute(namedAtom);
- StringBuffer outname;
- if (nameAttr && nameAttr->queryChild(0) && nameAttr->queryChild(0)->queryValue())
- nameAttr->queryChild(0)->queryValue()->getStringValue(outname);
- else
- outname.append(name).toLowerCase();
- if(type)
- {
- type_t tc = type->getTypeCode();
- if (tc == type_row)
- {
- getEclSchemaChildFields(schemas, expr->queryRecord(), isConditional);
- }
- else
- {
- if (type->getTypeCode() == type_alien)
- {
- IHqlAlienTypeInfo * alien = queryAlienType(type);
- type = alien->queryPhysicalType();
- }
- Owned<IEspECLSchemaItem> schema = createECLSchemaItem("","");
- StringBuffer eclType;
- type->getECLType(eclType);
- schema->setColumnName(outname);
- schema->setColumnType(eclType.str());
- schema->setColumnTypeCode(tc);
- schema->setIsConditional(isConditional);
- schemas.append(*schema.getClear());
- }
- }
- break;
- }
- }
- }
- bool WsWuInfo::getResultEclSchemas(IConstWUResult &r, IArrayOf<IEspECLSchemaItem>& schemas)
- {
- SCMStringBuffer schema;
- r.getResultEclSchema(schema);
- if (!schema.length())
- return false;
- MultiErrorReceiver errs;
- Owned<IHqlExpression> expr = ::parseQuery(schema.str(), &errs);
- if (errs.errCount() != 0)
- return false;
- getEclSchemaFields(schemas, expr, false);
- return true;
- }
- void WsWuInfo::getResult(IConstWUResult &r, IArrayOf<IEspECLResult>& results, unsigned flags)
- {
- SCMStringBuffer name;
- r.getResultName(name);
- SCMStringBuffer filename;
- r.getResultLogicalName(filename);
- StringBuffer value, link;
- if (r.getResultStatus() == ResultStatusUndefined)
- value.set("[undefined]");
- else if (r.isResultScalar())
- {
- try
- {
- SCMStringBuffer xml;
- r.getResultXml(xml);
- Owned<IPropertyTree> props = createPTreeFromXMLString(xml.str(), ipt_caseInsensitive);
- IPropertyTree *val = props->queryPropTree("Row/*");
- if(val)
- value.set(val->queryProp(NULL));
- else
- {
- Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
- Owned<INewResultSet> result;
- result.setown(resultSetFactory->createNewResultSet(&r, wuid.str()));
- Owned<IResultSetCursor> cursor(result->createCursor());
- cursor->first();
- if (cursor->getIsAll(0))
- {
- value.set("<All/>");
- }
- else
- {
- Owned<IResultSetCursor> childCursor = cursor->getChildren(0);
- if (childCursor)
- {
- ForEach(*childCursor)
- {
- StringBuffer out;
- StringBufferAdaptor adaptor(out);
- childCursor->getDisplayText(adaptor, 0);
- if (!value.length())
- value.append('[');
- else
- value.append(", ");
- value.append('\'').append(out.str()).append('\'');
- }
- if (value.length())
- value.append(']');
- }
- }
- }
- }
- catch(...)
- {
- value.append("[value not available]");
- }
- }
- else
- {
- value.append('[').append(r.getResultTotalRowCount()).append(" rows]");
- if(r.getResultSequence()>=0)
- {
- if(filename.length())
- {
- StringBuffer username;
- context.getUserID(username);
- Owned<IUserDescriptor> userdesc(createUserDescriptor());
- userdesc->set(username.str(), context.queryPassword());
- Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(filename.str(), userdesc);
- if(df && df->queryAttributes().hasProp("ECL"))
- link.append(r.getResultSequence());
- }
- else
- link.append(r.getResultSequence());
- }
- }
- Owned<IEspECLResult> result= createECLResult("","");
- if (flags & WUINFO_IncludeEclSchemas)
- {
- IArrayOf<IEspECLSchemaItem> schemas;
- if (getResultEclSchemas(r, schemas))
- result->setECLSchemas(schemas);
- }
- if (flags & WUINFO_IncludeXmlSchema)
- {
- Owned<IResultSetFactory> resultSetFactory = getSecResultSetFactory(context.querySecManager(), context.queryUser(), context.queryUserId(), context.queryPassword());
- Owned<INewResultSet> rs = resultSetFactory->createNewResultSet(&r, wuid.str());
- Owned<IResultSetCursor> cursor(rs->createCursor());
- SCMStringBuffer xsd;
- const IResultSetMetaData & meta = cursor->queryResultSet()->getMetaData();
- meta.getXmlXPathSchema(xsd, false);
- result->setXmlSchema(xsd.str());
- }
- if (filename.length())
- result->setShowFileContent(shouldFileContentBeShown(context, filename.str()));
- result->setName(name.str());
- result->setLink(link.str());
- result->setSequence(r.getResultSequence());
- result->setValue(value.str());
- result->setFileName(filename.str());
- result->setIsSupplied(r.getResultStatus() == ResultStatusSupplied);
- result->setTotal(r.getResultTotalRowCount());
- results.append(*result.getLink());
- }
- void WsWuInfo::getResults(IEspECLWorkunit &info, unsigned flags)
- {
- try
- {
- unsigned count = 0;
- IArrayOf<IEspECLResult> results;
- Owned<IConstWUResultIterator> it = &(cw->getResults());
- ForEach(*it)
- {
- IConstWUResult &r = it->query();
- if(r.getResultSequence()>=0)
- {
- if (flags & WUINFO_IncludeResults)
- getResult(r, results, flags);
- count++;
- }
- }
- if (version >= 1.17)
- info.setResultCount(count);
- if ((flags & WUINFO_IncludeResults) && results.length() > 0)
- info.setResults(results);
- results.kill();
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- info.setResultsDesc(eMsg.str());
- e->Release();
- }
- }
- bool WsWuInfo::getFileSize(const char* fileName, const char* IPAddress, offset_t& fileSize)
- {
- if (!fileName || !*fileName)
- return false;
- Owned<IFile> aFile;
- if (!IPAddress || !*IPAddress)
- {
- aFile.setown(createIFile(fileName));
- }
- else
- {
- RemoteFilename rfn;
- rfn.setRemotePath(fileName);
- SocketEndpoint ep(IPAddress);
- rfn.setIp(ep);
- aFile.setown(createIFile(rfn));
- }
- if (!aFile)
- return false;
- bool isDir;
- CDateTime modtime;
- if (!aFile->getInfo(isDir, fileSize, modtime) || isDir)
- return false;
- return true;
- }
- void WsWuInfo::getHelpFiles(IConstWUQuery* query, WUFileType type, IArrayOf<IEspECLHelpFile>& helpers)
- {
- if (!query)
- return;
- Owned<IConstWUAssociatedFileIterator> iter = &query->getAssociatedFiles();
- ForEach(*iter)
- {
- SCMStringBuffer name, Ip, description;
- IConstWUAssociatedFile & cur = iter->query();
- if (cur.getType() != type)
- continue;
- cur.getName(name);
- Owned<IEspECLHelpFile> h= createECLHelpFile("","");
- h->setName(name.str());
- h->setType(getEnumText(type, queryFileTypes));
- if (version > 1.31)
- {
- cur.getIp(Ip);
- h->setIPAddress(Ip.str());
- cur.getDescription(description);
- if ((description.length() < 1) && (name.length() > 0))
- {
- const char* desc = pathTail(name.str());
- if (desc && *desc)
- description.set(desc);
- }
- if (description.length() < 1)
- description.set("Help File");
- h->setDescription(description.str());
- if (version >= 1.43)
- {
- offset_t fileSize;
- if (getFileSize(name.str(), Ip.str(), fileSize))
- h->setFileSize(fileSize);
- }
- }
- helpers.append(*h.getLink());
- }
- }
- void WsWuInfo::getSubFiles(IPropertyTreeIterator* f, IEspECLSourceFile* eclSuperFile, StringArray& fileNames)
- {
- IArrayOf<IEspECLSourceFile> files;
- ForEach(*f)
- {
- IPropertyTree &query = f->query();
- const char *clusterName = query.queryProp("@cluster");
- const char *fileName = query.queryProp("@name");
- int fileCount = query.getPropInt("@useCount");
- bool bFound = false;
- if (fileName && *fileName && (fileNames.length() > 0))
- {
- for (unsigned i = 0; i < fileNames.length(); i++ )
- {
- const char *fileName0 = fileNames.item(i);
- if (!stricmp(fileName, fileName0))
- {
- bFound = true;
- break;
- }
- }
- }
- if (bFound)
- continue;
- Owned<IEspECLSourceFile> file= createECLSourceFile("","");
- if(clusterName && *clusterName)
- {
- file->setFileCluster(clusterName);
- }
- if (fileName && *fileName)
- {
- file->setName(fileName);
- fileNames.append(fileName);
- }
- file->setCount(fileCount);
- Owned<IPropertyTreeIterator> filetrees= query.getElements("Subfile");
- if (filetrees->first())
- {
- file->setIsSuperFile(true);
- getSubFiles(filetrees, file, fileNames);
- }
- files.append(*file.getLink());
- }
- eclSuperFile->setECLSourceFiles(files);
- return;
- }
- bool WsWuInfo::getResourceInfo(StringArray &viewnames, StringArray &urls, unsigned flags)
- {
- if (!(flags & (WUINFO_IncludeResultsViewNames | WUINFO_IncludeResourceURLs)))
- return true;
- try
- {
- Owned<IWuWebView> wv = createWuWebView(*cw, NULL, NULL, false);
- if (wv)
- {
- if (flags & WUINFO_IncludeResultsViewNames)
- wv->getResultViewNames(viewnames);
- if (flags & WUINFO_IncludeResourceURLs)
- wv->getResourceURLs(urls, NULL);
- }
- return true;
- }
- catch(IException* e)
- {
- StringBuffer eMsg;
- ERRLOG("%s", e->errorMessage(eMsg).str());
- e->Release();
- }
- return false;
- }
- void appendIOStreamContent(MemoryBuffer &mb, IFileIOStream *ios, bool forDownload)
- {
- StringBuffer line;
- bool eof = false;
- while (!eof)
- {
- line.clear();
- loop
- {
- char c;
- size32_t numRead = ios->read(1, &c);
- if (!numRead)
- {
- eof = true;
- break;
- }
- line.append(c);
- if (c=='\n')
- break;
- }
- mb.append(line.length(), line.str());
- if (!forDownload && (mb.length() > 640000))
- break;
- }
- }
- void WsWuInfo::getWorkunitEclAgentLog(const char* fileName, const char* agentPid, MemoryBuffer& buf)
- {
- if(!fileName || !*fileName)
- throw MakeStringException(ECLWATCH_ECLAGENT_LOG_NOT_FOUND,"Log file not specified");
- Owned<IFile> rFile = createIFile(fileName);
- if(!rFile)
- throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Cannot open file %s.", fileName);
- OwnedIFileIO rIO = rFile->openShared(IFOread,IFSHfull);
- if(!rIO)
- throw MakeStringException(ECLWATCH_CANNOT_READ_FILE, "Cannot read file %s.", fileName);
- OwnedIFileIOStream ios = createBufferedIOStream(rIO);
- StringBuffer line;
- bool eof = false;
- bool wuidFound = false;
- StringBuffer pidstr;
- if (agentPid && *agentPid)
- pidstr.appendf(" %s ", agentPid);
- else
- pidstr.appendf(" %5d ", cw->getAgentPID());
- char const * pidchars = pidstr.str();
- while(!eof)
- {
- line.clear();
- loop
- {
- char c;
- size32_t numRead = ios->read(1, &c);
- if (!numRead)
- {
- eof = true;
- break;
- }
- line.append(c);
- if (c=='\n')
- break;
- }
- //Retain all rows that match a unique program instance - by retaining all rows that match a pid
- if(strstr(line.str(), pidchars))
- {
- //Check if this is a new instance using line sequence number
- if (strncmp(line.str(), "00000000", 8) == 0)
- {
- if (wuidFound) //If the correct instance has been found, return that instance before the next instance.
- break;
- //The last instance is not a correct instance. Clean the buf in order to start a new instance.
- buf.clear();
- }
- //If we spot the workunit id anywhere in the tacing for this pid then assume it is the correct instance.
- if(!wuidFound && strstr(line.str(), wuid.str()))
- wuidFound = true;
- buf.append(line.length(), line.str());
- }
- }
- if (buf.length() < 1)
- buf.append(47, "(Not found a log line related to this workunit)");
- }
- void WsWuInfo::getWorkunitThorLog(const char* fileName, MemoryBuffer& buf)
- {
- if(!fileName || !*fileName)
- throw MakeStringException(ECLWATCH_ECLAGENT_LOG_NOT_FOUND,"Log file not specified");
- Owned<IFile> rFile = createIFile(fileName);
- if (!rFile)
- throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE,"Cannot open file %s.",fileName);
- OwnedIFileIO rIO = rFile->openShared(IFOread,IFSHfull);
- if (!rIO)
- throw MakeStringException(ECLWATCH_CANNOT_READ_FILE,"Cannot read file %s.",fileName);
- OwnedIFileIOStream ios = createBufferedIOStream(rIO);
- StringBuffer line;
- bool eof = false;
- bool include = false;
- VStringBuffer startwuid("Started wuid=%s", wuid.str());
- VStringBuffer endwuid("Finished wuid=%s", wuid.str());
- const char *sw = startwuid.str();
- const char *ew = endwuid.str();
- while (!eof)
- {
- line.clear();
- loop
- {
- char c;
- size32_t numRead = ios->read(1, &c);
- if (!numRead)
- {
- eof = true;
- break;
- }
- line.append(c);
- if (c=='\n')
- break;
- }
- if (strstr(line.str(), sw))
- include = true;
- if (include)
- buf.append(line.length(), line.str());
- if (strstr(line.str(), ew))
- include = false;
- }
- }
- void WsWuInfo::getWorkunitThorSlaveLog(const char *groupName, const char *ipAddress, const char* logDate, const char* logDir, int slaveNum, MemoryBuffer& buf, bool forDownload)
- {
- if (isEmpty(logDir))
- throw MakeStringException(ECLWATCH_INVALID_INPUT,"ThorSlave log path not specified.");
- if (isEmpty(logDate))
- throw MakeStringException(ECLWATCH_INVALID_INPUT,"ThorSlave log date not specified.");
- StringBuffer slaveIPAddress, logName;
- if (slaveNum > 0)
- {
- if (isEmpty(groupName))
- throw MakeStringException(ECLWATCH_INVALID_INPUT,"Thor group not specified.");
- Owned<IGroup> nodeGroup = queryNamedGroupStore().lookup(groupName);
- if (!nodeGroup || (nodeGroup->ordinality() == 0))
- {
- WARNLOG("Node group %s not found", groupName);
- return;
- }
- nodeGroup->queryNode(slaveNum-1).endpoint().getIpText(slaveIPAddress);
- if (slaveIPAddress.length() < 1)
- throw MakeStringException(ECLWATCH_INVALID_INPUT,"ThorSlave log network address not found.");
- logName.appendf("thorslave.%d.%s.log", slaveNum, logDate);
- }
- else
- {//legacy wuid: a user types in an IP address for a thor slave
- if (isEmpty(ipAddress))
- throw MakeStringException(ECLWATCH_INVALID_INPUT,"ThorSlave address not specified.");
- //thorslave.10.239.219.6_20100.2012_05_23.log
- logName.appendf("thorslave.%s*.%s.log", ipAddress, logDate);
- const char* portPtr = strchr(ipAddress, '_');
- if (!portPtr)
- slaveIPAddress.append(ipAddress);
- else
- {
- StringBuffer ipAddressStr = ipAddress;
- ipAddressStr.setLength(portPtr - ipAddress);
- slaveIPAddress.append(ipAddressStr.str());
- }
- }
- RemoteFilename rfn;
- rfn.setRemotePath(logDir);
- SocketEndpoint ep(slaveIPAddress.str());
- rfn.setIp(ep);
- Owned<IFile> dir = createIFile(rfn);
- Owned<IDirectoryIterator> diriter = dir->directoryFiles(logName.str());
- if (!diriter->first())
- throw MakeStringException(ECLWATCH_FILE_NOT_EXIST,"Cannot find Thor slave log file %s.", logName.str());
- Linked<IFile> logfile = &diriter->query();
- diriter.clear();
- dir.clear();
- // logfile is now the file to load
- OwnedIFileIO rIO = logfile->openShared(IFOread,IFSHfull);
- if (!rIO)
- throw MakeStringException(ECLWATCH_CANNOT_READ_FILE,"Cannot read file %s.",logName.str());
- OwnedIFileIOStream ios = createBufferedIOStream(rIO);
- if (slaveNum > 0)
- {
- StringBuffer line;
- bool eof = false;
- bool include = false;
- VStringBuffer startwuid("Started wuid=%s", wuid.str());
- VStringBuffer endwuid("Finished wuid=%s", wuid.str());
- const char *sw = startwuid.str();
- const char *ew = endwuid.str();
- while (!eof)
- {
- line.clear();
- loop
- {
- char c;
- size32_t numRead = ios->read(1, &c);
- if (!numRead)
- {
- eof = true;
- break;
- }
- line.append(c);
- if (c=='\n')
- break;
- }
- if (strstr(line.str(), sw))
- include = true;
- if (include)
- buf.append(line.length(), line.str());
- if (strstr(line.str(), ew))
- include = false;
- }
- }
- else
- {//legacy wuid
- appendIOStreamContent(buf, ios.get(), forDownload);
- }
- }
- void WsWuInfo::getWorkunitResTxt(MemoryBuffer& buf)
- {
- Owned<IConstWUQuery> query = cw->getQuery();
- if(!query)
- throw MakeStringException(ECLWATCH_QUERY_NOT_FOUND_FOR_WU,"No query for workunit %s.",wuid.str());
- SCMStringBuffer resname;
- queryDllServer().getDll(query->getQueryResTxtName(resname).str(), buf);
- }
- void WsWuInfo::getWorkunitArchiveQuery(MemoryBuffer& buf)
- {
- Owned<IConstWUQuery> query = cw->getQuery();
- if(!query)
- throw MakeStringException(ECLWATCH_QUERY_NOT_FOUND_FOR_WU,"No query for workunit %s.",wuid.str());
- SCMStringBuffer queryText;
- query->getQueryText(queryText);
- if ((queryText.length() < 1) || !isArchiveQuery(queryText.str()))
- throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Archive Query not found for workunit %s.", wuid.str());
- buf.append(queryText.length(), queryText.str());
- }
- void WsWuInfo::getWorkunitDll(StringBuffer &dllname, MemoryBuffer& buf)
- {
- Owned<IConstWUQuery> query = cw->getQuery();
- if(!query)
- throw MakeStringException(ECLWATCH_QUERY_NOT_FOUND_FOR_WU,"No query for workunit %s.",wuid.str());
- StringBufferAdaptor isvName(dllname);
- query->getQueryDllName(isvName);
- queryDllServer().getDll(dllname.str(), buf);
- }
- void WsWuInfo::getWorkunitXml(const char* plainText, MemoryBuffer& buf)
- {
- const char* header;
- if (plainText && (!stricmp(plainText, "yes")))
- header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
- else
- header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet href=\"../esp/xslt/xmlformatter.xsl\" type=\"text/xsl\"?>";
- SCMStringBuffer xml;
- exportWorkUnitToXML(cw, xml, true, false);
- buf.append(strlen(header), header);
- buf.append(xml.length(), xml.str());
- }
- void WsWuInfo::getWorkunitCpp(const char *cppname, const char* description, const char* ipAddress, MemoryBuffer& buf, bool forDownload)
- {
- if (isEmpty(description))
- throw MakeStringException(ECLWATCH_INVALID_INPUT, "File not specified.");
- if (isEmpty(ipAddress))
- throw MakeStringException(ECLWATCH_INVALID_INPUT, "File location not specified.");
- if (isEmpty(cppname))
- throw MakeStringException(ECLWATCH_INVALID_FILE_NAME, "File path not specified.");
- RemoteFilename rfn;
- rfn.setRemotePath(cppname);
- SocketEndpoint ep(ipAddress);
- rfn.setIp(ep);
- Owned<IFile> cppfile = createIFile(rfn);
- if (!cppfile)
- throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Cannot open %s.", description);
- OwnedIFileIO rIO = cppfile->openShared(IFOread,IFSHfull);
- if (!rIO)
- throw MakeStringException(ECLWATCH_CANNOT_READ_FILE,"Cannot read %s.", description);
- OwnedIFileIOStream ios = createBufferedIOStream(rIO);
- if (!ios)
- throw MakeStringException(ECLWATCH_CANNOT_READ_FILE,"Cannot read %s.", description);
- appendIOStreamContent(buf, ios.get(), forDownload);
- }
- void WsWuInfo::getWorkunitAssociatedXml(const char* name, const char* ipAddress, const char* plainText,
- const char* description, bool forDownload, MemoryBuffer& buf)
- {
- if (isEmpty(description)) //'File Name' as shown in WU Details page
- throw MakeStringException(ECLWATCH_INVALID_INPUT, "File not specified.");
- if (isEmpty(ipAddress))
- throw MakeStringException(ECLWATCH_INVALID_INPUT, "File location not specified.");
- if (isEmpty(name)) //file name with full path
- throw MakeStringException(ECLWATCH_INVALID_FILE_NAME, "File path not specified.");
- RemoteFilename rfn;
- rfn.setRemotePath(name);
- SocketEndpoint ep(ipAddress);
- rfn.setIp(ep);
- Owned<IFile> rFile = createIFile(rfn);
- if (!rFile)
- throw MakeStringException(ECLWATCH_CANNOT_OPEN_FILE, "Cannot open %s.", description);
- OwnedIFileIO rIO = rFile->openShared(IFOread,IFSHfull);
- if (!rIO)
- throw MakeStringException(ECLWATCH_CANNOT_READ_FILE,"Cannot read %s.", description);
- OwnedIFileIOStream ios = createBufferedIOStream(rIO);
- if (!ios)
- throw MakeStringException(ECLWATCH_CANNOT_READ_FILE,"Cannot read %s.", description);
- const char* header;
- if (plainText && (!stricmp(plainText, "yes")))
- header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
- else
- header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet href=\"../esp/xslt/xmlformatter.xsl\" type=\"text/xsl\"?>";
- buf.append(strlen(header), header);
- appendIOStreamContent(buf, ios.get(), forDownload);
- }
- WsWuSearch::WsWuSearch(IEspContext& context,const char* owner,const char* state,const char* cluster,const char* startDate,const char* endDate,const char* ecl,const char* jobname,const char* appname,const char* appkey,const char* appvalue)
- {
- SecAccessFlags accessOwn;
- SecAccessFlags accessOthers;
- getUserWuAccessFlags(context, accessOwn, accessOthers, true);
- Owned<IWorkUnitFactory> factory = getWorkUnitFactory(context.querySecManager(), context.queryUser());
- StringBuffer xpath("*");
- if(ecl && *ecl)
- xpath.append("[Query/Text=?~\"*").append(ecl).append("*\"]");
- if(state && *state)
- xpath.append("[@state=\"").append(state).append("\"]");
- if(cluster && *cluster)
- xpath.append("[@clusterName=\"").append(cluster).append("\"]");
- if(owner && *owner)
- xpath.append("[@submitID=?~\"").append(owner).append("\"]");
- if(jobname && *jobname)
- xpath.append("[@jobName=?~\"*").append(jobname).append("*\"]");
- if((appname && *appname) || (appkey && *appkey) || (appvalue && *appvalue))
- {
- xpath.append("[Application/").append(appname && *appname ? appname : "*");
- xpath.append("/").append(appkey && *appkey ? appkey : "*");
- if(appvalue && *appvalue)
- xpath.append("=?~\"").append(appvalue).append("\"");
- xpath.append("]");
- }
- Owned<IConstWorkUnitIterator> it(factory->getWorkUnitsByXPath(xpath.str()));
- StringBuffer wuFrom, wuTo;
- if(startDate && *startDate)
- createWuidFromDate(startDate, wuFrom);
- if(endDate && *endDate)
- createWuidFromDate(endDate, wuTo);
- ForEach(*it)
- {
- IConstWorkUnit &cw = it->query();
- if (chooseWuAccessFlagsByOwnership(context.queryUserId(), cw, accessOwn, accessOthers) < SecAccess_Read)
- continue;
- SCMStringBuffer wuid;
- cw.getWuid(wuid);
- if (wuFrom.length() && strcmp(wuid.str(),wuFrom.str())<0)
- continue;
- if (wuTo.length() && strcmp(wuid.str(),wuTo.str())>0)
- continue;
- if (state && *state)
- {
- SCMStringBuffer descr;
- if(!strieq(cw.getStateDesc(descr).str(),state))
- continue;
- }
- SCMStringBuffer parent;
- if (!cw.getParentWuid(parent).length())
- {
- parent.clear();
- wuids.push_back(cw.getWuid(parent).str());
- }
- }
- std::sort(wuids.begin(),wuids.end(),std::greater<std::string>());
- }
- StringBuffer& WsWuSearch::createWuidFromDate(const char* timestamp,StringBuffer& s)
- {
- CDateTime wuTime;
- wuTime.setString(timestamp,NULL,true);
- unsigned year, month, day, hour, minute, second, nano;
- wuTime.getDate(year, month, day, true);
- wuTime.getTime(hour, minute, second, nano, true);
- s.appendf("W%4d%02d%02d-%02d%02d%02d",year,month,day,hour,minute,second);
- return s;
- }
- struct CompareData
- {
- CompareData(const char* _filter): filter(_filter) {}
- bool operator()(const Linked<DataCacheElement>& e) const
- {
- return stricmp(e->m_filter.c_str(),filter)==0;
- }
- const char* filter;
- };
- DataCacheElement* DataCache::lookup(IEspContext &context, const char* filter, unsigned timeOutMin)
- {
- CriticalBlock block(crit);
- if (cache.size() < 1)
- return NULL;
- //erase data if it should be
- CDateTime timeNow;
- int timeout = timeOutMin;
- timeNow.setNow();
- timeNow.adjustTime(-timeout);
- while (true)
- {
- std::list<Linked<DataCacheElement> >::iterator list_iter = cache.begin();
- if (list_iter == cache.end())
- break;
- DataCacheElement* awu = list_iter->getLink();
- if (!awu || (awu->m_timeCached > timeNow))
- break;
- cache.pop_front();
- }
- if (cache.size() < 1)
- return NULL;
- //Check whether we have the data cache for this cluster. If yes, get the version
- std::list<Linked<DataCacheElement> >::iterator it = std::find_if(cache.begin(),cache.end(),CompareData(filter));
- if(it!=cache.end())
- {
- return it->getLink();
- }
- return NULL;
- }
- void DataCache::add(const char* filter, const char* data, const char* name, const char* localName, const char* wuid,
- const char* resultName, unsigned seq, __int64 start, unsigned count, __int64 requested, __int64 total)
- {
- CriticalBlock block(crit);
- //Save new data
- Owned<DataCacheElement> e=new DataCacheElement(filter, data, name, localName, wuid, resultName, seq, start, count, requested, total);
- if (cacheSize > 0)
- {
- if (cache.size() >= cacheSize)
- cache.pop_front();
- cache.push_back(e.get());
- }
- return;
- }
- struct CompareArchivedWUs
- {
- CompareArchivedWUs(const char* _filter): filter(_filter) {}
- bool operator()(const Linked<ArchivedWuCacheElement>& e) const
- {
- return stricmp(e->m_filter.c_str(),filter)==0;
- }
- const char* filter;
- };
- ArchivedWuCacheElement* ArchivedWuCache::lookup(IEspContext &context, const char* filter, const char* sashaUpdatedWhen, unsigned timeOutMin)
- {
- CriticalBlock block(crit);
- if (cache.size() < 1)
- return NULL;
- //erase data if it should be
- CDateTime timeNow;
- int timeout = timeOutMin;
- timeNow.setNow();
- timeNow.adjustTime(-timeout);
- while (true)
- {
- std::list<Linked<ArchivedWuCacheElement> >::iterator list_iter = cache.begin();
- if (list_iter == cache.end())
- break;
- ArchivedWuCacheElement* awu = list_iter->getLink();
- if (awu && !stricmp(sashaUpdatedWhen, awu->m_sashaUpdatedWhen.c_str()) && (awu->m_timeCached > timeNow))
- break;
- cache.pop_front();
- }
- if (cache.size() < 1)
- return NULL;
- //Check whether we have the data cache for this cluster. If yes, get the version
- std::list<Linked<ArchivedWuCacheElement> >::iterator it = std::find_if(cache.begin(),cache.end(),CompareArchivedWUs(filter));
- if(it!=cache.end())
- return it->getLink();
- return NULL;
- }
- void ArchivedWuCache::add(const char* filter, const char* sashaUpdatedWhen, bool hasNextPage, IArrayOf<IEspECLWorkunit>& wus)
- {
- CriticalBlock block(crit);
- //Save new data
- Owned<ArchivedWuCacheElement> e=new ArchivedWuCacheElement(filter, sashaUpdatedWhen, hasNextPage, /*data.str(),*/ wus);
- if (cacheSize > 0)
- {
- if (cache.size() >= cacheSize)
- cache.pop_front();
- cache.push_back(e.get());
- }
- return;
- }
- WsWuJobQueueAuditInfo::WsWuJobQueueAuditInfo(IEspContext &context, const char *cluster, const char *from , const char *to, CHttpResponse* response, const char *xls)
- {
- if(!response)
- return;
- unsigned maxDisplay = 125;
- IArrayOf<IEspThorQueue> items;
- CDateTime fromTime;
- CDateTime toTime;
- StringBuffer fromstr;
- StringBuffer tostr;
- if(from && *from)
- {
- fromTime.setString(from,NULL,false);
- fromTime.getString(fromstr, false);
- }
- if(to && *to)
- {
- toTime.setString(to,NULL,false);
- toTime.getString(tostr, false);
- }
- StringBuffer filter("ThorQueueMonitor");
- if(notEmpty(cluster))
- filter.appendf(",%s", cluster);
- StringAttrArray lines;
- queryAuditLogs(fromTime, toTime, filter.str(), lines);
- unsigned countLines = 0;
- unsigned maxConnected = 0;
- unsigned longestQueue = 0;
- ForEachItemIn(idx, lines)
- {
- const char* line = lines.item(idx).text;
- if(!line || !*line)
- continue;
- if (idx < (lines.length() - 1))
- getAuditLineInfo(line, longestQueue, maxConnected, maxDisplay, 1, items);
- else
- getAuditLineInfo(line, longestQueue, maxConnected, maxDisplay, 2, items);
- countLines++;
- }
- StringBuffer responsebuf;
- if (items.length() < 1)
- {
- responsebuf.append("<script language=\"javascript\">\r\nparent.displayQEnd(\'No data found\')</script>\r\n");
- response->sendChunk(responsebuf.str());
- return;
- }
- unsigned itemCount = items.length();
- if (itemCount > maxDisplay)
- itemCount = maxDisplay;
- responsebuf.append("<script language=\"javascript\">parent.displayQLegend()</script>\r\n");
- response->sendChunk(responsebuf.str());
- responsebuf.clear();
- responsebuf.append("<script language=\"javascript\">parent.displayQBegin(").append(longestQueue).append(",").append(maxConnected).append(",").append(itemCount).append(")</script>\r\n");
- response->sendChunk(responsebuf.str());
- responsebuf.clear();
- responsebuf.append("<script language=\"javascript\">\r\n");
- //bool displayDT = false;
- unsigned count = 0;
- unsigned jobpending=0;
- ForEachItemIn(i,items)
- {
- IEspThorQueue& tq = items.item(i);
- //displayDT = !displayDT;
- count++;
- if (count > maxDisplay)
- break;
- StringBuffer countStr, dtStr;
- countStr.appendulong(count);
- //if (displayDT)
- dtStr = tq.getDT();
- responsebuf.append("parent.displayQueue(\'").append(count).append("\',\'").append(dtStr.str()).append("\',\'").append(tq.getRunningWUs()).append("\',");
- responsebuf.append("\'").append(tq.getQueuedWUs()).append("\',\'").append(tq.getWaitingThors()).append("\',");
- responsebuf.append("\'").append(tq.getConnectedThors()).append("\',\'").append(tq.getIdledThors()).append("\',");
- responsebuf.append("\'").append(tq.getRunningWU1()).append("\',\'").append(tq.getRunningWU2()).append("\')\r\n");
- if(++jobpending>=50)
- {
- responsebuf.append("</script>\r\n");
- response->sendChunk(responsebuf.str());
- responsebuf.clear();
- responsebuf.append("<script language=\"javascript\">\r\n");
- jobpending=0;
- }
- }
- StringBuffer countStr;
- countStr.appendulong(count);
- StringBuffer msg("<table><tr><td>");
- 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>).");
- msg.append("</td></tr><tr><td>");
- if (count > maxDisplay)
- msg.append("Displayed: First ").append(maxDisplay).append(". ");
- msg.append("Max. Queue Length: ").append(longestQueue).append(".");
- msg.append("</td></tr></table>");
- responsebuf.append("parent.displayQEnd(\'").append(msg).append("\')</script>\r\n");
- response->sendChunk(responsebuf.str());
- }
- void WsWuJobQueueAuditInfo::getAuditLineInfo(const char* line, unsigned& longestQueue, unsigned& maxConnected, unsigned maxDisplay, unsigned showAll, IArrayOf<IEspThorQueue>& items)
- {
- //2009-08-12 02:44:12 ,ThorQueueMonitor,thor400_88_dev,0,0,1,1,114,---,---
- if(!line || !*line)
- return;
- Owned<IEspThorQueue> tq = createThorQueue();
- StringBuffer dt, runningWUs, queuedWUs, waitingThors, connectedThors, idledThors, runningWU1, runningWU2;
- // date/time
- const char* bptr = line;
- const char* eptr = strchr(bptr, ',');
- if(eptr)
- dt.append(eptr - bptr, bptr);
- else
- dt.append(bptr);
- tq->setDT(dt.str());
- if(!eptr)
- {
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- return;
- }
- //skip title
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(!eptr)
- {
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- return;
- }
- //skip queue name
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(!eptr)
- {
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- return;
- }
- //running
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(eptr)
- runningWUs.append(eptr - bptr, bptr);
- else
- runningWUs.append(bptr);
- tq->setRunningWUs(runningWUs.str());
- if(!eptr)
- {
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- return;
- }
- //queued
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(eptr)
- queuedWUs.append(eptr - bptr, bptr);
- else
- queuedWUs.append(bptr);
- if (maxDisplay > items.length())
- {
- unsigned queueLen = atoi(queuedWUs.str());
- if (queueLen > longestQueue)
- longestQueue = queueLen;
- }
- tq->setQueuedWUs(queuedWUs.str());
- if(!eptr)
- {
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- return;
- }
- //waiting
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(eptr)
- waitingThors.append(eptr - bptr, bptr);
- else
- waitingThors.append(bptr);
- tq->setWaitingThors(waitingThors.str());
- if(!eptr)
- {
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- return;
- }
- //connected
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(eptr)
- connectedThors.append(eptr - bptr, bptr);
- else
- connectedThors.append(bptr);
- if (maxDisplay > items.length())
- {
- unsigned connnectedLen = atoi(connectedThors.str());
- if (connnectedLen > maxConnected)
- maxConnected = connnectedLen;
- }
- tq->setConnectedThors(connectedThors.str());
- if(!eptr)
- {
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- return;
- }
- //idled
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(eptr)
- idledThors.append(eptr - bptr, bptr);
- else
- idledThors.append(bptr);
- tq->setIdledThors(idledThors.str());
- if(!eptr)
- {
- items.append(*tq.getClear());
- return;
- }
- //runningWU1
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(eptr)
- runningWU1.append(eptr - bptr, bptr);
- else
- {
- runningWU1.append(bptr);
- }
- if (!strcmp(runningWU1.str(), "---"))
- runningWU1.clear();
- if (runningWU1.length() > 0)
- tq->setRunningWU1(runningWU1.str());
- if(!eptr)
- {
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- return;
- }
- //runningWU2
- bptr = eptr + 1;
- eptr = strchr(bptr, ',');
- if(eptr)
- runningWU2.append(eptr - bptr, bptr);
- else
- {
- runningWU2.append(bptr);
- }
- if (!strcmp(runningWU2.str(), "---"))
- runningWU2.clear();
- if (runningWU2.length() > 0)
- tq->setRunningWU2(runningWU2.str());
- if (checkNewThorQueueItem(tq, showAll, items))
- items.append(*tq.getClear());
- DBGLOG("Queue log: [%s]", line);
- }
- bool WsWuJobQueueAuditInfo::checkSameStrings(const char* s1, const char* s2)
- {
- if (s1)
- {
- if (!s2)
- return false;
- if (strcmp(s1, s2))
- return false;
- }
- else if (s2)
- {
- if (!s1)
- return false;
- }
- return true;
- }
- bool WsWuJobQueueAuditInfo::checkNewThorQueueItem(IEspThorQueue* tq, unsigned showAll, IArrayOf<IEspThorQueue>& items)
- {
- bool bAdd = false;
- if (showAll < 1) //show every lines
- bAdd = true;
- else if (items.length() < 1)
- bAdd = true;
- else if (showAll > 1) //last line now
- {
- IEspThorQueue& tq0 = items.item(items.length()-1);
- if (!checkSameStrings(tq->getDT(), tq0.getDT()))
- bAdd = true;
- }
- else
- {
- IEspThorQueue& tq0 = items.item(items.length()-1);
- if (!checkSameStrings(tq->getRunningWUs(), tq0.getRunningWUs()))
- bAdd = true;
- if (!checkSameStrings(tq->getQueuedWUs(), tq0.getQueuedWUs()))
- bAdd = true;
- if (!checkSameStrings(tq->getConnectedThors(), tq0.getConnectedThors()))
- bAdd = true;
- if (!checkSameStrings(tq->getConnectedThors(), tq0.getConnectedThors()))
- bAdd = true;
- if (!checkSameStrings(tq->getRunningWU1(), tq0.getRunningWU1()))
- bAdd = true;
- if (!checkSameStrings(tq->getRunningWU2(), tq0.getRunningWU2()))
- bAdd = true;
- }
- return bAdd;
- }
- void xsltTransform(const char* xml, const char* sheet, IProperties *params, StringBuffer& ret)
- {
- StringBuffer xsl;
- if(!checkFileExists(sheet))
- throw MakeStringException(ECLWATCH_FILE_NOT_EXIST, "Could not find stylesheet %s.",sheet);
- Owned<IXslProcessor> proc = getXslProcessor();
- Owned<IXslTransform> trans = proc->createXslTransform();
- trans->setXmlSource(xml, strlen(xml));
- trans->loadXslFromFile(sheet);
- trans->copyParameters(params);
- trans->transform(ret);
- }
- bool addToQueryString(StringBuffer &queryString, const char *name, const char *value, const char delim)
- {
- if (isEmpty(name) || isEmpty(value))
- return false;
- if (queryString.length() > 0)
- queryString.append(delim);
- queryString.append(name).append("=").append(value);
- return true;
- }
- int WUSchedule::run()
- {
- try
- {
- while(!stopping)
- {
- Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
- Owned<IConstWorkUnitIterator> itr = factory->getWorkUnitsByState(WUStateScheduled);
- if (itr)
- {
- ForEach(*itr)
- {
- try
- {
- IConstWorkUnit & cw = itr->query();
- if (cw.aborting())
- {
- WorkunitUpdate wu(&cw.lock());
- wu->setState(WUStateAborted);
- continue;
- }
- WsWuDateTime dt, now;
- now.setNow();
- cw.getTimeScheduled(dt);
- if (now.compare(dt)>=0)
- {
- SCMStringBuffer wuid;
- runWorkUnit(cw.getWuid(wuid).str());
- }
- }
- catch(IException *e)
- {
- StringBuffer msg;
- ERRLOG("Exception %d:%s in WsWorkunits Schedule::run", e->errorCode(), e->errorMessage(msg).str());
- e->Release();
- }
- }
- }
- semSchedule.wait(1000*60);
- }
- }
- catch(IException *e)
- {
- StringBuffer msg;
- ERRLOG("Exception %d:%s in WsWorkunits Schedule::run", e->errorCode(), e->errorMessage(msg).str());
- e->Release();
- }
- catch(...)
- {
- ERRLOG("Unknown exception in WsWorkunits Schedule::run");
- }
- if (m_container)
- m_container->exitESP();
- return 0;
- }
- }
|