import os
import pymongo
import datetime

def AggregatesAllIssuesEventsInRepos(collection):
    collection.aggregate([{"$match": {"type": "IssuesEvent"}},
                          {"$group": {"_id": "$repo",
                                      "arrivals": {"$push": {"d": "$date", "a": "$actor", "ac": "$action"}}}},
                          {"$project": {"_id": "$_id", "arrivals": "$arrivals", "count": {"$size": "$arrivals"}}},
                          {"$out": "repos_issues"}], allowDiskUse=True)

def AggregatePerIssues(collection):
    """
    Notice that for aggregating several months,
    to include say a year of github work, one most reaggregate the collections
    :param collection:
    :return:
    """
    collection.aggregate([{"$match": {"type": "IssuesEvent"}},
                          {"$group": {"_id": {"r": "$repo", "i": "$issue"},
                                      "arrivals": {"$push": {"d": "$date", "a": "$actor", "ac": "$action"}}}},
                          {"$project": {"_id": "$_id", "arrivals": "$arrivals", "count": {"$size": "$arrivals"}}},
                          {"$out": "issues"}], allowDiskUse=True)

def issue_time_line_to_arrivals_and_departures(issue_arrivals,date0,datef):
    """
    reads from the aggregation
    """
    gap_size = (datef - date0).total_seconds()

    issue_time_line = [(event["d"],event["ac"],event["a"]) for event in issue_arrivals]
    arrivals_departures = []
    arrival_pair = []
    open_issue = False
    for a in issue_time_line:
        if not open_issue:
            if a[1] == "opened" or a[1] == "reopened":
                open_issue = True
                arrival_pair = [(a[0]-date0).total_seconds()]
        if open_issue:
            if a[1] == "closed":
                open_issue = False
                arrival_pair.append((a[0]-date0).total_seconds())
                arrivals_departures.append(arrival_pair)

    if len(arrival_pair) > 0:
        if open_issue:
            arrival_pair.append((arrival_pair[0]-gap_size))
            arrivals_departures.append(arrival_pair)
    return arrivals_departures

if __name__=="__main__"():

    all_data_dir = "/home/anon/Desktop/Doctorado/Projects/RecurrentPointProcesses/Queues/raw_data/github/all_months/"
    data_dir = "/home/anon/Desktop/Doctorado/Projects/RecurrentNeuralPointProcess/Queues/RawData/GitHub/2015-01/"
    client = pymongo.MongoClient()
    db = client["GitHub"]

    #client = pymongo.MongoClient("129.26.78.40:27017")
    #db = client["github"]
    all_months = [a for a in os.listdir(all_data_dir)]
    for month_string in all_months:
        year = int(month_string.split("-")[0])
        month = int(month_string.split("-")[1])
        if month < 12:
            date0 = datetime.datetime(year,month,1)
            datef = datetime.datetime(year, month+1, 1)
        else:
            date0 = datetime.datetime(year,month,1)
            datef = datetime.datetime(year+1,1, 1)

