Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Dump stats.ttb to CSV for PivotTable analysis
#!/usr/bin/env python
from datetime import datetime
import os
def get_dt(cm_str):
if len(cm_str) == 13:
t = float(cm_str)/1000
t = int(cm_str)
ts = datetime.fromtimestamp(t)
return ts
#dts = ts.strftime("%Y-%m-%d %H:%M:%S.%f")
#return dts
class Table(object):
cols = ["id", "date", "time", "hour", "min", "sec", "active",
"app_name", "url", "title", "short_url", "short_title",
"last_query", "length(sec)"]
def __init__(self):
self.rows = [self.cols]
self.last_query = ""
def empty_row(self, dt):
d = {k: "" for k in self.cols}
d['id'] = len(self.rows) - 1
d['date'] = dt.strftime("%Y-%m-%d")
d['time'] = dt.strftime("%H:%M:%S")
t = {'hour':dt.hour, 'min': dt.minute, 'sec': dt.second}
return d
def last_query_params(self, url):
from urlparse import urlparse, parse_qs
parsed = urlparse(url)
# google
print parsed
str_len = len("")
if parsed.netloc and len(parsed.netloc) >= str_len and parsed.netloc[-str_len:] == "":
params = parse_qs(parsed.query)
if "q" in params.keys():
print "params[q]", params["q"]
self.last_query = params["q"][0].replace("+", " ")
print "last_query", self.last_query
return self.last_query
def add_app(self, dt, app_name, url, title, length_sec):
d = self.empty_row(dt)
d['url'] = url
d['title'] = title
d['app_name'] = app_name
d['short_url'] = shorten(url)
d['short_title'] = shorten(title)
d['length(sec)'] = length_sec
d['active'] = "active"
d['last_query'] = self.last_query_params(url)
print "adding", d
self.rows.append([d[i] for i in self.cols])
def add_idle(self, dt, length_sec):
d = self.empty_row(dt)
d['length(sec)'] = length_sec
d['active'] = "idle"
print "adding", d
self.rows.append([d[i] for i in self.cols])
def csv(self):
return os.linesep.join([",".join([str(i).replace(",", " ") for i in r]) for r in self.rows])
errors = []
def shorten(string):
return string if len(string) < 64 else string[:61] + "..."
if __name__ == "__main__":
table = Table()
last_app = last_activity = None
last_idle = None
errors = []
for line in open("stats.ttb", "rt").readlines():
print line.strip()
cols = [c.strip() for c in line.split("\t")]
dt = get_dt(cols[0])
if last_app:
start, name, url, title, short_url, short_title = last_app
sec = (dt-start).total_seconds()
if sec < 0:
error = "apps", cols[1], sec, get_dt(cols[0]), get_dt(cols[-1]), last_app
sec = 1
print "apps", start, name, url, title, sec
table.add_app(start, name, url, title, sec)
if cols[1] not in ["start", "resume", "pause", "stop"]:
last_app = [dt] + cols[1:4] + [shorten(cols[2]), shorten(cols[3])]
elif cols[1] in ["start", "resume"]:
if last_idle:
last_dt = last_idle
sec = (dt-last_dt).total_seconds()
if sec < 0:
error = "idle", cols[1], sec, get_dt(cols[0]), get_dt(cols[-1]), last_dt, last_act
sec = 1
table.add_idle(last_dt, sec)
last_idle = None
print "idle end", last_dt, sec
elif cols[1] in ["pause", "stop"]:
print "idle start", dt
last_idle = dt
last_app = None
open("activity.csv", "wt").write(table.csv())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment