2021-08-16 01:14:36 +08:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import csv
|
|
|
|
import datetime
|
|
|
|
from drive import Drive
|
|
|
|
from csvfile import CSVFile
|
|
|
|
from pathlib import Path
|
|
|
|
from utils import subtract, nextWeek, sumKV
|
2021-08-17 07:54:24 +08:00
|
|
|
from datetime import timedelta
|
2021-08-16 01:14:36 +08:00
|
|
|
|
|
|
|
SEARCH_PATH = "/mnt/"
|
|
|
|
|
|
|
|
class DiskUsage:
|
|
|
|
# I probably should be using a database instead of CSV.
|
|
|
|
def __init__(self, basepath) -> None:
|
|
|
|
basepath = Path(basepath)
|
|
|
|
self.SIZE_FILE = basepath.joinpath("size_daily.csv")
|
|
|
|
self.DIFF_FILE = basepath.joinpath("diff_daily.csv")
|
|
|
|
self.WEEKLY_SIZE_FILE = basepath.joinpath("size_weekly.csv")
|
|
|
|
self.WEEKLY_DIFF_FILE = basepath.joinpath("diff_weekly.csv")
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def __daily2weeklyRow(row_):
|
|
|
|
row = row_.copy()
|
|
|
|
row["time"] = row["next_week"]
|
|
|
|
row.pop("next_week")
|
|
|
|
return row
|
|
|
|
|
2021-08-17 07:54:24 +08:00
|
|
|
@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()
|
|
|
|
|
2021-08-16 01:14:36 +08:00
|
|
|
|
|
|
|
def update(self):
|
2021-08-17 07:54:24 +08:00
|
|
|
nextWeek_ = nextWeek()
|
2021-08-16 01:14:36 +08:00
|
|
|
|
|
|
|
sizeDaily = CSVFile(self.SIZE_FILE)
|
|
|
|
currentlyUsed = Drive.getUsed(SEARCH_PATH)
|
|
|
|
sizeDaily.addRow(currentlyUsed)
|
|
|
|
sizeDaily.efficientWriteFile()
|
|
|
|
if sizeDaily.length < 2:
|
|
|
|
return 0 # Break for new files
|
|
|
|
|
|
|
|
sizeWeekly = CSVFile(self.WEEKLY_SIZE_FILE)
|
|
|
|
sizeWeeklyRow = self.__daily2weeklyRow(currentlyUsed)
|
|
|
|
previousNextWeek = sizeDaily.getRow(-2).get("next_week")
|
2021-08-17 07:54:24 +08:00
|
|
|
self.__update_weekly_data(sizeWeekly, sizeWeeklyRow, previousNextWeek, nextWeek_,
|
|
|
|
duplicateEmpty=1, updateLast=True)
|
2021-08-16 01:14:36 +08:00
|
|
|
|
|
|
|
delta = subtract(sizeDaily.getRow(-1),sizeDaily.getRow(-2))
|
|
|
|
diffDaily = CSVFile(self.DIFF_FILE)
|
|
|
|
diffDaily.addRow(delta)
|
|
|
|
diffDaily.efficientWriteFile()
|
|
|
|
|
|
|
|
|
|
|
|
if sizeWeekly.length < 2:
|
|
|
|
return 0 # Break for new files
|
|
|
|
diffWeekly = CSVFile(self.WEEKLY_DIFF_FILE)
|
2021-08-17 07:54:24 +08:00
|
|
|
diffWeeklyRow = subtract(sizeWeekly.getRow(-1), sizeWeekly.getRow(-2))
|
2021-08-16 01:14:36 +08:00
|
|
|
print(delta)
|
2021-08-17 07:54:24 +08:00
|
|
|
self.__update_weekly_data(diffWeekly, diffWeeklyRow, previousNextWeek, nextWeek_,
|
|
|
|
makeEmpty=True)
|
2021-08-16 01:14:36 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
DiskUsage("/home/peter/scripts/datalogging/disk-usage/testfiles").update()
|