<commit_before>(ns ruuvi-server.database.event-dao
  (:use korma.db)
  (:use korma.core)
  (:use ruuvi-server.database.entities)
  (:require [ruuvi-server.configuration :as conf])
  (:use [clojure.tools.logging :only (debug info warn error)])
  (:require [ruuvi-server.util :as util])
  (:import org.joda.time.DateTime)
  )

;; private functions
(defn- to-sql-timestamp [date]
  (if date
    (java.sql.Timestamp. (.getMillis date))
    nil))
  
(defn- current-sql-timestamp []
  (to-sql-timestamp (org.joda.time.DateTime)))

;; public functions
(defn get-trackers [ids]
  (select tracker
          (where (in :id ids))))

(defn get-tracker-by-code [tracker-code]
  (first (select tracker
                 (where {:tracker_code tracker-code}))))

;; TODO add caching
(defn get-tracker-by-code! [tracker-code & tracker-name]
  (let [existing-tracker (get-tracker-by-code tracker-code)]
    (if existing-tracker
      existing-tracker
      (insert tracker (values {:tracker_code tracker-code
                               :name (or tracker-name tracker-code)}))
      )))

(defn get-all-trackers []
  (select tracker)
  )

(defn get-tracker [id]
  ;; TODO support also fetching with tracker_indentifier?
  ;; duplicate with get-trackers
  (first (select tracker
                 (where {:id id}))))  

;; TODO
(defn get-event-sessions [{:keys [tracker_ids event_session_ids]}]
  (let [tracker-ids-crit (when tracker_ids {:tracker_id ['in tracker_ids]})
        session-ids-crit (when event_session_ids {:id ['in event_session_ids]})
        conditions (filter identity (list tracker-ids-crit session-ids-crit))]
    (select event-session
            (where (apply and conditions)))))


(defn- update-tracker-latest-activity [id]
  (update tracker
          (set-fields {:latest_activity (java.sql.Timestamp. (System/currentTimeMillis)) })
          (where {:id id})))

(defn create-tracker [code name shared-secret]
  (info "Create new tracker" name "(" code ")")
  (insert tracker (values
                   {:tracker_code code
                    :shared_secret shared-secret
                    :name name})))

(defn- get-event-session-for-code [tracker-id session-code]
  (first (select event-session
                 (where {:tracker_id tracker-id
                         :session_code session-code})))
  )

(defn- get-event-session-for-code! [tracker-id session-code & timestamp]
  (let [existing-session (get-event-session-for-code tracker-id session-code)]
    (if existing-session
      ;; update latest timestamp, if timestamp != null<commit_msg>Handle tracker codes as lowercase. Fix #55.<commit_after>(ns ruuvi-server.database.event-dao
  (:use korma.db)
  (:use korma.core)
  (:use ruuvi-server.database.entities)
  (:require [ruuvi-server.configuration :as conf])
  (:use [clojure.tools.logging :only (debug info warn error)])
  (:require [ruuvi-server.util :as util])
  (:import org.joda.time.DateTime)
  (:require [clojure.string :as string])
  )

;; private functions
(defn- to-sql-timestamp [date]
  (if date
    (java.sql.Timestamp. (.getMillis date))
    nil))
  
(defn- current-sql-timestamp []
  (to-sql-timestamp (org.joda.time.DateTime)))

;; public functions
(defn get-trackers [ids]
  (select tracker
          (where (in :id ids))))

(defn get-tracker-by-code [tracker-code]
  (let [tracker-code (string/lower-case tracker-code)]
    (first (select tracker
                   (where {:tracker_code tracker-code})))))

;; TODO add caching
(defn get-tracker-by-code! [tracker-code & tracker-name]
  (let [tracker-code (string/lower-case tracker-code)
        existing-tracker (get-tracker-by-code tracker-code)]
    (if existing-tracker
      existing-tracker
      (insert tracker (values {:tracker_code tracker-code
                               :name (or tracker-name tracker-code)})))))

(defn get-all-trackers []
  (select tracker)
  )

(defn get-tracker [id]
  ;; TODO support also fetching with tracker_indentifier?
  ;; duplicate with get-trackers
  (first (select tracker
                 (where {:id id}))))  

;; TODO
(defn get-event-sessions [{:keys [tracker_ids event_session_ids]}]
  (let [tracker-ids-crit (when tracker_ids {:tracker_id ['in tracker_ids]})
        session-ids-crit (when event_session_ids {:id ['in event_session_ids]})
        conditions (filter identity (list tracker-ids-crit session-ids-crit))]
    (select event-session
            (where (apply and conditions)))))


(defn- update-tracker-latest-activity [id]
  (update tracker
          (set-fields {:latest_activity (java.sql.Timestamp. (System/currentTimeMillis)) })
          (where {:id id})))

(defn create-tracker [code name shared-secret]
  (let [tracker-code (string/lower-case code)]
    (info "Create new tracker" name "(" tracker-code ")")
    (insert tracker (values
                     {:tracker_code tracker-code
                      :shared_secret shared-secret
                      :name name}))))

(defn- get-event-session-for-code [tracker-id session-code]
  (first (select event-session
                 (where {:tracker_id tracker-id
                         :session_code session-code})))
  )

(defn- get-event-session-for-code! [tracker-id session-code & timestamp]
  (let [existing-session (get-event-session-for-code tracker-id session-code)]
    (if existing-session
      ;; update latest timestamp, if timestamp != null
{'fraction_non_alphanumeric': 0.13851603281133482, 'fraction_numerical': 0.0003728560775540641, 'mean_word_length': 2.9075018208302987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3505963', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1693, 'n_words': 416}