Last active
April 15, 2019 13:04
-
-
Save Kevin-Mok/f9fcfd0c92d6164cb64179e108f0e9a4 to your computer and use it in GitHub Desktop.
Daily average for Fish commands.
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
#!/usr/bin/python3 | |
# Print out table of daily average for every week of Fish command history. | |
import re | |
from datetime import datetime, timedelta | |
from pprint import pprint | |
from prettytable import PrettyTable | |
from os import getenv | |
from argparse import ArgumentParser | |
DAY_DELTA = timedelta(days=1) | |
WEEK_DELTA = timedelta(weeks=1) | |
TABLE_HEADERS = ["Week", "Av."] | |
HISTORY_FILE_PATH = f"{getenv('HOME')}/.local/share/fish/fish_history" | |
def convert_unix_timestamp(unix_timestamp): | |
return datetime.utcfromtimestamp(unix_timestamp) | |
def get_timestamps_from_history(): | |
history_file = open(HISTORY_FILE_PATH, "r") | |
cmd_timestamps = [int(line.split()[1]) for line in | |
history_file.readlines() if re.search("^\s*when: \d*$", line)] | |
history_file.close() | |
return cmd_timestamps | |
def create_week_average_table(timestamps): | |
week_table = PrettyTable(TABLE_HEADERS) | |
cur_week = convert_unix_timestamp(timestamps[0]) | |
cur_week_tally = 0 | |
for timestamp in timestamps: | |
cur_date = convert_unix_timestamp(timestamp) | |
if cur_week + WEEK_DELTA <= cur_date: | |
week_table.add_row([cur_week.strftime('%m-%d'), | |
int(cur_week_tally / 7)]) | |
cur_week += WEEK_DELTA | |
cur_week_tally = 0 | |
cur_week_tally += 1 | |
week_table.align[TABLE_HEADERS[1]] = "r" | |
return week_table | |
def print_daily_av(timestamps): | |
daily_commands = [] | |
cur_day = convert_unix_timestamp(timestamps[0]) | |
cur_day_tally = 0 | |
for timestamp in timestamps: | |
cur_date = convert_unix_timestamp(timestamp) | |
if cur_day + DAY_DELTA <= cur_date: | |
daily_commands.append(cur_day_tally) | |
cur_day += DAY_DELTA | |
cur_day_tally = 0 | |
cur_day_tally += 1 | |
print(int(sum(daily_commands) / len(daily_commands))) | |
def main(): | |
arg_parser = ArgumentParser() | |
arg_parser.add_argument('--weekly', action='store_true') | |
timestamps = get_timestamps_from_history() | |
if arg_parser.parse_args().weekly: | |
print(create_week_average_table(timestamps).get_string(sortby=TABLE_HEADERS[1], | |
reversesort=True)) | |
else: | |
print_daily_av(timestamps) | |
if __name__== "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment