Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

109
LINES

< > BotCompany Repo | #1002391 // Slack Analyzer (parses slack lines)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (3195L/22K/68K).

!752

static O parser;

// must be run together with sub-bot #1002185 (queries the
// slack slurper's database)

static class SlackMsg {
  S user, botName; // one of these is going to be set
  S userName; // looked up if user != null
  S type, text, ts;
  O reactions;
  S channelID;
}

static PersistentMap<S, Tag> tagsByTS;

static class Tag {
  S parseResult;
}

p {
  tagsByTS = new PersistentMap("tagsByTS.log");
}

synchronized answer {
  if match "get analysis for msg *" {
    S msgID = m.unq(0);
    Tag tag = tagsByTS.get(msgID);
    if (tag == null)
      ret "No analysis for that msg";
    else
      ret structure(tag);
  }
}

static synchronized S html(S uri, Map<S, S> params) {
  L<S> keepFields = litlist("channel", "n");
  
  S channelName = formatChannelName(or(params.get("channel"), "talkingbots"));
  O slurper = call(getMainBot(), "getBot", "#1002185");
  L log = cast call(slurper, "getLog", channelName);
  
  new L data;
  int n;
  if (!empty(params.get("n")))
    n = parseInt(params.get("n"));
  else
    n = max(0, l(log)-1)/100*100;
  int n_end = min(n+100, l(log));
  
  boolean analyze = !empty(params.get("analyze"));
    
  for (int i = n; i < n_end; i++) {
    O o = log.get(i);
    S ts = getString(o, "ts");
    Tag tag = tagsByTS.get(ts);
    S text = cast get(o, "text");
    
    if (analyze) {
      tag = new Tag;
      try {
        tag.parseResult = nlparse(text);
      } catch (Exception e) {
        tag.parseResult = exceptionToUser(e);
      }
      tagsByTS.put(ts, tag);
    }

    // make line and add to table
    
    Map map = htmlencode(litmap(
      "Timestamp", ts,
      "User", get(o, "userName"),
      "Text", text,
      "#", i+1,
      "Parse Result", tag == null ? "-" : tag.parseResult
    ));
    
    data.add(map);
  }
  
  new StringBuilder buf;
  S title = "Analyzing " + channelName;
  buf.append(htag("title", title));
  buf.append(htag("h3", title));
  
  // navigation
  new L<S> l;
  for (int i = 0; i < l(log); i += 100)
    l.add(htag("a", str(i/100+1), "href", selfLink(params, keepFields, "n", str(i))));
  buf.append(htag("p", "Pages: " + join("\n", l)));
  
  S formContents = htmlTable(data, false);
  formContents += htag("p", htag("input", "", "name", "analyze", "type", "submit", "value", "Re-analyze!"));
  formContents += hiddenFields(params, keepFields);
  buf.append(htag("form", formContents, "method", "POST"));
  ret str(buf);
}

static S nlparse(S text) {
  if (parser == null) {
    parser = hotwire_overBot("#1002369");
    call(getBot("#1002372"), "fillNLParser", parser);
  }
  
  O result = call(parser, "parse", text);
  ret structure(call(result, "explainLongestMatch"));
}

Author comment

Began life as a copy of #1002192

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002391
Snippet name: Slack Analyzer (parses slack lines)
Eternal ID of this version: #1002391/1
Text MD5: 7344e2b0c5022f03948687502ce0798e
Transpilation MD5: 8319bf5182dc1054eb29b6a61021bfcd
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-18 21:30:40
Source code size: 2785 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 645 / 15445
Referenced in: [show references]