weekly stats working properly now.

This commit is contained in:
Peter 2021-08-17 07:54:24 +08:00
parent 09af817c6a
commit 223d4c5fd7
3 changed files with 38 additions and 30 deletions

View File

@ -1,4 +1,5 @@
import csv import csv
from copy import copy
from pathlib import Path from pathlib import Path
class CSVFile: class CSVFile:
@ -35,6 +36,7 @@ class CSVFile:
self.__fields.append(field) self.__fields.append(field)
self.__data.append(row) self.__data.append(row)
self.__addBuffer.append(row) self.__addBuffer.append(row)
self.length = len(self.__data)
def getRow(self,rowIdx): def getRow(self,rowIdx):
return self.__data[rowIdx] return self.__data[rowIdx]
@ -118,7 +120,10 @@ class CSVFile:
return count > 0 return count > 0
return False return False
def fill(self, v=0): def fill(self, row, fillV=0, updateKVs=[]):
if len(self.__data) == 0: row = copy(row)
return False for k in row:
return { k:v for k in self.__data[0].keys() } row[k] = fillV
for kv in updateKVs:
row.update(kv)
self.addRow(row)

View File

@ -7,6 +7,7 @@ from drive import Drive
from csvfile import CSVFile from csvfile import CSVFile
from pathlib import Path from pathlib import Path
from utils import subtract, nextWeek, sumKV from utils import subtract, nextWeek, sumKV
from datetime import timedelta
SEARCH_PATH = "/mnt/" SEARCH_PATH = "/mnt/"
@ -27,9 +28,28 @@ class DiskUsage:
row.pop("next_week") row.pop("next_week")
return row return row
@staticmethod
def __update_weekly_data(csvfile, updatedRow, previousNextWeek, nextWeek,
makeEmpty=False, duplicateEmpty=0, updateLast=False):
if csvfile.length < 1:
if makeEmpty:
csvfile.fill(updatedRow,updateKVs=[{ "time" : (nextWeek + timedelta(days=-7)).isoformat() }])
for i in range(0,duplicateEmpty):
csvfile.addRow(updatedRow)
csvfile.addRow(updatedRow)
elif previousNextWeek == nextWeek.isoformat():
csvfile.replaceByKV("time", nextWeek.isoformat(), updatedRow, backwards=True, start=-1)
else:
csvfile.addRow(updatedRow)
previousWeeklyRow = updatedRow.copy()
previousWeeklyRow.update({ "time" : previousNextWeek })
if updateLast:
csvfile.replaceByKV("time", previousNextWeek, previousWeeklyRow, backwards=True, start=-1)
csvfile.efficientWriteFile()
def update(self): def update(self):
nextWeek_ = nextWeek().isoformat() nextWeek_ = nextWeek()
sizeDaily = CSVFile(self.SIZE_FILE) sizeDaily = CSVFile(self.SIZE_FILE)
currentlyUsed = Drive.getUsed(SEARCH_PATH) currentlyUsed = Drive.getUsed(SEARCH_PATH)
@ -41,16 +61,8 @@ class DiskUsage:
sizeWeekly = CSVFile(self.WEEKLY_SIZE_FILE) sizeWeekly = CSVFile(self.WEEKLY_SIZE_FILE)
sizeWeeklyRow = self.__daily2weeklyRow(currentlyUsed) sizeWeeklyRow = self.__daily2weeklyRow(currentlyUsed)
previousNextWeek = sizeDaily.getRow(-2).get("next_week") previousNextWeek = sizeDaily.getRow(-2).get("next_week")
if sizeWeekly.length < 1: self.__update_weekly_data(sizeWeekly, sizeWeeklyRow, previousNextWeek, nextWeek_,
sizeWeekly.addRow(sizeWeeklyRow) duplicateEmpty=1, updateLast=True)
elif previousNextWeek == nextWeek_:
sizeWeekly.replaceByKV("time", nextWeek_, sizeWeeklyRow, backwards=True, start=-1)
else:
sizeWeekly.addRow(sizeWeeklyRow)
previousWeeklyRow = sizeWeeklyRow.copy()
previousWeeklyRow.update({ "time" : previousNextWeek })
sizeWeekly.replaceByKV("time", previousNextWeek, previousWeeklyRow, backwards=True, start=-1)
sizeWeekly.efficientWriteFile()
delta = subtract(sizeDaily.getRow(-1),sizeDaily.getRow(-2)) delta = subtract(sizeDaily.getRow(-1),sizeDaily.getRow(-2))
diffDaily = CSVFile(self.DIFF_FILE) diffDaily = CSVFile(self.DIFF_FILE)
@ -61,19 +73,10 @@ class DiskUsage:
if sizeWeekly.length < 2: if sizeWeekly.length < 2:
return 0 # Break for new files return 0 # Break for new files
diffWeekly = CSVFile(self.WEEKLY_DIFF_FILE) diffWeekly = CSVFile(self.WEEKLY_DIFF_FILE)
previousNextWeek = diffDaily.getRow(-2).get("next_week") diffWeeklyRow = subtract(sizeWeekly.getRow(-1), sizeWeekly.getRow(-2))
delta = subtract(sizeWeekly.getRow(-1), sizeWeekly.getRow(-2))
print(previousNextWeek == nextWeek_)
print(delta) print(delta)
if previousNextWeek == nextWeek_: self.__update_weekly_data(diffWeekly, diffWeeklyRow, previousNextWeek, nextWeek_,
diffWeekly.replaceByKV("time", previousNextWeek, delta, backwards=True, start=-1) makeEmpty=True)
else:
delta.update({ "time" : previousNextWeek })
diffWeekly.replaceByKV("time", previousNextWeek, delta, backwards=True, start=-1)
empty = diffDaily.fill()
empty.update({"time" : nextWeek_})
diffWeekly.addRow(empty)
diffWeekly.efficientWriteFile()
if __name__ == '__main__': if __name__ == '__main__':
DiskUsage("/home/peter/scripts/datalogging/disk-usage/testfiles").update() DiskUsage("/home/peter/scripts/datalogging/disk-usage/testfiles").update()

