!7 !include once #1030833 // BEACalculations set flag NoNanoHTTPD. concept BEAObject > ConceptWithGlobalID { new Ref mirrorPost; void change :: after { ((GazelleBEA) botMod()).rstUpdateBEAMirrors.add(this); } void delete :: before { cdelete(mirrorPost!); } void updateMirrorPost { GazelleBEA mod = cast botMod(); if (isDeleted() || !mod.mirrorBEAObjects) ret; if (!mirrorPost.has()) cset(this, mirrorPost := cnew UserPost( type := "BEA Object", creator := mod.internalUser(), botInfo := "BEA Mirror Bot")); S text = structureString(); cset(mirrorPost!, title := str(this), +text); } S structureString() { S text = "Error"; pcall { structure_Data data = new { structure_ClassInfo newClass(Class c) { structure_ClassInfo info = super.newClass(c); if (c == Concept.Ref.class) { info.special = true; info.serializeObject = o -> { Concept cc = cast deref((Concept.Ref) o); //append("cu CRef " + (cc != null ? str(cc.id) : "null"), 3); if (cc cast BEAObject) append("CRef(gid=" + quote(cc.globalID()) + ")", 6); else if (cc != null) append("CRef(id=" + cc.id + ")", 6); else append("CRef", 1); }; } ret info; } void setFields(structure_ClassInfo info, L fields) { if (isSubclassOf(info.c, BEAObject)) { // Don't serialize "refs" and "backRefs" fields removeAll(fields, getField(BEAObject, "refs"), getField(BEAObject, "backRefs")); } super.setFields(info, fields); } }; S struct = structure(this, data); struct = dropLoadableUtilsPackageFromStruct(struct); text = indentStructureString_firstLevels(1, struct); } ret text; } toString { S type = strOrNull(cget type(this)); if (nempty(type)) { S s = type + " " + id; if (eqic(type, "Match")) s += " " + this.~mapping + appendBracketed(this.~input + " + " + this~.pattern); else if (eqicOneOf(type, "Pattern", "Input")) s += " " + quote(this~.text); ret s; } ret super.toString(); } } module GazelleBEA > DynGazelleRocks { switchable bool mirrorBEAObjects; // don't do it for now transient BEACalculations calculcations = new(this); void init { super.init(); botName = heading = adminName = "Gazelle BEA"; set enableVars; set showTalkToBotLink; unset phoneNumberSpecialInputField; } start { set showCRUDToEveryone; webPushEnabled = false; // mirror all objects to be sure rstUpdateBEAMirrors.addAll(list(BEAObject)); } void makeIndices :: after { indexConceptFieldDesc(BEAObject, "_modified"); indexConceptFieldIC(BEAObject, "type"); indexConceptField(BEAObject, "input"); // e.g. for counting how many matches we have for a given input } L crudClasses(Req req) { ret listPlus(super.crudClasses(req), BEAObject); } S authFormHeading() { ret h3("Gazelle BEA"); } void makeFramer(Req req) { super.makeFramer(req); req.framer.renderTitle = () -> h1(ahref(baseLink + "/", "Gazelle BEA") + " " + htmlEncode2(req.framer.title)); } HCRUD_Concepts crudData(Class c, Req req) { HCRUD_Concepts cc = super.crudData(c, req); if (c == BEAObject) { cc.humanizeFieldNames = false; cc.convertConceptValuesToRefs = true; cc.itemName = () -> "BEA Object"; cc.getObjectForDuplication = id -> { MapSO item = cc.getObjectForDuplication_base(id); item.put(creator := req.auth.user!); // set default creator to current user item.put(createdFrom := getConcept(toLong(id))); ret item; }; } ret cc; } HCRUD makeCRUD(Class c, Req req, HTMLFramer1 framer) { HCRUD crud = super.makeCRUD(c, req, framer); crud.showSearchField = true; if (c == BEAObject) { crud.cellColumnToolTips = true; crud.unshownFields = litset("mirrorPost", "globalID"); HCRUD_Concepts cc = cast crud.data; cc.massageItemMapForList = (item, map) -> { BEAObject o = cast item; Cl matches = objectsWhereCI(findBackRefs(o, BEAObject), type := "match"); map/Map.put("Best Matches", HTML(hparagraphs( lmap matchDescHTML(takeFirst(3, matches))))); }; crud.massageFormMatrix = (map, matrix) -> { S refSelector = crud.renderInput("newField_conceptValue", cc.makeConceptsComboBox("newField_conceptValue", BEAObject), null) + hjs([[$("[name=newField_conceptValue]").hide()]]); /*S refSelector = hselect_list(name := "newField_conceptValue", onchange := [[function() { var value = this.value; $("[name=newField_value]").toggle(value == "String"); $("[name=newField_conceptValue]").toggle(value == "BEAObject"); }]]);*/ LS types = ll("String", "BEAObject"); //S typeSelector = crud.renderComboBox("newField_type", "String", types, false); S typeSelector = hselect_list(types, name := "newField_type", onchange := [[ var value = this.value; $("[name=newField_value]").toggle(value == "String"); $("#newField_refBox").toggle(value == "BEAObject"); ]]); matrix.add(ll("Add field", htmlTable2_noHtmlEncode(ll( ll("Name", htextfield newField_name(), "Value", // string input htextfield newField_value(), span(refSelector, id := "newField_refBox", display := "none"), "Type", typeSelector )), noHeader := true))); }; crud.preprocessUpdateParams = params -> { params = cloneMap(params); // drop empty strings //removeFromMapWhereValue(params, v -> eq(v, "")); params = mapValues(params, v -> eq(v, "") ? null : v); S name = params.get("newField_name"), type = params.get("newField_type"), refValue = params.get("newField_conceptValue"), value = params.get("newField_value"); if (eq(type, "BEAObject")) { value = refValue; params.put("metaInfo_" + name, "concept"); } if (eq(value, "")) value = null; if (nempty(name) /*&& neqOneOf(value, null, "")*/) params.put(crud.fieldPrefix + name, value); ret params; }; } ret crud; } O serveBotFunction(Req req, S function, Map data, User user) { if (eq(function, "beaList")) { long changedAfter = toLong(data.get("changedAfter")); double pollFor = min(bot_maxPollSeconds, toLong(data.get("pollFor"))); // how long to poll (seconds) long startTime = sysNow(); // We're super-anal about catching all changes. This will probably never trigger if (changedAfter > 0 && changedAfter == now()) sleep(1); Cl objects; while true { objects = changedAfter == 0 ? list(BEAObject) : conceptsWithFieldGreaterThan_sorted(BEAObject, _modified := changedAfter); // return when there are results, no polling or poll expired if (nempty(objects) || pollFor == 0 || elapsedSeconds_sysNow(startTime) >= pollFor) ret serveJSON_breakAtLevels(2, result := map(objects, obj -> litorderedmap(gid := str(obj.globalID()), struct := obj.structureString()) )); // sleep and try again sleep(bot_pollInterval); } } ret super.serveBotFunction(req, function, data, user); } transient ReliableSingleThread_Multi rstUpdateBEAMirrors = new(100, c -> c.updateMirrorPost()); O serveOtherPage2(Req req) null { printVars_str serveOtherPage2(uri := req.uri); try object super.serveOtherPage2(req); S uri = dropTrailingSlashIfNemptyAfterwards(req.uri); if (eqOneOf(uri, "/inputs", "/inputs-v2")) ret renderBEAObjectTable(req, "input"); if (eq(uri, "/patterns")) ret renderBEAObjectTable(req, "pattern"); if (eq(uri, "/matches")) ret renderBEAObjectTable(req, "match"); if (!inMasterMode(req)) null; if (eq(uri, "/uploadInputs")) ret serveUploadTexts(req, "Input"); if (eq(uri, "/uploadPatterns")) ret serveUploadTexts(req, "Pattern"); if (eq(uri, "/analyzeInput")) ret calculcations.serveAnalyzeInput(req); if (eq(uri, "/query")) ret calculcations.serveQueryPage(req); } /*O renderBEAObjectTable(Req req, S type) { { HCRUD crud = makeCRUD(BEAObject, req); crud.disableAllMutationRights(); crud.baseLink = req.uri(); ((HCRUD_Concepts) crud.data).ciFilters = litmap(+type); makeFramer(req); HTMLFramer1 framer = req.framer; S baseTitle = framer.title; S theTitle = firstToUpper(plural(type)); crud.makeFrame = (title, contents) -> { framer.title = joinNemptiesWithVBar(hTitleClean(theTitle), baseTitle); ret h1(title) + contents; }; crud.processSortParameter(req.params); framer.add(crud.renderPage(req.params)); ret framer.render(); } }*/ O renderBEAObjectTable(Req req, S type) { HCRUD crud = makeCRUD(BEAObject, req); //crud.disableAllMutationRights(); crud.baseLink = req.uri(); HCRUD_Concepts data = cast crud.data; data.itemName = () -> firstToUpper(type); data.ciFilters = litmap(+type); ret serveCRUD(req, BEAObject, crud); } O serveUploadTexts(Req req, S type) { S inputs = req.get("text"); new LS output; if (nempty(inputs)) { for (S text : tlft(inputs)) { Pair p = uniqCI2_sync BEAObject(+type, +text); if (cget uploadedBy(p.a) == null) cset(p.a, uploadedBy := req.auth.user); output.add(type + " " + (p.b ? "added" : "exists") + " (ID " + p.a.id + "): " + text); } } ret h2("Upload " + plural(type)) + hpostform( p(plural(type) + " (one per line):") + p(htextarea(inputs, name := "text")) + pIfNempty(htmlEncode_nlToBR(lines(output))) + hsubmit("Upload " + plural(type))); } Cl beaObjectsOfType(S type) { ret conceptsWhereCI BEAObject(+type); } void reactAllInputsWithSomePatterns { calculcations.reactAllInputsWithSomePatterns(); } S navDiv() { //ret div_vbar(navLinks(), style := "margin-bottom: 0.5em"); ret joinNemptiesWithVBar( ahref(baseLink + "/inputs", "Inputs"), ahref(baseLink + "/patterns", "Patterns"), ahref(baseLink + "/matches", "Matches"), ahref(baseLink + "/query", "Query"), hPopDownButtonWithText("Bot Forum", navLinks()), hPopDownButtonWithText("Misc", ahref(baseLink + "/stats", "Stats") )); } S matchDescHTML(BEAObject m) { pcall { BEAObject pat = cget pattern(m); SS mapping = cast cget mapping(m); ret htmlEncode2(quote(getString text(pat))) + "
  with " + renderEqualsCommaProperties(mapping); } ret htmlEncode2(str(m)); } } // end of module