WIP
[tt.git] / tt.rkt
1 #lang typed/racket/no-check
2
3 (require openssl/sha1)
4 (require racket/date)
5 (require
6 net/head
7 net/uri-codec
8 net/url)
9
10 (require (prefix-in info: "info.rkt"))
11
12 (module+ test
13 (require rackunit))
14
15 (define-type Url
16 net/url-structs:url)
17
18 (define-type Out-Format
19 (U 'single-line
20 'multi-line))
21
22 (define-type Timeline-Order
23 (U 'old->new
24 'new->old))
25
26 (struct Msg
27 ([ts-epoch : Integer]
28 [ts-orig : String]
29 [nick : (Option String)]
30 [uri : Url]
31 [text : String]
32 [mentions : (Listof Peer)]))
33
34 (struct Peer
35 ([nick : (Option String)]
36 [uri : Url])
37 #:transparent)
38
39 (struct Follower
40 ([nick : (Option String)]
41 [uri : Url]
42 [client : String]
43 [version : String])
44 #:transparent)
45
46 ; TODO Normalize dir var naming
47 (: tt-home-dir Path-String)
48 (define tt-home-dir (build-path (expand-user-path "~") ".tt"))
49
50 (: concurrent-filter-map (∀ (α β) (-> Natural (-> α β) (Listof α) (Listof β))))
51 (define (concurrent-filter-map num-workers f xs)
52 ; TODO preserve order of elements OR communicate that reorder is expected
53 ; TODO switch from mailboxes to channels
54 (define (make-worker id f)
55 (define parent (current-thread))
56 (λ ()
57 (define self : Thread (current-thread))
58 (: work (∀ (α) (-> α)))
59 (define (work)
60 (thread-send parent (cons 'next self))
61 (match (thread-receive)
62 ['done (thread-send parent (cons 'exit id))]
63 [(cons 'unit x) (begin
64 (define y (f x))
65 (when y (thread-send parent (cons 'result y)))
66 (work))]))
67 (work)))
68 (: dispatch (∀ (α β) (-> (Listof Nonnegative-Integer) (Listof α) (Listof β))))
69 (define (dispatch ws xs ys)
70 (if (empty? ws)
71 ys
72 (match (thread-receive)
73 [(cons 'exit w) (dispatch (remove w ws =) xs ys)]
74 [(cons 'result y) (dispatch ws xs (cons y ys))]
75 [(cons 'next thd) (match xs
76 ['() (begin
77 (thread-send thd 'done)
78 (dispatch ws xs ys))]
79 [(cons x xs) (begin
80 (thread-send thd (cons 'unit x))
81 (dispatch ws xs ys))])])))
82 (define workers (range num-workers))
83 (define threads (map (λ (id) (thread (make-worker id f))) workers))
84 (define results (dispatch workers xs '()))
85 (for-each thread-wait threads)
86 results)
87
88 (module+ test
89 (let* ([f (λ (x) (if (even? x) x #f))]
90 [xs (range 11)]
91 [actual (sort (concurrent-filter-map 10 f xs) <)]
92 [expected (sort ( filter-map f xs) <)])
93 (check-equal? actual expected "concurrent-filter-map")))
94
95 (: msg-print (-> Out-Format Integer Msg Void))
96 (define msg-print
97 (let* ([colors (vector 36 33)]
98 [n (vector-length colors)])
99 (λ (out-format color-i msg)
100 (let ([color (vector-ref colors (modulo color-i n))]
101 [nick (Msg-nick msg)]
102 [uri (url->string (Msg-uri msg))]
103 [text (Msg-text msg)]
104 [mentions (Msg-mentions msg)])
105 (match out-format
106 ['single-line
107 (let ([nick (if nick nick uri)])
108 (printf "~a \033[1;37m<~a>\033[0m \033[0;~am~a\033[0m~n"
109 (parameterize
110 ([date-display-format 'iso-8601])
111 (date->string (seconds->date (Msg-ts-epoch msg)) #t))
112 nick color text))]
113 ['multi-line
114 (let ([nick (if nick (string-append nick " ") "")])
115 (printf "~a (~a)~n\033[1;37m<~a~a>\033[0m~n\033[0;~am~a\033[0m~n~n"
116 (parameterize
117 ([date-display-format 'rfc2822])
118 (date->string (seconds->date (Msg-ts-epoch msg)) #t))
119 (Msg-ts-orig msg)
120 nick uri color text))])))))
121
122 (: rfc3339->epoch (-> String (Option Nonnegative-Integer)))
123 (define rfc3339->epoch
124 (let ([re (pregexp "^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})(:([0-9]{2}))?(\\.[0-9]+)?(Z|([+-])([0-9]{1,2}):?([0-9]{2}))?$")])
125 (λ (ts)
126 (match (regexp-match re ts)
127 [(list _wholething yyyy mm dd HH MM _:SS SS _fractional tz-whole tz-sign tz-HH tz-MM)
128 (let*
129 ([tz-offset
130 (match* (tz-whole tz-sign tz-HH tz-MM)
131 [("Z" #f #f #f)
132 0]
133 [(_ (or "-" "+") (? identity h) (? identity m))
134 (let ([h (string->number h)]
135 [m (string->number m)]
136 ; Reverse to get back to UTC:
137 [op (match tz-sign ["+" -] ["-" +])])
138 (op 0 (+ (* 60 m) (* 60 (* 60 h)))))]
139 [(a b c d)
140 (log-warning "Impossible TZ string: ~v, components: ~v ~v ~v ~v" tz-whole a b c d)
141 0])]
142 [ts-orig ts]
143 [local-time? #f]
144 [ts-epoch (find-seconds (if SS (string->number SS) 0)
145 (string->number MM)
146 (string->number HH)
147 (string->number dd)
148 (string->number mm)
149 (string->number yyyy)
150 local-time?)])
151 (+ ts-epoch tz-offset))]
152 [_
153 (log-error "Invalid timestamp: ~v" ts)
154 #f]))))
155
156 (: str->msg (-> (Option String) Url String (Option Msg)))
157 (define str->msg
158 (let ([re (pregexp "^([^\\s\t]+)[\\s\t]+(.*)$")])
159 (λ (nick uri str)
160 (define str-head (substring str 0 (min 100 (string-length str))))
161 (with-handlers*
162 ([exn:fail?
163 (λ (e)
164 (log-error
165 "Failed to parse msg: ~v, from: ~v, at: ~v, because: ~v"
166 str-head nick (url->string uri) e)
167 #f)])
168 (match (regexp-match re str)
169 [(list _wholething ts-orig text)
170 (let ([ts-epoch (rfc3339->epoch ts-orig)])
171 (if ts-epoch
172 (let ([mentions
173 (filter-map
174 (λ (m) (match (regexp-match #px"@<([^>]+)>" m)
175 [(list _wholething nick-uri)
176 (str->peer nick-uri)]))
177 (regexp-match* #px"@<[^\\s]+([\\s]+)?[^>]+>" text))])
178 (Msg ts-epoch ts-orig nick uri text mentions))
179 (begin
180 (log-error
181 "Msg rejected due to invalid timestamp: ~v, nick:~v, uri:~v"
182 str-head nick (url->string uri))
183 #f)))]
184 [_
185 (log-debug "Non-msg line from nick:~v, line:~a" nick str-head)
186 #f])))))
187
188 (module+ test
189 ; TODO Test for when missing-nick case
190 (let* ([tzs (for*/list ([d '("-" "+")]
191 [h '("5" "05")]
192 [m '("00" ":00" "57" ":57")])
193 (string-append d h m))]
194 [tzs (list* "" "Z" tzs)])
195 (for* ([n '("fake-nick")]
196 [u '("fake-uri")]
197 [s '("" ":10")]
198 [f '("" ".1337")]
199 [z tzs]
200 [sep (list "\t" " ")]
201 [txt '("foo bar baz" "'jaz poop bear giraffe / tea" "@*\"``")])
202 (let* ([ts (string-append "2020-11-18T22:22"
203 (if (non-empty-string? s) s ":00")
204 z)]
205 [m (str->msg n u (string-append ts sep txt))])
206 (check-not-false m)
207 (check-equal? (Msg-nick m) n)
208 (check-equal? (Msg-uri m) u)
209 (check-equal? (Msg-text m) txt)
210 (check-equal? (Msg-ts-orig m) ts (format "Given: ~v" ts))
211 )))
212
213 (let* ([ts "2020-11-18T22:22:09-0500"]
214 [tab " "]
215 [text "Lorem ipsum"]
216 [nick "foo"]
217 [uri "bar"]
218 [actual (str->msg nick uri (string-append ts tab text))]
219 [expected (Msg 1605756129 ts nick uri text '())])
220 (check-equal?
221 (Msg-ts-epoch actual)
222 (Msg-ts-epoch expected)
223 "str->msg ts-epoch")
224 (check-equal?
225 (Msg-ts-orig actual)
226 (Msg-ts-orig expected)
227 "str->msg ts-orig")
228 (check-equal?
229 (Msg-nick actual)
230 (Msg-nick expected)
231 "str->msg nick")
232 (check-equal?
233 (Msg-uri actual)
234 (Msg-uri expected)
235 "str->msg uri")
236 (check-equal?
237 (Msg-text actual)
238 (Msg-text expected)
239 "str->msg text")))
240
241 (: str->lines (-> String (Listof String)))
242 (define (str->lines str)
243 (string-split str (regexp "[\r\n]+")))
244
245 (module+ test
246 (check-equal? (str->lines "abc\ndef\n\nghi") '("abc" "def" "ghi")))
247
248 (: str->msgs (-> (Option String) Url String (Listof Msg)))
249 (define (str->msgs nick uri str)
250 (filter-map (λ (line) (str->msg nick uri line)) (filter-comments (str->lines str))))
251
252 (: cache-dir Path-String)
253 (define cache-dir (build-path tt-home-dir "cache"))
254
255 (define cache-object-dir (build-path cache-dir "objects"))
256
257 (: url->cache-file-path-v1 (-> Url Path-String))
258 (define (url->cache-file-path-v1 uri)
259 (define (hash-sha1 str) : (-> String String)
260 (define in (open-input-string str))
261 (define digest (sha1 in))
262 (close-input-port in)
263 digest)
264 (build-path cache-object-dir (hash-sha1 (url->string uri))))
265
266 (: url->cache-file-path-v2 (-> Url Path-String))
267 (define (url->cache-file-path-v2 uri)
268 (build-path cache-object-dir (uri-encode (url->string uri))))
269
270 (define url->cache-object-path url->cache-file-path-v2)
271
272 (: cache-object-filename->url (-> Path-String Url))
273 (define (cache-object-filename->url name)
274 (string->url (uri-decode (path->string name))))
275
276 (define (url->cache-etag-path uri)
277 (build-path cache-dir "etags" (uri-encode (url->string uri))))
278
279 (define (url->cache-lmod-path uri)
280 (build-path cache-dir "lmods" (uri-encode (url->string uri))))
281
282 ; TODO Return Option
283 (: uri-read-cached (-> Url String))
284 (define (uri-read-cached uri)
285 (define path-v1 (url->cache-file-path-v1 uri))
286 (define path-v2 (url->cache-file-path-v2 uri))
287 (when (file-exists? path-v1)
288 (rename-file-or-directory path-v1 path-v2 #t))
289 (if (file-exists? path-v2)
290 (file->string path-v2)
291 (begin
292 (log-warning "Cache file not found for URI: ~a" (url->string uri))
293 "")))
294
295 (: uri? (-> String Boolean))
296 (define (uri? str)
297 (regexp-match? #rx"^[a-z]+://.*" (string-downcase str)))
298
299 (: str->peer (String (Option Peer)))
300 (define (str->peer str)
301 (log-debug "Parsing peer string: ~v" str)
302 (with-handlers*
303 ([exn:fail?
304 (λ (e)
305 (log-error "Invalid URI in string: ~v, exn: ~v" str e)
306 #f)])
307 (match (string-split str)
308 [(list u) #:when (uri? u) (Peer #f (string->url u))]
309 [(list n u) #:when (uri? u) (Peer n (string->url u))]
310 [_
311 (log-error "Invalid peer string: ~v" str)
312 #f])))
313
314
315 (: filter-comments (-> (Listof String) (Listof String)))
316 (define (filter-comments lines)
317 (filter-not (λ (line) (string-prefix? line "#")) lines))
318
319 (: str->peers (-> String (Listof Peer)))
320 (define (str->peers str)
321 (filter-map str->peer (filter-comments (str->lines str))))
322
323 (: peers->file (-> (Listof Peers) Path-String Void))
324 (define (peers->file peers path)
325 (display-lines-to-file
326 (map (match-lambda
327 [(Peer n u)
328 (format "~a~a" (if n (format "~a " n) "") (url->string u))])
329 peers)
330 path
331 #:exists 'replace))
332
333 (: file->peers (-> Path-String (Listof Peer)))
334 (define (file->peers file-path)
335 (if (file-exists? file-path)
336 (str->peers (file->string file-path))
337 (begin
338 (log-warning "File does not exist: ~v" (path->string file-path))
339 '())))
340
341 (define re-rfc2822
342 #px"^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), ([0-9]{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{4}) ([0-2][0-9]):([0-6][0-9]):([0-6][0-9]) GMT")
343
344 (: b->n (-> Bytes (Option Number)))
345 (define (b->n b)
346 (string->number (bytes->string/utf-8 b)))
347
348 (: mon->num (-> Bytes Natural))
349 (define/match (mon->num mon)
350 [(#"Jan") 1]
351 [(#"Feb") 2]
352 [(#"Mar") 3]
353 [(#"Apr") 4]
354 [(#"May") 5]
355 [(#"Jun") 6]
356 [(#"Jul") 7]
357 [(#"Aug") 8]
358 [(#"Sep") 9]
359 [(#"Oct") 10]
360 [(#"Nov") 11]
361 [(#"Dec") 12])
362
363 (: rfc2822->epoch (-> Bytes (Option Nonnegative-Integer)))
364 (define (rfc2822->epoch timestamp)
365 (match (regexp-match re-rfc2822 timestamp)
366 [(list _ _ dd mo yyyy HH MM SS)
367 #:when (and dd mo yyyy HH MM SS)
368 (find-seconds (b->n SS)
369 (b->n MM)
370 (b->n HH)
371 (b->n dd)
372 (mon->num mo)
373 (b->n yyyy)
374 #f)]
375 [_
376 #f]))
377
378 (: user-agent String)
379 (define user-agent
380 (let*
381 ([prog-name "tt"]
382 [prog-version (info:#%info-lookup 'version)]
383 [prog-uri "https://github.com/xandkar/tt"]
384 [user-peer-file (build-path tt-home-dir "me")]
385 [user
386 (if (file-exists? user-peer-file)
387 (match (first (file->peers user-peer-file))
388 [(Peer #f u) (format "+~a" (url->string u) )]
389 [(Peer n u) (format "+~a; @~a" (url->string u) n)])
390 (format "+~a" prog-uri))])
391 (format "~a/~a (~a)" prog-name prog-version user)))
392
393 (: header-get (-> (Listof Bytes) Bytes (Option Bytes)))
394 (define (header-get headers name)
395 (match (filter-map (curry extract-field name) headers)
396 [(list val) val]
397 [_ #f]))
398
399 (: uri-download (-> Url Void))
400 (define (uri-download u)
401 (define cached-object-path (url->cache-object-path u))
402 (define cached-etag-path (url->cache-etag-path u))
403 (define cached-lmod-path (url->cache-lmod-path u))
404 (log-debug "uri-download ~v into ~v" u cached-object-path)
405 (define-values (status-line headers body-input)
406 ; TODO Timeout. Currently hangs on slow connections.
407 (http-sendrecv/url u #:headers (list (format "User-Agent: ~a" user-agent))))
408 (log-debug "headers: ~v" headers)
409 (log-debug "status-line: ~v" status-line)
410 (define status
411 (string->number (second (string-split (bytes->string/utf-8 status-line)))))
412 (log-debug "status: ~v" status)
413 ; TODO Handle redirects
414 (match status
415 [200
416 (let* ([etag (header-get headers #"ETag")]
417 [lmod (header-get headers #"Last-Modified")]
418 [lmod-curr (if lmod (rfc2822->epoch lmod) #f)]
419 [lmod-prev (if (file-exists? cached-lmod-path)
420 (rfc2822->epoch (file->bytes cached-lmod-path))
421 #f)])
422 (log-debug "lmod-curr:~v lmod-prev:~v" lmod-curr lmod-prev)
423 (unless (or (and etag
424 (file-exists? cached-etag-path)
425 (bytes=? etag (file->bytes cached-etag-path))
426 (begin
427 (log-info "ETags match, skipping the rest of ~v" (url->string u))
428 #t))
429 (and lmod-curr
430 lmod-prev
431 (<= lmod-curr lmod-prev)
432 (begin
433 (log-info "Last-Modified <= current skipping the rest of ~v" (url->string u))
434 #t)))
435 (begin
436 (log-info
437 "Downloading the rest of ~v. ETag: ~a, Last-Modified: ~v"
438 (url->string u) etag lmod)
439 (make-parent-directory* cached-object-path)
440 (make-parent-directory* cached-etag-path)
441 (make-parent-directory* cached-lmod-path)
442 (call-with-output-file cached-object-path
443 (curry copy-port body-input)
444 #:exists 'replace)
445 (when etag
446 (display-to-file etag cached-etag-path #:exists 'replace))
447 (when lmod
448 (display-to-file lmod cached-lmod-path #:exists 'replace))))
449 (close-input-port body-input))]
450 [_
451 (raise status)]))
452
453 (: timeline-print (-> Out-Format (Listof Msg) Void))
454 (define (timeline-print out-format timeline)
455 (void (foldl (match-lambda**
456 [((and m (Msg _ _ nick _ _ _)) (cons prev-nick i))
457 (let ([i (if (equal? prev-nick nick) i (+ 1 i))])
458 (msg-print out-format i m)
459 (cons nick i))])
460 (cons "" 0)
461 timeline)))
462
463 (: peer->msgs (-> Peer (Listof Msg)))
464 (define (peer->msgs f)
465 (match-define (Peer nick uri) f)
466 (log-info "Reading peer nick:~v uri:~v" nick (url->string uri))
467 (str->msgs nick uri (uri-read-cached uri)))
468
469 (: peer-download (-> Peer Void))
470 (define (peer-download f)
471 (match-define (Peer nick uri) f)
472 (define u (url->string uri))
473 (log-info "Downloading peer uri:~a" u)
474 (with-handlers
475 ([exn:fail?
476 (λ (e)
477 (log-error "Network error nick:~v uri:~v exn:~v" nick u e)
478 #f)]
479 [integer?
480 (λ (status)
481 (log-error "HTTP error nick:~v uri:~a status:~a" nick u status)
482 #f)])
483 (define-values (_result _tm-cpu-ms tm-real-ms _tm-gc-ms)
484 (time-apply uri-download (list uri)))
485 (log-info "Peer downloaded in ~a seconds, uri: ~a" (/ tm-real-ms 1000.0) u)))
486
487 (: timeline-download (-> Integer (Listof Peer) Void))
488 (define (timeline-download num-workers peers)
489 ; TODO No need for map - can just iter
490 (void (concurrent-filter-map num-workers peer-download peers)))
491
492 (: uniq (∀ (α) (-> (Listof α) (Listof α))))
493 (define (uniq xs)
494 (set->list (list->set xs)))
495
496 (: peers->timeline (-> (listof Peer) (listof Msg)))
497 (define (peers->timeline peers)
498 (append* (filter-map peer->msgs peers)))
499
500 (: timeline-sort (-> (listof Msg) timeline-order (Listof Msgs)))
501 (define (timeline-sort msgs order)
502 (define cmp (match order
503 ['old->new <]
504 ['new->old >]))
505 (sort msgs (λ (a b) (cmp (Msg-ts-epoch a)
506 (Msg-ts-epoch b)))))
507
508 (: paths->peers (-> (Listof String) (Listof Peer)))
509 (define (paths->peers paths)
510 (let* ([paths (match paths
511 ['()
512 (let ([peer-refs-file (build-path tt-home-dir "peers")])
513 (log-debug
514 "No peer ref file paths provided, defaulting to ~v"
515 (path->string peer-refs-file))
516 (list peer-refs-file))]
517 [paths
518 (log-debug "Peer ref file paths provided: ~v" paths)
519 (map string->path paths)])]
520 [peers (append* (map file->peers paths))])
521 (log-info "Read-in ~a peers." (length peers))
522 (uniq peers)))
523
524 (: mentioned-peers-in-cache (-> (Listof Peer)))
525 (define (mentioned-peers-in-cache)
526 (define msgs
527 (append* (map (λ (filename)
528 (define path (build-path cache-object-dir filename))
529 (define size (/ (file-size path) 1000000.0))
530 (log-info "BEGIN parsing ~a MB from file: ~v"
531 size
532 (path->string path))
533 (define t0 (current-inexact-milliseconds))
534 (define m (filter-map
535 (λ (line)
536 (str->msg #f (cache-object-filename->url filename) line))
537 (filter-comments
538 (file->lines path))))
539 (define t1 (current-inexact-milliseconds))
540 (log-info "END parsing ~a MB in ~a seconds from file: ~v."
541 size
542 (* 0.001 (- t1 t0))
543 (path->string path))
544 (when (empty? m)
545 (log-warning "No messages found in ~a" (path->string path)))
546 m)
547 (directory-list cache-object-dir))))
548 (uniq (append* (map Msg-mentions msgs))))
549
550 (: follower->peer (-> Follower Peer))
551 (define/match (follower->peer f)
552 [((Follower n u _ _)) (Peer n u)])
553
554 (: weblog-line->follower (-> String (Option Peer)))
555 (define weblog-line->follower
556 (let ([re #px"([^/]+)/([^ ]+) +\\(\\+([a-z]+://[^;]+); *@([^\\)]+)\\)"])
557 (λ (log-line)
558 (match (regexp-match re log-line)
559 [(list _ client version uri nick)
560 (let ([f (Follower nick (string->url uri) client version)])
561 (log-debug "Found follower: ~v" f)
562 f) ]
563 [_ #f]))))
564
565 (define (weblog-file->peers file-path)
566 (define size (/ (file-size file-path) 1000000.0))
567 (log-info "BEGIN parsing ~a MB from file: ~v" size (path->string file-path))
568 (define t0 (current-inexact-milliseconds))
569 (define peers
570 (let* ([prefilter-cmd-path
571 (build-path tt-home-dir "hooks" "web-log-prefilter")]
572 [lines
573 (match (process* prefilter-cmd-path file-path)
574 [(list in _out pid err ctrl)
575 (ctrl 'wait)
576 (match (ctrl 'exit-code)
577 [(or 0 1) ; Assuming grep's: 0: found, 1: not found, 2: error
578 (port->lines in)]
579 [_
580 (log-warning "Prefilter hook failed: ~a" (port->string err))
581 (file->lines file-path)])])])
582 (map follower->peer (filter-map weblog-line->follower lines))))
583 (define t1 (current-inexact-milliseconds))
584 (log-info "END parsing ~a MB in ~a seconds from file: ~v."
585 size
586 (* 0.001 (- t1 t0))
587 (path->string file-path))
588 (when (empty? peers)
589 (log-warning "No peers found in ~a" (path->string file-path)))
590 (uniq peers))
591
592 (define (weblog-dir->peers dir-path)
593 (uniq (append*
594 (map weblog-file->peers
595 (filter-map
596 (λ (filename)
597 (define file-path (build-path dir-path filename))
598 (if (equal? 'file (file-or-directory-type file-path))
599 file-path
600 #f))
601 (if (directory-exists? dir-path)
602 (directory-list dir-path)
603 '()))))))
604
605 (define (follower-peers-in-web-logs log-dirs)
606 (uniq (append* (map weblog-dir->peers log-dirs))))
607
608 (: log-writer-stop (-> Thread Void))
609 (define (log-writer-stop log-writer)
610 (log-message (current-logger) 'fatal 'stop "Exiting." #f)
611 (thread-wait log-writer))
612
613 (: log-writer-start (-> Log-Level Thread))
614 (define (log-writer-start level)
615 (let* ([logger
616 (make-logger #f #f level #f)]
617 [log-receiver
618 (make-log-receiver logger level)]
619 [log-writer
620 (thread
621 (λ ()
622 (parameterize
623 ([date-display-format 'iso-8601])
624 (let loop ()
625 (match-define (vector level msg _ topic) (sync log-receiver))
626 (unless (equal? topic 'stop)
627 (eprintf "~a [~a] ~a~n" (date->string (current-date) #t) level msg)
628 (loop))))))])
629 (current-logger logger)
630 log-writer))
631
632 (module+ main
633 (let ([log-level 'info])
634 (command-line
635 #:program
636 "tt"
637 #:once-each
638 [("-d" "--debug")
639 "Enable debug log level."
640 (set! log-level 'debug)]
641 #:help-labels
642 ""
643 "and <command> is one of"
644 "r, read : Read the timeline (offline operation)."
645 "d, download : Download the timeline."
646 ; TODO Add path dynamically
647 "u, upload : Upload your twtxt file (alias to execute ~/.tt/upload)."
648 "c, crawl : Discover new peers mentioned by known peers (offline operation)."
649 ""
650 #:args (command . args)
651 (define log-writer (log-writer-start log-level))
652 (current-command-line-arguments (list->vector args))
653 (match command
654 [(or "d" "download")
655 ; Initially, 15 was fastest out of the tried: 1, 5, 10, 20. Then I
656 ; started noticing significant slowdowns. Reducing to 5 seems to help.
657 (let ([num-workers 5])
658 (command-line
659 #:program
660 "tt download"
661 #:once-each
662 [("-j" "--jobs")
663 njobs "Number of concurrent jobs."
664 (set! num-workers (string->number njobs))]
665 #:args file-paths
666 (let ([peers (paths->peers file-paths)])
667 (define-values (_res _cpu real-ms _gc)
668 (time-apply timeline-download (list num-workers peers)))
669 (log-info "Downloaded timelines from ~a peers in ~a seconds."
670 (length peers)
671 (/ real-ms 1000.0)))
672 (let ([hook-path (build-path tt-home-dir "hooks" "download")])
673 (if (file-exists? hook-path)
674 (if (member 'execute (file-or-directory-permissions hook-path))
675 (if (system (path->string hook-path))
676 (exit 0)
677 (exit 1))
678 (log-warning "Download hook found, but not executable."))
679 (log-warning "Download hook not found.")))))]
680 [(or "u" "upload")
681 (command-line
682 #:program
683 "tt upload"
684 #:args ()
685 (if (system (path->string (build-path tt-home-dir "hooks" "upload")))
686 (exit 0)
687 (exit 1)))]
688 [(or "r" "read")
689 (let ([out-format 'multi-line]
690 [order 'old->new]
691 [ts-min #f]
692 [ts-max #f])
693 (command-line
694 #:program
695 "tt read"
696 #:once-each
697 [("-r" "--rev")
698 "Reverse displayed timeline order."
699 (set! order 'new->old)]
700 [("-m" "--min")
701 m "Earliest time to display (ignore anything before it)."
702 (set! ts-min (rfc3339->epoch m))]
703 [("-x" "--max")
704 x "Latest time to display (ignore anything after it)."
705 (set! ts-max (rfc3339->epoch x))]
706 #:once-any
707 [("-s" "--short")
708 "Short output format"
709 (set! out-format 'single-line)]
710 [("-l" "--long")
711 "Long output format"
712 (set! out-format 'multi-line)]
713 #:args file-paths
714 (let* ([peers
715 (paths->peers file-paths)]
716 [timeline
717 (timeline-sort (peers->timeline peers) order)]
718 [timeline
719 (filter (λ (m) (and (if ts-min (>= (Msg-ts-epoch m)
720 ts-min)
721 #t)
722 (if ts-max (<= (Msg-ts-epoch m)
723 ts-max)
724 #t)))
725 timeline)])
726 (timeline-print out-format timeline))))]
727 [(or "c" "crawl")
728 (command-line
729 #:program
730 "tt crawl"
731 #:args log-files-directories
732 (let* ([peers-sort
733 (λ (peers) (sort peers (match-lambda**
734 [((Peer n1 _) (Peer n2 _))
735 (string<? (if n1 n1 "")
736 (if n2 n2 ""))])))]
737 ; TODO Tidy-up redundant set conversions
738 [peers-all-file
739 (build-path tt-home-dir "peers-all")]
740 [peers-mentioned-file
741 (build-path tt-home-dir "peers-mentioned")]
742 [peers-followers-file
743 (build-path tt-home-dir "peers-followers")]
744 [peers-followers-curr
745 (follower-peers-in-web-logs
746 (if (empty? log-files-directories)
747 (list (build-path tt-home-dir "web-logs"))
748 log-files-directories))]
749 [peers-mentioned-curr
750 (mentioned-peers-in-cache)]
751 [peers-mentioned-prev
752 (file->peers peers-mentioned-file)]
753 [peers-followers-prev
754 (file->peers peers-followers-file)]
755 [peers-mentioned
756 (peers-sort (uniq (append peers-mentioned-prev
757 peers-mentioned-curr)))]
758 [peers-all-prev
759 (file->peers peers-all-file)]
760 [peers-followers
761 (list->set (append peers-followers-prev
762 peers-followers-curr))]
763 [peers-all
764 (list->set (append peers-mentioned
765 (set->list peers-followers)
766 peers-all-prev))]
767 [peers-discovered-followers
768 (set-subtract (list->set peers-followers)
769 (list->set peers-followers-prev))]
770 [peers-discovered
771 (set-subtract peers-all (list->set peers-all-prev))]
772 [peers-all
773 (peers-sort (set->list peers-all))])
774 (log-info "Known peers mentioned: ~a" (length peers-mentioned))
775 (log-info "Known peers total: ~a" (length peers-all))
776 (log-info "Discovered ~a new followers:~n~a"
777 (set-count peers-discovered-followers)
778 (pretty-format (map
779 (λ (p) (cons (Peer-nick p)
780 (url->string (Peer-uri p))))
781 (set->list peers-discovered-followers))))
782 (log-info "Discovered ~a new peers:~n~a"
783 (set-count peers-discovered)
784 (pretty-format (map
785 (λ (p) (cons (Peer-nick p)
786 (url->string (Peer-uri p))))
787 (set->list peers-discovered))))
788 (peers->file (peers-sort (set->list peers-followers))
789 peers-followers-file)
790 (peers->file peers-mentioned
791 peers-mentioned-file)
792 (peers->file peers-all
793 peers-all-file)))]
794 [command
795 (eprintf "Error: invalid command: ~v\n" command)
796 (eprintf "Please use the \"--help\" option to see a list of available commands.\n")
797 (exit 1)])
798 (log-writer-stop log-writer))))
This page took 0.115765 seconds and 4 git commands to generate.