Exemplo n.º 1
0
    def put(self, dataName):
        """
        ref: web_api.yaml
        :param dataName:
        :return:
        """
        data_name = utils.encode_if_unicode(dataName)
        data_service = DataService(data_name)
        if not data_service.exists():
            return self.render(msg='%s not found' % data_name, status=404)
        if not data_service.auth_manage():
            return self.render(msg='%s: data access forbidden' % data_name, status=403)
        data_abstract = data_service.get_abstract()
        if 'public_read' in flask.g.args:
            public_read = flask.g.args['public_read']
            if not public_read:
                data_abstract.public_edit = False
            data_abstract.public_read = public_read
        if 'public_edit' in flask.g.args:
            public_edit = flask.g.args['public_edit']
            if public_edit:
                data_abstract.public_read = True
            data_abstract.public_edit = public_edit
        data_service.update_privilege(data_abstract.public_read, data_abstract.public_edit)

        return self.render()
Exemplo n.º 2
0
    def get(self, dataName):
        """
        ref: web_api.yaml
        :param dataName:
        :return:
        """
        # parse params in request
        data_name = utils.encode_if_unicode(dataName)
        data_service = DataService(data_name)
        if not data_service.exists():
            return self.render(msg='%s not found' % data_name, status=404)
        if not data_service.auth_read():
            return self.render(msg='%s: data access forbidden' % data_name,
                               status=403)
        plugin = Plugin(data_service)
        start_time = g.args['startTime'] / 1000
        end_time = g.args['endTime'] / 1000
        # get raw
        line = data_service.get_line(start_time, end_time)
        # get base line
        base_line = self._get_raw(plugin, line)
        # get label line
        label_line = self._get_label(line, base_line['data'])
        # get ref
        ref_lines = self._get_refs(data_service, plugin, start_time, end_time)
        # get band
        bands, band_lines = self._get_bands(data_service, base_line['data'],
                                            start_time, end_time)

        trends = [base_line, label_line] + ref_lines + band_lines
        return self.render(data={
            'trends': trends,
            'bands': bands,
            'yAxis': data_service.get_y_axis()
        })
Exemplo n.º 3
0
 def delete(self, dataName):
     """
     ref: web_api.yaml
     :param dataName:
     :return:
     """
     data_name = utils.encode_if_unicode(dataName)
     data_service = DataService(data_name)
     if not data_service.exists():
         return self.render(msg='%s not found' % data_name, status=404)
     if not data_service.auth_edit():
         return self.render(msg='%s: data access forbidden' % data_name, status=403)
     data_service.delete()
     return self.render()
Exemplo n.º 4
0
    def put(self, dataName):
        """
        ref: web_api.yaml
        :param dataName:
        :return:
        """
        data_name = utils.encode_if_unicode(dataName)
        data_service = DataService(data_name)
        if not data_service.exists():
            return self.render(msg='%s not found' % data_name, status=404)
        if not data_service.auth_edit():
            return self.render(msg='%s: data access forbidden' % data_name,
                               status=403)
        start_time = g.args['startTime'] / 1000
        end_time = g.args['endTime'] / 1000
        label = g.args['label']

        data_service.set_label(start_time, end_time, label)

        return self.render()
Exemplo n.º 5
0
    def put(self, dataName):
        """
        ref: web_api.yaml
        :param dataName:
        :return:
        """
        data_name = utils.encode_if_unicode(dataName)
        data_service = DataService(data_name)
        if not data_service.exists():
            return self.render(msg='%s not found' % data_name, status=404)
        if not data_service.auth_edit():
            return self.render(msg='%s: data access forbidden' % data_name, status=403)
        plugin = Plugin(data_service)

        start_time = g.args['startTime'] / 1000
        end_time = g.args['endTime'] / 1000
        action = g.args['action']

        res = plugin(action, start_time, end_time)

        return self.render(data=res)
Exemplo n.º 6
0
    def get(self, dataName):
        """
        ref: web_api.yaml
        :param dataName:
        :return:
        """
        data_name = utils.encode_if_unicode(dataName)
        data_service = DataService(data_name)
        if not data_service.exists():
            return self.render(msg='%s not found' % data_name, status=404)
        if not data_service.auth_read():
            return self.render(msg='%s: data access forbidden' % data_name, status=403)
        data_abstract = data_service.get_abstract()
        data_raw = data_service.get_raw()
        time_format = getattr(utils, data_abstract.time_formatter)
        data_raw = [raw.view(time_format) for raw in data_raw]
        string_io = StringIO()
        writer = csv.writer(string_io)
        writer.writerow(self.csv_header)
        writer.writerows(data_raw)

        return self.render_file('%s.csv' % data_name, string_io.getvalue())
