Example #1
0
 def listeventwrapper(self):
     r = JOb()
     r.eventwrapper = []
     eds = self.rm.getEventWrapperDescriptions()
     for ed in eds:
         r.eventwrapper.append(ed)
     return r.dumps()
Example #2
0
    def snapshot(self, uuid, start=None, end=None):
        """
        Get previous observations
        :param uuid: The uuid of the wrapper
        :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
        try:
            from virtualisation.resourcemanagement.resourcemanagement import ResourceManagement
            if ResourceManagement.args.triplestore:
                sd = self.rm.getSensorDescriptionByUUID(uuid)
                if sd:
                    data = ThreadedTriplestoreAdapter.triplestore.getObservationGraph(sd.graphName, formatSensorID(sd), start, end, False)
                    resp.status = "Ok"
                    resp.message = ""
                    resp.data = data
                else:
                    raise Exception("no stream with given uuid known")
            else:
                raise Exception("Triplestore not enabled in Resource Management")

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

        return resp.dumps()
Example #3
0
 def listwrapper(self):
     r = JOb()
     c = 0
     r.wrappers = []
     for w in self.rm.wrappers:
         sd = w.getSensorDescription()
         if isinstance(sd, list):
             for _sd in sd:
                 r.wrappers.append(JOb())
                 r.wrappers[c].information = _sd.information
                 r.wrappers[c].uuid = _sd.uuid
                 r.wrappers[c].sensorName = _sd.sensorName
                 r.wrappers[c].author = _sd.author
                 r.wrappers[c].sensorID = _sd.sensorID
                 r.wrappers[c].fullSensorID = _sd.fullSensorID
                 r.wrappers[c].source = _sd.source
                 r.wrappers[c].sensorType = _sd.sensorType
                 r.wrappers[c].sourceFormat = _sd.sourceFormat
                 r.wrappers[c].messagebus.routingKey = _sd.messagebus.routingKey
                 c += 1
         else:
             r.wrappers.append(JOb())
             r.wrappers[c].information = sd.information
             r.wrappers[c].uuid = sd.uuid
             r.wrappers[c].sensorName = sd.sensorName
             r.wrappers[c].author = sd.author
             r.wrappers[c].sensorID = sd.sensorID
             r.wrappers[c].fullSensorID = sd.fullSensorID
             r.wrappers[c].source = sd.source
             r.wrappers[c].sensorType = sd.sensorType
             r.wrappers[c].sourceFormat = sd.sourceFormat
             r.wrappers[c].messagebus.routingKey = sd.messagebus.routingKey
             c += 1
     return r.dumps()
Example #4
0
    def data_timeframe(self, uuid):
        """
        Returns the time frame (start and end date) for which data from the stream, identified by the UUID,
        is available.
        :param uuid:
        :return:
        """
        resp = JOb()
        resp.uuid = uuid
        try:
            if ThreadedTriplestoreAdapter.triplestore:
                sd = self.rm.getSensorDescriptionByUUID(uuid)
                if sd:
                    data = ThreadedTriplestoreAdapter.triplestore.getStreamMinMaxDate(sd.graphName, formatSensorID(sd))
                    resp.status = "Ok"
                    resp.message = ""
                    resp.data = data
                else:
                    raise Exception("no stream with given uuid known")
            else:
                resp.status = "Fail"
                resp.message = "Triplestore not activated."
        except Exception as e:
            resp.status = "Fail"
            resp.message = e.message

        return resp.dumps()
Example #5
0
 def listwrapperfull(self):
     r = JOb()
     r.wrappers = []
     for w in self.rm.wrappers:
         sd = w.getSensorDescription()
         if isinstance(sd, list):
             for _sd in sd:
                 r.wrappers.append(_sd)
         else:
             r.wrappers.append(sd)
     return r.dumps()
Example #6
0
 def get_static_stream_data(self, uuid):
     resp = JOb()
     resp.uuid = uuid
     if uuid in self.static_stream_data_cache:
         resp.status = "Ok"
         resp.message = ""
         resp.data = self.static_stream_data_cache[uuid]
     else:
         resp.status = "Fail"
         resp.message = "No sensor stream with uuid " + uuid + " known."
     return resp.dumps()
 def avg_processing_time(self, uuid = None, category = None):
     job = JSONObject()
     if uuid:
         uuid = uuid.split(",")
         for u in uuid:
             stats = Stats.getOrMake(u)
             job[u] = []
             pTimes = stats.getAverageProcessingTimes()
             if pTimes:
                 job[u].extend(pTimes)
             else:
                 failJob = JSONObject()
                 failJob.status = "Fail"
                 failJob.message = "UUID not found"
                 job[u] = failJob
     elif category:
         category = category.split(",")
         avgList = {}
         for c in category:
             uuidList = self.rm.getUUIDsForCategory(c)
             statList = []
             valueMap={}
             for uuid in uuidList:
                 stats = Stats.getOrMake(uuid)
                 self.test(valueMap, stats.getAverageProcessingTimes())
                 statList += stats.getAverageProcessingTimes()
             for key, value in valueMap.items():
                 valueMap[key] = value / len(uuidList)
             
             avgList[c] = []
             self.valueMapToJson(valueMap, avgList[c], None, None)
         job = JSONObject()
         for element in avgList:
             job[element] = []
             pTimes = avgList[element]
             if pTimes:
                 job[element].extend(pTimes)
             else:
                 failJob = JSONObject()
                 failJob.status = "Fail"
                 failJob.message = "Category not found"
                 job[element] = failJob
     else:
         statList = Stats.getAllStats()
         for s in statList:
             job[s.name] = []
             job[s.name].extend(s.getAverageProcessingTimes())
     return job.dumps()
Example #8
0
 def snapshot_sql(self, uuid, start=None, end=None, format='json', last=False, fields=None, offset=0):
     if self.rm.sql:
         offset = int(offset)
         observations = self.rm.sql.get_observations(uuid, start, end, format, last, fields, offset)
         data = JOb()
         data.data = observations
         #TODO                  remove the 2 below
         data.next_url = "/api/snapshot_sql2?uuid=%s%s%s%s%s%s%s" % (uuid,
               "&start=" + start if start else "",
               "&end=" + end if end else "" ,
               "&format=" + format,
               "&last=" + last if last else "",
               "&fields=" + fields if fields else "",
               "&offset=" + str(offset + self.rm.sql.PAGINATION_LIMIT)) if len(observations) == self.rm.sql.PAGINATION_LIMIT else ""
         return data.dumps()
     else:
         return "Error. SQL feature not enabled in Resource Management"
Example #9
0
 def get_description(self, uuid):
     resp = JOb()
     sd = self.rm.getSensorDescriptionByUUID(uuid)
     if sd:
         resp.status = "Ok"
         resp.message = ""
         resp.data = sd
     else:
         ed = self.rm.getEventDescriptionByUUID(uuid)
         if ed:
             resp.status = "Ok"
             resp.message = ""
             resp.data = ed
         else:
             resp.status = "Fail"
             resp.message = "no stream with given uuid known"
     return resp.dumps()
Example #10
0
 def deactivate_fault_recovery(self, uuid):
     """
     Disable the fault recovery for a wrapper
     :param uuid: UUID identifying the wrapper
     :return:
     """
     resp = JOb()
     resp.uuid = uuid
     w = self.rm.getWrapperByUUID(uuid)
     if w:
         w.deactivateFaultRecovery()
         resp.status = "Ok"
         resp.message = ""
     else:
         resp.status = "Fail"
         resp.message = "no stream with given uuid known"
     return resp.dumps()
Example #11
0
 def deploy(self, deployunit):
     resp = JOb()
     if deployunit.filename.endswith(".zip"):
         from virtualisation.resourcemanagement.resourcemanagement import ResourceManagement
         trg = os.path.join(ResourceManagement.deployfoldername, deployunit.filename)
         dst = open(trg, "w")
         dst.write(deployunit.file.read())
         dst.close()
         try:
             resp.status, resp.message, resp.sensordescriptions = self.rm.deploy(trg, True)
         except Exception as e:
             resp.status = "Fail"
             resp.message = e.message
     else:
         resp.status = "Fail"
         resp.message = "Wrong file type."
     return resp.dumps()
Example #12
0
 def register_event(self, eventdescription):
     resp = JOb()
     if eventdescription.filename.endswith(".json"):
         from virtualisation.resourcemanagement.resourcemanagement import ResourceManagement
         trg = os.path.join(ResourceManagement.eventdescriptionfoldername, eventdescription.filename)
         dst = open(trg, "w")
         dst.write(eventdescription.file.read())
         dst.close()
         try:
             resp.status, resp.message = self.rm.registerEvent(trg)
         except Exception as e:
             resp.status = "Fail"
             resp.message = e.message
     else:
         resp.status = "Fail"
         resp.message = "Wrong file type."
     return resp.dumps()
Example #13
0
 def snapshot_last(self, uuid):
     """
     Get previous observations
     :param uuid: The uuid of the wrapper
     :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
     if uuid in self.observation_cache:
         resp.status = "Ok"
         resp.message = ""
         resp.data = self.observation_cache[uuid]
     else:
         resp.status = "Fail"
         resp.message = "No observation with the UUID " + uuid + " cached."
     return resp.dumps()
