import json
import os
import pymongo
import datetime
import numpy as np

date0 = datetime.datetime(2015,1,1)
datef = datetime.datetime(2016,1,1)
gap_size = (datef-date0).total_seconds()

def issue_time_line_to_arrivals_and_departures(a):
    """
    reads from the aggregation, this function is requiered as issues can be reopened,
    in such a case, each time the issue is reopened, a new arrival is defined, departure
    being its subsequent closure

    """
    issue_time_line = [(event["d"],event["ac"],event["a"]) for event in a]
    issue_time_line.sort()
    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
                depart = (a[0] - date0).total_seconds()
                arrival_pair.append(depart)
                arrivals_departures.append(np.asarray(arrival_pair))
    if len(arrival_pair) > 0: #close with negative
        if open_issue:
            arrival_pair.append((arrival_pair[0]-gap_size))
            arrivals_departures.append(arrival_pair)

    return arrivals_departures

def create_file_per_repo(issues_collection,repo_num,date0,datef,results_dir):
    issues_collection = db["issues"]
    issues_collection.count_documents({})
    metadata = {"arrivals_files": {},
                "arrivals_file_header": {0:"arrivals_id",1:"arrivasl_time",2:"departure_time"},
                "departures_files": {},
                "departures_file_header": {0:"arrivals_id",1:"departure_time"},
                "real_temporal_support": [
                    (date0.strftime("%Y-%m-%d %H:%M:%S"), datef.strftime("%Y-%m-%d %H:%M:%S"))],
                "temporal_support": [(0., (datef - date0).total_seconds())],
                "total_arrivals": 0.,
                "total_departures": 0.}


    current_folder = results_dir + repo_num + "/"
    os.makedirs(current_folder)

    print current_folder
    arrivals_events = [a["arrivals"] for a in issues_collection.find({"_id.r": int(repo_num)})]
    ARRIVALS_AND_DEPARTURES_PER_REPO = []
    for a in arrivals_events:
        ARRIVALS_AND_DEPARTURES_PER_REPO.extend(issue_time_line_to_arrivals_and_departures(a))
    ARRIVALS_AND_DEPARTURES_PER_REPO = np.asarray(ARRIVALS_AND_DEPARTURES_PER_REPO)
    arrivals_num = ARRIVALS_AND_DEPARTURES_PER_REPO.shape[0]
    indexes = np.arange(arrivals_num).reshape((arrivals_num, 1))
    ARRIVALS_AND_DEPARTURES_PER_REPO = np.concatenate([indexes, ARRIVALS_AND_DEPARTURES_PER_REPO], axis=1)

    arrivals_index_sort = np.argsort(ARRIVALS_AND_DEPARTURES_PER_REPO[:, 1])
    departures_index_sort = np.argsort(ARRIVALS_AND_DEPARTURES_PER_REPO[:, 2])
    departure_file = ARRIVALS_AND_DEPARTURES_PER_REPO[departures_index_sort, :]
    departure_file = departure_file[:,np.asarray([0, 2])]
    arrivals_file = ARRIVALS_AND_DEPARTURES_PER_REPO[arrivals_index_sort, :]


    np.savetxt(current_folder + "arrivals_covariates_1.txt", arrivals_file)
    np.savetxt(current_folder + "departures_covarites_1.txt", departure_file)

    metadata["total_arrivals"] = arrivals_file.shape[0]
    metadata["total_departures"] = departure_file.shape[0]
    metadata["arrivals_files"] = {1:arrivals_file.shape[0]}
    metadata["departures_files"] = {1:departure_file.shape[0]}

    json.dump(metadata, open(current_folder + "metadata.json", "w"))

if __name__=="__main__":
    """
    ISSUES AGGREGATION
    """
    date0 = datetime.datetime(2015, 1, 1)
    datef = datetime.datetime(2016, 1, 1)
    gap_size = (datef - date0).total_seconds()

    client = pymongo.MongoClient("129.26.78.40:27017")
    db = client["github"]
    issues_collection = db["issues"]
    push_collection = db["pushes"]
    #best repos
    COUNT = []
    for a in push_collection.find():
        COUNT.append((a["count"], a["_id"]["r"]))
    COUNT.sort()
    COUNT = COUNT[::-1]
    REPOS = [str(c[1]) for c in COUNT]

    data_dir = "/home/anon/Desktop/Doctorado/Projects/RecurrentPointProcesses/Queues/service_data/github/"
    data_dir = "/home/anon/Desktop/Doctorado/Projects/RecurrentNeuralPointProcess/Queues/PreprocessData/Github/"
    data_dir = "/home/anon/Desktop/Doctorado/Projects/RecurrentPointProcesses/Queues/service_data/github2/"

    for repo_num in REPOS:
        create_file_per_repo(issues_collection,repo_num, date0, datef, data_dir)
