<commit_before>(ns panpan.hestia
  (:require
    [clojure.string    :as str]
    [jubot.adapter     :as ja]
    [jubot.handler     :as jh]
    [jubot.scheduler   :as js]
    [panpan.util.match :refer :all]
    [panpan.toggl.api  :as toggl]))

(def ^:const NAME "ヘスティア") ; {{{
(def ^:const ICON "https://dl.dropboxusercontent.com/u/14918307/slack_icon/hestia.png")
(def ^:private out #(do (ja/out (apply str %&) :as NAME :icon-url ICON) nil))
(def ^:private pre #(str "```\n" % "\n```")) ; }}}

(def ^:const WORKSPACE_ID "141468")
(def ^:const REST_PID "3141045")
(def ^:const WARN_SEC (* 25 60)) ; 25 min
(def ^:const REST_WARN_SEC (* 5 60)) ; 5 min


(def ^:const MESSAGES ; {{{
  {:start-entry
   ["了解だよ！"
    "無理するなよ！"
    ]
   :stop-entry
   ["お疲れ！"
    "お疲れ様！"
    ]
   :delete-entry
   ["おっと、了解だよ！"
    "おっとゴメンよ！"
    ]
   :no-entry-to-delete
   ["ん？何が違うんだい？"]
   :start-rest
   ["オッケーだよ！"
    ]
   :reply
   ["ちょっとまって"
    ]
   :rest-warn
   ["そろそろ休憩終わりじゃないかい？"
    "いつまで休んでるんだい？"
    ]
   :warn
   ["そろそろ休憩したらどうだい？"
    "集中できてるかい？"
    ]
   :thanks
   ["どういたしまして！"
    "君のためだからね！"
    ]
   :sorry
   ["わかればいいんだよ"
    "次から気をつけるんだよ？"
    ]
   }) ; }}}

(defn- desc->pid ; {{{
  [desc]
  (jh/regexp {:text desc}
    #"cont"  (fn [& _] "870449")
    #"tdsei" (fn [& _] "8276549")
    #"sso"   (fn [& _] "3141182")
    #"ple"   (fn [& _] "3146901")
    #"(MTG|mtg|打ち合わせ|ミーティング)"
    (fn [& _] "3141739")
    #"(休憩|きゅうけい)"
    (fn [& _] "3141045")
    #"(ゲーム|げーむ)"
    (fn [& _] "9863311")
    #".*"
    (fn [& _] "8256158"))) ; }}}

(defn hestia-handler
  "^(.+?).*(を)?(開始|始め|はじめ)           - toggl開始
   ^(再開|さいかい)                          - toggl再開
   ^(終了|終わった|おわった|おわた)          - toggl停止
   ^(神様|神さま).*(違い|違う|消して|けして) - toggl削除
   ^(神様|神さま).*(休憩|一休み)             - toggl休憩エントリー開始
  "
  [{:keys [user] :as arg}]
  (jh/regexp arg
    #"^(.+?).*(を)?(開始|始め|はじめ)"
    (matchfn [desc]
      (toggl/start-entry desc :pid (desc->pid desc))
      (->> MESSAGES :start-entry rand-nth (out "@" user " ")))
    #"^(再開|さいかい)"
    (matchfn []
      (when-let [entry (toggl/get-last-entry)]
        (hestia-handler
          (assoc arg :text (str (:description entry) "開始")))
        nil))
    #"^(終了|終わった|おわった|おわた)"
    (matchfn []
      (when (toggl/stop-entry)
        (->> MESSAGES :stop-entry rand-nth (out "@" user " "))))
    #"^(神様|神さま).*(違い|違う|消して|けして)"
    (matchfn []
      (let [key (if (toggl/delete-entry) :delete-entry :no-entry-to-delete)]
        (->> MESSAGES
             key
             rand-nth
             (out "@" user " "))))
    #"^(神様|神さま).*(休憩|一休み)"
    (matchfn []
      (->> MESSAGES :start-rest rand-nth (out "@" user " "))
      (toggl/start-entry "休憩" :pid REST_PID)
      nil)

    #"^(神様|神さま).*ありがと"
    (matchfn []
      (->> MESSAGES :thanks rand-nth (out "@" user " ")))

    #"^(神様|神さま).*(ごめん|ゴメン)"
    (matchfn []
      (->> MESSAGES :sorry rand-nth (out "@" user " ")))

    #"^(神様|神さま).*プロジェクト"
    (matchfn []
      (->> MESSAGES :reply rand-nth (out "@" user " "))
      (->> (toggl/get-projects WORKSPACE_ID)
           (map #(str (:name %) " (" (:id %) ")"))
           (str/join "\n")
           pre out))
    ))

(def hestia-schedule
  (js/schedules
    "0 /5 * * * * *"
    #(when-let [{:keys [pid sec]} (toggl/get-running-entry)]
       (cond
         (and (= (str pid) REST_PID) (> sec REST_WARN_SEC))
         (->> MESSAGES :rest-warn rand-nth (out "@uochan "))

         (> sec WARN_SEC)
         (->> MESSAGES :warn rand-nth (out "@uochan "))))
    ))<commit_msg>do not check running entry when entry is not started by bot<commit_after>(ns panpan.hestia
  (:require
    [clojure.string    :as str]
    [jubot.adapter     :as ja]
    [jubot.handler     :as jh]
    [jubot.brain       :as jb]
    [jubot.scheduler   :as js]
    [panpan.util.match :refer :all]
    [panpan.toggl.api  :as toggl]))

(def ^:const NAME "ヘスティア") ; {{{
(def ^:const ICON "https://dl.dropboxusercontent.com/u/14918307/slack_icon/hestia.png")
(def ^:private out #(do (ja/out (apply str %&) :as NAME :icon-url ICON) nil))
(def ^:private pre #(str "```\n" % "\n```")) ; }}}

(def ^:const WORKSPACE_ID "141468")
(def ^:const REST_PID "3141045")
(def ^:const WARN_SEC (* 25 60)) ; 25 min
(def ^:const REST_WARN_SEC (* 5 60)) ; 5 min
(def ^:const RUNNING_KEY "toggl_running")


(def ^:const MESSAGES ; {{{
  {:start-entry
   ["了解だよ！"
    "無理するなよ！"
    ]
   :stop-entry
   ["お疲れ！"
    "お疲れ様！"
    ]
   :delete-entry
   ["おっと、了解だよ！"
    "おっとゴメンよ！"
    ]
   :no-entry-to-delete
   ["ん？何が違うんだい？"]
   :start-rest
   ["オッケーだよ！"
    ]
   :reply
   ["ちょっとまって"
    ]
   :rest-warn
   ["そろそろ休憩終わりじゃないかい？"
    "いつまで休んでるんだい？"
    ]
   :warn
   ["そろそろ休憩したらどうだい？"
    "集中できてるかい？"
    ]
   :thanks
   ["どういたしまして！"
    "君のためだからね！"
    ]
   :sorry
   ["わかればいいんだよ"
    "次から気をつけるんだよ？"
    ]
   }) ; }}}

(defn- desc->pid ; {{{
  [desc]
  (jh/regexp {:text desc}
    #"cont"  (fn [& _] "870449")
    #"tdsei" (fn [& _] "8276549")
    #"sso"   (fn [& _] "3141182")
    #"ple"   (fn [& _] "3146901")
    #"(MTG|mtg|打ち合わせ|ミーティング)"
    (fn [& _] "3141739")
    #"(休憩|きゅうけい)"
    (fn [& _] "3141045")
    #"(ゲーム|げーむ)"
    (fn [& _] "9863311")
    #".*"
    (fn [& _] "8256158"))) ; }}}

(defn hestia-handler
  "^(.+?).*(を)?(開始|始め|はじめ)           - toggl開始
   ^(再開|さいかい)                          - toggl再開
   ^(終了|終わった|おわった|おわた)          - toggl停止
   ^(神様|神さま).*(違い|違う|消して|けして) - toggl削除
   ^(神様|神さま).*(休憩|一休み)             - toggl休憩エントリー開始
  "
  [{:keys [user] :as arg}]
  (jh/regexp arg
    #"^(.+?).*(を)?(開始|始め|はじめ)"
    (matchfn [desc]
      (toggl/start-entry desc :pid (desc->pid desc))
      (jb/set RUNNING_KEY "true")
      (->> MESSAGES :start-entry rand-nth (out "@" user " ")))
    #"^(再開|さいかい)"
    (matchfn []
      (when-let [entry (toggl/get-last-entry)]
        (hestia-handler
          (assoc arg :text (str (:description entry) "開始")))
        nil))
    #"^(終了|終わった|おわった|おわた)"
    (matchfn []
      (when (toggl/stop-entry)
        (jb/set RUNNING_KEY nil)
        (->> MESSAGES :stop-entry rand-nth (out "@" user " "))))
    #"^(神様|神さま).*(違い|違う|消して|けして)"
    (matchfn []
      (let [key (if (toggl/delete-entry) :delete-entry :no-entry-to-delete)]
        (jb/set RUNNING_KEY nil)
        (->> MESSAGES
             key
             rand-nth
             (out "@" user " "))))
    #"^(神様|神さま).*(休憩|一休み)"
    (matchfn []
      (->> MESSAGES :start-rest rand-nth (out "@" user " "))
      (jb/set RUNNING_KEY "true")
      (toggl/start-entry "休憩" :pid REST_PID)
      nil)

    #"^(神様|神さま).*ありがと"
    (matchfn []
      (->> MESSAGES :thanks rand-nth (out "@" user " ")))

    #"^(神様|神さま).*(ごめん|ゴメン)"
    (matchfn []
      (->> MESSAGES :sorry rand-nth (out "@" user " ")))

    #"^(神様|神さま).*プロジェクト"
    (matchfn []
      (->> MESSAGES :reply rand-nth (out "@" user " "))
      (->> (toggl/get-projects WORKSPACE_ID)
           (map #(str (:name %) " (" (:id %) ")"))
           (str/join "\n")
           pre out))
    ))

(def hestia-schedule
  (js/schedules
    "0 /5 * * * * *"
    #(when (jb/get RUNNING_KEY)
       (when-let [{:keys [pid sec]} (toggl/get-running-entry)]
         (cond
           (and (= (str pid) REST_PID) (> sec REST_WARN_SEC))
           (->> MESSAGES :rest-warn rand-nth (out "@uochan "))

           (> sec WARN_SEC)
           (->> MESSAGES :warn rand-nth (out "@uochan ")))))
    ))
{'fraction_non_alphanumeric': 0.22314840120251433, 'fraction_numerical': 0.02405028696365127, 'mean_word_length': 2.197466142420271, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 2, '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': '3999452', 'n_tokens_mistral': 3763, 'n_tokens_neox': 3577, 'n_words': 567}