Example #1
0
    def snapshot_quality(self, uuid, start=None, end=None):
        """
        Get previous quality annotations
        :param uuid: The uuid of the wrapper/stream
        :param start: The start date in the format %Y-%m-%dT%H:%M:%S
        :param end: The end date in the format %Y-%m-%dT%H:%M:%S
        :return: a JSON answer
        """
        resp = JOb()
        resp.uuid = uuid
        resp.result = []
        uuid = uuid.split(",")
        
        try:
            from virtualisation.resourcemanagement.resourcemanagement import ResourceManagement
            if ResourceManagement.args.triplestore:
                graphMap = {}
                working_uuids = []
                for _id in uuid:
                    sd = self.rm.getSensorDescriptionByUUID(_id)
                    if sd is not None:
                        if not graphMap.has_key(sd.graphName):
                            graphMap[sd.graphName] = [formatSensorID(sd)]
                        else:
                            graphMap[sd.graphName].append(formatSensorID(sd))
                        working_uuids.append(_id)
                    else:
                        d = JOb()
                        d.message = "Wrong UUID"
                        resp.result.append(JOb({"uuid": _id, "error": d}))    
                resp.status = "Ok"
                
                sortedReturn = {}
                for graph in graphMap:
                    tmpData = ThreadedTriplestoreAdapter.triplestore.getLastQoIData_List(graph, graphMap[graph], start, end)
                    if tmpData is None:
                        raise Exception("Virtuoso Exception or no data for given start/end date")
                    else:
                        if len(tmpData["results"]["bindings"]) == 0:
                            pass
                        else:
                            for key in tmpData["results"]["bindings"]:
                                sensorUUID = key["sensor"]["value"].replace("http://ict-citypulse.eu/SensorID-", "")
                                if sensorUUID in sortedReturn:
                                    sortedReturn[sensorUUID].append(key)
                                else:
                                    sortedReturn[sensorUUID] = []
                                    sortedReturn[sensorUUID].append(key)
                
                for _id in working_uuids:
                    if _id in sortedReturn:
                        data = []
                        for tmp in sortedReturn[_id]:
                            d = JOb()
                            d.time = tmp["resultTimeValue"]["value"]
                            d.data = tmp
                            data.append(d) 
                        data.sort(cmp=lambda x, y: cmp(x.time, y.time))
                        resp.result.append(JOb({"uuid": _id, "dataset": data}))
                    else:
                        d = JOb()
                        d.message = "No data available"
                        resp.result.append(JOb({"uuid": _id, "error": d}))    
                    
                resp.result.sort(cmp=lambda x, y: uuid.index(x.uuid) - uuid.index(y.uuid))    
            else:
                raise Exception("Triplestore not enabled in Resource Management")

        except Exception as e:
            resp.status = "Fail"
            resp.message = e.message

        return resp.dumps()