<commit_before>(ns polismath.components.config
  (:require [polismath.utils :as utils]
            [clojure.tools.logging :as log]
            [com.stuartsierra.component :as component]
            [environ.core :as environ]
            ))


(defn ->long [x]
  (try
    (Long/parseLong x)
    (catch Exception e
      (when-not (= x "")
        ;; Otherwise, assume nil was intended...
        (log/warn "Failed to parse ->long:" x))
      nil)))

(defn ->keyword [x]
  ;; Otherwise want to return nil, so merging works
  (when (and x (not (= x "")))
    (keyword x)))

(def defaults
  {:nrepl-port 12345
   :math-env   :dev
   :darwin     {:server-port 3123}
   ;; XXX Hmm... How do we express a dependency here?
   :primary-polis-url :localhost ;; Must do it in the component load...
   :database   {:pool-size 3}
   :math-schema-date "2014_08_22"
   })

(def rules
  "Mapping of env keys to parsing options"
  {:nrepl-port                 {:parse ->long}
   :math-env                   {:parse ->keyword}
   ;; Have to use :port since that's what heroku expects...
   :port                       {:path [:darwin :server-port] :parse ->long}
   :database-url               {:path [:database :url]}
   :database-for-reads-name    {:path [:database :reads-name]}
   :database-pool-size         {:path [:database :pool-size] :parse ->long}
   :mongo-url                  {:path [:mongo :url]}
   :mailgun-api-key            {:path [:email :api-key]}
   :mailgun-url                {:path [:email :url]}
   :primary-polis-url          {:path [:email :api-key]}
   :math-matrix-implementation {:path [:math :matrix-implementation] :parse ->keyword}
   :math-cutoff-medium         {:path [:math :cutoffs :medium] :parse ->long
                                :doc "This is the maximum size of a conversation before running in :medium mode"}
   :math-cutoff-large          {:path [:math :cutoffs :large] :parse ->long
                                :doc "This is the maximum size of a conversation before running in :large mode"}
   :math-cutoff-max-ptpts      {:path [:math :cutoffs :max-ptpts] :parse ->long
                                :doc "This is the maximum number of participants before the conversation stops accepting new participants"}
   :math-cutoff-max-cmnts      {:path [:math :cutoffs :max-ptpts] :parse ->long
                                :doc "This is the maximum number of comments before the conversation stops accepting new comments"}
   :math-schema-date           {:doc "This helps us version our mongo buckets."}
   ;; Should change these to be more abstract in key name; not hostedgraphite-apikey; just graphite-apikey etc XXX
   :hostedgraphite-apikey      {:path [:meta :graphite :api-key]
                                :doc "API key for graphite db (perf monitoring)"}
   :hostedgraphite-hostname    {:path [:meta :graphite :hostname]
                                :doc "The hostname for sending messages to graphite"}
   ;; XXX TODO & Thoughts
   ;; Mini batch sizes (see polismath.math.conversation)
   })

(defn get-environ-config [rules env]
  (reduce<commit_msg>Added export expiry days to config<commit_after>(ns polismath.components.config
  (:require [polismath.utils :as utils]
            [clojure.tools.logging :as log]
            [com.stuartsierra.component :as component]
            [environ.core :as environ]))


(defn ->long [x]
  (try
    (Long/parseLong x)
    (catch Exception e
      (when-not (= x "")
        ;; Otherwise, assume nil was intended...
        (log/warn "Failed to parse ->long:" x))
      nil)))

(defn ->keyword [x]
  ;; Otherwise want to return nil, so merging works
  (when (and x (not (= x "")))
    (keyword x)))

(def defaults
  {:nrepl-port 12345
   :math-env   :dev
   :darwin     {:server-port 3123}
   ;; XXX Hmm... How do we express a dependency here?
   :primary-polis-url :localhost ;; Must do it in the component load...
   :database   {:pool-size 3}
   :math-schema-date "2014_08_22"
   :export-expiry-days 10
   })

(def rules
  "Mapping of env keys to parsing options"
  {:nrepl-port                 {:parse ->long}
   :math-env                   {:parse ->keyword}
   ;; Have to use :port since that's what heroku expects...
   :port                       {:path [:darwin :server-port] :parse ->long}
   :database-url               {:path [:database :url]}
   :database-for-reads-name    {:path [:database :reads-name]}
   :database-pool-size         {:path [:database :pool-size] :parse ->long}
   :mongo-url                  {:path [:mongo :url]}
   :mailgun-api-key            {:path [:email :api-key]}
   :mailgun-url                {:path [:email :url]}
   :primary-polis-url          {:path [:email :api-key]}
   :math-matrix-implementation {:path [:math :matrix-implementation] :parse ->keyword}
   :math-cutoff-medium         {:path [:math :cutoffs :medium] :parse ->long
                                :doc "This is the maximum size of a conversation before running in :medium mode"}
   :math-cutoff-large          {:path [:math :cutoffs :large] :parse ->long
                                :doc "This is the maximum size of a conversation before running in :large mode"}
   :math-cutoff-max-ptpts      {:path [:math :cutoffs :max-ptpts] :parse ->long
                                :doc "This is the maximum number of participants before the conversation stops accepting new participants"}
   :math-cutoff-max-cmnts      {:path [:math :cutoffs :max-ptpts] :parse ->long
                                :doc "This is the maximum number of comments before the conversation stops accepting new comments"}
   :math-schema-date           {:doc "This helps us version our mongo buckets."}
   ;; Should change these to be more abstract in key name; not hostedgraphite-apikey; just graphite-apikey etc XXX
   :hostedgraphite-apikey      {:path [:meta :graphite :api-key]
                                :doc "API key for graphite db (perf monitoring)"}
   :hostedgraphite-hostname    {:path [:meta :graphite :hostname]
                                :doc "The hostname for sending messages to graphite"}
   :export-expiry-days         {:path [:export :expiry-days] :parse ->long
                                :doc "The number of days before a mongo record representing a data exports gets removed"}
   ;; XXX TODO & Thoughts
   ;; Mini batch sizes (see polismath.math.conversation)
   })

(defn get-environ-config [rules env]
  (reduce
{'fraction_non_alphanumeric': 0.12280429037773978, 'fraction_numerical': 0.005907041815638116, 'mean_word_length': 2.1632251720747298, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4210698', 'n_tokens_mistral': 1979, 'n_tokens_neox': 1885, 'n_words': 635}