1414import json
1515import psutil
1616import copy
17+ import gzip
1718
1819try :
1920 import xlsxwriter
@@ -955,10 +956,14 @@ def pushToDatabase(self):
955956 newData [sigID ][4 ] = localData [sigID ][4 ]
956957 newData [sigID ][2 ] = list (localData [sigID ][2 ])[int (idx ):]
957958 newData [sigID ][3 ] = list (localData [sigID ][3 ])[int (idx ):]
959+ if self .logger .config ['backup' ]['resample' ] != 0 :
960+ newData [sigID ][2 ], newData [sigID ][3 ] = resample (newData [sigID ][2 ], newData [sigID ][3 ], self .logger .config ['backup' ]['resample' ])
958961 # new = True
959962 break
960963 else :
961964 newSignals [sigID ] = localData [sigID ]
965+ if self .logger .config ['backup' ]['resample' ] != 0 :
966+ newSignals [sigID ][2 ], newSignals [sigID ][3 ] = resample (newSignals [sigID ][2 ], newSignals [sigID ][3 ], self .logger .config ['backup' ]['resample' ])
962967 # if not new:
963968 # popIDs.append(sigID)
964969
@@ -1099,6 +1104,7 @@ def _commit(self, query):
10991104 logging .info ("Error while execution+commit in PostgreSQL table" )
11001105 logging .error (error )
11011106 logging .error (query )
1107+ logging .error (traceback .format_exc ())
11021108 ans = False
11031109
11041110 def _getSQLSignalUnit (self , sigID ):
@@ -1148,9 +1154,13 @@ def _SQLdeviceExists(self, devicename):
11481154 sig = False
11491155 return sig
11501156
1151- def _SQLsignalExists (self , signalname ):
1157+ def _SQLsignalExists (self , signalname , devicename ):
1158+ if self ._SQLdeviceExists (devicename ):
1159+ devID = self ._SQLgetDeviceID (devicename )
1160+ else :
1161+ return False
11521162 existtest = "SELECT EXISTS (select ID from " + SIGNAL_TABLE_NAME + \
1153- " where NAME = '" + str (signalname )+ "');"
1163+ " where NAME = '" + str (signalname )+ "' and DEVICE_ID = " + str ( devID ) + " );"
11541164 sig = self ._execute_n_fetchall (existtest )
11551165 if sig is not None and sig != []:
11561166 sig = bool (sig [0 ][0 ])
@@ -1351,9 +1361,9 @@ def _SQLplotNewData(self, x, y, dataunit, devicename, signalname, createCallback
13511361 logging .error ('mergeY is NOT IMPLEMENTED YET for postgresql' )
13521362 else :
13531363 sql = 'UPDATE ' + SIGNAL_TABLE_NAME + ' SET X = ARRAY' + \
1354- str (x )+ '::NUMERIC[] WHERE ID =' + str (sigID )+ ';'
1364+ str (list ( x ) )+ '::NUMERIC[] WHERE ID =' + str (sigID )+ ';'
13551365 sql += '\n UPDATE ' + SIGNAL_TABLE_NAME + \
1356- ' SET Y = ARRAY' + str (y )+ '::NUMERIC[] WHERE ID =' + str (sigID )+ ';'
1366+ ' SET Y = ARRAY' + str (list ( y ) )+ '::NUMERIC[] WHERE ID =' + str (sigID )+ ';'
13571367 sql += '\n UPDATE ' + SIGNAL_TABLE_NAME + ' SET UNIT = \' ' + \
13581368 str (dataunit )+ '\' WHERE ID =' + str (sigID )+ ';'
13591369 self ._execute_n_commit (sql )
@@ -1362,8 +1372,13 @@ def _SQLplotNewData(self, x, y, dataunit, devicename, signalname, createCallback
13621372 def _SQLExportCSV (self , filenamepart ):
13631373 filenames = []
13641374 for table in [DEVICE_TABLE_NAME , SIGNAL_TABLE_NAME , EVENT_TABLE_NAME ]:
1365- filename = filenamepart + '_' + table + ".csv"
1366- sql = "COPY " + table + " TO '" + filename + "' DELIMITER ',' CSV HEADER;"
1375+ filename = filenamepart + '_' + table + ".gz"
1376+ #sql = "COPY "+table+" TO '"+filename+"' DELIMITER ',' CSV HEADER;"
1377+ #ans = self._execute_n_fetchall(sql)
1378+ #print(ans)
1379+ with gzip .open (filename , 'wb' ) as gzip_file :
1380+ #cursor.copy_to(gzip_file, 'my_table')
1381+ self ._cursor .copy_to (gzip_file , table , sep = "|" )
13671382 filenames .append (filename )
13681383 return filenames
13691384
@@ -1862,7 +1877,10 @@ def getSignalInfo(self, sigID, database=True):
18621877 xmax = max ([xmaxLocal , xmax ])
18631878 sigLen = max ([sigLenLocal , sigLen ])
18641879
1865- return float (xmin ), float (xmax ), int (sigLen )
1880+ if xmin is None or xmax is None or sigLen is None :
1881+ return 0 ,0 ,0
1882+ else :
1883+ return float (xmin ), float (xmax ), int (sigLen )
18661884
18671885 def removeSignal (self , sigID , xmin = None , xmax = None , database = False ):
18681886 """
@@ -2011,10 +2029,15 @@ def getSignal(self, sigID, xmin=None, xmax=None, database=False, maxN=None):
20112029 if signal is None :
20122030 signal = signal_local
20132031 else :
2014- for idx , x in enumerate (signal_local [2 ]):
2015- if x > signal [2 ][- 1 ]:
2016- signal [2 ] += list (signal_local [2 ])[idx :]
2017- signal [3 ] += list (signal_local [3 ])[idx :]
2032+ # for idx, x in enumerate(signal_local[2]):
2033+ # if x > signal[2][-1]:
2034+ # signal[2] += list(signal_local[2])[idx:]
2035+ # signal[3] += list(signal_local[3])[idx:]
2036+ # break
2037+ for idx , x in enumerate (signal [2 ]):
2038+ if signal_local [2 ][0 ] < x :
2039+ signal [2 ] = signal [2 ][:idx ]+ list (signal_local [2 ])
2040+ signal [3 ] = signal [3 ][:idx ]+ list (signal_local [3 ])
20182041 break
20192042
20202043 if signal is not None :
@@ -2167,12 +2190,14 @@ def resampleDatabase(self, samplerate):
21672190 signals = {signal [0 ]: list (signal )[1 :] for signal in signals }
21682191
21692192 for sigID in signals .keys ():
2193+ signals [sigID ][0 ] = [float (i ) for i in signals [sigID ][0 ]]
2194+ signals [sigID ][1 ] = [float (i ) for i in signals [sigID ][1 ]]
21702195 s = signals [sigID ]
21712196 if len (s [0 ])> 2 :
21722197 xrange = s [0 ][- 1 ]- s [0 ][0 ]
2173- n = samplerate * xrange
2198+ n = samplerate * float ( xrange )
21742199 if len (s [0 ]) == len (s [1 ]):
2175- x = np .linspace (s [0 ][0 ], s [0 ][- 1 ], n )
2200+ x = np .linspace (float ( s [0 ][0 ]), float ( s [0 ][- 1 ]) , n )
21762201 y = np .interp (x , s [0 ], s [1 ])
21772202
21782203 self ._SQLplot (x = x , y = y , sigID = sigID )
@@ -2202,3 +2227,13 @@ def column(matrix, i):
22022227 if i < len (row ):
22032228 ans .append (row [i ])
22042229 return ans
2230+
2231+
2232+ def resample (x ,y , samplerate ):
2233+ # xlen = len(x)
2234+ xtime = x [- 1 ]- x [0 ]
2235+ n = samplerate * xtime
2236+ x2 = np .linspace (x [0 ], x [- 1 ], n )
2237+ y = np .interp (x2 , x , y )
2238+
2239+ return list (x2 ), list (y )
0 commit comments