# %%


PROJECT_PATH = '/home/md/Work/ligalytics/leagues_stable/'
import os, sys
sys.path.insert(0, PROJECT_PATH)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "leagues.settings")
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
from leagues import settings
# settings.DATABASES['default']['NAME'] = PROJECT_PATH+'/db.sqlite3'
settings.DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
settings.DATABASES['default']['HOST'] = '0.0.0.0'
settings.DATABASES['default']['PORT'] = '5432'
settings.DATABASES['default']['USER'] = 'postgres'
settings.DATABASES['default']['PASSWORD'] = 'secret123'
settings.DATABASES['default']['NAME'] = 'mypgsqldb'
settings.DATABASES['default']['ATOMIC_REQUESTS'] = False
settings.DATABASES['default']['AUTOCOMMIT'] = True
settings.DATABASES['default']['CONN_MAX_AGE'] = 0
settings.DATABASES['default']['CONN_HEALTH_CHECKS'] = False
settings.DATABASES['default']['OPTIONS'] = {}
import django
django.setup()

from scheduler.models import *
from common.models import *
from common.stats.models import *
import csv
import datetime
# %%
# MOST ACTIVE


login_dict = defaultdict(lambda:0)
browsers = defaultdict(lambda:0)
devices = defaultdict(lambda:0)

with open('analyze_access.log', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        date = datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
        splitstr = row[1].split('LOGGED IN:')
        if len(splitstr) > 1:
            kind = splitstr[0].split('INFO')[1].strip()
            user = splitstr[1].split('from')[0].strip()
            plattform = splitstr[1].split('with (')
            if len(plattform) > 1:
                plattform = plattform[1].split('/')[0].strip()
            login_dict[(kind,user)] += 1


            if 'Firefox' in row[1]:
                browsers['Firefox'] += 1
            if 'Chrome' in row[1]:
                browsers['Chrome'] += 1
            if 'Safari' in row[1]:
                browsers['Safari'] += 1
            if 'Edge' in row[1]:
                browsers['Edge'] += 1
            if 'Opera' in row[1]:
                browsers['Opera'] += 1
            if 'MSIE' in row[1]:
                browsers['MSIE'] += 1
            if 'Trident' in row[1]:
                browsers['Trident'] += 1
            if 'Netscape' in row[1]:
                browsers['Netscape'] += 1
            if 'Mozilla' in row[1]:
                browsers['Mozilla'] += 1
            if 'AppleWebKit' in row[1]:
                browsers['AppleWebKit'] += 1
            if 'Gecko' in row[1]:
                browsers['Gecko'] += 1
            if 'KHTML' in row[1]:
                browsers['KHTML'] += 1


            if 'iPhone' in row[1]:
                devices['iPhone'] += 1
            elif 'PC' in row[1]:
                devices['PC'] += 1
            elif 'K' in row[1]:
                devices['Android'] += 1
            elif 'Other' in row[1]:
                devices['other'] += 1



browsers = sorted(browsers.items(), key=lambda x:x[1], reverse=True)[:10]
devices = sorted(devices.items(), key=lambda x:x[1], reverse=True)[:10]


# %%
login_dict = sorted(login_dict.items(), key=lambda x:x[1], reverse=True)
# %%



# Monday = 0
weekday_dict = defaultdict(lambda:[0,0,0,0,0,0,0])
weekday_ranking = [0,0,0,0,0,0,0]
hours_dict = defaultdict(lambda:[0]*24)
hours_ranking = [0]*24
clicks_per_day = defaultdict(lambda:defaultdict(lambda:0))
active_days = defaultdict(lambda:set())
active_hours = defaultdict(lambda:set())
wishes_added = defaultdict(lambda:0)
logouts = defaultdict(lambda:0)
solutions = defaultdict(lambda:0)
days = defaultdict(lambda:0)
deletions = defaultdict(lambda:0)
downloads = defaultdict(lambda:0)
actions = defaultdict(lambda:0)
active_teams = defaultdict(lambda:0)
active_clubs = defaultdict(lambda:0)

with open('analyze_info.log', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        date = datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
        verbose = row[1].split('INFO')
        if len(verbose) > 1:
            user = verbose[1].split(':')[0].strip()
            kind = verbose[1].split(':')[1].strip()
            if user == 'md':
                user = 'martin'
            weekday_dict[user][date.weekday()] += 1
            weekday_ranking[date.weekday()] += 1
            hours_dict[user][date.hour] += 1
            hours_ranking[date.hour] += 1
            actions[user] += 1

            clicks_per_day[user][date.strftime("%Y-%m-%d")] += 1
            active_days[user].add(date.strftime("%Y-%m-%d"))
            active_hours[user].add(date.strftime("%Y-%m-%d %H"))

            if '/add/' in kind:
                wishes_added[user] += 1
            if kind == '/accounts/logout/':
                logouts[user] += 1

            if kind == '/schedule_sol/':
                solutions[user] += 1

            if 'day' in kind:
                days[user] += 1

            if 'delete' in kind:
                deletions[user] += 1

            if 'download' in kind:
                downloads[user] += 1

            if user == "SINGLETEAM":
                active_teams[kind.split('/')[0].strip()] += 1

            if user == "CLUB":
                active_clubs[kind.split('/')[0].strip()] += 1

            

downloads = sorted(downloads.items(), key=lambda x:x[1], reverse=True)[:10]
wishes_added = sorted(wishes_added.items(), key=lambda x:x[1], reverse=True)[:10]
logouts = sorted(logouts.items(), key=lambda x:x[1], reverse=True)[:10]
solutions = sorted(solutions.items(), key=lambda x:x[1], reverse=True)[:10]
days = sorted(days.items(), key=lambda x:x[1], reverse=True)[:10]
deletions = sorted(deletions.items(), key=lambda x:x[1], reverse=True)[:10]
active_days = [(user,len(active_days[user])) for user in active_days]
active_days = sorted(active_days, key=lambda x:x[1], reverse=True)[:10]
active_hours = [(user,len(active_hours[user])) for user in active_hours]
active_hours = sorted(active_hours, key=lambda x:x[1], reverse=True)[:10]
actions = sorted(actions.items(), key=lambda x:x[1], reverse=True)[:10]
active_teams = sorted(active_teams.items(), key=lambda x:x[1], reverse=True)[:10]
active_clubs = sorted(active_clubs.items(), key=lambda x:x[1], reverse=True)[:10]

for user in clicks_per_day:
    clicks_per_day[user] = sorted(clicks_per_day[user].items(), key=lambda x:x[1], reverse=True)
# %%




staff_user = ['martin','stephanwestphal','dirk','philipp','Kilian']

actions = {
    u[0]:u[1] for u in actions
}
active = {
    u[0]:u[1] for u in active_days
}
for user in staff_user:
    print(user)
    print(actions[user]//active[user])

# for s in staff_user:
#     print(s)
#     print(weekday_dict[s])
#     print(hours_dict[s])
#     print('')
# # %%
# commits_per_hour = "{&quot;0&quot;:15,&quot;1&quot;:9,&quot;2&quot;:5,&quot;3&quot;:6,&quot;4&quot;:3,&quot;5&quot;:1,&quot;6&quot;:22,&quot;7&quot;:28,&quot;8&quot;:63,&quot;9&quot;:111,&quot;10&quot;:161,&quot;11&quot;:194,&quot;12&quot;:160,&quot;13&quot;:141,&quot;14&quot;:145,&quot;15&quot;:150,&quot;16&quot;:145,&quot;17&quot;:131,&quot;18&quot;:85,&quot;19&quot;:93,&quot;20&quot;:110,&quot;21&quot;:112,&quot;22&quot;:76,&quot;23&quot;:34}"
# commits_per_weekday = "{&quot;Sunday&quot;:135,&quot;Monday&quot;:338,&quot;Tuesday&quot;:341,&quot;Wednesday&quot;:388,&quot;Thursday&quot;:388,&quot;Friday&quot;:302,&quot;Saturday&quot;:108}"


# commits_per_hour = json.loads(commits_per_hour.replace('&quot;','"'))
# commits_per_weekday = json.loads(commits_per_weekday.replace('&quot;','"'))

# %%
import json
with open('trello.json') as f:
    data = json.load(f)

created = defaultdict(lambda:0)
for a in  data['actions']:
    if a['type'] == 'createCard':
        created[a['memberCreator']['fullName']] += 1
# %%

# MOST SCENARIOS
scenarios = {}
for league in League.objects.all():
    scenarios[league.name] = Scenario.objects.filter(season__league=league,lastComputation__gte=datetime.datetime(2023,1,1)).count()

scenarios = sorted(scenarios.items(), key=lambda x:x[1], reverse=True)
# %%

# MOST WISHES
wishes = {}
for league in League.objects.all():
    for scenario in Scenario.objects.filter(season__league=league,lastComputation__gte=datetime.datetime(2023,1,1)):
        wishes[league.name] = HAWish.objects.filter(scenario=scenario).count()+EncWish.objects.filter(scenario=scenario).count()+Blocking.objects.filter(scenario=scenario).count()+Pairing.objects.filter(scenario=scenario).count()

wishes = sorted(wishes.items(), key=lambda x:x[1], reverse=True)[:10]
# %%
