Помогите пожалуйста оптимизировать код

Всё привет! Мой код очень медленный (загрузка сайта более чем минута), всё из-за загрузки данных с бд. У кого есть возможность помочь, помогите пожалуйста. Что требуется: ускорить код как можно быстрее, что-бы можно было комфортно использовать сайт. Если есть вопросы, пишите

OneAccount.py

# =========== [ Основной класс ] ============
class AdsCabinet:
    def __init__(self, login=None, acid=None, start=0, end=0):
        if acid is None:
            self.static_ac = database.GetInfo("account", f"login='%s'" % login)[0]
            self.ac = database.GetInfo("account_monitor", f"office_login='%s'" % login)[0]

        else:
            self.ac = database.GetInfo("account_monitor", f" id='%s'" % acid)[0]
            self.static_ac = database.GetInfo("account", f"login='%s'" % self.ac['office_login'])[0]

        self.ads = database.GetInfo("ads", f"id_ads_office='%s'" % self.ac['id'])
        self.company = database.GetInfo('company', f"office_id='%s'" % self.ac['id'])

        if start == 0 and end == 0:
            self.stats = database.GetInfo('static')
            self.senler = database.GetInfo("senler", f"acid='%s'" % self.ac['id'])
        else:
            self.stats = database.GetAds(start, end)
            self.senler = database.SenlerInfo(start, end, f"acid='%s'" % self.ac['id'])

        self.group_senler = database.GetInfo('groups_senler', f"acid='%s'" % self.ac['id'])
        self.date = [start, end]

    def GetStaticInfo(self, type):
        if type in self.static_ac:
            return self.static_ac[type]
        if type in self.ac:
            return self.ac[type]
        return False

    def GetOnlineInfo(self, type):
        count = 0
        for stat in self.stats:
            if type in stat:
                count += stat[type]
        return count

    def UpdateStats(self, start, end):
        self.stats = database.GetAds(start, end)


# endregion


# region Company
# =========== [ Инфа по компаниям ] ============
class Company(AdsCabinet):

    def GetTypeCamp(self, type_camp, info_type, start=0, end=0):
        adids = []
        for ad in self.ads:
            for type in type_camp:
                if find(type, ad['name_camp']):
                    adids.append(ad['id'])

        return _getInfoAds(self.stats, adids, info_type, start, end)

    def GetListTypeCamp(self, type_camp, start=0, end=0):
        info = []

        for camp in self.company:
            for type in type_camp:
                if _low(type) in _low(camp['name_camp']):
                    info.append({
                        'Name': camp['name_camp'], 'id': camp['id'],'stat': {
                            'sub': self._stat(camp['name_camp'], 'join', start, end),
                            'spent': self._stat(camp['name_camp'], 'spent', start, end)
                        }})

        return info

    def GetIdCampForName(self, names):
        for camp in self.company:
            for name in names:
                if _low(name) in _low(camp['name_camp']):
                    return camp['id']

    def _stat(self, name_camp, type_info, start, end):
        adids = []
        for ad in self.ads:
            if _low(name_camp) == _low(ad['name_camp']):
                adids.append(ad['id'])

        return _getInfoAds(self.stats, adids, type_info, start, end)


# endregion


# region Senler
# =========== [ Инфа по сенлеру ] ============
class GetSenler(AdsCabinet):

    def GetSenlerType(self, types, start=0, end=0):
        sub, unsub = 0, 0
        for stat in self.senler:
            for type in types:
                if _low(type) in _low(stat['name']) and start != 0 and end != 0:
                    if _diapason(start, end, stat['date']):
                        sub += stat['sub']
                        unsub += stat['unsub']

                elif _low(type) in _low(stat['name']):
                    sub += stat['sub']
                    unsub += stat['unsub']

        return {'sub': sub, 'unsub': unsub}

    def GetAllSenler(self, start=0, end=0):
        sub, unsub = 0, 0
        for stat in self.senler:
            if start != 0 and end != 0 and _diapason(start, end, stat['date']):
                sub += stat['sub']
                unsub += stat['unsub']
            else:
                sub += stat['sub']
                unsub += stat['unsub']

        return {'sub': sub, 'unsub': unsub}

    def GetListSenlerType(self, types, start=0, end=0):
        stats = []

        for group in self.group_senler:
            for type in types:
                if _low(type) in group['name']:
                    stats.append({'Name': group['name'], 'stat': self._senler_stat(group['name'], start, end)})

        return stats

    def _senler_stat(self, type, start, end):
        _cs, _cu = 0, 0
        for stat in self.senler:
            if type == stat['name']:
                if start != 0 and end != 0:
                    if _diapason(start, end, stat['date']):
                        _cs += stat['sub']
                        _cu += stat['unsub']
                else:
                    _cs += stat['sub']
                    _cu += stat['unsub']

        return {'sub': _cs, 'unsub': _cu}


class Ads(AdsCabinet):

    def GetAds(self, id, start=0, end=0):
        info = []
        for ad in self.ads:
            if str(id) == str(ad['id_campagins']):
                adspent = _getInfoAds(self.stats, [ad['id']], "spent", start, end)
                join = _getInfoAds(self.stats, [ad['id']], "join", start, end)
                info.append({'Name': ad['name'], 'stats': {'spent': adspent, 'join': join}, 'limit': ad['limit'],
                             'status': ad['status']})

        return info

    def GetAdsForType(self, types, start=0, end=0):
        info = []
        for ad in self.ads:
            for type in types:
                if _low(type) in _low(ad['name_camp']):
                    adspent = _getInfoAds(self.stats, [ad['id']], "spent", start, end)
                    join = _getInfoAds(self.stats, [ad['id']], "join", start, end)
                    info.append({'Name': ad['name'], 'stats': {'spent': adspent, 'join': join}, 'limit': ad['limit'],
                                 'status': ad['status']})

        return info
