#!/usr/bin/env python

import json
import http.client
import os


def callRESTAPI(restURL, url="cmsweb.cern.ch"):
    conn = http.client.HTTPSConnection(url, cert_file=os.getenv('X509_USER_CERT'),
                                 key_file=os.getenv('X509_USER_KEY'))
    conn.request('GET', restURL, headers={"Accept": "application/json"})
    r2 = conn.getresponse()
    data = r2.read()
    s = json.loads(data)
    return s

def getRequestInfo(url):
    restURL = '/wmstatsserver/data/filtered_requests?mask=OutputDatasets&mask=RequestType'
    result = callRESTAPI(restURL, url)["result"]
    return result

def investigateACDCCollection(workflow):
    restURL = '/couchdb/acdcserver/_design/ACDC/_view/byCollectionName?key="%s"&reduce=false&include_docs=true' % workflow
    result = callRESTAPI(restURL)
    missingFiles = set()
    if "rows" in result:
        if result["rows"]:
            for row in result["rows"]:
                for inFile, value in row["doc"]["files"].items():
                    if not value["merged"] or value["merged"] == "0":
                        if value["parents"] and "/unmerged/" in value["parents"][0]:
                            missingFiles.add(inFile)
        return missingFiles
    else:
        print(result)
        return missingFiles

if __name__ == '__main__':

    for wf in getRequestInfo():
        missingFiles = investigateACDCCollection(wf["RequestName"])

    with open("./child_with_umerged_parents.json", "w") as f:
        json.dump(list(missingFiles), f, indent=4)

    print(len(missingFiles))