Exemplo n.º 7
0
    def post(self, dataName):
        """
        ref: web_api.yaml
        :param dataName:
        :return:
        """
        data_name = utils.encode_if_unicode(dataName)
        data_service = DataService(data_name)
        if data_service.exists():
            return self.render(msg='%s is exists' % data_name, status=422)
        if len(request.files) < 1:
            return self.render(msg='expect file input', status=422)
        upload_file = request.files.values()[0]
        try:
            # parses the incoming csv into long format
            # points, time_formatter = self._parse_file(upload_file)  # parse data in csv
            points, time_formatter = self._parse_file_pd(
                upload_file)  # parse data in csv
        except Exception as e:
            return self.render(msg=e.message, status=422)
        if len(points) < 2:
            return self.render(msg='at least 2 point', status=422)

        timestamps = np.asarray(sorted(points.keys()))
        periods = np.diff(timestamps)
        period = int(np.median(periods))
        start_time = utils.ifloor(timestamps.min(), period)
        end_time = utils.ifloor(timestamps.max(), period) + period

        # # drop those points not divisible by period
        # data_raw = []
        # data_raw_list = []
        # in_points = 0
        # for timestamp in range(start_time, end_time, period):
        #     if timestamp in points:
        #         point = points[timestamp]
        #         data_raw.append(
        #             Raw(timestamp=point[0], value=point[1], label=point[2]))
        #         data_raw_list.append((point[0], point[1], None))
        #         in_points += 1
        #     else:
        #         data_raw.append(Raw(timestamp=timestamp))
        #         data_raw_list.append((timestamp, None, None))

        # use raw points regardless of divisibility by period
        data_raw = []
        data_raw_list = []
        for timestamp, point in points.iteritems():
            data_raw.append(
                Raw(timestamp=point[0], value=point[1], label=point[2]))
            data_raw_list.append((point[0], point[1], None))

        logger.debug("""
start_time: {},
end_time: {},
period: {}""".format(start_time, end_time, period))

        plugin = Plugin(data_service)
        try:
            _, (axis_min,
                axis_max) = plugin('y_axis',
                                   data_raw_list)  # cal y_axis for data
        except Exception as e:
            logger.error(
                "Error implementing 'y_axis' plugin\nError: {}".format(
                    e.message))
            raise e

        data_abstract = DataAbstract(  # save abstract for data
            start_time=start_time,
            end_time=end_time,
            y_axis_min=axis_min,
            y_axis_max=axis_max,
            period=period,
            period_ratio=len(periods[periods == period]) * 1. / len(periods),
            label_ratio=sum([1 for point in data_raw if point.label]) * 1. /
            len(data_raw),
            time_formatter=time_formatter.__name__)
        data_service.abstract = data_abstract

        try:
            _, thumb = plugin('sampling', data_raw_list,
                              config.SAMPLE_PIXELS)  # init thumb for data
        except Exception as e:
            logger.error("Error calling 'sampling' plugin\nError: {}".format(
                e.message))
            raise e

        thumb = Thumb(thumb=json.dumps(
            {
                'msg': 'OK',
                'server': request.host,
                'traceId': '',
                'data': {
                    'data': [(point[0] * 1000, point[1]) for point in thumb],
                    'name': 'thumb',
                    'type': 'line'
                }
            },
            ensure_ascii=False))
        refs = plugin('reference', data_raw_list)  # init ref for data
        bands = plugin('init_band', data_raw_list)  # init band
        data_service.set(data_abstract, data_raw, thumb, refs, bands)
        return self.render(data=data_service.get_abstract().view(),
                           status=201,
                           header={'Location': '/v1/data/%s' % data_name})
Exemplo n.º 8
0
 def post(self, dataName):
     """
     ref: web_api.yaml
     :param dataName:
     :return:
     """
     data_name = utils.encode_if_unicode(dataName)
     data_service = DataService(data_name)
     if data_service.exists():
         return self.render(msg='%s is exists' % data_name, status=422)
     if len(request.files) < 1:
         return self.render(msg='expect file input', status=422)
     upload_file = request.files['file']
     current_app.logger.info('Info: %s %s %s', request.files['file'], request.files, request.files['file'].filename)
     try:
         points, time_formatter = self._parse_file(upload_file)  # parse data in csv
     except Exception as e:
         return self.render(msg=str(e), status=422)
     if len(points) < 2:
         return self.render(msg='at least 2 point', status=422)
     timestamps = np.asarray(sorted(points.keys()))
     periods = np.diff(timestamps)
     period = int(np.median(periods))
     start_time = utils.ifloor(timestamps.min(), period)
     end_time = utils.ifloor(timestamps.max(), period) + period
     data_raw = []  # drop those points not divisible by period
     data_raw_list = []
     for timestamp in range(start_time, end_time, period):
         if timestamp in points:
             point = points[timestamp]
             data_raw.append(
                 Raw(timestamp=point[0], value=point[1], label=point[2]))
             data_raw_list.append((point[0], point[1], None))
         else:
             data_raw.append(Raw(timestamp=timestamp))
             data_raw_list.append((timestamp, None, None))
     plugin = Plugin(data_service)
     #for item in data_raw_list:
     _, (axis_min, axis_max) = plugin('y_axis', data_raw_list)  # cal y_axis for data
     data_abstract = DataAbstract(  # save abstract for data
         start_time=start_time,
         end_time=end_time,
         y_axis_min=axis_min,
         y_axis_max=axis_max,
         period=period,
         period_ratio=len(periods[periods == period]) * 1. / len(periods),
         label_ratio=sum([1 for point in data_raw if point.label]) * 1. / len(data_raw),
         time_formatter=time_formatter.__name__
     )
     data_service.abstract = data_abstract
     _, thumb = plugin('sampling', data_raw_list, config.SAMPLE_PIXELS)  # init thumb for data
     thumb = Thumb(thumb=json.dumps({
         'msg': 'OK',
         'server': request.host,
         'traceId': '',
         'data': {
             'data': [(point[0] * 1000, point[1]) for point in thumb],
             'name': 'thumb',
             'type': 'line'
         }
     }, ensure_ascii=False))
     refs = plugin('reference', data_raw_list)  # init ref for data
     bands = plugin('init_band', data_raw_list)  # init band
     data_service.set(data_abstract, data_raw, thumb, refs, bands)
     return self.render(
         data=data_service.get_abstract().view(),
         status=201,
         header={'Location': '/v1/data/%s' % data_name}
     )