<commit_before>(ns mars-ogler.routes
  (:require [clj-time.core :as time]
            [clj-time.coerce :as cvt-time]
            [clojure.string :as str]
            [compojure.route :as route]
            [compojure.handler :as handler]
            [compojure.response :as response]
            [mars-ogler.views :as views])
  (:use [compojure.core :only [defroutes GET]]
        [ring.middleware cookies
                         keyword-params
                         params]))

(defn parse-cookie-params
  [cookie-params]
  (let [keys [:cams :per-page :sorting :thumbs :view]
        kw->sk (zipmap keys (map name keys))]
    (into {} (->> (for [[kw sk] kw->sk]
                    (if-let [v (get-in cookie-params [sk :value])]
                      (if (= kw :cams)
                        [kw (str/split v #" ")]
                        [kw v])))
               (keep identity)))))

(defn parse-params
  [{:keys [cams page per-page sorting thumbs query-string view]
    :or {cams ["mahli" "mastcam" "navcam"]
         page "1"
         per-page "25"
         sorting "released"
         thumbs "no"
         view "list"}
    :as params}]
  (let [cams' (if (vector? cams)
                (set (map keyword cams))
                #{(keyword cams)})
        page' (Integer/parseInt page)
        sorting' (keyword sorting)
        thumbs' (keyword thumbs)
        view' (keyword view)
        per-page' (min (Integer/parseInt per-page)
                       (if (= view' :grid) 200 100))]
    (merge params
           {:cams cams', :page page', :per-page per-page', :sorting sorting'
            :thumbs thumbs', :query-string (or query-string ""), :view view'})))

(defn visit-tick
  [visit-last visit-start visit-recent]
  (let [now (cvt-time/to-long (time/now))
        visit-last (if (nil? visit-last) now (Long/parseLong visit-last))
        visit-start (if (nil? visit-start) now (Long/parseLong visit-start))
        visit-recent (if (nil? visit-recent) now (Long/parseLong visit-recent))]
    (if (> (- now visit-recent) (* 10 60 1000))
      [visit-recent now now]
      [visit-last visit-start now])))

(defn set-expires
  [cookies]
  (into {} (for [[c v] cookies]
             [c {:value v, :expires "Wed, 13-Jan-2021 22:23:01 GMT"}])))

(defroutes ogler-routes
  (GET "/" [& params :as {:keys [query-string cookies]}]
    (let [[visit-last
           visit-start
           visit-recent] (visit-tick (get-in cookies ["visit-last" :value])
                                     (get-in cookies ["visit-start" :value])
                                     (get-in cookies ["visit-recent" :value]))
          parsed-params (-> cookies
                          parse-cookie-params
                          (merge params)
                          (assoc :query-string query-string
                                 :visit-last visit-last<commit_msg>See what pushing visit-recent back does<commit_after>(ns mars-ogler.routes
  (:require [clj-time.core :as time]
            [clj-time.coerce :as cvt-time]
            [clojure.string :as str]
            [compojure.route :as route]
            [compojure.handler :as handler]
            [compojure.response :as response]
            [mars-ogler.views :as views])
  (:use [compojure.core :only [defroutes GET]]
        [ring.middleware cookies
                         keyword-params
                         params]))

(defn parse-cookie-params
  [cookie-params]
  (let [keys [:cams :per-page :sorting :thumbs :view]
        kw->sk (zipmap keys (map name keys))]
    (into {} (->> (for [[kw sk] kw->sk]
                    (if-let [v (get-in cookie-params [sk :value])]
                      (if (= kw :cams)
                        [kw (str/split v #" ")]
                        [kw v])))
               (keep identity)))))

(defn parse-params
  [{:keys [cams page per-page sorting thumbs query-string view]
    :or {cams ["mahli" "mastcam" "navcam"]
         page "1"
         per-page "25"
         sorting "released"
         thumbs "no"
         view "list"}
    :as params}]
  (let [cams' (if (vector? cams)
                (set (map keyword cams))
                #{(keyword cams)})
        page' (Integer/parseInt page)
        sorting' (keyword sorting)
        thumbs' (keyword thumbs)
        view' (keyword view)
        per-page' (min (Integer/parseInt per-page)
                       (if (= view' :grid) 200 100))]
    (merge params
           {:cams cams', :page page', :per-page per-page', :sorting sorting'
            :thumbs thumbs', :query-string (or query-string ""), :view view'})))

(defn visit-tick
  [visit-last visit-start visit-recent]
  (let [now (cvt-time/to-long (time/now))
        visit-last (if (nil? visit-last) now (Long/parseLong visit-last))
        visit-start (if (nil? visit-start) now (Long/parseLong visit-start))
        visit-recent (if (nil? visit-recent) now (Long/parseLong visit-recent))]
    (if (> (- now visit-recent) (* 10 60 1000))
      ;; curiositymsl.com updates every 15 minutes, and we scrape every minute
      [(- visit-recent (* 16 60 1000)) now now]
      [visit-last visit-start now])))

(defn set-expires
  [cookies]
  (into {} (for [[c v] cookies]
             [c {:value v, :expires "Wed, 13-Jan-2021 22:23:01 GMT"}])))

(defroutes ogler-routes
  (GET "/" [& params :as {:keys [query-string cookies]}]
    (let [[visit-last
           visit-start
           visit-recent] (visit-tick (get-in cookies ["visit-last" :value])
                                     (get-in cookies ["visit-start" :value])
                                     (get-in cookies ["visit-recent" :value]))
          parsed-params (-> cookies
                          parse-cookie-params
                          (merge params)
                          (assoc :query-string query-string
                                 :visit-last visit-last
{'fraction_non_alphanumeric': 0.14141586867305062, 'fraction_numerical': 0.011627906976744186, 'mean_word_length': 1.7116365322206768, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6580922', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1781, 'n_words': 495}