예제 #1
0
    def delete(self, env):
        """
        Stop a session.

        Stop a data recording session
            :param env: Environement identifier
            :type env: string
        """
        self.logger.info("DELETE env=%s", env)

        svc = RecorderService()

        try:

            result = svc.load_session(env)
            svc.store_session(env, result.scenario, result.step, 0)
        except RecordingException as exc:
            api.abort(exc.http_status, exc.message)

        return result
예제 #2
0
    def get(self, env):
        """
        Get current recording session.

        Get informations about the current recording session
            :param env: Environement identifier
            :type env: string
        """
        self.logger.info("GET env=%s", env)

        args = PARSER.parse_args(request)
        time = args.get('time', None)

        svc = RecorderService()
        try:
            result = svc.load_session(env, time)
            self.logger.debug(result)
        except RecordingException as exc:
            api.abort(exc.http_status, exc.message)
        return result
예제 #3
0
    def post(self, env):
        """
        Start a session.

        Start a data recording session
            :param env: Environement identifier
            :type env: string
        """
        data = request.json
        self.logger.info("POST env=%s scenario=%s step=%s", env,
                         data.get("scenario"), data.get("step"))

        svc = RecorderService()
        try:
            result = svc.store_session(env, data.get("scenario"),
                                       data.get("step"))
        except RecordingException as exc:
            api.abort(exc.http_status, exc.message)

        return result
예제 #4
0
    def post(self, env):  # pylint: disable=no-self-use
        """
        Define current step.

        Define current step for a recording scenartion
            :param env: Environement identifier
            :type env: string
        """
        recorder = RecorderService()
        data = request.json

        self.logger.info("POST step env=%s step=%s", env, data.get("step"))

        try:

            result = recorder.load_session(env)

            result.step = data.get("step")
            recorder.store_session(env, result.scenario, result.step)
        except RecordingException as exc:
            api.abort(exc.http_status, exc.message)

        return result
예제 #5
0
    def post(self, server):  # pylint: disable=locally-disabled,no-self-use
        """
        Power consumption receiver (see /equipments/{equipement}/measurements).

        Use instead /equipments/{equipement}/measurements with
        <code>[{"sensor": "power", "unit": "W", "value": power-value}]</code>
        as measurements payload<hr>

        Store a new power consumption measurement for a particular server
            :param server: Environement identifier
            :type server: string
        """

        data = request.json
        recorder_manager = RecorderService()

        self.log.info("POST server %s consumption %s in environment %s",
                      server, data.get("power"), data.get("environment"))

        time = data.get("time", None)
        try:
            recorder = recorder_manager.load_session(data.get("environment"),
                                                     time)
        except RecordingException as exc:
            if exc.http_status == 404:
                if settings.ALWAYS_RECORD:
                    recorder = RunningScenarioClass(data.get("environment"),
                                                    "n/s", "n/s")
            else:
                raise exc

        result = PowerMeasurementClass(recorder.environment, data.get("power"),
                                       recorder.scenario, recorder.step)

        influx_data = "PowerMeasurement,hardware="
        influx_data += server.replace(' ', '\\ ')
        influx_data += ",environment="
        influx_data += result.environment.replace(' ', '\\ ')
        influx_data += ",scenario="
        influx_data += result.scenario.replace(' ', '\\ ')
        influx_data += ",step="
        influx_data += result.step.replace(' ', '\\ ')
        influx_data += " power="
        influx_data += str(data.get("power"))
        if time is not None:
            influx_data = influx_data + " " + str(time)

        if settings.INFLUX["user"] is not None:
            auth = (settings.INFLUX["user"], settings.INFLUX["pass"])
        else:
            auth = None

        influx_url = settings.INFLUX["host"] + "/write?db="
        influx_url += settings.INFLUX["db"]
        response = requests.post(influx_url,
                                 data=influx_data,
                                 auth=auth,
                                 verify=False)
        if response.status_code != 204:
            log_msg = "Error while storing measurment: {}"
            log_msg = log_msg.format(response.text)
            api.abort(500, log_msg)

        influx_data = "SensorMeasurement,equipement="
        influx_data += server.replace(' ', '\\ ')
        influx_data += ",environment="
        influx_data += recorder.environment.replace(' ', '\\ ')
        influx_data += ",scenario="
        influx_data += recorder.scenario.replace(' ', '\\ ')
        influx_data += ",step="
        influx_data += recorder.step.replace(' ', '\\ ')
        influx_data += ",sensor=power"
        influx_data += ",unit=W"
        influx_data += " value="
        influx_data += str(data["power"])

        response = requests.post(influx_url,
                                 data=influx_data,
                                 auth=auth,
                                 verify=False)
        if response.status_code != 204:
            log_msg = "Error while storing measurment: {}"
            log_msg = log_msg.format(response.text)
            api.abort(500, log_msg)

        return result
