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/tildetown-py/stats.py

78 lines
2.4 KiB
Python
Raw Normal View History

2015-07-28 22:05:52 +00:00
import json
from functools import partial
from os import listdir
from os.path import getmtime, join
from datetime import datetime
from sh import find, facter
2015-07-29 00:33:42 +00:00
from util import slurp, thread, p
2015-07-28 22:05:52 +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.
DEFAULT_HTML = slurp("/etc/skel/public_html/index.html")
username_to_html_path = lambda u: "/home/{}/public_html".format(u)
def default_p(username):
return DEFAULT_HTML == slurp(join(username_to_html_path(username), "index.html"))
def bounded_find(path):
# find might return 1 but still have worked fine.
return find(path, "-maxdepth" "3", _ok_code=[0,1])
def guarded_mtime(path):
try:
return getmtime(path.rstrip())
2015-07-29 00:28:21 +00:00
except Exception as _:
2015-07-28 22:05:52 +00:00
return 0
def modify_time(username):
2015-07-29 00:33:42 +00:00
p(username)
2015-07-28 22:05:52 +00:00
files_to_mtimes = partial(map, guarded_mtime)
return thread(username,
username_to_html_path,
bounded_find,
files_to_mtimes,
2015-07-29 00:33:42 +00:00
p
2015-07-28 22:05:52 +00:00
list,
max)
def sort_user_list(usernames):
return sorted(usernames, key=modify_time)
def user_generator():
ignore_system_users = lambda f: f != "ubuntu" and f != "poetry"
return filter(ignore_system_users, listdir("/home"))
def get_user_data():
username_to_data = lambda u: {'username': u,
'default': default_p(u),
'favicon':'TODO'}
live_p = lambda user: not user['default']
all_users = thread(user_generator(),
sort_user_list,
reversed,
partial(map, username_to_data),
list)
live_users = list(filter(live_p, all_users))
active_user_count = int(facter("active_user_count").stdout.strip())
return {'all_users': all_users,
'num_users': len(all_users),
'num_live_users': len(live_users),
'active_user_count': active_user_count,
'live_users': live_users,}
def get_data():
user_data = get_user_data()
data = {'generated_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
2015-07-28 22:17:13 +00:00
data.update(user_data)
return data
2015-07-28 22:05:52 +00:00
if __name__ == '__main__':
print(json.dumps(get_data()))