<commit_before>(ns org.altlaw.www.SearchResource
  (:require [org.altlaw.pages.templates :as tmpl]
            [org.altlaw.util.date :as date]
            [org.altlaw.util :as util]
            [org.altlaw.util.context :as context]
            [clojure.contrib.stacktrace :as stacktrace])
  (:use [clojure.contrib.str-utils :only (str-join)])
  (:gen-class :extends org.restlet.resource.Resource)
  (:import (org.restlet.resource Variant StringRepresentation ResourceException)
           (org.restlet.data MediaType Language CharacterSet Reference Status)
           (org.apache.solr.client.solrj SolrQuery SolrQuery$ORDER)
           (org.apache.commons.lang StringUtils)
           (org.altlaw.util DateUtils)))

;;; UTILITIES 

(defn get-query-params [this]
  (.. this getRequest getResourceRef getQueryAsForm getValuesMap))

(defn clean-param [param-string]
  (when param-string (.trim (Reference/decode param-string))))

(defn log [this & args]
  (.. this getContext getLogger (info (apply str args))))

;;; PAGE NUMBERING

(def *page-size* 10)

(def *window-size* 5)

(defn page-ref [ref number]
  (let [newref (Reference. ref)
        query (.getQueryAsForm newref)]
    (.set query "page" (str number) true)
    (.setQuery newref (.encode query))
    newref))

(defn pagination-html [ref current total-hits]
  (let [last-page (int (Math/ceil (/ total-hits (float *page-size*))))
        window-start (if (< (- current *window-size*) 1)
                       1 (- current *window-size*))
        window-end (if (> (+ current *window-size*) last-page)
                     last-page (+ current *window-size*))]
    (with-out-str
      (print "<div class=\"pagination\"><ul>")
      (print "<li>Pages:</li>")
      (if (= current 1)
        (print "<li class=\"disablepage\">&#171; previous</li>")
        (printf "<li class=\"prevpage\"><a href=\"%s\">&#171; previous</a></li>"
                (str (page-ref ref (dec current)))))
      (when (not= window-start 1)
        (printf "<li><a href=\"%s\">1</a></li>" (str (page-ref ref 1)))
        (print "<li>...</li>"))
      (doseq [n (range window-start (inc window-end))]
        (if (= n current)
          (printf "<li class=\"currentpage\">%d</li>" n)
          (printf "<li><a href=\"%s\">%d</a></li>" (str (page-ref ref n)) n)))
      (when (not= window-end last-page)
        (print "<li>...</li>")
        (printf "<li><a href=\"%s\">%d</a></li>"
                (str (page-ref ref last-page)) last-page))
      (if (= current last-page)
        (print "<li class=\"disablepage\">next &#187;</li>")
        (printf "<li class=\"prevpage\"><a href=\"%s\">next &#187;</a></li>"
                (str (page-ref ref (inc current)))))
      (print "</ul></div>"))))

;;; RENDERING RESULTS

