Skip to content

Commit

Permalink
New Version 1.13.0
Browse files Browse the repository at this point in the history
- E Filter for filenames
- B #142, #141, #140, #138, #135, #118
- E #100 Parsing SclicerSettings via regular expression
- E #128 update the tab screenshot
- I ugrade database scheme to V5 (e.g. rename vendor) and backupfilename include now the scheme version
  • Loading branch information
OllisGit committed May 16, 2021
1 parent da131a0 commit b360fa2
Show file tree
Hide file tree
Showing 24 changed files with 586 additions and 321 deletions.
80 changes: 71 additions & 9 deletions octoprint_PrintJobHistory/DatabaseManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
FORCE_CREATE_TABLES = False
SQL_LOGGING = True

CURRENT_DATABASE_SCHEME_VERSION = 4
CURRENT_DATABASE_SCHEME_VERSION = 5

# List all Models
MODELS = [PluginMetaDataModel, PrintJobModel, FilamentModel, TemperatureModel]
Expand Down Expand Up @@ -83,6 +83,58 @@ def _upgradeDatabase(self,currentDatabaseSchemeVersion, targetDatabaseSchemeVers

def _upgradeFrom4To5(self):
self._logger.info(" Starting 4 -> 5")
# What is changed:
# - FilamentModel:
# - renameing:
# profileVendor -> vendor
# spoolWeight -> weight
# (ALTER TABLE spo_spoolmodel RENAME COLUMN encloserTemperature to enclosureTemperature; not working SQLite did not support the ALTER TABLE RENAME COLUMN syntax before version 3.25.0.
# see https://www.sqlitetutorial.net/sqlite-rename-column/#:~:text=SQLite%20did%20not%20support%20the,the%20version%20lower%20than%203.25.)

connection = sqlite3.connect(self._databaseFileLocation)
cursor = connection.cursor()

sql = """
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
ALTER TABLE 'pjh_filamentmodel' RENAME TO 'pjh_filamentmodel_old';
CREATE TABLE "pjh_filamentmodel" (
"databaseId" INTEGER NOT NULL PRIMARY KEY,
"created" DATETIME NOT NULL,
"printJob_id" INTEGER NOT NULL,
"vendor" VARCHAR(255),
"diameter" REAL,
"density" REAL,
"material" VARCHAR(255),
"spoolName" VARCHAR(255),
"spoolCost" REAL,
"spoolCostUnit" VARCHAR(255),
"weight" REAL,
"usedLength" REAL,
"calculatedLength" REAL,
"usedWeight" REAL,
"usedCost" REAL,
'toolId' VARCHAR(255),
FOREIGN KEY ("printJob_id") REFERENCES "pjh_printjobmodel" ("databaseId") ON DELETE CASCADE);
INSERT INTO 'pjh_filamentmodel'
(databaseId, created, printJob_id, vendor, diameter, density, material, spoolName, spoolCost, spoolCostUnit, weight, usedLength, calculatedLength, usedWeight, usedCost, toolId)
SELECT databaseId, created, printJob_id, profileVendor, diameter, density, material, spoolName, spoolCost, spoolCostUnit, spoolWeight, usedLength, calculatedLength, usedWeight, usedCost, toolId
FROM 'pjh_filamentmodel_old';
DROP TABLE 'pjh_filamentmodel_old';
UPDATE 'pjh_pluginmetadatamodel' SET value=5 WHERE key='databaseSchemeVersion';
COMMIT;
PRAGMA foreign_keys=on;
"""
cursor.executescript(sql)

connection.close()
self._logger.info(" Successfully 4 -> 5")
pass

def _upgradeFrom3To4(self):
self._logger.info(" Starting 3 -> 4")
Expand Down Expand Up @@ -110,7 +162,6 @@ def _upgradeFrom3To4(self):
self._logger.info(" Successfully 3 -> 4")
pass


def _upgradeFrom2To3(self):
self._logger.info(" Starting 2 -> 3")
# What is changed:
Expand All @@ -136,7 +187,6 @@ def _upgradeFrom2To3(self):
self._logger.info(" Successfully 2 -> 3")
pass


def _upgradeFrom1To2(self):
self._logger.info(" Starting 1 -> 2")
# What is changed:
Expand Down Expand Up @@ -310,7 +360,14 @@ def showSQLLogging(self, enabled):
def backupDatabaseFile(self, backupFolder):
now = datetime.datetime.now()
currentDate = now.strftime("%Y%m%d-%H%M")
backupDatabaseFileName = "printJobHistory-backup-"+currentDate+".db"
currentSchemeVersion = "unknown"
try:
currentSchemeVersion = PluginMetaDataModel.get(PluginMetaDataModel.key == PluginMetaDataModel.KEY_DATABASE_SCHEME_VERSION)
if (currentSchemeVersion != None):
currentSchemeVersion = str(currentSchemeVersion.value)
except Exception as e:
self._logger.exception("Could not read databasescheme version:" + str(e))
backupDatabaseFileName = "printJobHistory-backup-V"+currentSchemeVersion +"-"+currentDate+".db"
backupDatabaseFilePath = os.path.join(backupFolder, backupDatabaseFileName)
if not os.path.exists(backupDatabaseFilePath):
shutil.copy(self._databaseFileLocation, backupDatabaseFilePath)
Expand Down Expand Up @@ -435,8 +492,7 @@ def calculatePrintJobsStatisticByQuery(self, tableQuery):
else:
statusDict[statusResult] = 1

job.loadFilamentsFromAssoziation()
allFilaments = job.allFilaments
allFilaments = job.getFilamentModels()
if allFilaments != None:
for filla in allFilaments:
if (StringUtils.isEmpty(filla.usedLength) == False):
Expand All @@ -462,8 +518,6 @@ def calculatePrintJobsStatisticByQuery(self, tableQuery):
else:
materialDict[filla.material] = 1



# do formatting
queryString = self._buildQueryString(tableQuery)
fromToString = firstDate.strftime('%d.%m.%Y %H:%M') + " - " + lastDate.strftime('%d.%m.%Y %H:%M')
Expand Down Expand Up @@ -616,11 +670,12 @@ def _addTableQueryToSelect(self, myQuery, tableQuery):
sortOrder = tableQuery["sortOrder"]
filterName = tableQuery["filterName"]

# - status
if (filterName == "onlySuccess"):
myQuery = myQuery.where(PrintJobModel.printStatusResult == "success")
elif (filterName == "onlyFailed"):
myQuery = myQuery.where(PrintJobModel.printStatusResult != "success")

# -sorting
if ("printStartDateTime" == sortColumn):
if ("desc" == sortOrder):
myQuery = myQuery.order_by(PrintJobModel.printStartDateTime.desc())
Expand All @@ -631,6 +686,7 @@ def _addTableQueryToSelect(self, myQuery, tableQuery):
myQuery = myQuery.order_by(fn.Lower(PrintJobModel.fileName).desc())
else:
myQuery = myQuery.order_by(fn.Lower(PrintJobModel.fileName))
# - date range
if ("startDate" in tableQuery):
startDate = tableQuery["startDate"]
endDate = tableQuery["endDate"]
Expand All @@ -644,6 +700,12 @@ def _addTableQueryToSelect(self, myQuery, tableQuery):
# ((PrintJobModel.printStartDateTime == endDate) | ( PrintJobModel.printStartDateTime < startDate)) )
myQuery = myQuery.where( ( ( PrintJobModel.printStartDateTime > startDateTime) & ( PrintJobModel.printStartDateTime < endDateTime))
)
# - search query (only filename)
if ("searchQuery" in tableQuery):
searchQueryValue = tableQuery["searchQuery"]
if (len(searchQueryValue) > 0):
myQuery = myQuery.where(PrintJobModel.fileName.contains(searchQueryValue))
pass
return myQuery


Expand Down
Loading

0 comments on commit b360fa2

Please sign in to comment.