def read_node_history(self, node_id, start, end, nb_values): with self._lock: _c_read = self._conn.cursor() table = self._get_table_name(node_id) start_time, end_time, order, limit = self._get_bounds( start, end, nb_values) cont = None results = [] # select values from the database; recreate UA Variant from binary try: for row in _c_read.execute( 'SELECT * FROM "{tn}" WHERE "ServerTimestamp" BETWEEN ? AND ? ' 'ORDER BY "_Id" {dir} LIMIT ?'.format(tn=table, dir=order), ( start_time, end_time, limit, )): # rebuild the data value object dv = ua.DataValue(ua.Variant.from_binary(Buffer(row[6]))) dv.ServerTimestamp = row[1] dv.SourceTimestamp = row[2] dv.StatusCode = ua.StatusCode(row[3]) results.append(dv) except sqlite3.Error as e: self.logger.error('Historizing SQL Read Error for %s: %s', node_id, e) if nb_values: if len(results) > nb_values: cont = results[nb_values].ServerTimestamp results = results[:nb_values] return results, cont
def read_event_history(self, source_id, start, end, nb_values, evfilter): with self._lock: _c_read = self._conn.cursor() table = self._get_table_name(source_id) start_time, end_time, order, limit = self._get_bounds(start, end, nb_values) clauses, clauses_str = self._get_select_clauses(source_id, evfilter) cont = None cont_timestamps = [] results = [] # select events from the database; SQL select clause is built from EventFilter and available fields try: for row in _c_read.execute( 'SELECT "_Timestamp", {cl} FROM "{tn}" WHERE "_Timestamp" BETWEEN ? AND ? ORDER BY "_Id" {dir} LIMIT ?' .format(cl=clauses_str, tn=table, dir=order), (start_time, end_time, limit)): fdict = {} cont_timestamps.append(row[0]) for i, field in enumerate(row[1:]): if field is not None: fdict[clauses[i]] = variant_from_binary(Buffer(field)) else: fdict[clauses[i]] = ua.Variant(None) results.append(events.Event.from_field_dict(fdict)) except sqlite3.Error as e: self.logger.error('Historizing SQL Read Error events for node %s: %s', source_id, e) if nb_values: if len(results) > nb_values: # start > ua.get_win_epoch() and cont = cont_timestamps[nb_values] results = results[:nb_values] return results, cont
def read_event_history(self, source_id, start, end, nb_values, evfilter): with self._lock: _c_read = self._conn.cursor() order = "ASC" if start is None or start == ua.DateTimeMinValue: order = "DESC" start = ua.DateTimeMinValue if end is None or end == ua.DateTimeMinValue: end = datetime.utcnow() + timedelta(days=1) if start < end: start_time = start.isoformat(' ') end_time = end.isoformat(' ') else: order = "DESC" start_time = end.isoformat(' ') end_time = start.isoformat(' ') if nb_values: limit = nb_values + 1 # add 1 to the number of values for retrieving a continuation point else: limit = -1 # in SQLite a LIMIT of -1 returns all results table = self._get_table_name(source_id) clauses, clauses_str = self._get_select_clauses( source_id, evfilter) cont = None cont_timestamps = [] results = [] # select events from the database; SQL select clause is built from EventFilter and available fields try: for row in _c_read.execute( 'SELECT "_Timestamp", {cl} FROM "{tn}" WHERE "_Timestamp" BETWEEN ? AND ? ORDER BY "_Id" {dir} LIMIT ?' .format(cl=clauses_str, tn=table, dir=order), (start_time, end_time, limit)): fdict = {} cont_timestamps.append(row[0]) for i, field in enumerate(row[1:]): if field is not None: fdict[clauses[i]] = ua.Variant.from_binary( Buffer(field)) else: fdict[clauses[i]] = ua.Variant(None) results.append(events.Event.from_field_dict(fdict)) except sqlite3.Error as e: self.logger.error( 'Historizing SQL Read Error events for node %s: %s', source_id, e) if nb_values: if len(results) > nb_values: # start > ua.DateTimeMinValue and cont = cont_timestamps[nb_values] results = results[:nb_values] return results, cont
def read_node_history(self, node_id, start, end, nb_values): with self._lock: _c_read = self._conn.cursor() order = "ASC" if start is None or start == ua.DateTimeMinValue: order = "DESC" start = ua.DateTimeMinValue if end is None or end == ua.DateTimeMinValue: end = datetime.utcnow() + timedelta(days=1) if start < end: start_time = start.isoformat(' ') end_time = end.isoformat(' ') else: order = "DESC" start_time = end.isoformat(' ') end_time = start.isoformat(' ') if nb_values: limit = nb_values + 1 # add 1 to the number of values for retrieving a continuation point else: limit = -1 # in SQLite a LIMIT of -1 returns all results table = self._get_table_name(node_id) cont = None results = [] # select values from the database; recreate UA Variant from binary try: for row in _c_read.execute( 'SELECT * FROM "{tn}" WHERE "ServerTimestamp" BETWEEN ? AND ? ' 'ORDER BY "_Id" {dir} LIMIT ?'.format(tn=table, dir=order), ( start_time, end_time, limit, )): # rebuild the data value object dv = ua.DataValue(ua.Variant.from_binary(Buffer(row[6]))) dv.ServerTimestamp = row[1] dv.SourceTimestamp = row[2] dv.StatusCode = ua.StatusCode(row[3]) results.append(dv) except sqlite3.Error as e: self.logger.error('Historizing SQL Read Error for %s: %s', node_id, e) if nb_values: if len(results) > nb_values: cont = results[nb_values].ServerTimestamp results = results[:nb_values] return results, cont
def read_event_history(self, source_id, start, end, nb_values, evfilter): with self._lock: _c_read = self._conn.cursor() order = "ASC" if start is None or start == ua.DateTimeMinValue: order = "DESC" start = ua.DateTimeMinValue if end is None or end == ua.DateTimeMinValue: end = datetime.utcnow() + timedelta(days=1) if start < end: start_time = start.isoformat(' ') end_time = end.isoformat(' ') else: order = "DESC" start_time = end.isoformat(' ') end_time = start.isoformat(' ') if nb_values: limit = nb_values + 1 # add 1 to the number of values for retrieving a continuation point else: limit = -1 # in SQLite a LIMIT of -1 returns all results table = self._get_table_name(source_id) clauses = self._get_select_clauses(source_id, evfilter) cont = None cont_timestamps = [] results = [] # select events from the database; SQL select clause is built from EventFilter and available fields try: for row in _c_read.execute('SELECT "_Timestamp", {cl} FROM "{tn}" WHERE "_Timestamp" BETWEEN ? AND ? ' 'ORDER BY "_Id" {dir} LIMIT ?'.format(cl=clauses, tn=table, dir=order), (start_time, end_time, limit,)): # place all the variants in the event field list object hist_ev_field_list = ua.HistoryEventFieldList() i = 0 for field in row: # if the field is the _Timestamp column store it in a list used for getting the continuation if i == 0: cont_timestamps.append(field) else: if field is not None: hist_ev_field_list.EventFields.append(ua.Variant.from_binary(Buffer(field))) else: hist_ev_field_list.EventFields.append(ua.Variant(None)) i += 1 results.append(hist_ev_field_list) except sqlite3.Error as e: self.logger.error('Historizing SQL Read Error events for node %s: %s', source_id, e) if nb_values: if len(results) > nb_values: # start > ua.DateTimeMinValue and cont = cont_timestamps[nb_values] results = results[:nb_values] return results, cont