libs/status.py
def insert_status_smart(self, db):
        """
        Instead of inserting a new status always, this API tries to update the last healthy status if the healthy
        status record is the last one. Otherwise, insert_status is still called.
        This is superb helpful to reduce many rows of similar healthy records.
        """
        if self['url'] not in self.last_url_status:
            no_record = False

            last_status_sql = f'''
            SELECT id, status, phrase from web_activity_{self.topic} WHERE url = '{self["url"]}'
            ORDER BY id DESC LIMIT 1;
            '''
            with db.cursor() as curs:
                curs.execute(last_status_sql)
                r = curs.fetchone()
                if r:
                    rid, status, phrase = r
                    if status == 'responsive' or phrase == 'ok':
                        self.last_url_status[self['url']] = (True, rid)
                    else:
                        self.last_url_status[self['url']] = (False, rid)
                else:
                    no_record = True
            db.commit()

            if no_record:  # No record found which means no healthy record at all. Do insert instead
                self.insert_status(db)
                return

        if self.last_url_status[f'{self["url"]}'][0] and self.healthy():
            # Last status record is healthy and this new status is healthy
            # Update the last healthy record with latest created_at, topic_offset, event_time
            sql = f'''
            UPDATE web_activity_{self.topic} SET (created_at, topic_offset, event_time) = (CURRENT_TIMESTAMP,
            {self['offset']}, {self['timestamp']}) WHERE id = {self.last_url_status[self['url']][1]};
            '''
            with db.cursor() as curs:
                if self.last_url_status[self['url']][0]:
                    curs.execute(sql)
            db.commit()
        else:  # Otherwise, insert a new one
            self.insert_status(db)

        Instead of inserting a new status always, this API tries to update the last healthy status if the healthy
        status record is the last one. Otherwise, insert_status is still called.
        This is superb helpful to reduce many rows of similar healthy records.
        
Instead of inserting a new status always, this API tries to update the last healthy status if the healthy
status record is the last one. Otherwise, insert_status is still called.
This is superb helpful to reduce many rows of similar healthy records.
Instead of inserting a new status always this API tries to update the last healthy status if the healthy status record is the last one . Otherwise insert_status is still called . This is superb helpful to reduce many rows of similar healthy records .
def insert_status_smart(self, db):
        if self['url'] not in self.last_url_status:
            no_record = False
            last_status_sql = f'''
            SELECT id, status, phrase from web_activity_{self.topic} WHERE url = '{self["url"]}'
            ORDER BY id DESC LIMIT 1;
            '''
            with db.cursor() as curs:
                curs.execute(last_status_sql)
                r = curs.fetchone()
                if r:
                    rid, status, phrase = r
                    if status == 'responsive' or phrase == 'ok':
                        self.last_url_status[self['url']] = (True, rid)
                    else:
                        self.last_url_status[self['url']] = (False, rid)
                else:
                    no_record = True
            db.commit()
            if no_record:  
                self.insert_status(db)
                return
        if self.last_url_status[f'{self["url"]}'][0] and self.healthy():
            sql = f'''
            UPDATE web_activity_{self.topic} SET (created_at, topic_offset, event_time) = (CURRENT_TIMESTAMP,
            {self['offset']}, {self['timestamp']}) WHERE id = {self.last_url_status[self['url']][1]};
            '''
            with db.cursor() as curs:
                if self.last_url_status[self['url']][0]:
                    curs.execute(sql)
            db.commit()
        else:  
            self.insert_status(db)
def insert_status_smart ( self , db ) : if self [ 'url' ] not in self . last_url_status : no_record = False last_status_sql = f'''
            SELECT id, status, phrase from web_activity_{self.topic} WHERE url = '{self["url"]}'
            ORDER BY id DESC LIMIT 1;
            ''' with db . cursor ( ) as curs : curs . execute ( last_status_sql ) r = curs . fetchone ( ) if r : rid , status , phrase = r if status == 'responsive' or phrase == 'ok' : self . last_url_status [ self [ 'url' ] ] = ( True , rid ) else : self . last_url_status [ self [ 'url' ] ] = ( False , rid ) else : no_record = True db . commit ( ) if no_record : self . insert_status ( db ) return if self . last_url_status [ f'{self["url"]}' ] [ 0 ] and self . healthy ( ) : sql = f'''
            UPDATE web_activity_{self.topic} SET (created_at, topic_offset, event_time) = (CURRENT_TIMESTAMP,
            {self['offset']}, {self['timestamp']}) WHERE id = {self.last_url_status[self['url']][1]};
            ''' with db . cursor ( ) as curs : if self . last_url_status [ self [ 'url' ] ] [ 0 ] : curs . execute ( sql ) db . commit ( ) else : self . insert_status ( db )
Instead of inserting a new status always, this API tries to update the last healthy status if the healthy
status record is the last one.
Instead of inserting a new status always this API tries to update the last healthy status if the healthy status record is the last one .
"""
        Instead of inserting a new status always, this API tries to update the last healthy status if the healthy
        status record is the last one. Otherwise, insert_status is still called.
        This is superb helpful to reduce many rows of similar healthy records.
        """ # No record found which means no healthy record at all. Do insert instead # Last status record is healthy and this new status is healthy # Update the last healthy record with latest created_at, topic_offset, event_time # Otherwise, insert a new one
self
db