!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)); S s = super.toString(); if (nempty(type)) { s = type + " " + id; if (eqic(type, "Match")) s += appendBracketed(strOrNull(this~.label)) + " " + this.~mapping + appendBracketed(this.~input + " + " + this~.pattern); } S text = text(); if (text != null) s += " " + quote(text); ret s; } S type() { ret getStringOpt type(this); } bool typeIs(S type) { ret eqic(type(), type); } bool typeIsOneOf(S... types) { ret eqicOneOf(type(), types); } S text() { ret getStringOpt text(this); } } cmodule2 GazelleBEA > DynGazelleRocks { switchable bool mirrorBEAObjects; // don't do it for now transient BEACalculations calculcations = new(this); transient int newFieldsToShow = 3; transient bool allowAnonymousInputUpload = true; // TODO // add more fields here 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.onCreate.add(o -> cset(o, createdBy := currentUser())); 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; crud.renderCmds = map -> { BEAObject o = getConcept BEAObject(toLong(crud.itemID(map))); new LS cmds; // special commands for BEA types if (o.typeIs("Input")) cmds.add(ahref(addParamsToURL(crudLink(BEAObject), cmd := "new", title := "Add Pattern For Input", f_type := "Pattern", f_text := getStringOpt text(o), f_shouldMatch := o.id, metaInfo_shouldMatch := "concept", ), "Add pattern")); if (o.typeIsOneOf("Input", "Pattern")) cmds.add(ahref(addParamsToURL(crudLink(BEAObject), cmd := "new", title := "Add Rewrite", f_type := "Rewrite", f_text := getStringOpt text(o), f_isRewriteOf := o.id, metaInfo_isRewriteOf := "concept", ), "Add Rewrite")); if (o.typeIs("Pattern")) cmds.add(ahref(appendParamsToURL(baseLink + "/query", q := o.id, algorithm := "Run pattern"), "Run pattern against all inputs")); if (o.typeIs("Match")) cmds.add(ahref(appendParamsToURL(baseLink + "/query", q := o.id, algorithm := "Find rewrites for match"), "Find rewrites")); cmds.add(ahref(addParamsToURL(crudLink(BEAObject), cmd := "new", title := "Add Comment", f_type := "Comment", f_on := o.id, f_text := "", metaInfo_on := "concept"), "Add comment")); ret joinNemptiesWithVBar(crud.renderCmds_base(map), hPopDownButton(cmds)); }; cc.massageItemMapForList = (item, map) -> { BEAObject o = cast item; if (o.typeIs("Input")) { Cl matches = objectsWhereCI(findBackRefs(o, BEAObject), type := "match"); map/Map.put("Best Matches", HTML(hparagraphs( lmap matchDescHTML(takeFirst(3, matches))))); } }; crud.massageFormMatrix = (map, matrix) -> { for (int i = 1; i <= newFieldsToShow; i++) { S nf = "newField" + i; S refSelector = crud.renderInput("\*nf*/_conceptValue", cc.makeConceptsComboBox("\*nf*/_conceptValue", BEAObject), null) + hjs([[$("[name=]] + nf + [[_conceptValue]").hide()]]); LS types = ll("String", "BEAObject"); S typeSelector = hselect_list(types, name := "\*nf*/_type", onchange := [[ var value = this.value; $("[name=]] + nf + [[_value]").toggle(value == "String"); $("#]] + nf + [[_refBox").toggle(value == "BEAObject"); ]]); matrix.add(ll("Add field", htmlTable2_noHtmlEncode(ll( ll("Name", htextfield("\*nf*/_name"), "Value", // string input htextfield("\*nf*/_value"), span(refSelector, id := "\*nf*/_refBox", style := "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); for (int i = 1; i <= max(newFieldsToShow, 10); i++) { S nf = "newField" + i; S name = params.get("\*nf*/_name"), type = params.get("\*nf*/_type"), refValue = params.get("\*nf*/_conceptValue"), value = params.get("\*nf*/_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 (eq(uri, "/rewrites")) ret renderBEAObjectTable(req, "rewrite"); if (eq(uri, "/query")) ret calculcations.serveQueryPage(req); // add more public URLs here 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, "/allBEATypes")) { MultiSet ms = asCIMultiSet(collect type(list(BEAObject))); ret h2_title("All BEA object types") + hpre(renderMultiSetAsLines_byPopularity(ms)); } // add more master-mode URLs here if (eq(uri, "/storeMatch")) { BEAObject pattern = beaGet(req.get("pattern")); BEAObject input = beaGet(req.get("input")); S label = req.get("label"); BEAObject match = calculcations.reactInputWithPattern(input, pattern); if (match == null) ret "Couldn't match"; cset(match, +label); ret hrefresh(beaObjectURL(match)); } } O renderBEAObjectTable(Req req, S type) { HCRUD crud = makeCRUD(BEAObject, req); crud.baseLink = req.uri(); HCRUD_Concepts data = cast crud.data; data.itemName = () -> firstToUpper(type); data.ciFilters = litmap(+type); if (eqicOneOf(type, "Input", "Pattern")) { IF0 prev = data.emptyObject; data.emptyObject = () -> { MapSO item = data.emptyObject_fallback(prev); item.put(text := ""); // show text field when creating new objects ret item; }; } 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"); HCRUD crud = makeCRUD(BEAObject, currentReq!); ret joinNemptiesWithVBar( beaNavLink("Input", crud), beaNavLink("Pattern", crud), beaNavLink("Match", crud), beaNavLink("Rewrite", crud), ahref(baseLink + "/query", "Query"), hPopDownButtonWithText("Bot Forum", navLinks(flat := true, withStats := false)), hPopDownButton( ahref(baseLink + "/allBEATypes", "All object types"), ahref(baseLink + "/stats", "Stats") )); } // crud is just the cached BEAObject crud to check for creation rights S beaNavLink(S type, HCRUD crud) { S plural = firstToLower(plural(type)); S link = baseLink + "/" + plural; ret ahref(link, beaCount(type) + " " + firstToUpper(plural)) + (!crud.actuallyAllowCreate() ? "" : " " + ahref(addParamToURL(link, cmd := "new"), "+")); } S beaObjectURL(BEAObject o) { ret o == null ?: addParamsToURL(baseLink + "/crud/BEAObject", selectObj := o.id) + "#" + o.id; } S matchDescHTML(BEAObject m) { pcall { BEAObject pat = cget pattern(m); SS mapping = cast cget mapping(m); ret ahref_undecorated(crudLink(m), htmlEncode2(quote(getString text(pat))) + "
  with " + renderEqualsCommaProperties(mapping)); } ret htmlEncode2(str(m)); } int beaCount(S type) { ret countConceptsWhereCI BEAObject(+type); } Cl beaList(S type) { ret conceptsWhereCI BEAObject(+type); } BEAObject beaGet(long id) { ret getConceptOpt BEAObject(id); } BEAObject beaGet(S id) { ret beaGet(parseFirstLong(id)); } S beaLinkHTML(BEAObject o) { ret o == null ?: ahref(conceptLink(o), htmlEncode2_nlToBr(str(o)); } Cl findBEABackRefs(BEAObject o, S type) { ret objectsWhereCI(findBackRefs BEAObject(o), +type); } O serveDefaultPage(Req req) { HTMLFramer1 framer = req.framer; framer.add(hcenter3(hsnippetimg_scaleToWidth(200, #1102967, 200, 110, title := "Gazelle"), style := "margin-top: 100px")); ret completeFrame(req); } S html_loggedIn() { User user = user(currentReq!); ret user == null ? /*"Not logged in"*/ ahref(baseLink + "/", "Log in") : "Logged in as " + htmlEncode2(user.name); } } // end of module