#!/usr/bin/env python
# coding: utf-8
#
# Display done, running, pending and failed tasks in terminal (http://i.imgur.com/yXMIfFT.png).
#
# Poor man's top:
#
#     $ watch taskps localhost:8082
#
# ----
#
# curl -s http://localhost:8082/api/task_list -d 'data={"status":"PENDING","upstream_status":"","search":""}' | jq '.'
#
# DONE
# ----
#
# * AIFilterConfig(date=2016-03-23)
# * AIIntermediateSchema(date=2016-03-23)
# * CrossrefIntermediateSchema(begin=2006-01-01, date=2016-03-23)
# * CrossrefUniqItems(begin=2006-01-01, date=2016-03-23)
# * DOAJFiltered(date=2016-03-23)
# * DOAJIntermediateSchema(date=2016-03-23)
# * DegruyterIntermediateSchema(date=2016-03-23)
# * DegruyterXML(date=2016-03-23, group=SSH)
# * Executable(name=pigz, message=http://zlib.net/pigz/)
# * Executable(name=span-import, message=http://git.io/vI8NV)
# * GBIIntermediateSchemaByKind(issue=20151101000000, since=2015-11-01, date=2016-03-23, kind=fulltext)
# * GBIIntermediateSchemaByKind(issue=20151101000000, since=2015-11-01, date=2016-03-23, kind=references)
# * JstorIntermediateSchema(date=2016-03-23)
# * JstorXML(date=2016-03-23)
#
# RUNNING
# -------
#
# * AILicensing(date=2016-03-23)
#
# PENDING
# -------
#
# * AIExport(date=2016-03-23)
#
# FAILED
# ------
#
# ----------------
#
# CentOS, Python 2.7.9:
#
# close failed in file object destructor:
#                                       sys.excepthook is missing
#                                                               lost sys.stderr
#
# http://bugs.python.org/issue11380


from __future__ import print_function

import argparse
import datetime
import errno
import json
import sys

import requests

if __name__ == '__main__':

    addr = 'localhost:8082'

    if len(sys.argv) >= 2:
        addr = sys.argv[1]
        if ':' not in addr:
            addr = '%s:8082' % addr

    try:
        print()
        print('>> %s, %s' % (addr, datetime.datetime.now()))
        print()

        for status in ['RUNNING', 'FAILED', 'PENDING', 'DONE', 'DISABLED']:

            data = {"status": status, "upstream_status": "", "search": ""}
            r = requests.get('http://%s/api/task_list' % addr, params={'data': json.dumps(data)})
            if r.status_code >= 400:
                raise RuntimeError('API (%s) returned %s: %s' % (r.url, r.status_code, r.text))
            response = json.loads(r.text)

            tasks = [task for task, _ in sorted(response['response'].items())]

            if len(tasks) == 0:
                continue

            head = '%s (%s)' % (status, len(tasks))
            print(head)
            print('-' * len(head))
            print()
            for task in tasks:
                print('%s\t* %s' % (status[0].lower(), task))
            print()
    except IOError as err:
        if err.errno == errno.EPIPE:
            pass
        else:
            raise

    # http://stackoverflow.com/a/8690674/89391, http://bugs.python.org/issue11380
    try:
        sys.stdout.close()
    except:
        pass

    try:
        sys.stderr.close()
    except:
        pass