# endregion


# =========== [ Служебный класс ] ============
class OneAcc(GetSenler, Company, Ads):
    pass


# region Utils
def _low(string):
    return string.lower()


def _getInfoAds(stats, ids, type_info, start=0, end=0):
    count = 0

    for stat in stats:
        for adid in ids:
            if type(start) != int and type(end) != int:
                if _diapason(start, end, stat['date']):
                    if adid == stat['id_ads'] and type_info in stat:
                        count += stat[type_info]
            else:
                if adid == stat['id_ads'] and type_info in stat:
                    count += stat[type_info]
    return count


def _diapason(start, end, dat):
    if start <= dat <= end:
        return True
    return False


def find(who, str):
    who = _low(who)
    str = _low(str)
    if who in str:
        str = str.split(' ')
        for s in str:
            if _low(s) == who:
                return True
    return False

# endregion

core.py



def Monitor(start, end):
    info = []
    for account in database.GetInfo('account'):
            print(account)
            acc_info = OneAccount.OneAcc(account['login'])

            info.append(
                {'AccName': acc_info.GetStaticInfo('name'),
                 'Id': acc_info.GetStaticInfo('id'),
                 'budget': acc_info.GetStaticInfo('summ'),
                 'minimal_budget': acc_info.GetStaticInfo('minimal_budjet'),
                 'senler': acc_info.GetSenlerType(['др', 'день рождения'], start, end),
                 'spent_senler': acc_info.GetTypeCamp(['ДР', 'день рождения'], 'spent', start, end),
                 'clicbate':
                     {
                         'join': acc_info.GetTypeCamp(['кликбейт', 'кликбэйт'], 'join', start, end),
                         'spent': acc_info.GetTypeCamp(['кликбейт', 'кликбэйт'], 'spent', start, end)
                     }
                 })

    return info


def Company(acid, start, end):
    acc = OneAccount.OneAcc(acid=acid)
    info = {'company': acc.GetListTypeCamp(['кликб'], start, end),
            'dr': acc.GetListSenlerType(['др', 'день рождения'], start, end),
            'dr_spent': acc.GetTypeCamp(['ДР', 'день рождения'], 'spent', start, end),
            'dr_id': acc.GetIdCampForName(['ДР', 'день рождения'])}

    return info


def Ads(company, accid, start, end):
    acc = OneAccount.OneAcc(acid=accid)
    ads = acc.GetAds(company, start, end)
    return ads

database.py



def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d


conn = sqlite3.connect(str(Path(__file__).resolve().parent)+'/sfds.db', check_same_thread=False )
conn.row_factory = dict_factory

c = conn.cursor()


def GetInfo(table, sorting="", what="*"):
    if sorting == "":
        sql = "SELECT {0} FROM {1}".format(what, table)
    else:
        sql = "SELECT {0} FROM {1} WHERE {2}".format(what, table, sorting)
    obj = c.execute(sql)
    return obj.fetchall()


def GetAds(datestart, dateend, sorting=""):
    sql = ""
    if sorting == "":
        sql = "SELECT * FROM static WHERE date BETWEEN '{0}' and '{1}' ".format(datestart, dateend)
    else:
        sql = "SELECT * FROM static WHERE date BETWEEN '{0}' and '{1}' AND {2} ".format(datestart, dateend, sorting)
    obj = c.execute(sql)
    return obj.fetchall()


def SenlerInfoDr(datestart, dateend, sorting=""):
    sql = ""
    if sorting == "":
        sql = "SELECT * FROM senler_dr WHERE date BETWEEN '{0}' and '{1}' ".format(datestart, dateend)
    else:
        sql = "SELECT * FROM senler_dr WHERE date BETWEEN '{0}' and '{1}' AND {2} ".format(datestart, dateend, sorting)
    obj = c.execute(sql)
    return obj.fetchall()


def SenlerInfo(datestart, dateend, sorting=""):
    sql = ""
    if sorting == "":
        sql = "SELECT * FROM senler WHERE date BETWEEN '{0}' and '{1}' ".format(datestart, dateend)
    else:
        sql = "SELECT * FROM senler WHERE date BETWEEN '{0}' and '{1}' AND {2} ".format(datestart, dateend, sorting)
    obj = c.execute(sql)
    return obj.fetchall()


def Insert(table, value):
    sql = "INSERT INTO {0} VALUES({1})".format(table, value)
    c.execute(sql)
    conn.commit()


def InsertSenler(type, static, acid):
    print("db", static)
    sql = ''
    if type != '':
        sql = f"INSERT INTO senler_%s VALUES(%d, ?,?,?)" % (type, acid)
    else:
        sql = f"INSERT INTO senler VALUES(%d, ?,?,?,?)" % (acid)

    c.executemany(sql, static)
    conn.commit()


def InsertStatic(static):
    c.executemany("INSERT INTO static VALUES (?,?,?,?,?,?,?,?,?)", static)
    conn.commit()


def InsertTempory(static):
    c.executemany("INSERT INTO temporary_data VALUES (?,?,?,?,?,?,?,?)", static)
    conn.commit()


def Clear(table):
    sql = "DELETE FROM {0}".format(table)
    c.execute(sql)
    conn.commit()```

Ответы (0 шт):