import dateutil
import pymysql
connection = pymysql.connect(host='paws-db',
                             user='cscw',
                             password='cscw',
                             cursorclass=pymysql.cursors.DictCursor
                             )
%matplotlib inline
def graphData(data_lists, title):
#     plt.set_xlim([datetime.date(2015, 1, 1), datetime.date(2015, 2, 1)])
    plt.figure(figsize=(10,10))
    plt.scatter(data_lists[0], data_lists[1], s=data_lists[2])
#     plt.scatter(data_lists[0], data_lists[1])
    
#     c=data_lists[2], cmap=matplotlib.cm.Reds
    plt.ylabel("session duration (minutes)")
    plt.xlabel("session start date")
    plt.title("edit sessions for " + title)
    plt.show()
# test_data = [{'events': 2, 'start': b'20150114220459', 'index': 389, 'user_text': b'Staeiou', 'end': b'20150114220536'}, {'events': 1, 'start': b'20150210181112', 'index': 390, 'user_text': b'Staeiou', 'end': b'20150210181112'}, {'events': 10, 'start': b'20150219061205', 'index': 391, 'user_text': b'Staeiou', 'end': b'20150219071615'}, {'events': 1, 'start': b'20150226212413', 'index': 392, 'user_text': b'Staeiou', 'end': b'20150226212413'}, {'events': 1, 'start': b'20150429030742', 'index': 393, 'user_text': b'Staeiou', 'end': b'20150429030742'}, {'events': 7, 'start': b'20150506180853', 'index': 394, 'user_text': b'Staeiou', 'end': b'20150506181459'}, {'events': 1, 'start': b'20150607050200', 'index': 395, 'user_text': b'Staeiou', 'end': b'20150607050200'}, {'events': 5, 'start': b'20150618044849', 'index': 396, 'user_text': b'Staeiou', 'end': b'20150618052333'}, {'events': 1, 'start': b'20150618185751', 'index': 397, 'user_text': b'Staeiou', 'end': b'20150618185751'}, {'events': 4, 'start': b'20150618195802', 'index': 398, 'user_text': b'Staeiou', 'end': b'20150618202637'}, {'events': 11, 'start': b'20150618231923', 'index': 399, 'user_text': b'Staeiou', 'end': b'20150619010816'}, {'events': 1, 'start': b'20150624061811', 'index': 400, 'user_text': b'Staeiou', 'end': b'20150624061811'}, {'events': 2, 'start': b'20150626010848', 'index': 401, 'user_text': b'Staeiou', 'end': b'20150626010911'}, {'events': 2, 'start': b'20150716091421', 'index': 402, 'user_text': b'Staeiou', 'end': b'20150716091506'}, {'events': 1, 'start': b'20150730165911', 'index': 403, 'user_text': b'Staeiou', 'end': b'20150730165911'}, {'events': 1, 'start': b'20150731182217', 'index': 404, 'user_text': b'Staeiou', 'end': b'20150731182217'}]

# parseData(test_data)
import math
import matplotlib
import matplotlib.pyplot as plt
import numpy
def parseData(data):
    session_start_date = [dateutil.parser.parse(x['start']) for x in data]
#     print(len(session_start_date))
    session_length = [((dateutil.parser.parse(x['end'])-dateutil.parser.parse(x['start'])).seconds)//60 for x in data]
    session_events = [int(math.pi * (x['events'] * x['events'])) for x in data]
#     session_intensity = calcSessionIntensity(session_length, session_events, data)

    
    return (session_start_date, session_length, session_events)
#     return (session_start_date, session_length, session_events, session_intensity)
# data = [{'events': 2, 'start': b'20150114220459', 'index': 389, 'user_text': b'Staeiou', 'end': b'20150114220536'}, {'events': 1, 'start': b'20150210181112', 'index': 390, 'user_text': b'Staeiou', 'end': b'20150210181112'}, {'events': 10, 'start': b'20150219061205', 'index': 391, 'user_text': b'Staeiou', 'end': b'20150219071615'}, {'events': 1, 'start': b'20150226212413', 'index': 392, 'user_text': b'Staeiou', 'end': b'20150226212413'}, {'events': 1, 'start': b'20150429030742', 'index': 393, 'user_text': b'Staeiou', 'end': b'20150429030742'}, {'events': 7, 'start': b'20150506180853', 'index': 394, 'user_text': b'Staeiou', 'end': b'20150506181459'}, {'events': 1, 'start': b'20150607050200', 'index': 395, 'user_text': b'Staeiou', 'end': b'20150607050200'}, {'events': 5, 'start': b'20150618044849', 'index': 396, 'user_text': b'Staeiou', 'end': b'20150618052333'}, {'events': 1, 'start': b'20150618185751', 'index': 397, 'user_text': b'Staeiou', 'end': b'20150618185751'}, {'events': 4, 'start': b'20150618195802', 'index': 398, 'user_text': b'Staeiou', 'end': b'20150618202637'}, {'events': 11, 'start': b'20150618231923', 'index': 399, 'user_text': b'Staeiou', 'end': b'20150619010816'}, {'events': 1, 'start': b'20150624061811', 'index': 400, 'user_text': b'Staeiou', 'end': b'20150624061811'}, {'events': 2, 'start': b'20150626010848', 'index': 401, 'user_text': b'Staeiou', 'end': b'20150626010911'}, {'events': 2, 'start': b'20150716091421', 'index': 402, 'user_text': b'Staeiou', 'end': b'20150716091506'}, {'events': 1, 'start': b'20150730165911', 'index': 403, 'user_text': b'Staeiou', 'end': b'20150730165911'}, {'events': 1, 'start': b'20150731182217', 'index': 404, 'user_text': b'Staeiou', 'end': b'20150731182217'}]
# parseData(data)
def queryDB(username, start, end):
    with connection.cursor() as cur:
        cur.execute('use datasets_p;')
        cur.execute('select * from enwiki_sessions_20150801 where user_text = %s and start between %s and %s;', (username, start, end))
        data = cur.fetchall()
#         print(data)
        return data
from ipywidgets import interact
@interact
def makeGraph(username='Staeiou', start='20150101000000', end='20151231000000'):
    data = queryDB(username, start, end)
    data_lists = parseData(data)
    graphData(data_lists, username)
def calcSessionIntensity(session_length, session_events, data):
#     print(session_events)
#     session_intensity = []
#     session_length_seconds = [(dateutil.parser.parse(x['end'])-dateutil.parser.parse(x['start'])).seconds for x in data]

#         session_intensity.append(s//int(session_length[s]))
#     print(session_intensity)    
#     print(session_events)
#     session_events_area = [(math.pi * x) * 1000 for x in session_events]
#     print (session_length)
    return session_intensity
 
# %matplotlib inline

# plt.figure(figsize=(10,10))
# plt.scatter([1,2], [3600,750], s=[10,100], c=[360,730], cmap=matplotlib.cm.Reds)
# plt.ylabel("session duration (minutes)")
# plt.xlabel("session start date")
# plt.show()