<commit_before>; Copyright (C) 2013, 2014, 2015 Dr. <NAME>  (<EMAIL>, <EMAIL>)
; Licensed under the terms of the GNU Affero General Public License v3.
; See the "LICENSE.txt" file provided with this software.

(ns cider-ci.dispatcher.task
  (:require
    [cider-ci.dispatcher.job :as job]
    [cider-ci.dispatcher.result :as result]
    [cider-ci.dispatcher.stateful-entity :as stateful-entity]
    [drtom.logbug.debug :as debug]
    [cider-ci.utils.messaging :as messaging]
    [cider-ci.utils.rdbms :as rdbms]
    [drtom.logbug.catcher :as catcher]
    [clj-logging-config.log4j :as logging-config]
    [clojure.java.jdbc :as jdbc]
    [clojure.tools.logging :as logging]
    ))


;### utils ####################################################################
(defonce terminal-states #{"aborted" "failed" "passed"})

(defn get-task [id]
  (first (jdbc/query (rdbms/get-ds)
                                ["SELECT * FROM tasks
                                 WHERE id = ?" id])))
(defn get-task-spec [task-id]
  (let [ task_specifications (jdbc/query (rdbms/get-ds)
                                ["SELECT task_specifications.data FROM task_specifications
                                 JOIN tasks ON tasks.task_specification_id = task_specifications.id
                                 WHERE tasks.id = ?" task-id])
        spec (clojure.walk/keywordize-keys (:data (first task_specifications)))]
    spec))

(defn- get-trial-states [task]
  (let [id (:id task)]
    (map :state
         (jdbc/query (rdbms/get-ds)
                     ["SELECT state FROM trials
                      WHERE task_id = ?" id]))))

(defn- get-job-for-task [task]
  (->> (jdbc/query (rdbms/get-ds)
                   ["SELECT jobs.* FROM jobs
                    JOIN tasks ON tasks.job_id = jobs.id
                    WHERE tasks.id = ? " (:id task)]) first))


;### re-evaluate  #############################################################

(defn- eval-new-state [task trial-states]
  (cond (some #{"passed"} trial-states) "passed"
        (every? #{"aborted"} trial-states ) "aborted"
        (every? #{"failed" "aborted"} trial-states) "failed"
        (some #{"executing"} trial-states) "executing"
        (some #{"pending"} trial-states) "pending"
        (some #{"aborting"} trial-states) "aborting"
        (every? #{"aborted"} trial-states) "aborted"
        :else (do (logging/warn 'eval-new-state "Unmatched condition"
                                {:task task :trial-states trial-states})
                "failed")))

(defn- evaluate-trials-and-update
  "Returns a truthy value when the state of the task has changed."
  [task]
  (catcher/wrap-with-log-error
    (let [id (:id task)
          task (get-task id)
          trial-states (get-trial-states task)
          new-state (eval-new-state task trial-states)]
      (result/update-task-and-job-result id)
      (stateful-entity/update-state :tasks id new-state {:assert-existence true}))))


;### create trial #############################################################

(defn create-trial [task]
  (let [task-id (:id task)
        spec (get-task-spec task-id)
        scripts (:scripts spec) ]<commit_msg>Set task state to executing when trial is dispatching<commit_after>; Copyright (C) 2013, 2014, 2015 Dr. <NAME>  (<EMAIL>, <EMAIL>)
; Licensed under the terms of the GNU Affero General Public License v3.
; See the "LICENSE.txt" file provided with this software.

(ns cider-ci.dispatcher.task
  (:require
    [cider-ci.dispatcher.job :as job]
    [cider-ci.dispatcher.result :as result]
    [cider-ci.dispatcher.stateful-entity :as stateful-entity]
    [drtom.logbug.debug :as debug]
    [cider-ci.utils.messaging :as messaging]
    [cider-ci.utils.rdbms :as rdbms]
    [drtom.logbug.catcher :as catcher]
    [clj-logging-config.log4j :as logging-config]
    [clojure.java.jdbc :as jdbc]
    [clojure.tools.logging :as logging]
    ))


;### utils ####################################################################
(defonce terminal-states #{"aborted" "failed" "passed"})

(defn get-task [id]
  (first (jdbc/query (rdbms/get-ds)
                                ["SELECT * FROM tasks
                                 WHERE id = ?" id])))
(defn get-task-spec [task-id]
  (let [ task_specifications (jdbc/query (rdbms/get-ds)
                                ["SELECT task_specifications.data FROM task_specifications
                                 JOIN tasks ON tasks.task_specification_id = task_specifications.id
                                 WHERE tasks.id = ?" task-id])
        spec (clojure.walk/keywordize-keys (:data (first task_specifications)))]
    spec))

(defn- get-trial-states [task]
  (let [id (:id task)]
    (map :state
         (jdbc/query (rdbms/get-ds)
                     ["SELECT state FROM trials
                      WHERE task_id = ?" id]))))

(defn- get-job-for-task [task]
  (->> (jdbc/query (rdbms/get-ds)
                   ["SELECT jobs.* FROM jobs
                    JOIN tasks ON tasks.job_id = jobs.id
                    WHERE tasks.id = ? " (:id task)]) first))


;### re-evaluate  #############################################################

(defn- eval-new-state [task trial-states]
  (cond (some #{"passed"} trial-states) "passed"
        (every? #{"aborted"} trial-states ) "aborted"
        (every? #{"failed" "aborted"} trial-states) "failed"
        (some #{"executing" "dispatching"} trial-states) "executing"
        (some #{"pending"} trial-states) "pending"
        (some #{"aborting"} trial-states) "aborting"
        :else (do (logging/warn 'eval-new-state "Unmatched condition"
                                {:task task :trial-states trial-states})
                "failed")))

(defn- evaluate-trials-and-update
  "Returns a truthy value when the state of the task has changed."
  [task]
  (catcher/wrap-with-log-error
    (let [id (:id task)
          task (get-task id)
          trial-states (get-trial-states task)
          new-state (eval-new-state task trial-states)]
      (result/update-task-and-job-result id)
      (stateful-entity/update-state :tasks id new-state {:assert-existence true}))))


;### create trial #############################################################

(defn create-trial [task]
  (let [task-id (:id task)
        spec (get-task-spec task-id)
        scripts (:scripts spec) ]
{'fraction_non_alphanumeric': 0.19167061050638903, 'fraction_numerical': 0.004417100489036125, 'mean_word_length': 2.4400434074877917, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '3846234', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1921, 'n_words': 489}