21
21
FORCE_CREATE_TABLES = False
22
22
SQL_LOGGING = True
23
23
24
- CURRENT_DATABASE_SCHEME_VERSION = 4
24
+ CURRENT_DATABASE_SCHEME_VERSION = 5
25
25
26
26
# List all Models
27
27
MODELS = [PluginMetaDataModel , PrintJobModel , FilamentModel , TemperatureModel ]
@@ -83,6 +83,58 @@ def _upgradeDatabase(self,currentDatabaseSchemeVersion, targetDatabaseSchemeVers
83
83
84
84
def _upgradeFrom4To5 (self ):
85
85
self ._logger .info (" Starting 4 -> 5" )
86
+ # What is changed:
87
+ # - FilamentModel:
88
+ # - renameing:
89
+ # profileVendor -> vendor
90
+ # spoolWeight -> weight
91
+ # (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.
92
+ # see https://www.sqlitetutorial.net/sqlite-rename-column/#:~:text=SQLite%20did%20not%20support%20the,the%20version%20lower%20than%203.25.)
93
+
94
+ connection = sqlite3 .connect (self ._databaseFileLocation )
95
+ cursor = connection .cursor ()
96
+
97
+ sql = """
98
+ PRAGMA foreign_keys=off;
99
+ BEGIN TRANSACTION;
100
+
101
+ ALTER TABLE 'pjh_filamentmodel' RENAME TO 'pjh_filamentmodel_old';
102
+
103
+ CREATE TABLE "pjh_filamentmodel" (
104
+ "databaseId" INTEGER NOT NULL PRIMARY KEY,
105
+ "created" DATETIME NOT NULL,
106
+ "printJob_id" INTEGER NOT NULL,
107
+ "vendor" VARCHAR(255),
108
+ "diameter" REAL,
109
+ "density" REAL,
110
+ "material" VARCHAR(255),
111
+ "spoolName" VARCHAR(255),
112
+ "spoolCost" REAL,
113
+ "spoolCostUnit" VARCHAR(255),
114
+ "weight" REAL,
115
+ "usedLength" REAL,
116
+ "calculatedLength" REAL,
117
+ "usedWeight" REAL,
118
+ "usedCost" REAL,
119
+ 'toolId' VARCHAR(255),
120
+ FOREIGN KEY ("printJob_id") REFERENCES "pjh_printjobmodel" ("databaseId") ON DELETE CASCADE);
121
+
122
+ INSERT INTO 'pjh_filamentmodel'
123
+ (databaseId, created, printJob_id, vendor, diameter, density, material, spoolName, spoolCost, spoolCostUnit, weight, usedLength, calculatedLength, usedWeight, usedCost, toolId)
124
+ SELECT databaseId, created, printJob_id, profileVendor, diameter, density, material, spoolName, spoolCost, spoolCostUnit, spoolWeight, usedLength, calculatedLength, usedWeight, usedCost, toolId
125
+ FROM 'pjh_filamentmodel_old';
126
+
127
+ DROP TABLE 'pjh_filamentmodel_old';
128
+
129
+ UPDATE 'pjh_pluginmetadatamodel' SET value=5 WHERE key='databaseSchemeVersion';
130
+ COMMIT;
131
+ PRAGMA foreign_keys=on;
132
+ """
133
+ cursor .executescript (sql )
134
+
135
+ connection .close ()
136
+ self ._logger .info (" Successfully 4 -> 5" )
137
+ pass
86
138
87
139
def _upgradeFrom3To4 (self ):
88
140
self ._logger .info (" Starting 3 -> 4" )
@@ -110,7 +162,6 @@ def _upgradeFrom3To4(self):
110
162
self ._logger .info (" Successfully 3 -> 4" )
111
163
pass
112
164
113
-
114
165
def _upgradeFrom2To3 (self ):
115
166
self ._logger .info (" Starting 2 -> 3" )
116
167
# What is changed:
@@ -136,7 +187,6 @@ def _upgradeFrom2To3(self):
136
187
self ._logger .info (" Successfully 2 -> 3" )
137
188
pass
138
189
139
-
140
190
def _upgradeFrom1To2 (self ):
141
191
self ._logger .info (" Starting 1 -> 2" )
142
192
# What is changed:
@@ -310,7 +360,14 @@ def showSQLLogging(self, enabled):
310
360
def backupDatabaseFile (self , backupFolder ):
311
361
now = datetime .datetime .now ()
312
362
currentDate = now .strftime ("%Y%m%d-%H%M" )
313
- backupDatabaseFileName = "printJobHistory-backup-" + currentDate + ".db"
363
+ currentSchemeVersion = "unknown"
364
+ try :
365
+ currentSchemeVersion = PluginMetaDataModel .get (PluginMetaDataModel .key == PluginMetaDataModel .KEY_DATABASE_SCHEME_VERSION )
366
+ if (currentSchemeVersion != None ):
367
+ currentSchemeVersion = str (currentSchemeVersion .value )
368
+ except Exception as e :
369
+ self ._logger .exception ("Could not read databasescheme version:" + str (e ))
370
+ backupDatabaseFileName = "printJobHistory-backup-V" + currentSchemeVersion + "-" + currentDate + ".db"
314
371
backupDatabaseFilePath = os .path .join (backupFolder , backupDatabaseFileName )
315
372
if not os .path .exists (backupDatabaseFilePath ):
316
373
shutil .copy (self ._databaseFileLocation , backupDatabaseFilePath )
@@ -435,8 +492,7 @@ def calculatePrintJobsStatisticByQuery(self, tableQuery):
435
492
else :
436
493
statusDict [statusResult ] = 1
437
494
438
- job .loadFilamentsFromAssoziation ()
439
- allFilaments = job .allFilaments
495
+ allFilaments = job .getFilamentModels ()
440
496
if allFilaments != None :
441
497
for filla in allFilaments :
442
498
if (StringUtils .isEmpty (filla .usedLength ) == False ):
@@ -462,8 +518,6 @@ def calculatePrintJobsStatisticByQuery(self, tableQuery):
462
518
else :
463
519
materialDict [filla .material ] = 1
464
520
465
-
466
-
467
521
# do formatting
468
522
queryString = self ._buildQueryString (tableQuery )
469
523
fromToString = firstDate .strftime ('%d.%m.%Y %H:%M' ) + " - " + lastDate .strftime ('%d.%m.%Y %H:%M' )
@@ -616,11 +670,12 @@ def _addTableQueryToSelect(self, myQuery, tableQuery):
616
670
sortOrder = tableQuery ["sortOrder" ]
617
671
filterName = tableQuery ["filterName" ]
618
672
673
+ # - status
619
674
if (filterName == "onlySuccess" ):
620
675
myQuery = myQuery .where (PrintJobModel .printStatusResult == "success" )
621
676
elif (filterName == "onlyFailed" ):
622
677
myQuery = myQuery .where (PrintJobModel .printStatusResult != "success" )
623
-
678
+ # -sorting
624
679
if ("printStartDateTime" == sortColumn ):
625
680
if ("desc" == sortOrder ):
626
681
myQuery = myQuery .order_by (PrintJobModel .printStartDateTime .desc ())
@@ -631,6 +686,7 @@ def _addTableQueryToSelect(self, myQuery, tableQuery):
631
686
myQuery = myQuery .order_by (fn .Lower (PrintJobModel .fileName ).desc ())
632
687
else :
633
688
myQuery = myQuery .order_by (fn .Lower (PrintJobModel .fileName ))
689
+ # - date range
634
690
if ("startDate" in tableQuery ):
635
691
startDate = tableQuery ["startDate" ]
636
692
endDate = tableQuery ["endDate" ]
@@ -644,6 +700,12 @@ def _addTableQueryToSelect(self, myQuery, tableQuery):
644
700
# ((PrintJobModel.printStartDateTime == endDate) | ( PrintJobModel.printStartDateTime < startDate)) )
645
701
myQuery = myQuery .where ( ( ( PrintJobModel .printStartDateTime > startDateTime ) & ( PrintJobModel .printStartDateTime < endDateTime ))
646
702
)
703
+ # - search query (only filename)
704
+ if ("searchQuery" in tableQuery ):
705
+ searchQueryValue = tableQuery ["searchQuery" ]
706
+ if (len (searchQueryValue ) > 0 ):
707
+ myQuery = myQuery .where (PrintJobModel .fileName .contains (searchQueryValue ))
708
+ pass
647
709
return myQuery
648
710
649
711
0 commit comments