 
   (:require
    [clojure.string :as str]
+   [clojure.pprint :refer [pprint]]
    [clojure.set :as set]
    [clojure.data.json :as json]
    [clojure.java.io :as io]








(defn htseq-file-get [args reqmap]
  (infof "Args %s\nParams %s" args (reqmap :params))
  #_(infof "ReqMap %s" reqmap)
  (binding [*ns* (find-ns 'aerobio.server)]
    (let [params (reqmap :params)
          {:keys [user cmd action rep compfile eid]} params
          eid (if (str/ends-with? eid "/") (austr/butlast 1 eid) eid)
          eid (if (str/starts-with? eid "/") (austr/drop 1 eid) eid)
          user-agent (get-in reqmap [:headers "user-agent"])
          reqtype (get-in reqmap [:headers "reqtype"])]
      (if (= reqtype "cmdline")
        (try
          (let [_ (cmn/set-exp eid)
                exp (cmn/get-exp-info eid :exp)
                work-item (if action action cmd)
                tempnm (str (name exp) "-" work-item "-job-template")
                template (get-jobinfo tempnm)
                result (actions/action cmd eid params get-toolinfo template)]
            (aerial.utils.misc/sleep 250)
            (swap! dbg (fn[M] (assoc M eid result)))
            (infof "%s : %s" cmd result)
            (json/json-str
             {:params params
              :result (str result)
              :template tempnm
              :recipient user}))
          (catch Error e
            (let [estg (format "Error %s" (or (.getMessage e) e))]
              (errorf "%s: %s - Assert: %s" eid cmd estg)
              (json/json-str
             {:args args
              :params params
              :result estg
              :recipient user
              :user-agent user-agent
              :reqtype reqtype})))
          (catch Exception e
            (let [emsg (or (.getMessage e) e)]
              (errorf "%s: %s, Exception on Job launch: %s" eid cmd emsg)
              (json/json-str
               {:args args
                :params params
                :result (format "Exception: %s" emsg)
                :recipient user
                :user-agent user-agent
                :reqtype reqtype}))))
        (hiccup/html
         [:h1 "HI from HTSeq command route"]
         [:hr]
         [:p (str "command and args: " args)]
         [:hr]
         [:p (str "Params" params)]
         [:hr]
         [:p (str "User Agent: " user-agent)])))))

  (infof "ERROR: unknown user message %s" msg)
  #_(srv/send-msg
     (msg :ws) {:op "error" :payload "ERROR: unknown user message"}))


(defmethod user-msg :status [msg]
  (let [params (msg :params)
        {:keys [ws user eid action]} params]
    (let [jinfo (hc/get-db (atom (valuefy @job-db)) [user eid action])
          result (if (seq jinfo)
                   (->> jinfo (sort-by #(-> % :request :timestamp))
                        (mapv #(vector (-> % :request :timestamp)
                                       (% :status)
                                       (% :result))))
                   (format "No job information for [%s %s %s]"
                           user eid action))
          retmsg (if (string? result)
                   result
                   (str/join
                    "\n"
                    (mapv #(let [timestamp (first %)
                                 status (second %)
                                 res (last %)
                                 rstg (if (= res :future-not-yet-finished)
                                        (format
                                         "Currently still running:\n%s"
                                         (with-out-str (pprint status)))
                                        (format
                                         "Finished:\n%s\nFinal result:\n%s"
                                         (with-out-str (pprint status))
                                         (with-out-str (pprint res))))]
                             (with-out-str
                               (print (format "%s : %s started @ %s\n%s"
                                              eid action timestamp rstg))))
                          result)))]
      (send-end-msg ws {:op :status :payload retmsg}))))


(defmethod user-msg :run [msg]
  (let [params (msg :params)
