data.cljs 2.93 KB
Newer Older
1
(ns relevance.data
2 3
  (:require [relevance.utils :refer [url-key host-key hostname]]
            [khroma.log :as console]))
4 5


6 7 8 9 10
(defn accumulate-site-times
  "Accumulates the total time for a site from a hashmap of URL times"
  [url-times]
  (->>
    (group-by #(hostname (:url %)) (vals url-times))
11 12 13 14 15 16 17 18 19
    (remove #(empty? (key %)))
    (map #(vector (host-key (key %))
                  (hash-map :host (key %)
                            :time (apply + (map :time (val %)))
                            :icon (:icon (first (val %))))
                  ))
    (into {})
    )
  )
20 21


22
(defn clean-up-by-time
23 24 25
  "Removes from url-times all the items that are older than cut-off-ts
  and which were viewed for less than min-seconds"
  [url-times cut-off-ts min-seconds]
26
  (into {} (remove #(and (< (:ts (val %))
27
                            cut-off-ts)
28
                         (< (:time (val %)) min-seconds))
29 30
                   url-times)))

31 32 33 34 35 36 37 38
(defn clean-up-ignored
  "Removes from url-times all the items for which the domain
  matches an ignore set"
  [url-times ignore-set]
  (into {} (remove #(contains? ignore-set (hostname (:url (val %))))
                   url-times))
  )

39 40 41
(defn track-url-time
  "Receives a url time database, a tab record and a time to track, and returns
  new time database which is the result of adding the time to the URL. It also
42
  timestamps the record with the timestamp received."
43
  [url-times tab time timestamp & {:keys [ignore-set]}]
44
  (let [url      (or (:url tab) "")
45
        id       (url-key url)
46
        url-item (or (get url-times id)
47 48 49 50
                     {:url  url
                      :time 0
                      :ts   0})
        track?   (and (not= 0 id)
51
                      (not (contains? ignore-set (hostname url)))
52
                      (pos? time))
53
        new-item (assoc url-item :time (+ (:time url-item) time)
54
                                 :title (:title tab)
55
                                 :ts timestamp)]
56
    (if track?
57 58
      (assoc url-times id new-item)
      url-times)))
59 60 61 62 63 64 65


(defn track-site-time
  "Receives a site time database, a tab record and a time to track, and returns
  new time database which is the result of adding the time to the site. It also
  timestamps the record with the timestamp received, and adds the favIconUrl of
  the tab as the one for the entire site."
66
  [site-times tab time timestamp & {:keys [ignore-set]}]
67 68
  (let [host      (hostname (or (:url tab) ""))
        id        (host-key host)
69
        site-item (or (get site-times id)
70 71 72 73
                      {:host host
                       :time 0
                       :ts   0})
        track?    (and (not= 0 id)
74
                       (not (contains? ignore-set host))
75
                       (pos? time))
76
        new-item  (assoc site-item :time (+ (:time site-item) time)
77
                                   :icon (:favIconUrl tab)
78
                                   :ts timestamp)]
79
    (if track?
80
      (assoc site-times id new-item)
81
      site-times)))