Example #1
0
    def test_return_empty_params(self):
        """
        Test sending empty param's return themes
        """
        themes = Theme.get_all()
        response = self.testing_client.get('/data')

        self.assertEqual([a.json() for a in themes], response.get_json())
 def get_all_themes(self, user_id: int = None) -> None:
     """
     Create Theme Tree containing all Themes, SubThemes, Attributes and Attribute Aliases if user_id exists
     :param user_id: User Id for Attribute Aliases
     """
     theme_ids = {theme.id for theme in Theme.get_all()}
     self.response.extend([
         resp for resp in [
             self.create_theme_tree(theme_id, user_id)
             for theme_id in theme_ids
         ] if resp
     ])
    def get(self) -> ([Theme], HTTPStatus):
        """
        Fetch Themes
        Parameters can be passed using a POST request that contains a JSON with the following fields:
        :param limit:   the maximum number of entries to return
        :param name:    themes name
        :param id:      the themes identification number
        :type limit:    int
        :type name:     str
        :type id:       str

        :return: a list of Theme/s and an HTTPStatus code of 200 on success otherwise an JSON with an error message
                 and appropriate http status
        """
        # Get arguments passed in POST request
        args = self.reqparser.parse_args()

        # fetch themes using arguments passed in post request
        themes = []
        if "id" in args:
            theme = Theme.get_by_id(args["id"])
            if theme:
                themes.append(theme.json())
        elif "name" in args:
            theme = Theme.get_by_name(args["name"])
            if theme:
                themes.append(theme.json())
        else:
            [themes.append(theme.json()) for theme in Theme.get_all()]

        # were any themes found in the database?
        if len(themes) < 1:
            # no themes were found
            return [], HTTPStatus.OK

        if "limit" in args:
            try:
                themes = themes[:int(args["limit"])]
            except ValueError:
                return {"error": "Limit parsed is not an int"}, HTTPStatus.BAD_REQUEST

        # themes were found
        return themes, HTTPStatus.OK
    def get(self):
        args = self.parser.parse_args()
        theme, subtheme = None, None

        if "subtheme" in args:
            subtheme = args['subtheme']
            if subtheme is not None and subtheme != '':
                attributes = Attributes.get_by_sub_theme_id(subtheme)
                return [a.json() for a in attributes], 200
        elif "theme" in args:
            theme = args['theme']
            if theme != "":
                subthemes = SubTheme.get_by_theme_id(theme)
                return [a.json() for a in subthemes], 200

        if theme is None and subtheme is None:
            themes = Theme.get_all()
            return [a.json() for a in themes], 200

        return {"error": "error occured while processing request"}, 400
	def get(self):
		args = self.parser.parse_args()
		theme, subtheme, attribute_data, sensor, sensor_name, sensor_attribute, attributes, sensorid, n_predictions, predictions, grouped, harmonising_method, per_sensor, freq = None, None, None, None, None, None, [], None, 100, None, None, None, None, '1H'

		if 'theme' in args:
			theme = args['theme']

		if 'subtheme' in args:
			subtheme = args['subtheme']

		if 'attributedata' in args:
			attribute_data = args['attributedata']

		if 'attribute' in args and args['attribute'] is not None:
			_attributes = args['attribute']
			if _attributes != '':
				attributes = _attributes.split(',')

		if 'sensor' in args and args['sensor'] is not None:
			sensor = args['sensor']
			if sensor != '':
				if sensor == 'all':
					sensors = Sensor.get_all()
					return [a.json() for a in sensors], 200
				else:
					return (Sensor.get_by_id(sensor)).json(), 200

		if 'sensorname' in args and args['sensorname'] is not None:
			sensor_name = args['sensorname']
			if sensor_name != '':
				_sensors = sensor_name.split(',')
				_by_name = Sensor.get_by_name_in(_sensors)
				return [a.json() for a in _by_name], 200

		if 'sensorattribute' in args and args['sensorattribute'] is not None:
			sensor_attribute = args['sensorattribute']
			if sensor_attribute != '':
				_sen_attrs_ids = sensor_attribute.split(',')
				_sen_attrs = SensorAttribute.get_by_id_in(_sen_attrs_ids)
				attrs_ids = [_id.a_id for _id in _sen_attrs]
				_attributes = Attributes.get_by_id_in(attrs_ids)
				return [a.json() for a in _attributes], 200

		if 'grouped' in args:
			grouped = args['grouped']

		if 'harmonising_method' in args:
			harmonising_method = args['harmonising_method']

		if 'per_sensor' in args:
			per_sensor = args['per_sensor']

		if 'freq' in args:
			freq = args['freq']

		if 'predictions' in args:
			predictions = args['predictions']
			if predictions >=100:
				predictions = 100

		if 'n_predictions' in args:
			n_predictions = args['n_predictions']

		if 'sensorid' in args:
			sensorid = args['sensorid']

		if theme is None and subtheme is None \
			and len(attributes) == 0 and attribute_data is None \
			and sensor is None and sensor_name is None and sensor_attribute is None:
			themes = Theme.get_all()
			return [a.json() for a in themes], 200

		if attribute_data is not None:
			global LIMIT, OFFSET
			data = None
			operation = None
			if 'limit' in args and args['limit'] is not None:
				LIMIT = args['limit']

			if 'offset' in args and args['offset'] is not None:
				OFFSET = args['offset']

			if 'operation' in args and args['operation'] is not None:
				operation = args['operation']

			if ('fromdate' in args and args['fromdate'] is not None 
				and 'todate' in args and args['todate'] is not None):
				data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, 
												args['fromdate'], args['todate'], operation)
				if predictions:
					data.append(self.get_predictions(attribute_table = data[0]["Attribute_Table"],
														sensor_id = sensorid,
														n_pred = n_predictions))
			else:
				if grouped:
					if harmonising_method:
						data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation)
						data = request_harmonised_data(data, harmonising_method=harmonising_method)
					else:
						data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation)
						data = request_grouped_data(data, per_sensor=per_sensor, freq=freq)
				else:
					data = self.get_attribute_data(attribute_data, LIMIT, OFFSET, operation=operation)

				if predictions:
					#### Ceck for data
					if data[0]["Total_Records"] != 0:
					#### Check for non numeric data
						if is_number(data[0]["Attribute_Values"][0]["Value"]):
							data.append(self.get_predictions(attribute_table = data[0]["Attribute_Table"],
																sensor_id = sensorid,
																n_pred = n_predictions))
						else:
							print("Cannot predict non-numeric data")
							pass
					else:
						pass
			return data, 200

		if attributes:
			_attrs = []
			attr = Attributes.get_by_name_in(attributes)
			for a in attr:
				_attrs.append(a.json())
			return _attrs, 200

		if subtheme is not None and subtheme != '':
			attributes = Attributes.get_by_sub_theme_id(subtheme)
			return [a.json() for a in attributes], 200

		if theme is not None and theme != '':
			subthemes = SubTheme.get_by_theme_id(theme)
			return [a.json() for a in subthemes], 200

		return {
			"error": "error occured while processing request"
		}, 400