<commit_before>(define spec (create-specification))


(define ast
  (with-specification
    spec
    (ast-rule 'Config->Worker*<Workers-Switch*<Switches)
    (ast-rule 'Worker->id-state-timestamp-switchid-Request*<Queue)
    (ast-rule 'Request->id-size-time)
    (ast-rule 'Switch->id-state-timestamp)
    (compile-ast-specifications 'Config)
    (ag-rule
      lookup-worker
      (Config
        (lambda (n id)
          (ast-find-child
            (lambda (i worker) (= (ast-child 'id worker) id))
            (ast-child 'Workers n)))))
    (compile-ag-specifications)
    (create-ast 'Config
                (list
                  (create-ast-list (list))
                  (create-ast-list (list))))))


(define workers (ast-child 'Workers ast))


(define switches (ast-child 'Switches ast))


(define add-worker-to-ast
  (lambda (id time switch-id)
    (rewrite-add
      workers
      (create-ast spec 'Worker (list id 'OFF time switch-id (create-ast-list (list)))))))


(define add-switch-to-ast
  (lambda (id time)
    (rewrite-add
      switches
      (create-ast spec 'Switch (list id 'OFF time)))))


(define get-worker
  (lambda (id)
    (ast-find-child
      (lambda (i worker) (= (ast-child 'id worker) id))
      (ast-child 'Workers ast))))




(define display-ast
  (lambda ()
    (print-ast
      ast
      (list)
      (current-output-port))))



(define status
  (lambda ()
    (display "id | state | since | queue-length\n")
    (let ((time (timestamp)))
      (ast-for-each-child
        (lambda (i worker)
          (display (ast-child 'id worker))
          (display " ")
          (display (ast-child 'state worker))
          (display " ")
          (display (/ (floor (* (- time (ast-child 'timestamp worker)) 100)) 100))
          (display " ")
          (display (ast-num-children (ast-child 'Queue worker)))
          (newline))
        (ast-child 'Workers ast)))))



(define event-worker-online
  (lambda (id time)
    (let ((worker (get-worker id)))
      (rewrite-terminal 'state worker 'RUNNING)
      (rewrite-terminal 'timestamp worker time))))


(define event-worker-off
  (lambda (id time)
    (let ((worker (get-worker id)))
      (rewrite-terminal 'state worker 'OFF)
      (rewrite-terminal 'timestamp worker time))))


;(define round-robbin
;  (let ((pos 0))
;    (lambda ()
;      (let ((running-workers (list)))
;        (ast-for-each-child
;          (lambda (i worker)
;            (if (eq? (ast-child 'state worker) 'RUNNING)
;              (set! running-workers (append running-workers (list worker)))))
;          workers)
;        (set! pos
;          (if (< pos (length running-workers))
;            (+ pos 1)
;            1))
;        (if (= 0 (length running-workers))
;          #f
;          (list-ref running-workers (- pos 1)))))))


(define round-robbin
  (lambda ()
    (let
      ((running-workers
         (filter
           (lambda (worker) (eq? (ast-child 'state worker) 'RUNNING))
           (ast-children workers))))
      (fold-left
        (lambda (w1 w2)
          (if
            (<=
              (ast-num-children (ast-child 'Queue w1))
              (ast-num-children (ast-child 'Queue w2)))
            w1
            w2))
        (car running-workers)
        (cdr running-workers)))))




(define assign-next-request
  (lambda (worker)
    (let ((queue (ast-child 'Queue worker)))
      (if (not (= 0 (ast-num-children queue)))
        (let ((request (ast-child 1 queue)))
          (add-event
            'event-work-command
            (ast-child 'id worker)
            (ast-child 'id request)
            1
            (ast-child 'size request)))
        #f))))


<commit_msg>fix round robbin<commit_after>(define spec (create-specification))


(define ast
  (with-specification
    spec
    (ast-rule 'Config->Worker*<Workers-Switch*<Switches)
    (ast-rule 'Worker->id-state-timestamp-switchid-Request*<Queue)
    (ast-rule 'Request->id-size-time)
    (ast-rule 'Switch->id-state-timestamp)
    (compile-ast-specifications 'Config)
    (ag-rule
      lookup-worker
      (Config
        (lambda (n id)
          (ast-find-child
            (lambda (i worker) (= (ast-child 'id worker) id))
            (ast-child 'Workers n)))))
    (compile-ag-specifications)
    (create-ast 'Config
                (list
                  (create-ast-list (list))
                  (create-ast-list (list))))))


(define workers (ast-child 'Workers ast))


(define switches (ast-child 'Switches ast))


(define add-worker-to-ast
  (lambda (id time switch-id)
    (rewrite-add
      workers
      (create-ast spec 'Worker (list id 'OFF time switch-id (create-ast-list (list)))))))


(define add-switch-to-ast
  (lambda (id time)
    (rewrite-add
      switches
      (create-ast spec 'Switch (list id 'OFF time)))))


(define get-worker
  (lambda (id)
    (ast-find-child
      (lambda (i worker) (= (ast-child 'id worker) id))
      (ast-child 'Workers ast))))




(define display-ast
  (lambda ()
    (print-ast
      ast
      (list)
      (current-output-port))))



(define status
  (lambda ()
    (display "id | state | since | queue-length\n")
    (let ((time (timestamp)))
      (ast-for-each-child
        (lambda (i worker)
          (display (ast-child 'id worker))
          (display " ")
          (display (ast-child 'state worker))
          (display " ")
          (display (/ (floor (* (- time (ast-child 'timestamp worker)) 100)) 100))
          (display " ")
          (display (ast-num-children (ast-child 'Queue worker)))
          (newline))
        (ast-child 'Workers ast)))))



(define event-worker-online
  (lambda (id time)
    (let ((worker (get-worker id)))
      (rewrite-terminal 'state worker 'RUNNING)
      (rewrite-terminal 'timestamp worker time))))


(define event-worker-off
  (lambda (id time)
    (let ((worker (get-worker id)))
      (rewrite-terminal 'state worker 'OFF)
      (rewrite-terminal 'timestamp worker time))))


;(define round-robbin
;  (let ((pos 0))
;    (lambda ()
;      (let ((running-workers (list)))
;        (ast-for-each-child
;          (lambda (i worker)
;            (if (eq? (ast-child 'state worker) 'RUNNING)
;              (set! running-workers (append running-workers (list worker)))))
;          workers)
;        (set! pos
;          (if (< pos (length running-workers))
;            (+ pos 1)
;            1))
;        (if (= 0 (length running-workers))
;          #f
;          (list-ref running-workers (- pos 1)))))))


(define round-robbin
  (lambda ()
    (let
      ((running-workers
         (filter
           (lambda (worker) (eq? (ast-child 'state worker) 'RUNNING))
           (ast-children workers))))
      (if (null? running-workers)
        #f
        (fold-left
          (lambda (w1 w2)
            (if
              (<=
                (ast-num-children (ast-child 'Queue w1))
                (ast-num-children (ast-child 'Queue w2)))
              w1
              w2))
          (car running-workers)
          (cdr running-workers))))))


(define assign-next-request
  (lambda (worker)
    (let ((queue (ast-child 'Queue worker)))
      (if (not (= 0 (ast-num-children queue)))
        (let ((request (ast-child 1 queue)))
          (add-event
            'event-work-command
            (ast-child 'id worker)
            (ast-child 'id request)
            1
            (ast-child 'size request)))
        #f))))



{'fraction_non_alphanumeric': 0.1548151158065827, 'fraction_numerical': 0.005417851821752675, 'mean_word_length': 1.9702333065164923, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '204542', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2286, 'n_words': 592}