From 223d4c5fd7fc8730e8da3533610c3326ca013d8f Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 17 Aug 2021 07:54:24 +0800 Subject: [PATCH] weekly stats working properly now. --- csvfile.py | 13 +++++++++---- disk-usage.py | 49 ++++++++++++++++++++++++++----------------------- viewer.py | 6 +++--- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/csvfile.py b/csvfile.py index cdae917..4bd7ef0 100755 --- a/csvfile.py +++ b/csvfile.py @@ -1,4 +1,5 @@ import csv +from copy import copy from pathlib import Path class CSVFile: @@ -35,6 +36,7 @@ class CSVFile: self.__fields.append(field) self.__data.append(row) self.__addBuffer.append(row) + self.length = len(self.__data) def getRow(self,rowIdx): return self.__data[rowIdx] @@ -118,7 +120,10 @@ class CSVFile: return count > 0 return False - def fill(self, v=0): - if len(self.__data) == 0: - return False - return { k:v for k in self.__data[0].keys() } \ No newline at end of file + def fill(self, row, fillV=0, updateKVs=[]): + row = copy(row) + for k in row: + row[k] = fillV + for kv in updateKVs: + row.update(kv) + self.addRow(row) \ No newline at end of file diff --git a/disk-usage.py b/disk-usage.py index 9c587b4..b54b7da 100755 --- a/disk-usage.py +++ b/disk-usage.py @@ -7,6 +7,7 @@ from drive import Drive from csvfile import CSVFile from pathlib import Path from utils import subtract, nextWeek, sumKV +from datetime import timedelta SEARCH_PATH = "/mnt/" @@ -27,9 +28,28 @@ class DiskUsage: row.pop("next_week") 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): - nextWeek_ = nextWeek().isoformat() + nextWeek_ = nextWeek() sizeDaily = CSVFile(self.SIZE_FILE) currentlyUsed = Drive.getUsed(SEARCH_PATH) @@ -41,16 +61,8 @@ class DiskUsage: sizeWeekly = CSVFile(self.WEEKLY_SIZE_FILE) sizeWeeklyRow = self.__daily2weeklyRow(currentlyUsed) previousNextWeek = sizeDaily.getRow(-2).get("next_week") - if sizeWeekly.length < 1: - sizeWeekly.addRow(sizeWeeklyRow) - 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() + self.__update_weekly_data(sizeWeekly, sizeWeeklyRow, previousNextWeek, nextWeek_, + duplicateEmpty=1, updateLast=True) delta = subtract(sizeDaily.getRow(-1),sizeDaily.getRow(-2)) diffDaily = CSVFile(self.DIFF_FILE) @@ -61,19 +73,10 @@ class DiskUsage: if sizeWeekly.length < 2: return 0 # Break for new files diffWeekly = CSVFile(self.WEEKLY_DIFF_FILE) - previousNextWeek = diffDaily.getRow(-2).get("next_week") - delta = subtract(sizeWeekly.getRow(-1), sizeWeekly.getRow(-2)) - print(previousNextWeek == nextWeek_) + diffWeeklyRow = subtract(sizeWeekly.getRow(-1), sizeWeekly.getRow(-2)) print(delta) - if previousNextWeek == nextWeek_: - diffWeekly.replaceByKV("time", previousNextWeek, delta, backwards=True, start=-1) - 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() + self.__update_weekly_data(diffWeekly, diffWeeklyRow, previousNextWeek, nextWeek_, + makeEmpty=True) if __name__ == '__main__': DiskUsage("/home/peter/scripts/datalogging/disk-usage/testfiles").update() \ No newline at end of file diff --git a/viewer.py b/viewer.py index 23ace1f..8e90e0d 100755 --- a/viewer.py +++ b/viewer.py @@ -79,7 +79,7 @@ set grid xtics; PLOT_DIFF = """ 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 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'; 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 mytics 2; @@ -119,7 +119,7 @@ set offset graph 0, graph 0, graph 0.02, graph 0.02; PLOT_2=""" 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 ytics 0.25*10**12; set mytics 2;