예제 #1
0
class Stats:
    def __init__(self):
        self.__db = Database()

    def get_bin_per_day(self, order, interval=30):
        """
        Getting a list of bin per day on a specific interval
        :return: List of bin with count and date
        """
        # Getting the list of the number of bin creations in the last 60 days
        cursor = self.__db.get_cursor()
        logger.info(f"Getting stats for the latest {interval} days with {order} order")
        logger.debug(f"Retrieving bin number per day")
        query = """
                        select
                            count(*) as count,
                            date(created) as date
                        from
                            `bin`
                        where
                            created >= date_sub(curdate(), interval %s day)
                        group by 
                            date
                        order by `date` ASC

                        """
        cursor.execute(query, (interval,))
        result = list(cursor.fetchall())
        insertions_list = []
        if len(result) >= 1:
            if order == 'DESC':
                logger.debug("Reversing the stats list")
                result = result[::-1]
            logger.debug(f"Found {len(result)} Bins in time range")
            for data in result:
                log_data = {
                    "insertions": data[0],
                    "day": data[1]
                }
                insertions_list.append(log_data)
        cursor.close()
        self.__db.done()
        return insertions_list

    def get_last_bin_timestamp(self):
        cursor = self.__db.get_cursor()
        logger.debug(f"Getting last insertion datetime")
        query = """
                    SELECT `created` FROM `bin` ORDER BY id DESC LIMIT 1
                        """
        cursor.execute(query, )
        result = list(cursor.fetchall())
        if len(result) >= 1:
            logger.debug(f"Found latest insertion date")
            lastInsertTime = datetimeutil.ISO8601.from_datetime_obj(result[0][0])
            cursor.close()
            self.__db.done()
            return lastInsertTime
예제 #2
0
class BinLikes:
    def __init__(self):
        self.__db = Database()

    def get_bin_likes(self, url):
        cursor = self.__db.get_cursor()
        logger.debug(f"Retrieving likes for url {url}")
        query = "SELECT total_likes FROM binLikes WHERE slug = %s"
        cursor.execute(query, (url, ))
        result = list(cursor.fetchall())
        total_likes = 0
        if result:
            total_likes = result[0][0]
        else:
            query = "INSERT INTO binLikes (slug, total_likes) VALUES(%s, 0)"
            cursor.execute(query, (url, ))

        cursor.close()
        self.__db.done()
        return total_likes

    def increment_bin_likes(self, url):
        cursor = self.__db.get_cursor()
        logger.debug(f"Incrementing likes for url")
        query = "SELECT total_likes FROM binLikes WHERE slug = %s"
        cursor.execute(query, (url, ))
        result = list(cursor.fetchall())

        if result:
            query = "UPDATE binLikes SET total_likes = total_likes + 1 WHERE slug = %s"
            cursor.execute(query, (url, ))
        else:
            query = "INSERT INTO binLikes (slug, total_likes) VALUES(%s, 1)"
            cursor.execute(query, (url, ))

        query = "SELECT total_likes FROM binLikes WHERE slug = %s"
        cursor.execute(query, (url, ))
        result = list(cursor.fetchall())

        cursor.close()
        self.__db.done()
        return result[0][0]
예제 #3
0
class Bin:
    def __init__(self):
        self.__db = Database()

    def insert_bin_legacy(self, payload, url):
        cursor = self.__db.get_cursor()
        logger.debug(f"Inserting new Bind at url {str(url)}")
        query = """INSERT INTO bin(`data`,url)
                        VALUES (%s,%s) """
        cursor.execute(
            query,
            (str(payload).encode(sys.stdout.encoding,
                                 errors='replace'), str(url)),
        )
        cursor.close()
        self.__db.done()
        return True

    def get_bin_legacy(self, url):
        cursor = self.__db.get_cursor()
        logger.debug(f"Retrieving bin for url {url}")
        query = """
                        SELECT
                            *
                        FROM
                            `bin`
                        WHERE url = %s
                        """
        cursor.execute(query, (url, ))
        result = list(cursor.fetchall())
        logs_list = []
        if len(result) >= 1:
            logger.debug(f"Found {len(result)} Bins for url {url}")
            for data in result:
                log_data = {
                    "id": data[0],
                    "data": data[1].decode(),
                    "url": data[2],
                    "created": data[3]
                }
                logs_list.append(log_data)
        cursor.close()
        self.__db.done()
        return logs_list

    def insert_bin_v2(self, creator, title, data, private, url, language):
        cursor = self.__db.get_cursor()
        logger.debug(f"Inserting new Bind at url {str(url)}")
        query = """INSERT INTO bin(`data`, url, creator, title, isPrivate, `language`)
                        VALUES (%s,%s,%s,%s,%s,%s) """
        cursor.execute(
            query,
            (str(data).encode(sys.stdout.encoding, errors='replace'), str(url),
             str(creator), str(title), private, str(language)),
        )
        cursor.close()
        self.__db.done()
        return True

    def get_bin_v2(self, url):
        cursor = self.__db.get_cursor()
        logger.debug(f"Retrieving bin for url {url}")
        query = """
                        SELECT
                            *
                        FROM
                            `bin`
                        WHERE url = %s
                        """
        cursor.execute(query, (url, ))
        result = list(cursor.fetchall())
        logs_list = []
        if len(result) >= 1:
            logger.debug(f"Found {len(result)} Bins for url {url}")
            for data in result:
                log_data = {
                    "id": data[0],
                    "data": data[1].decode(),
                    "url": data[2],
                    "created": data[3],
                    "creator": data[4],
                    "title": data[5],
                    "isPrivate": data[6],
                    "language": data[7]
                }
                logs_list.append(log_data)
        cursor.close()
        self.__db.done()
        return logs_list