Skip to content

Commit

Permalink
Added summary for data used in this week, this month and last 30 days
Browse files Browse the repository at this point in the history
  • Loading branch information
KhalidAshaibani committed May 5, 2023
1 parent 9348017 commit 6a7f670
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
21 changes: 19 additions & 2 deletions NetworkWatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# View the stats from the database

import tkinter as tk
from tkinter import ttk
from tkinter import *
from Table import Table
from Stat import Stat
Expand All @@ -28,17 +29,33 @@ def main():
},
root
).table

# create table for summary table
summaryTable = Table({
"Date": {'anchor':tk.CENTER,'width':220},
"Recieved": {'anchor':tk.CENTER,'width':100},
"Sent": {'anchor':tk.CENTER,'width':100},
"Total": {'anchor':tk.CENTER,'width':100},
},
{
},
root
).table
style = ttk.Style()
style.configure("summaryStats.Treeview.Heading", font=('',1), background="none")
summaryTable.configure(style="summaryStats.Treeview", height=3)

# create Stat object to deal with data
stat = Stat(root, dailyTable)
stat = Stat(root, dailyTable, summaryTable)
# set the app icon and title
img = PhotoImage(file='/lib/NetworkWatcher/icon.png')
root.title('Network Watcher v1.0')
root.wm_iconphoto(True, img)

# fill data into the table
stat.fillDailyTable()
stat.fillSummaryTable()
dailyTable.pack(expand=True, fill='both')
summaryTable.pack(side="bottom")
# start the window
root.mainloop()

Expand Down
30 changes: 28 additions & 2 deletions Stat.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from DBConnection import conn
import datetime

class Stat:
cursor = conn.cursor()
REFRESH_TIMEOUT = 2000
dailyTable = None
summaryTable = None
root = None

def __init__(self, root, dailyTable):
def __init__(self, root, dailyTable, summaryTable):
self.root = root
self.dailyTable = dailyTable
self.summaryTable = summaryTable

# convert data into a readable format
def getDataAmount(self, data):
Expand Down Expand Up @@ -36,4 +39,27 @@ def fillDailyTable(self):
self.dailyTable.insert(parent='', index='end', iid=None, values=(
i+1, row[2], row[1], self.getDataAmount(row[4]), self.getDataAmount(row[3]), self.getDataAmount(row[5])))
# keep doing this again and again
self.root.after(self.REFRESH_TIMEOUT, self.fillDailyTable)
self.root.after(self.REFRESH_TIMEOUT, self.fillDailyTable)

# fill the summary stats table from the database
def fillSummaryTable(self):
today = datetime.date.today()
# get start dates
dates = {
'This Week': today - datetime.timedelta(days=today.weekday()),
'This Month': today.replace(day=1),
'Last 30 Days': today - datetime.timedelta(days=30)
}

# clear all existing data from the table
self.summaryTable.delete(*self.summaryTable.get_children())

# fill new data
for date, value in dates.items():
self.cursor.execute(f"SELECT SUM(sent), SUM(recieved), SUM(usage) FROM dailyStats WHERE date >= '{value}'")
row = self.cursor.fetchone()
self.summaryTable.insert(parent='', index='end', iid=None, values=(
date, self.getDataAmount(row[0]), self.getDataAmount(row[1]), self.getDataAmount(row[2])))

# keep doing this again and again
self.root.after(self.REFRESH_TIMEOUT, self.fillSummaryTable)

0 comments on commit 6a7f670

Please sign in to comment.