def get_ref(self, start_time, end_time): """ get reference lines for data :param start_time: :param end_time: :return: """ period = self.get_abstract().period start_time = utils.iceil(start_time, period) end_time = utils.iceil(end_time, period) ref_names = db.session.query(db.distinct(Point.name)).all() lines = [] for ref_name, in ref_names: points = Point.query.filter( db.and_(Point.data_id.is_(self.get_id()), Point.name.is_(ref_name), Point.timestamp.between(start_time, end_time))) points = {point.timestamp: point for point in points} line = [] for timestamp in range(start_time, end_time, period): if timestamp in points: line.append((timestamp, points[timestamp].value, points[timestamp].range)) else: line.append((timestamp, None, None)) lines.append((ref_name, line)) return lines
def get_line(self, start_time, end_time): """ get a slice of raw points of data :param start_time: :param end_time: :return: """ period = self.get_abstract().period start_time = utils.iceil(start_time, period) end_time = utils.iceil(end_time, period) data_raw = Raw.query.filter( db.and_(Raw.data_id.is_(self.get_id()), Raw.timestamp.between(start_time, end_time))) # data_raw = {point.timestamp: point for point in data_raw} line = [ Raw(timestamp=point.timestamp, value=point.value) for point in data_raw ] # line = [] # for timestamp in range(start_time, end_time, period): # if timestamp in data_raw: # line.append(data_raw[timestamp]) # else: # line.append(Raw(timestamp=timestamp)) return line
def _get_bands(self, data_service, base_line, start_time, end_time): band_service = BandService(data_service.get_id()) band_names = band_service.get_band_names() window = end_time - start_time # use aggr period period = data_service.get_period() y_axis_max = data_service.get_abstract().y_axis_max bands, lines = [], [] for band_name, in band_names: band_name = urllib.unquote(band_name) band_items = band_service.get_band_items(band_name, start_time, end_time) band = {'name': band_name, 'bands': []} line = {'name': band_name, 'type': 'area', 'data': []} if len(band_items) == 0: bands.append(band) lines.append(line) continue tmp = set([]) for band_item in band_items: for x in range( utils.iceil(band_item.start_time, period) - period / 2, utils.ifloor(band_item.end_time, period) + period / 2, period): tmp.add(x) for timestamp in range( utils.iceil(start_time, period) - period / 2, utils.ifloor(end_time, period) + period / 2, period): if timestamp in tmp: line['data'].append([timestamp * 1000, y_axis_max]) else: line['data'].append([timestamp * 1000, None]) lines.append(line) band_count = band_service.get_band_count(band_name) pre_band = band_service.get_band_item(band_name, band_items[0].index - 1) next_band = band_service.get_band_item(band_name, band_items[-1].index - 1) band_items = [ band_item.view(band_count, window) for band_item in band_items ] for x in range(1, len(band_items) - 1): band_items[x]['preTime'] = band_items[x - 1]['currentTime']['show'] band_items[x]['nextTime'] = band_items[ x + 1]['currentTime']['show'] if pre_band: band_items[0]['preTime'] = pre_band.view( band_count, window)['currentTime']['show'] if pre_band: band_items[0]['nextTime'] = next_band.view( band_count, window)['currentTime']['show'] band['bands'] = band_items return bands, lines
def sampling(api, line, target_amount): """ default sampling plugin :param api: plugin api :param line: data raws :param target_amount: target amount of points :return: """ # Assume timestamp, value, range is not nullable if len(line) > target_amount and len(line) > 2: period = api.get_abstract( ).period # timestamp is marked as the start time of a period start_time = line[0][0] end_time = line[-1][0] amount = (end_time - start_time) / period # point amount without sampling aggr_period = utils.iceil(amount, target_amount) / target_amount * period start_time = utils.ifloor(line[0][0], aggr_period) tmp = { timestamp: [] for timestamp in range(start_time, end_time + period, aggr_period) } for point in line: tmp[utils.ifloor(point[0], aggr_period)].append(point) line = [[ timestamp, utils.mean(points, lambda x: x[1]), utils.mean(points, lambda x: x[2]) ] for timestamp, points in sorted(tmp.items())] return 'default', line