<commit_before>(ns tictag.db
  (:require [com.stuartsierra.component :as component]
            [taoensso.timbre :as timbre]
            [clojure.string :as str]
            [clojure.data.csv :as csv]
            [clojure.java.io :as io]
            [tictag.beeminder :as beeminder]
            [tictag.config :as config :refer [config]]
            [tictag.tagtime :as tagtime]
            [clojure.java.jdbc :as j]
            [tictag.utils :as utils]))

(defn table-exists? [db name]
  (seq
   (j/query db ["select name from sqlite_master where type='table' and name=?" name])))

(defn create-pings! [db]
  (j/execute! db
              [(j/create-table-ddl :pings
                                   [[:timestamp :integer
                                     :primary :key]
                                    [:tags :text]
                                    [:local_time :text]])]))
(defrecord Database [file tagtime]
  component/Lifecycle
  (start [component]
    (timbre/debug "Starting database")
    (let [db-spec {:dbtype "sqlite" :dbname file}]
      (when-not (table-exists? db-spec "pings")
        (timbre/debug "Pings table does not exist, creating...")
        (create-pings! db-spec))
      (assoc component
             :db db-spec
             :pends (atom {}))))
  (stop [component]
    (dissoc component :db)))

(defn insert-tag! [db long-time tags & [local-time]]
  (j/execute!
   db
   ["insert or replace into pings (\"timestamp\", \"tags\", \"local_time\") VALUES (?, ?, ?)"
    long-time
    (str/join " " tags)
    (or local-time (utils/local-time long-time))]))

(defn add-pending! [{:keys [pends db]} long-time id]
  (insert-tag! db long-time ["afk"])
  (swap! pends assoc id long-time))

(defn pending-timestamp [{:keys [pends]} id]
  (get @pends id))

(defn local-day [local-time] (str/replace (subs local-time 0 10) #"-" ""))

(defn to-ping [{:keys [local_time timestamp tags]}]
  {:tags (set (str/split tags #" "))
   :local-time local_time
   :local-day (local-day local_time)
   :timestamp timestamp})

(defn get-pings [db]
  (map to-ping (j/query db ["select * from pings"])))

(defn add-tags [{db :db} long-time tags local-time]
  (insert-tag! db long-time tags local-time)
  (beeminder/sync! {:auth-token (:beeminder-auth-token config)}
                   (:beeminder-user config)
                   (:beeminder-goals config)
                   (get-pings db)))


(defn is-ping? [{tagtime :tagtime} long-time]
  (tagtime/is-ping? tagtime long-time))

(defn pings
  "An infinite list of pings from tagtime"<commit_msg>pull tags as a set of keywords<commit_after>(ns tictag.db
  (:require [com.stuartsierra.component :as component]
            [taoensso.timbre :as timbre]
            [clojure.string :as str]
            [clojure.data.csv :as csv]
            [clojure.java.io :as io]
            [tictag.beeminder :as beeminder]
            [tictag.config :as config :refer [config]]
            [tictag.tagtime :as tagtime]
            [clojure.java.jdbc :as j]
            [tictag.utils :as utils]))

(defn table-exists? [db name]
  (seq
   (j/query db ["select name from sqlite_master where type='table' and name=?" name])))

(defn create-pings! [db]
  (j/execute! db
              [(j/create-table-ddl :pings
                                   [[:timestamp :integer
                                     :primary :key]
                                    [:tags :text]
                                    [:local_time :text]])]))
(defrecord Database [file tagtime]
  component/Lifecycle
  (start [component]
    (timbre/debug "Starting database")
    (let [db-spec {:dbtype "sqlite" :dbname file}]
      (when-not (table-exists? db-spec "pings")
        (timbre/debug "Pings table does not exist, creating...")
        (create-pings! db-spec))
      (assoc component
             :db db-spec
             :pends (atom {}))))
  (stop [component]
    (dissoc component :db)))

(defn insert-tag! [db long-time tags & [local-time]]
  (j/execute!
   db
   ["insert or replace into pings (\"timestamp\", \"tags\", \"local_time\") VALUES (?, ?, ?)"
    long-time
    (str/join " " tags)
    (or local-time (utils/local-time long-time))]))

(defn add-pending! [{:keys [pends db]} long-time id]
  (insert-tag! db long-time ["afk"])
  (swap! pends assoc id long-time))

(defn pending-timestamp [{:keys [pends]} id]
  (get @pends id))

(defn local-day [local-time] (str/replace (subs local-time 0 10) #"-" ""))

(defn to-ping [{:keys [local_time timestamp tags]}]
  {:tags (set (map keyword (str/split tags #" ")))
   :local-time local_time
   :local-day (local-day local_time)
   :timestamp timestamp})

(defn get-pings [db]
  (map to-ping (j/query db ["select * from pings"])))

(defn add-tags [{db :db} long-time tags local-time]
  (insert-tag! db long-time tags local-time)
  (beeminder/sync! {:auth-token (:beeminder-auth-token config)}
                   (:beeminder-user config)
                   (:beeminder-goals config)
                   (get-pings db)))


(defn is-ping? [{tagtime :tagtime} long-time]
  (tagtime/is-ping? tagtime long-time))

(defn pings
  "An infinite list of pings from tagtime"
{'fraction_non_alphanumeric': 0.15255886359213855, 'fraction_numerical': 0.0011675423234092236, 'mean_word_length': 2.234738829452486, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '761438', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1719, 'n_words': 463}