예제 #6
0
    def post(self, equipement):  # pylint: disable=locally-disabled,no-self-use
        """
        Measurements receiver.

        Store new measurements for a particular equipement
            :param equipement: Equipement identifier
            :type equipement: string
        """

        data = request.json
        recorder_manager = RecorderService()

        self.log.info("POST measurements for equiment %s in environment %s",
                      equipement, data.get("environment"))

        time = data.get("time", None)
        try:
            recorder = recorder_manager.load_session(data.get("environment"),
                                                     time)
        except RecordingException as exc:
            if exc.http_status == 404:
                if settings.ALWAYS_RECORD:
                    recorder = RunningScenarioClass(data.get("environment"),
                                                    "n/s", "n/s")
            else:
                raise exc

        result = APIStatusClass("OK")

        sm_influx_data = ""
        pm_influx_data = ""
        for measurement in data["measurements"]:
            time = measurement.get("time", None)

            if sm_influx_data != "":
                sm_influx_data += "\n"

            sm_influx_data += "SensorMeasurement,equipement="
            sm_influx_data += equipement.replace(' ', '\\ ')
            sm_influx_data += ",environment="
            sm_influx_data += recorder.environment.replace(' ', '\\ ')
            sm_influx_data += ",scenario="
            sm_influx_data += recorder.scenario.replace(' ', '\\ ')
            sm_influx_data += ",step="
            sm_influx_data += recorder.step.replace(' ', '\\ ')
            sm_influx_data += ",sensor="
            sm_influx_data += measurement["sensor"].replace(' ', '\\ ')
            sm_influx_data += ",unit="
            sm_influx_data += measurement["unit"].replace(' ', '\\ ')
            sm_influx_data += " value="
            sm_influx_data += str(measurement["value"])

            if time and time > 10e+9:
                #Introduce aleat of 0..9999 nano sec to avoid data mixup
                sm_time = time + random.randint(0, 9999)

                sm_influx_data = sm_influx_data + " " + str(sm_time)

            self._mqtt_svc.publish(recorder.environment, equipement,
                                   recorder.scenario, recorder.step,
                                   measurement["sensor"], measurement["unit"],
                                   measurement["value"], time)

            if measurement["sensor"] == "power":
                if pm_influx_data != "":
                    pm_influx_data += "\n"

                pm_influx_data = "PowerMeasurement,hardware="
                pm_influx_data += equipement.replace(' ', '\\ ')
                pm_influx_data += ",environment="
                pm_influx_data += recorder.environment.replace(' ', '\\ ')
                pm_influx_data += ",scenario="
                pm_influx_data += recorder.scenario.replace(' ', '\\ ')
                pm_influx_data += ",step="
                pm_influx_data += recorder.step.replace(' ', '\\ ')
                pm_influx_data += " power="
                pm_influx_data += str(measurement["value"])

        if settings.INFLUX["user"] is not None:
            auth = (settings.INFLUX["user"], settings.INFLUX["pass"])
        else:
            auth = None

        influx_url = settings.INFLUX["host"] + "/write?db="
        influx_url += settings.INFLUX["db"]
        response = requests.post(influx_url,
                                 data=sm_influx_data.encode("utf-8"),
                                 auth=auth,
                                 headers={
                                     "Content-Type":
                                     "application/x-www-form-urlencoded; " +
                                     "charset=UTF-8"
                                 },
                                 verify=False)
        if response.status_code != 204:
            log_msg = "Error while storing measurment: {}"
            log_msg = log_msg.format(response.text)
            api.abort(500, log_msg)

            response = requests.post(
                influx_url,
                data=pm_influx_data.encode("utf-8"),
                auth=auth,
                headers={
                    "Content-Type":
                    "application/x-www-form-urlencoded; " + "charset=UTF-8"
                },
                verify=False)
            if response.status_code != 204:
                log_msg = "Error while storing measurment: {}"
                log_msg = log_msg.format(response.text)
                api.abort(500, log_msg)

        self.log.info("POST measurements done!")

        return result