from time import sleep

from sqlmodel import select

from icon_governance.config import settings
from icon_governance.log import logger
from icon_governance.models.preps import Prep
from icon_governance.utils.rpc import (
    convert_hex_int,
    get_preps_cps,
    get_sponsors_record,
    post_rpc_json,
)


def get_cps(session):
    sponsors = post_rpc_json(get_sponsors_record())

    if sponsors is None:
        logger.info("No sponsors found from rpc.")
        sleep(1)
        return

    for k, v in sponsors.items():
        prep = session.get(Prep, k)
        if prep is None:
            logger.info("No preps found in db? Should not ever happen cuz of db_init.")
            continue

        prep.sponsored_cps_grants = convert_hex_int(v)

        session.merge(prep)
        session.commit()

    result = session.execute(select(Prep))
    preps = result.scalars().all()

    cps_preps = post_rpc_json(get_preps_cps())
    prep_list = [i["address"] for i in cps_preps]

    for prep in preps:
        if prep.address in prep_list:
            prep.cps_governance = True
        else:
            prep.cps_governance = False

        session.merge(prep)
        session.commit()


def cps_cron(session):
    while True:
        logger.info("Starting cps cron")
        get_cps(session)
        logger.info("CPS cron ran.")
        sleep(settings.CRON_SLEEP_SEC * 10)


if __name__ == "__main__":
    from icon_governance.db import session_factory

    get_cps(session_factory())

icon_governance.utils.rpc.get_preps_cps icon_governance.utils.rpc.get_sponsors_record icon_governance.utils.rpc.convert_hex_int icon_governance.db.session_factory time.sleep icon_governance.log.logger.info sqlmodel.select
[((361, 382), 'icon_governance.utils.rpc.get_sponsors_record', 'get_sponsors_record', ([], {}), '()\n', (380, 382), False, 'from icon_governance.utils.rpc import convert_hex_int, get_preps_cps, get_sponsors_record, post_rpc_json\n'), ((418, 460), 'icon_governance.log.logger.info', 'logger.info', (['"""No sponsors found from rpc."""'], {}), "('No sponsors found from rpc.')\n", (429, 460), False, 'from icon_governance.log import logger\n'), ((469, 477), 'time.sleep', 'sleep', (['(1)'], {}), '(1)\n', (474, 477), False, 'from time import sleep\n'), ((735, 753), 'icon_governance.utils.rpc.convert_hex_int', 'convert_hex_int', (['v'], {}), '(v)\n', (750, 753), False, 'from icon_governance.utils.rpc import convert_hex_int, get_preps_cps, get_sponsors_record, post_rpc_json\n'), ((838, 850), 'sqlmodel.select', 'select', (['Prep'], {}), '(Prep)\n', (844, 850), False, 'from sqlmodel import select\n'), ((918, 933), 'icon_governance.utils.rpc.get_preps_cps', 'get_preps_cps', ([], {}), '()\n', (931, 933), False, 'from icon_governance.utils.rpc import convert_hex_int, get_preps_cps, get_sponsors_record, post_rpc_json\n'), ((1243, 1275), 'icon_governance.log.logger.info', 'logger.info', (['"""Starting cps cron"""'], {}), "('Starting cps cron')\n", (1254, 1275), False, 'from icon_governance.log import logger\n'), ((1309, 1337), 'icon_governance.log.logger.info', 'logger.info', (['"""CPS cron ran."""'], {}), "('CPS cron ran.')\n", (1320, 1337), False, 'from icon_governance.log import logger\n'), ((1346, 1381), 'time.sleep', 'sleep', (['(settings.CRON_SLEEP_SEC * 10)'], {}), '(settings.CRON_SLEEP_SEC * 10)\n', (1351, 1381), False, 'from time import sleep\n'), ((1475, 1492), 'icon_governance.db.session_factory', 'session_factory', ([], {}), '()\n', (1490, 1492), False, 'from icon_governance.db import session_factory\n'), ((601, 676), 'icon_governance.log.logger.info', 'logger.info', (['"""No preps found in db? Should not ever happen cuz of db_init."""'], {}), "('No preps found in db? Should not ever happen cuz of db_init.')\n", (612, 676), False, 'from icon_governance.log import logger\n')]