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

180
LINES

< > BotCompany Repo | #1004336 // Big Collector - collects all dialogs on the system (and updates when they change)

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

Libraryless. Click here for Pure Java version (3421L/22K/77K).

1  
!759
2  
3  
sclass Post extends ELPost {
4  
  Dialog dialog;
5  
  
6  
  *() {}
7  
  *(ELPost p, Dialog *dialog) {
8  
    copyFields(p, this);
9  
  }
10  
}
11  
12  
sclass Dialog {
13  
  File file;
14  
  long modified, fileSize;
15  
  S dialog, progID;
16  
  L<Post> log;
17  
}
18  
19  
static L<Dialog> dialogs = synchroList();
20  
static MultiMap<S, Post> lowerCaseIndex = new MultiMap(true);
21  
22  
p {
23  
  loadAll();
24  
  print("Updating every 10 seconds, full scan every 5 minutes.");
25  
  thread "Big Collector Updater" {
26  
    int counter = 0;
27  
    while licensed {
28  
      pcall {
29  
        if (counter++ >= 5*60/10) {
30  
          print("Full scan.");
31  
          counter = 0;
32  
          loadAll();
33  
        } else {
34  
          int n = loadChangedLogs();
35  
          /*if (n != 0)
36  
            print(n(n, "log") + " updated");*/
37  
        }
38  
      }
39  
      sleepSeconds(10);
40  
    }
41  
  }
42  
  makeBot("Big Collector.");
43  
}
44  
45  
svoid loadAll {
46  
  long time = now();
47  
  for (File f : allEventLogs())
48  
    loadLog(f);
49  
  done("Loaded " + n(l(dialogs), "dialog") + ", " + n(numberOfLines(), "line"), time);
50  
}
51  
52  
ssvoid loadLog(File f) {
53  
  pcall {
54  
    File dialogDir = f.getParentFile();
55  
    S dialog = dialogDir.getName();
56  
    S progID = dialogDir.getParentFile().getName();
57  
    if (!isSnippetID(progID)) {
58  
      if (!isSnippetID(dialog)) ret;
59  
      progID = dialog;
60  
      dialog = "_";
61  
    }
62  
    progID = formatSnippetID(progID);
63  
    print("Loading dialog: " + progID + " / " + dialog);
64  
    final new Dialog d;
65  
    d.file = f;
66  
    d.modified = f.lastModified();
67  
    d.fileSize = f.length();
68  
    d.dialog = dialog;
69  
    d.progID = progID;
70  
    pcall {
71  
      d.log = map(func(ELPost p) { new Post(p, d) }, scanEventLogForPosts(f));
72  
    }
73  
    addDialog(d);
74  
  }
75  
}
76  
77  
static void unindexDialog(Dialog d) {
78  
  for (Post post : d.log)
79  
    lowerCaseIndex.remove(toLower(post.text), post);
80  
}
81  
82  
static void indexDialog(Dialog d) {
83  
  for (Post post : d.log)
84  
    lowerCaseIndex.put(toLower(post.text), post);
85  
}
86  
87  
synchronized svoid addDialog(Dialog d) {
88  
  for (Dialog old : getWhere(dialogs, "dialog", d.dialog, "progID", d.progID)) {
89  
    unindexDialog(old);
90  
    dialogs.remove(old);
91  
  }
92  
    
93  
  indexDialog(d);
94  
  dialogs.add(d);
95  
}
96  
97  
static int numberOfLines() {
98  
  int n = 0;
99  
  for (Dialog d : dialogs) {
100  
    n += l(d.log);
101  
  }
102  
  ret n;
103  
}
104  
105  
static int loadChangedLogs() {
106  
  int n = 0;
107  
  for (Dialog d : cloneList(dialogs))
108  
    if (d.file.length() != d.fileSize) {
109  
      loadLog(d.file);
110  
      ++n;
111  
    }
112  
  ret n;
113  
}
114  
115  
synchronized answer {
116  
  if "load all dialogs" {
117  
    loadAll();
118  
    ret "ok";
119  
  }
120  
  
121  
  if "load dialog * in *" {
122  
    File f = getProgramFile(m.unq(1), m.unq(0));
123  
    if (!f.exists()) ret format("not found: *", f);
124  
    loadLog(f);
125  
    ret "OK";
126  
  }
127  
  
128  
  if "* posts that swic *" {
129  
    final S pat = m.unq(1);
130  
    L<Post> l = concatValues(keysStartingWith(pat, (TreeMap) lowerCaseIndex.data));
131  
    if (eq(m.unq(0), "find"))
132  
      ret structure(map("formatPost", l));
133  
    else if (eq(m.unq(0), "count"))
134  
      ret lstr(l);
135  
  }
136  
  
137  
  if "* posts that * *" {
138  
    final S pat = m.unq(1);
139  
    final O pred = makePred(m.unq(1), m.unq(2));
140  
    if (pred != null)
141  
      if (eq(m.unq(0), "count"))
142  
        ret str(countPosts(pred));
143  
      else if (eq(m.unq(0), "find"))
144  
        ret structure(findPosts(pred));
145  
  }
146  
}
147  
148  
static O makePred(S a, final S pat) {
149  
  if (eq(a, "swic"))
150  
    ret func(ELPost p) { swic(p.text, pat) };
151  
  if (eq(a, "containsIgnoreCase"))
152  
    ret func(ELPost p) { containsIgnoreCase(p.text, pat) };
153  
  if (eq(a, "match"))
154  
    ret func(ELPost p) { match(pat, p.text) };
155  
  if (eq(a, "jmatch"))
156  
    ret func(ELPost p) { jmatch(pat, p.text) };
157  
  null;
158  
}
159  
160  
static L<Map> findPosts(O pred) {
161  
  new L<Map> posts;
162  
  for (final Dialog d : cloneList(dialogs)) {
163  
    L<Post> filtered = filter(pred, d.log);
164  
    for (Post p : filtered)
165  
      posts.add(formatPost(p));
166  
  }
167  
  ret posts;
168  
}
169  
170  
static Map formatPost(Post p) {
171  
  ret litorderedmap("text", p.text, "prog", p.dialog.progID, "dialog", p.dialog.dialog,
172  
    "time", p.chatTime);
173  
}
174  
175  
static int countPosts(O pred) {
176  
  int n = 0;
177  
  for (final Dialog d : cloneList(dialogs))
178  
    n += nfilter(pred, d.log);
179  
  ret n;
180  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004336
Snippet name: Big Collector - collects all dialogs on the system (and updates when they change)
Eternal ID of this version: #1004336/1
Text MD5: 50bbc5472acff09bd8a87ff3cca9528c
Transpilation MD5: d8226cafaa0ce8d796f57dbc68431a70
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-14 16:39:11
Source code size: 4214 bytes / 180 lines
Pitched / IR pitched: No / No
Views / Downloads: 568 / 696
Referenced in: [show references]