<commit_before>(ns metlog-vault.core
  (:gen-class :main true)
  (:use metlog-common.core
        compojure.core)
  (:require [clojure.tools.logging :as log]
            [ring.adapter.jetty :as jetty]
            [ring.middleware.resource :as ring-resource]
            [ring.util.response :as ring]
            [compojure.route :as route]
            [compojure.handler :as handler]
            [clojure.edn :as edn]
            [metlog-vault.data :as data]
            [hiccup.core :as hiccup]))

(defmacro get-version []
  ;; Capture compile-time property definition from Lein
  (System/getProperty "metlog-vault.version"))

(defn edn-response [data & [status]]
  {:status (or status 200)
   :headers {"Content-Type" "application/edn"}
   :body (pr-str data)})

(defn get-data-for-series-name [ series-name window-size-secs ]
  (edn-response
   (merge {:data (data/get-data-for-series-name series-name window-size-secs)}
          (data/get-time-range window-size-secs))))

(defn render-dashboard []
  (hiccup/html
   [:html
    [:head
     [:link { :href "/metlog.css" :rel "stylesheet" :type "text/css"}]
     [:title "Metlog - " (get-version)]]
    [:body
     [:div {:id "metlog"}]
     [:script {:src (str "metlog-" (get-version) ".js")
               :type "text/javascript"}]]]))

(defroutes all-routes
  (GET "/series-names" []
    (edn-response
     (data/get-series-names)))

  (GET "/data/:series-name" {{series-name :series-name
                              query-window-secs :query-window-secs}
                             :params}
    (get-data-for-series-name series-name (or (parsable-integer? query-window-secs) 86400)))

  (POST "/data" req
    (data/store-data-samples (edn/read-string (slurp (:body req))))
    "Incoming data accepted.")
  
  (route/resources "/")
  (GET "/" [] (ring/redirect "/dashboard"))
  (GET "/dashboard" [] (render-dashboard))
  (route/not-found "Resource Not Found"))

(defn wrap-request-logging [ app ]
  (fn [req]
    (log/debug 'REQUEST (:request-method req) (:uri req))
    (let [resp (app req)]
      (log/trace 'RESPONSE (:status resp))
      resp)))

(def handler (-> all-routes
                 (data/wrap-db-connection)
                 (ring-resource/wrap-resource "public")
                 (wrap-request-logging)
                 (handler/api)))

(defn start-webserver [ http-port ]
  (log/info "Starting Vault Webserver on port" http-port)
  (let [server (jetty/run-jetty handler  { :port http-port :join? false })]
    (add-shutdown-hook
     (fn []
       (log/info "Shutting down webserver")
       (.stop server)))
    (.join server)))

(defn -main
  []
  (log/info "Starting Vault")
  (start-webserver (config-property "http.port" 8080))
  (log/info "end run."))
<commit_msg>Modify middleware stack to return 304's on unmodified files.<commit_after>(ns metlog-vault.core
  (:gen-class :main true)
  (:use metlog-common.core
        compojure.core)
  (:require [clojure.tools.logging :as log]
            [ring.adapter.jetty :as jetty]
            [ring.middleware.resource :as ring-resource]
            [ring.middleware.not-modified :as not-modified]
            [ring.util.response :as ring]
            [compojure.route :as route]
            [compojure.handler :as handler]
            [clojure.edn :as edn]
            [metlog-vault.data :as data]
            [hiccup.core :as hiccup]))

(defmacro get-version []
  ;; Capture compile-time property definition from Lein
  (System/getProperty "metlog-vault.version"))

(defn edn-response [data & [status]]
  {:status (or status 200)
   :headers {"Content-Type" "application/edn"}
   :body (pr-str data)})

(defn get-data-for-series-name [ series-name window-size-secs ]
  (edn-response
   (merge {:data (data/get-data-for-series-name series-name window-size-secs)}
          (data/get-time-range window-size-secs))))

(defn render-dashboard []
  (hiccup/html
   [:html
    [:head
     [:link { :href "/metlog.css" :rel "stylesheet" :type "text/css"}]
     [:title "Metlog - " (get-version)]]
    [:body
     [:div {:id "metlog"}]
     [:script {:src (str "metlog-" (get-version) ".js")
               :type "text/javascript"}]]]))

(defroutes all-routes
  (GET "/series-names" []
    (edn-response
     (data/get-series-names)))

  (GET "/data/:series-name" {{series-name :series-name
                              query-window-secs :query-window-secs}
                             :params}
    (get-data-for-series-name series-name (or (parsable-integer? query-window-secs) 86400)))

  (POST "/data" req
    (data/store-data-samples (edn/read-string (slurp (:body req))))
    "Incoming data accepted.")
  
  (GET "/" [] (ring/redirect "/dashboard"))
  (GET "/dashboard" [] (render-dashboard))
  (route/not-found "Resource Not Found"))

(defn wrap-request-logging [ app num ]
  (fn [req]
    (log/debug 'REQUEST (:request-method req) (:uri req))
    (let [resp (app req)]
      (log/debug 'RESPONSE (:status resp))
      resp)))

(def handler (-> all-routes
                 (data/wrap-db-connection)
                 (ring-resource/wrap-resource "public")
                 (not-modified/wrap-not-modified)
                 (handler/site)))

(defn start-webserver [ http-port ]
  (log/info "Starting Vault Webserver on port" http-port)
  (let [server (jetty/run-jetty handler  { :port http-port :join? false })]
    (add-shutdown-hook
     (fn []
       (log/info "Shutting down webserver")
       (.stop server)))
    (.join server)))

(defn -main
  []
  (log/info "Starting Vault")
  (start-webserver (config-property "http.port" 8080))
  (log/info "end run."))

{'fraction_non_alphanumeric': 0.16344432509846044, 'fraction_numerical': 0.004833512352309345, 'mean_word_length': 2.474502487562189, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2485177', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1876, 'n_words': 427}