Last active
June 12, 2019 06:21
-
-
Save Tycholiz/0e7b016a25c4536a2a891b1f31c3db6b to your computer and use it in GitHub Desktop.
A Watson time tracker script that generates a freelance client report for weekly allocation of hours
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 | |
import os, json, sys, xlwt, time, datetime, argparse | |
from datetime import timedelta, datetime | |
from docx import Document | |
from docx.shared import Inches | |
def parseArguments(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("projectName", help="name of client") | |
parser.add_argument("-i", "--invoice", help="include the invoice section or not?", action='store_true') | |
parser.add_argument("--version", action="version", version='%(prog)s - Version 1.0') | |
args = parser.parse_args() | |
return args | |
args = parseArguments() | |
numberOfDaysToReport = 7 | |
endDate = datetime.now().date() | |
startDate = (datetime.today() - timedelta(days=numberOfDaysToReport)).date() | |
# generate Watson json report and send it to data.json | |
bashCommand = "watson report --json --from {} --to {} > data.json".format(startDate, endDate) | |
os.system(bashCommand) | |
# load data.json into file | |
filePath = './data.json' | |
file_object = open(filePath, 'r') | |
os.system("rm -rf data.json") | |
wholeData = json.load(file_object) | |
userInputtedProjectName = sys.argv[1] | |
if (args.invoice): | |
isInvoiced = True | |
projects = wholeData['projects'] | |
# data for user's passed argument is retrieved and assigned to variables | |
for project in projects: | |
if (project['name'] == args.projectName): | |
projectName = project['name'] | |
totalDuration = project['time'] | |
tags = project['tags'] | |
# Create a list of objects, each object containing one key-value pair, name: time | |
records = [] | |
for tag in tags: | |
tagName = tag['name'] | |
duration = tag["time"] | |
combined = (tagName, duration) | |
records.append(combined) | |
for data in enumerate(records): #! can remove 'row'? | |
tag = data[0] | |
duration = data[1] | |
# generate document name and save document | |
document = Document() | |
document.add_heading('Weekly Report', 0) | |
def formatDate(boundaryDate): | |
return boundaryDate.strftime("%B %d") | |
dates = document.add_paragraph("{} to {}, ".format(formatDate(startDate), formatDate(endDate))) | |
currentYear = (datetime.now().year) | |
dates.add_run(str(currentYear)) | |
intro = document.add_paragraph('Here is your weekly breakdown of how my hours have been allocated. Please let me know if there are any questions or concerns!') | |
document.add_paragraph('') | |
document.add_heading('Allocation of hours', level=1) | |
document.add_paragraph('') | |
timeAllocationTable = document.add_table(rows=1, cols=2) | |
hdr_cells = timeAllocationTable.rows[0].cells | |
hdr_cells[0].text = 'Task' | |
hdr_cells[1].text = 'Time Spent (HH:MM:SS)' | |
for task, timeSpent in records: | |
row_cells = timeAllocationTable.add_row().cells | |
formattedTimeSpent = str(timedelta(seconds=timeSpent)) | |
row_cells[0].text = task | |
row_cells[1].text = formattedTimeSpent | |
totalRow = timeAllocationTable.add_row().cells | |
totalRow[0].text = 'Total' | |
formattedTotalTimeSpent = str(timedelta(seconds=totalDuration)) | |
totalRow[1].text = formattedTotalTimeSpent | |
if (args.invoice): | |
document.add_paragraph('') | |
document.add_heading('Summary of Accounts', level=1) | |
document.add_paragraph('') | |
balanceOutstandingTable = document.add_table(rows=1, cols=3) | |
hdr_cells = balanceOutstandingTable.rows[0].cells | |
hdr_cells[0].text = '' | |
hdr_cells[1].text = 'Hours' | |
hdr_cells[2].text = "Value ($50/hr)" | |
row_cells = balanceOutstandingTable.add_row().cells | |
row_cells[0].text = "Previous Outstanding Hours" | |
row_cells[1].text = "" | |
row_cells[2].text = "" | |
row_cells2 = balanceOutstandingTable.add_row().cells | |
row_cells2[0].text = "This week's hours" | |
row_cells2[1].text = formattedTotalTimeSpent | |
row_cells2[2].text = "{0:.2f}".format(totalDuration/60/60 * 50) | |
row_cells3 = balanceOutstandingTable.add_row().cells | |
row_cells3[0].text = "Total Balance" | |
row_cells3[1].text = "" | |
row_cells3[2].text = "" | |
document.add_paragraph('') | |
document.add_heading('Moving forward', level=1) | |
document.add_paragraph('') | |
# generate document name and save document | |
currentDate = datetime.now().strftime("%Y-%m-%d") | |
outputFile = "{}-weeklyreport-{}.docx".format(userInputtedProjectName, currentDate) | |
document.save(outputFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment