from app.libraries.setup import Setup
from app.libraries.database import Database
from app.libraries.peers import Peers
from app.libraries.nodeinfo import Nodeinfo
from app.libraries.graphit import Graphit
from mastodon import Mastodon
import ray
import pdb

ray.init(num_cpus = 25) # Specify this system CPUs.

@ray.remote
def get_nodeinfo(server):

    server, api = db.get_nodeinfo_endpoint(server)
    
    try:

        server, soft, version, users, mau, alive = ndi.getdata(server, api)

        if soft != '':

            db.write_alive_server(server, soft, version, users, mau, alive)

        else:

            db.write_not_alive_server(server)

    except:

        pass

if __name__ == '__main__':

    db = Database()

    setup = Setup()

    peers = Peers()

    ndi = Nodeinfo()

    mastodon = Mastodon(
        access_token = setup.mastodon_app_token,
        api_base_url= setup.mastodon_hostname
        )

    saved_servers = db.get_saved_servers()

    ray.get([get_nodeinfo.remote(server) for server in saved_servers])

    # get current total servers and users, get users from every software

    total_servers = 0

    total_users = 0

    soft_total_project, soft_total_users, soft_total_mau, soft_total_servers, total_servers, total_users, total_mau = db.soft_totals()

    i = 0

    while i < 5:


        db.write_top_soft(soft_total_project[i], soft_total_users[i], soft_total_mau[i], soft_total_servers[i])

        i += 1

    # get last check values and write current total ones

    evo_servers, evo_users, evo_mau = db.last_values(total_servers, total_users, total_mau)

    # write evo values

    db.write_evo(evo_servers, evo_users, evo_mau)

    graph = Graphit()

    # get max servers and mau

    graph.servers_max, graph.users_max, graph.mau_max = db.max()

    # get plots

    graph.servers, graph.users, graph.mau, graph.global_day, graph.global_servers, graph.global_users, graph.global_mau, graph.global_week, graph.global_week_users, graph.global_week_mau = db.get_plots()

    graph.generate()

    graph.generate_weeks()

    ###############################################################################
    # P O S T !

    post_text = "#fediverse alive servers stats" + " \n"

    post_text += "\n"

    if evo_servers >= 0:

        post_text += "servers: " + str(f"{total_servers:,}") + " (+"+ str(f"{evo_servers:,}") + ", max: " + str(f"{graph.servers_max:,}") + ")\n"

    elif evo_servers < 0:

        post_text += "servers: " + str(f"{total_servers:,}") + " ("+ str(f"{evo_servers:,}") + ", max: " + str(f"{graph.servers_max:,}") + ")\n"

    if evo_users >= 0:

        post_text += "users: " + str(f"{total_users:,}") + " (+"+ str(f"{evo_users:,}") + ", max: " + str(f"{graph.users_max:,}") + ")\n"

    elif evo_users < 0:

        post_text += "users: " + str(f"{total_users:,}") + " ("+ str(f"{evo_users:,}") + ", max: " + str(f"{graph.users_max:,}") + ")\n"

    if evo_mau >= 0:

        post_text += "MAU: " + str(f"{total_mau:,}") + " (+"+ str(f"{evo_mau:,}") + ", max: " + str(f"{graph.mau_max:,}") + ")\n"

    elif evo_mau < 0:

        post_text += "MAU: " + str(f"{total_mau:,}") + " ("+ str(f"{evo_mau:,}") + ", max: " + str(f"{graph.mau_max:,}") + "\n"

    post_text += "\ntop five projects (users / MAU / servers):\n\n"

    i = 0

    while i < 5:

        project_soft = soft_total_project[i]

        project_mau = soft_total_mau[i]

        project_users = soft_total_users[i]

        project_servers = soft_total_servers[i]

        len_pr_soft = len(project_soft)

        if project_soft == 'activity-relay':

            project_soft = 'activityrelay'

        post_text += f":{project_soft}: {project_users:,} / {project_mau:,} / {project_servers:,}\n"

        i += 1

    print("Tooting...")

    print(post_text)

    servers_image_id = mastodon.media_post('app/graphs/global_servers.png', "image/png", description='servers graph').id

    users_image_id = mastodon.media_post('app/graphs/global_users.png', "image/png", description='users graph').id

    mau_image_id = mastodon.media_post('app/graphs/global_mau.png', "image/png", description='mau graph').id

    weeks_image_id = mastodon.media_post('app/graphs/global_weeks.png', "image/png", description='weeks graph').id 

    mastodon.status_post(post_text, in_reply_to_id=None, media_ids={servers_image_id, users_image_id, mau_image_id, weeks_image_id})