Example #14
0
    def data_timeframe_sql(self, uuid):
        """
        Returns the time frame (start and end date) for which data from the stream, identified by the UUID,
        is available.
        :param uuid:
        :return:
        """
        resp = JOb()
        resp.uuid = uuid
        try:
            if self.rm.sql:
                data = self.rm.sql.data_timeframe(uuid)
                resp.status = "Ok"
                resp.message = ""
                resp.data = data
            else:
                resp.status = "Fail"
                resp.message = "SQL feature not activated."
        except Exception as e:
            resp.status = "Fail"
            resp.message = e.message

        return resp.dumps()
        EventDescription to show Dan what we need and to test the annotation part. You will probably need a pseudo
        EventGenerator for that.
        Later, the ResourceManagement API will receive new EventDescriptions and instanciate EventWrapper
        (or best case we can reuse one instance). There is no need to make the metadata file nor to instanciate an
        EventWrapper for each REPORT_ID."""

        self.eventDescription = eventdescription

    def getEventDescription(self):
        return self.eventDescription

if __name__ == "__main__":
    atew = AarhusTrafficEventWrapper()
    from virtualisation.annotation.genericannotation import GenericAnnotation
    annotator = GenericAnnotation()
    eventData = JSONObject()
    eventData.ceID = 123456
    eventData.ceType = "traffic"
    eventData.ceName = "traffic jam"
    eventData.ceTime = 1438591234000L
    eventData.ceCoordinate = "(56.12 10.13)"
    eventData.ceLevel = 1

    print "Incoming event data", eventData.dumps()
    print

    g = annotator.annotateEvent(eventData, atew.getEventDescription())
    print "Resulting graph", g.serialize(format='n3')
    print

    print atew.getEventDescription().dumps()
                    setattr(quality, "hasRatedQuality", "%.3f" % float(tmp))
                else:
                    setattr(quality, "hasRatedQuality", "%s" % tmp)

                tmp = element[qoiName][self.updateInterval].absoluteAvg
                if tmp:
                    setattr(quality, "hasAbsoluteQuality", "%.3f" % float(tmp))
                else:
                    setattr(quality, "hasAbsoluteQuality", "%s" % tmp)

                setattr(quality, "hasUnitOfMeasurement", saopy.muo.UnitOfMeasurement("%s" % element[qoiName].unit))
                eventprofile.hasQuality.add(quality)
                saoOut.add(quality)

            graph = saopy.RDFInterface.exportRDFGraph(saoOut)
            #             print graph.serialize(format="n3")
            graphList.append(graph)
        return graphList


if __name__ == "__main__":
    test = JSONObject(
        """[{"Latency": {"CURRENT": {"ratedValue": 1.0, "absoluteValue": 0.000349}, "HOURLY": {"absoluteAvg": 0.000798733333333333, "ratedMax": 0.001507, "ratedAvg": 1.0, "absoluteMin": 0.000347, "absoluteMax": 0.001507, "ratedMin": 0.000347}, "unit": "http://purl.oclc.org/NET/muo/ucum/unit/time/second"}, "Completeness": {"CURRENT": {"ratedValue": 1.0, "absoluteValue": 2}, "HOURLY": {"absoluteAvg": 2.0, "ratedMax": 2, "ratedAvg": 1.0, "absoluteMin": 2, "absoluteMax": 2, "ratedMin": 2}, "unit": "http://purl.oclc.org/NET/muo/ucum/physical-quality/number"}, "eventProfile": "http://test.de/EventProfile-4a838c4b-30d0-5fb4-b3b5-16d6c5c4ff9f", "uuid": "4a838c4b-30d0-5fb4-b3b5-16d6c5c4ff9f", "Age": {"CURRENT": {"ratedValue": 1.0, "absoluteValue": 0.0}, "HOURLY": {"absoluteAvg": 0.0, "ratedMax": 0.0, "ratedAvg": 1.0, "absoluteMin": 0.0, "absoluteMax": 0.0, "ratedMin": 0.0}, "unit": "http://purl.oclc.org/NET/muo/ucum/unit/time/second"}, "Correctness": {"CURRENT": {"ratedValue": 1.0, "absoluteValue": 1}, "HOURLY": {"absoluteAvg": 1.0, "ratedMax": 1, "ratedAvg": 1.0, "absoluteMin": 1, "absoluteMax": 1, "ratedMin": 1}, "unit": "http://purl.oclc.org/NET/muo/ucum/unit/fraction/percent"}, "Frequency": {"CURRENT": {"ratedValue": 0.6052631578947429, "absoluteValue": 0.01694915254237288}, "HOURLY": {"absoluteAvg": "inf", "ratedMax": "inf", "ratedAvg": 0.559912280701759, "absoluteMin": 0.015873015873015872, "absoluteMax": "inf", "ratedMin": 0.015873015873015872}, "unit": "http://purl.oclc.org/NET/muo/ucum/unit/frequency/Herz"}}]"""
    )
    print type(test)
    print test.dumps()
    a = AverageStreamQuality(None, None)
    a.createGraph(test)

    pass
Example #17
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()