(defn prepare-hit [doc]
  (let [name (.getFieldValue doc "name")
        the-name (if (or (nil? name) (empty? name))
                   (str "Untitled #" (.getFieldValue doc "docid"))
                   name)]
  {:docid (.getFieldValue doc "docid")
   :url (format "/v1/cases/%s" (.getFieldValue doc "docid"))
   :size (when-let [size (.getFieldValue doc "size")]
           (int (Math/ceil (/ size 1024.0))))
   :date (date/format-long-date (.getFieldValue doc "date"))
   :court (util/*court-names* (.getFieldValue doc "court"))
   :name (tmpl/h the-name)
   :citations (map tmpl/h (.getFieldValues doc "citations"))}))

(defn prepare-solr-results [solr-response]
  (let [docs (.getResults solr-response)]
    {:query (.. solr-response getHeader (get "params") (get "q"))
     :sort (.. solr-response getHeader (get "params") (get "sort"))
     :total_hits (.getNumFound docs)
     :start_hit (if (zero? (.getNumFound docs)) 0 (inc (.getStart docs)))
     :end_hit (+ (.getStart docs) (count docs))<commit_msg>www/SearchResource.clj: fixed requires<commit_after>(ns org.altlaw.www.SearchResource
  (:require [org.altlaw.www.render :as tmpl]
            [org.altlaw.util.date :as date]
            [org.altlaw.util.courts :as courts]
            [org.altlaw.util.context :as context]
            [clojure.contrib.stacktrace :as stacktrace])
  (:use [clojure.contrib.str-utils :only (str-join)])
  (:gen-class :extends org.restlet.resource.Resource)
  (:import (org.restlet.resource Variant StringRepresentation ResourceException)
           (org.restlet.data MediaType Language CharacterSet Reference Status)
           (org.apache.solr.client.solrj SolrQuery SolrQuery$ORDER)
           (org.apache.commons.lang StringUtils)
           (org.altlaw.util DateUtils)))

;;; UTILITIES 

(defn get-query-params [this]
  (.. this getRequest getResourceRef getQueryAsForm getValuesMap))

(defn clean-param [param-string]
  (when param-string (.trim (Reference/decode param-string))))

(defn log [this & args]
  (.. this getContext getLogger (info (apply str args))))

;;; PAGE NUMBERING

(def *page-size* 10)

(def *window-size* 5)

(defn page-ref [ref number]
  (let [newref (Reference. ref)
        query (.getQueryAsForm newref)]
    (.set query "page" (str number) true)
    (.setQuery newref (.encode query))
    newref))

(defn pagination-html [ref current total-hits]
  (let [last-page (int (Math/ceil (/ total-hits (float *page-size*))))
        window-start (if (< (- current *window-size*) 1)
                       1 (- current *window-size*))
        window-end (if (> (+ current *window-size*) last-page)
                     last-page (+ current *window-size*))]
    (with-out-str
      (print "<div class=\"pagination\"><ul>")
      (print "<li>Pages:</li>")
      (if (= current 1)
        (print "<li class=\"disablepage\">&#171; previous</li>")
        (printf "<li class=\"prevpage\"><a href=\"%s\">&#171; previous</a></li>"
                (str (page-ref ref (dec current)))))
      (when (not= window-start 1)
        (printf "<li><a href=\"%s\">1</a></li>" (str (page-ref ref 1)))
        (print "<li>...</li>"))
      (doseq [n (range window-start (inc window-end))]
        (if (= n current)
          (printf "<li class=\"currentpage\">%d</li>" n)
          (printf "<li><a href=\"%s\">%d</a></li>" (str (page-ref ref n)) n)))
      (when (not= window-end last-page)
        (print "<li>...</li>")
        (printf "<li><a href=\"%s\">%d</a></li>"
                (str (page-ref ref last-page)) last-page))
      (if (= current last-page)
        (print "<li class=\"disablepage\">next &#187;</li>")
        (printf "<li class=\"prevpage\"><a href=\"%s\">next &#187;</a></li>"
                (str (page-ref ref (inc current)))))
      (print "</ul></div>"))))

;;; RENDERING RESULTS

(defn prepare-hit [doc]
  (let [name (.getFieldValue doc "name")
        the-name (if (or (nil? name) (empty? name))
                   (str "Untitled #" (.getFieldValue doc "docid"))
                   name)]
  {:docid (.getFieldValue doc "docid")
   :url (format "/v1/cases/%s" (.getFieldValue doc "docid"))
   :size (when-let [size (.getFieldValue doc "size")]
           (int (Math/ceil (/ size 1024.0))))
   :date (date/format-long-date (.getFieldValue doc "date"))
   :court (courts/*court-names* (.getFieldValue doc "court"))
   :name (tmpl/h the-name)
   :citations (map tmpl/h (.getFieldValues doc "citations"))}))

(defn prepare-solr-results [solr-response]
  (let [docs (.getResults solr-response)]
    {:query (.. solr-response getHeader (get "params") (get "q"))
     :sort (.. solr-response getHeader (get "params") (get "sort"))
     :total_hits (.getNumFound docs)
     :start_hit (if (zero? (.getNumFound docs)) 0 (inc (.getStart docs)))
     :end_hit (+ (.getStart docs) (count docs))
{'fraction_non_alphanumeric': 0.1900224956993516, 'fraction_numerical': 0.007410348021701734, 'mean_word_length': 2.7158308751229105, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '4653684', 'n_tokens_mistral': 2612, 'n_tokens_neox': 2501, 'n_words': 639}