View File

@ -79,7 +79,7 @@ set grid xtics;
PLOT_DIFF = """ PLOT_DIFF = """
set autoscale y set autoscale y
set ylabel '{/Symbol D}Used [ISO definition]' textcolor 'white'; set ylabel '{/Symbol D}Used [SI definition]' textcolor 'white';
set format y '%+08.3s %cB'; set format y '%+08.3s %cB';
set y2label '{/Symbol D}Used (GB)' textcolor 'white'; set y2label '{/Symbol D}Used (GB)' textcolor 'white';
@ -90,7 +90,7 @@ set format y2 '%+08.3s %c';
PLOT_0="""set title 'Used capacity of disks (last 28 days)' textcolor 'white'; PLOT_0="""set title 'Used capacity of disks (last 28 days)' textcolor 'white';
set format y '%.2s %cB'; set format y '%.2s %cB';
set ylabel 'Used [ISO definition]' textcolor 'white'; set ylabel 'Used [SI definition]' textcolor 'white';
set ytics 0.25*10**12; set ytics 0.25*10**12;
set mytics 2; set mytics 2;
@ -119,7 +119,7 @@ set offset graph 0, graph 0, graph 0.02, graph 0.02;
PLOT_2=""" PLOT_2="""
set title 'Used capacity of disks (last 364 days)' textcolor 'white'; set title 'Used capacity of disks (last 364 days)' textcolor 'white';
set ylabel 'Used [ISO definition]' textcolor 'white'; set ylabel 'Used [SI definition]' textcolor 'white';
set format y '%.2s %cB'; set format y '%.2s %cB';
set ytics 0.25*10**12; set ytics 0.25*10**12;
set mytics 2; set mytics 2;