Exemplo n.º 1
0
    def run(self, params={}):
        force_offset = params.get("force_offset")

        date_offset = params.get("date_offset")
        # what happens when its left blank?
        self.logger.info("Date Offset: {} ".format(date_offset))

        # If date is left blank in the UI
        if date_offset == "0001-01-01T00:00:00Z":
            date_offset = None

        # Set date_offset to maya.MayaDT
        if date_offset:
            date_offset = maya.MayaDT.from_rfc3339(date_offset)

        # New cache util. Will return maya DT
        cache_file_name, cache_date = load_cache(self.CACHE_FILE_NAME,
                                                 self.connection.customer_id,
                                                 self.logger, force_offset,
                                                 date_offset)

        # testing printing out log
        self.logger.info(cache_date)

        last_cache_date = cache_date

        event_date_list = []

        while True:
            try:
                self.logger.info("[*] Pulling events!")
                events = self.connection.api.list_events()

                self.logger.info("[*] Reviewing events")
                for event in events:
                    event_date = maya.MayaDT.from_rfc3339(
                        event['attributes']['process']['attributes']
                        ['started_at']).datetime()
                    if event_date > cache_date:
                        event_date_list.append(event_date)
                        self.send({"event": event})

                # Set cache date to max its seen
                if event_date_list:
                    max_date = max(event_date_list)
                    if max_date > cache_date:
                        cache_date = max_date

                # reset list
                event_date_list = []

                # Write to cache if it needs updating
                if last_cache_date != cache_date:
                    last_cache_date = cache_date
                    cache(cache_file_name, cache_date, self.logger)

                time.sleep(params.get('frequency', 5))
            except Exception as e:
                raise Exception(
                    'An error occurred while reading events: {}'.format(e))
Exemplo n.º 2
0
    def run(self, params={}):

        force_offset = params.get("force_offset")

        date_offset = params.get("date_offset")

        self.logger.info("Date Offset: {} ".format(date_offset))

        # If date is left blank in the UI
        if date_offset == "0001-01-01T00:00:00Z":
            date_offset = None

        # Set date_offset to maya.MayaDT
        if date_offset:
            date_offset = maya.MayaDT.from_rfc3339(date_offset)

        # New cache util. Will return maya DT
        cache_file_name, cache_date = load_cache(
            self.CACHE_FILE_NAME,
            self.connection.customer_id,
            self.logger,
            force_offset,
            date_offset,
        )

        detection_date_list = []

        while True:
            try:

                detections = self.connection.api.get_detections(
                    since=cache_date)

                self.logger.info("[*] Reviewing detection")
                for detection in detections:
                    detection_date = maya.MayaDT.from_rfc3339(
                        detection["attributes"]
                        ["time_of_occurrence"]).datetime()
                    if detection_date > cache_date:
                        detection_date_list.append(detection_date)
                        self.send({"detection": detection})

                # Set cache date to max its seen
                if detection_date_list:
                    max_date = max(detection_date_list)
                    self.logger.info(
                        f"[*] Checking if Max Date {max_date} > Current Cache Date {cache_date}"
                    )
                    if max_date > cache_date:
                        cache_date = max_date
                        cache(cache_file_name, cache_date, self.logger)

                # reset list
                detection_date_list = []

                time.sleep(params.get("frequency", 5))
            except Exception as e:
                raise Exception(
                    "An error occurred while reading detections: {}".format(e))