This repository has been archived on 2019-12-12. You can view files and clone it, but cannot push or open issues/pull-requests.
tildetown-scripts/scripts/town_stats.hy

66 lines
2.2 KiB
Hy
Raw Normal View History

2015-07-26 06:23:01 +00:00
(import [json [dumps]])
2014-10-15 05:07:15 +00:00
(import [os [listdir]])
2014-11-09 01:09:28 +00:00
(import [os.path [getmtime]])
2014-10-15 05:07:15 +00:00
(import [datetime [datetime]])
2015-07-26 06:23:01 +00:00
(import [sh [find facter]])
2014-12-29 02:46:07 +00:00
2015-07-26 06:23:01 +00:00
;; this script emits json on standard out that has information about tilde.town
;; users. It denotes who has not updated their page from the default. It also
;; reports the time this script was run. The user list is sorted by public_html update time.
2014-10-15 05:07:15 +00:00
(defn slurp [filename]
2014-11-09 00:35:01 +00:00
(try
(.read (apply open [filename "r"] {"encoding" "utf-8"}))
(catch [e Exception]
"")))
2014-10-15 05:07:15 +00:00
(def default-html (slurp "/etc/skel/public_html/index.html"))
2015-07-26 06:23:01 +00:00
(defn default? [username]
(= default-html (slurp (.format "/home/{}/public_html/index.html" username))))
2014-10-15 05:07:15 +00:00
2015-02-13 21:22:09 +00:00
(defn bounded-find [path]
;; find might return 1 but still have worked fine (because of dirs it can't
;; go into)
(apply find [path "-maxdepth" "3"] {"_ok_code" [0 1]}))
2015-02-10 18:45:37 +00:00
2015-02-10 19:00:18 +00:00
(defn guarded-mtime [filename]
(let [[path (.rstrip filename)]]
(try
(getmtime path)
(catch [e Exception]
0))))
2014-11-09 01:09:28 +00:00
(defn modify-time [username]
2014-12-29 02:46:07 +00:00
(->> (.format "/home/{}/public_html" username)
2015-02-10 18:45:37 +00:00
bounded-find
2015-02-10 19:00:18 +00:00
(map guarded-mtime)
2014-12-29 02:46:07 +00:00
list
max))
2014-11-09 01:09:28 +00:00
(defn sort-user-list [usernames]
(apply sorted [usernames] {"key" modify-time}))
2014-11-09 01:16:11 +00:00
(defn user-generator [] (->> (listdir "/home")
2014-12-29 02:50:15 +00:00
(filter (fn [f] (and (not (= f "ubuntu")) (not (= f "poetry")))))))
2014-11-09 01:09:28 +00:00
2015-07-26 06:23:01 +00:00
(if (= __name__ "__main__")
2015-07-26 07:01:25 +00:00
(let [[all_users (->> (user-generator)
sort-user-list
reversed
(map (fn [un] {"username" un
"default" (default? un)}))
2015-07-26 07:03:17 +00:00
list)]
2015-07-26 07:01:25 +00:00
[live_users (-> (filter (fn [u] (not (get u "default"))) all_users)
2015-07-26 07:03:17 +00:00
list)]
2015-07-26 07:04:02 +00:00
[data {"all_users" all_users
2015-07-26 07:01:25 +00:00
"live_users" live_users
2015-07-26 06:23:01 +00:00
"active_user_count" (-> (. (facter "active_user_count") stdout)
.strip
int)
"generated_at" (.strftime (.now datetime) "%Y-%m-%d %H:%M:%S")
2015-07-26 07:04:02 +00:00
"num_users" (len all_users)
"num_live_users" (len live_users)}]]
2015-07-26 06:23:01 +00:00
(print (dumps data))))