#! /usr/bin/env python

import argparse
from collections import Counter
import logging
import multiprocessing

from okra.distributed import getwork_dbinfo, write_features_target
from okra.distributed import run_distributed_pool, consolidate_features_target
from okra.logging_utils import enable_cloud_log

logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser()
parser.add_argument('cache', help='location of file cache')

args = parser.parse_args()


# Configure logging

enable_cloud_log(level='INFO')

if args.cache:

    logger.info("STARTED distributed processing")
    
    work = getwork_dbinfo(args.cache)
    if len(work) == 0:
        raise TypeError("Unable to find work")
    
    n_cores = multiprocessing.cpu_count()
    result = run_distributed_pool(n_cores, write_features_target, work)
    summary = Counter(result)

    consolidate_features_target(cache=args.cache,
                                repo_id='__LOGDF__',
                                report=100)

    logger.info("Completed {} successes, {} failures".\
                format(summary[True], summary[False]))

    logger.info("FINISHED distributed processing")

else:
    parser.print_help()
