<commit_before>#!/usr/bin/env guile
-s
!#
;; Minimal web server can be started from command line. Current routes:
;;
;;    localhost:8080/status.json
;;
;; Note that this is a single blocking thread server - that makes
;; sense to not to overload the local redis instance

(use-modules (json)
             (redis)
             (web server)
             (web request)
             (web response)
             (web uri))

(define (request-path-components request)
  (split-and-decode-uri-path (uri-path (request-uri request))))

(define (get-status-as-json-string-list)
  ;; return a list of strings containing json records
  (let* ((conn (redis-connect))
         (sorted-list (sort-list (redis-send conn (smembers '("sheepdog:run"))) string>)))
    (redis-close conn)
    sorted-list
     ))

(define (get-status-as-single-json-string)
  (string-append "[" (string-join (get-status-as-json-string-list) ",\n") "]"))

(define (get-status-as-scm)
  (let ((js get-status-as-json-string-list))
    (map json-string->scm (get-status-as-json-string-list))))

(define (status-line rec)
  (string-append
   (cdr (assoc "time" rec)) "\t"
   (cdr (assoc "host" rec)) "\t"
   (cdr (assoc "err" rec)) "\t"
   (cdr (assoc "tag" rec)) "\t"
   (cdr (assoc "command" rec))
   ))

(define (get-status-as-html)
  (let ((l (get-status-as-scm)))
    (string-append "<pre>"
                   (string-join (map (lambda (rec) (status-line rec)) l) "\n")
                   "</pre>")))

(define (hello-world-handler request body)
  (let ((path (uri-path (request-uri request))))
    (cond
     ((member path (list "/status.json"))
      (values '((content-type . (application/json)))
              (get-status-as-single-json-string)
              ))
     ((member path (list "/status.html"))
      (values '((content-type . (text/html)))
              (get-status-as-html)
              ))
     (else
      (not-found request)))))

(define (not-found request)
  (values (build-response #:code 404)
          (string-append "Resource not found: "
                         (uri->string (request-uri request)))))

(run-server hello-world-handler)<commit_msg>Web: set port to 8091<commit_after>#!/usr/bin/env guile
-s
!#
;; Minimal web server can be started from command line. Current routes:
;;
;;    localhost:8080/status.json
;;
;; Note that this is a single blocking thread server - that makes
;; sense to not to overload the local redis instance

(use-modules (json)
             (redis)
             (web server)
             (web request)
             (web response)
             (web uri))

(define (request-path-components request)
  (split-and-decode-uri-path (uri-path (request-uri request))))

(define (get-status-as-json-string-list)
  ;; return a list of strings containing json records
  (let* ((conn (redis-connect))
         (sorted-list (sort-list (redis-send conn (smembers '("sheepdog:run"))) string>)))
    (redis-close conn)
    sorted-list
     ))

(define (get-status-as-single-json-string)
  (string-append "[" (string-join (get-status-as-json-string-list) ",\n") "]"))

(define (get-status-as-scm)
  (let ((js get-status-as-json-string-list))
    (map json-string->scm (get-status-as-json-string-list))))

(define (status-line rec)
  (string-append
   (cdr (assoc "time" rec)) "\t"
   (cdr (assoc "host" rec)) "\t"
   (cdr (assoc "err" rec)) "\t"
   (cdr (assoc "tag" rec)) "\t"
   (cdr (assoc "command" rec))
   ))

(define (get-status-as-html)
  (let ((l (get-status-as-scm)))
    (string-append "<pre>"
                   (string-join (map (lambda (rec) (status-line rec)) l) "\n")
                   "</pre>")))

(define (hello-world-handler request body)
  (let ((path (uri-path (request-uri request))))
    (cond
     ((member path (list "/status.json"))
      (values '((content-type . (application/json)))
              (get-status-as-single-json-string)
              ))
     ((member path (list "/status.html"))
      (values '((content-type . (text/html)))
              (get-status-as-html)
              ))
     (else
      (not-found request)))))

(define (not-found request)
  (values (build-response #:code 404)
          (string-append "Resource not found: "
                         (uri->string (request-uri request)))))

(run-server hello-world-handler 'http '(#:port 8091))
{'fraction_non_alphanumeric': 0.17540181691125087, 'fraction_numerical': 0.005124621476822734, 'mean_word_length': 2.496742671009772, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1309440', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1421, 'n_words': 364}