Skip to content

Instantly share code, notes, and snippets.

@philip-bl
Last active February 4, 2018 00:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philip-bl/df075cb70a501c53a1a3e06dcc900175 to your computer and use it in GitHub Desktop.
Save philip-bl/df075cb70a501c53a1a3e06dcc900175 to your computer and use it in GitHub Desktop.
Thyme helper scripts. I used dashes instead of slashes in filenames. Don't forget to create ~/logs/
# https://about.sourcegraph.com/blog/thyme-a-simple-cli-to-measure-human-time-and-focus/
# https://github.com/sourcegraph/thyme
[Unit]
Description=Call thyme to save windows usage to json for statistics
[Service]
Type=oneshot
ExecStart=/home/shibbiry/go/bin/thyme track -o /home/shibbiry/logs/thyme.json
# if thyme has not finished in 5 seconds, consider it failed and kill it
TimeoutStartSec=5
[Unit]
Description=Run thyme periodically
Requires=thyme.service
[Timer]
OnUnitActiveSec=30
OnBootSec=100ms
[Install]
WantedBy=default.target
# don't forget to do systemctl --user enable thyme.timer
#!/usr/bin/env python3
import click
import logging
import click_log
import datetime as dt
import re
import json
logger = logging.getLogger(__name__)
click_log.basic_config(logger)
def is_date(string):
return re.match(r"^\d\d\d\d-\d\d-\d\d$", string) is not None
def is_datetime(string):
return re.match(r"^\d\d\d\d-\d\d-\d\dT\d\d:\d\d$", string) is not None
def read_dt(string):
if is_date(string):
ts = dt.datetime.strptime(
string, "%Y-%m-%d"
)
return ts
elif is_datetime(string):
return dt.datetime.strptime(
string, "%Y-%m-%dT%H:%M"
)
raise ValueError("'{0} is not a valid date or datetime string".format(string))
ALWAYS_ON_WINDOWS = frozenset(["Desktop — Plasma", "Plasma"])
def remove_always_on(record):
"""filter out KDE always-on windows"""
good_windows = [
window for window in record["Windows"]
if window["Name"] not in ALWAYS_ON_WINDOWS
]
bad_window_ids = frozenset([
window["ID"] for window in record["Windows"]
if window["Name"] in ALWAYS_ON_WINDOWS
])
new_record = {
"Windows": good_windows,
"Visible": [id_ for id_ in record["Visible"] if id_ not in bad_window_ids]
}
for key in record.keys():
if key not in new_record:
new_record[key] = record[key]
return new_record
@click.command(help=__doc__)
@click_log.simple_verbosity_option(logger)
@click.option("-s", "--start", type=str)
@click.option("-e", "--end", type=str)
@click.argument("input", type=click.File(encoding="utf-8"))
@click.argument("output", type=click.File(mode="w", encoding="utf-8"))
def main(start, end, input, output):
records = json.load(input)
if start is not None:
start = read_dt(start)
logger.debug("Writing entries from {0}".format(start))
if end is not None:
end = read_dt(end)
if end.hour == 0 and end.minute == 0:
end += dt.timedelta(days=1)
end -= dt.timedelta(seconds=1)
logger.debug("Writing entries to {0}".format(end))
def filter(item):
match = re.match(r"(.*)\.\d+", item["Time"])
string = "".join(match.groups())
ts = dt.datetime.strptime(string, "%Y-%m-%dT%H:%M:%S")
if start is not None and ts < start:
return False
if end is not None and ts > end:
return False
return True
filtered_records = {
"Snapshots": [
remove_always_on(item) for item in records["Snapshots"]
if filter(item)
]
}
logger.debug(
"Writing {0} records".format(len(filtered_records["Snapshots"]))
)
json.dump(filtered_records, output)
if __name__ == "__main__":
main()
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
TEMP_JSON=`mktemp`
TEMP_HTML=`mktemp --suffix=.html`
thyme_extract.py "$@" --verbosity DEBUG \
"$HOME/logs/thyme.json" "$TEMP_JSON"
thyme show -i "$TEMP_JSON" --what=stats > "$TEMP_HTML"
xdg-open "$TEMP_HTML"
$ cat ~/.config/plasma-workspace/env/set_xorg_env_in_systemd.sh
#!/bin/sh
systemctl --user import-environment DISPLAY XAUTHORITY
if which dbus-update-activation-environment >/dev/null 2>&1; then
dbus-update-activation-environment DISPLAY XAUTHORITY
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment