def addData(self): # this is the function which is adding the data to the persist queue. 
        nginxStatus=os.popen('systemctl is-active nginx').read()
        temp1={}
        temp2={}
        if(nginxStatus.strip()=="active"):
            temp1=self.nginxCollector.setData()
        temp2=self.systemCollector.setData()
        for i in temp1:
            temp1[i]={
                'value':temp1[i]
            }
        for j in temp2:
            temp2[j]={
                'value':temp2[j]
            }
        

        data = {
                'timestamp': time.time()*1000,
                'nginxDynamicMetrics': temp1,
                'osDynamicMetrics': temp2
        }
        
            # accessing the queue.
        queue = persistqueue.FIFOSQLiteQueue(
            './database', auto_commit=True) # invoking the queue. 
        queue.put(data)  # putting the data into the persist queue
Beispiel #2
0
    def __init__(self, client: ActivityWatchClient) -> None:
        threading.Thread.__init__(self, daemon=True)

        self.client = client

        self.connected = False
        self._stop_event = threading.Event()

        # Buckets that will have events queued to them, will be created if they don't exist
        self._registered_buckets = []  # type: List[Bucket]

        self._attempt_reconnect_interval = 10

        # Setup failed queues file
        data_dir = get_data_dir("aw-client")
        queued_dir = os.path.join(data_dir, "queued")
        if not os.path.exists(queued_dir):
            os.makedirs(queued_dir)

        persistqueue_path = os.path.join(
            queued_dir, "{}{}.v{}.persistqueue".format(
                self.client.name, "-testing" if client.testing else "",
                self.VERSION))
        self._persistqueue = persistqueue.FIFOSQLiteQueue(persistqueue_path,
                                                          multithreading=True,
                                                          auto_commit=False)
        self._current = None  # type: Optional[QueuedRequest]
    def getData(self):
        self.getDataFinished = False
        queue = persistqueue.FIFOSQLiteQueue(
            './database', auto_commit=True)
        # here the chunks of number of requests are sent to the database.
        data = []
        while(queue.size > 0):
            cnt = 0
            while(queue.size > 0 and cnt < self.maxReqSize):
                data.append(queue.get())
                cnt += 1
        # API call
        try:
            print(data)
            response = requests.post('https://software-engineering-308707.el.r.appspot.com/aapi/agent/dyn', json={'data':data}, headers={
                'Authorization': 'Bearer '+os.environ.get("TOKEN")
            }) # this is  the posting api call to the backend. 
            logger.log("Successfully sent the data")

        except:
            logger.log("There was an api error, request failed")
            for i in data:
                queue.put(i)
        self.getDataFinished = True