<commit_before>(ns route-ccrs.active-routes
  (:require [clojure.java.jdbc :as jdbc]
            [yesql.core :refer [defquery]]))

(defquery active-routes "route_ccrs/sql/active_routes.sql")

(defn route-id [r]
  (select-keys r [:contract
                  :part_no
                  :lowest_level
                  :bom_type_db
                  :routing_revision_no
                  :routing_alternative_no]))

(defn sorted-operations []
  (sorted-set-by
    (fn [x y]
      (compare (:operation_no x)
               (:operation_no y))) ))

(defn transduce-routes
  "Transduces routing operation records into tuples of `[route-id operations]`
  where the `route-id` is a map of the fields uniquely identifying the route
  and `operations` is a sorted set of the operations that comprise the route.

  Requires the operations to have an `:operation_count` value signifying
  the total number of operations on the route to which they belong, so
  that the routes can be checked for completeness."
  [step]
  (let [routes (volatile! {})]
    (fn
      ([] (step))
      ([r]
       (let [v (if (empty? @routes)
                 r
                 (reduce step r @routes))]
         (vreset! routes {})
         (step v)))
      ([r o]
       (let [i (route-id o)
             route (conj (get @routes i (sorted-operations)) o)
             c (:operation_count o)]
         (if (= (int (count route)) (int c))
           (do
             (vreset! routes (dissoc @routes i))
             (step r [i route]))
           (do
             (vreset! routes (assoc @routes i route))
             r)))))))
<commit_msg>Pull route id keys out into a def so they can be referred to<commit_after>(ns route-ccrs.active-routes
  (:require [clojure.java.jdbc :as jdbc]
            [yesql.core :refer [defquery]]))

(defquery active-routes "route_ccrs/sql/active_routes.sql")

(def route-id-keys
  [:contract
   :part_no
   :lowest_level
   :bom_type_db
   :routing_revision_no
   :routing_alternative_no])

(defn route-id [r]
  (select-keys r route-id-keys))

(defn sorted-operations []
  (sorted-set-by
    (fn [x y]
      (compare (:operation_no x)
               (:operation_no y)))))

(defn transduce-routes
  "Transduces routing operation records into tuples of `[route-id operations]`
  where the `route-id` is a map of the fields uniquely identifying the route
  and `operations` is a sorted set of the operations that comprise the route.

  Requires the operations to have an `:operation_count` value signifying
  the total number of operations on the route to which they belong, so
  that the routes can be checked for completeness."
  [step]
  (let [routes (volatile! {})]
    (fn
      ([] (step))
      ([r]
       (let [v (if (empty? @routes)
                 r
                 (reduce step r @routes))]
         (vreset! routes {})
         (step v)))
      ([r o]
       (let [i (route-id o)
             route (conj (get @routes i (sorted-operations)) o)
             c (:operation_count o)]
         (if (= (int (count route)) (int c))
           (do
             (vreset! routes (dissoc @routes i))
             (step r [i route]))
           (do
             (vreset! routes (assoc @routes i route))
             r)))))))

{'fraction_non_alphanumeric': 0.11593304401735896, 'fraction_numerical': 0.0, 'mean_word_length': 2.133009708737864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1189375', 'n_tokens_mistral': 1019, 'n_tokens_neox': 1009, 'n_words': 340}