Last active
December 18, 2016 11:32
-
-
Save asqd/348ed5e9087c8da8e8e3843edff55d1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import datetime | |
def humanize_time(time, units): | |
INTERVALS = [1, 60, 3600, 86400, 604800, 2419200, 29030400] | |
NAMES = [('second', 'seconds'), | |
('minute', 'minutes'), | |
('hour', 'hours'), | |
('day', 'days'), | |
('week', 'weeks'), | |
('month', 'months'), | |
('year', 'years')] | |
res = [] | |
unit = list(map(lambda a: a[1], NAMES)).index(units) | |
# Convert to seconds | |
seconds = time * INTERVALS[unit] | |
for i in range(len(NAMES)-1, -1, -1): | |
a = seconds // INTERVALS[i] | |
if a > 0: | |
res.append("%s %s" % (a, NAMES[i][1 % a])) | |
seconds -= a * INTERVALS[i] | |
time_str = ", ".join(res) | |
return time_str | |
def data_from_str(dstr): | |
return datetime.datetime.strptime(dstr, "%d %b %Y - %H:%M:%S") | |
def time_diff(log_line): | |
if "date_start_time" in log_line and "date_end_time" in log_line: | |
start = data_from_str(log_line["date_start_time"]) | |
end = data_from_str(log_line["date_end_time"]) | |
return (end - start).seconds | |
else: | |
return 0 | |
# white_list is case insensitive | |
def get_stats(log_json, white_list=[]): | |
stats = dict() | |
for line in log_json: | |
skip_stat = len(white_list) !=0 and "ActiveText" in line and all([el.lower() not in line.get("ActiveText", "").lower() for el in white_list]) | |
if "ActiveText" in line and not skip_stat: | |
key = line.get("ActiveText", "").split("- ")[-1] | |
stats[key] = stats.get(key, 0) + time_diff(line) | |
return stats | |
def humanize_stats(stats, units= "seconds"): | |
humanize_stats = stats | |
for k in stats: | |
humanize_stats[k] = humanize_time(humanize_stats[k], units) | |
return humanize_stats | |
### | |
### How to use | |
### | |
# path to log | |
log_file = "activity.log" | |
# open log file to json | |
with open(log_file, encoding="utf-8") as f: | |
log_json = ([json.loads(line, encoding="utf-8") for line in f]) | |
#get stats from json | |
stats = get_stats(log_json) | |
whitelisted_stats = get_stats(log_json, white_list=['Opera','sublime']) | |
# print stats with time in seconds by each process | |
print("raw stats") | |
print(stats) | |
# pring stats for apps in whitelist | |
print("\nonly allowed stats") | |
print(whitelisted_stats) | |
# print stats with time in humanize_time by each process | |
print("\nhumanize_stats") | |
print(humanize_stats(stats)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment