#!/usr/bin/env python
#
# Copyright (c) 2010-2017, David Dittrich <dave.dittrich@gmail.com>
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""

"""

import time
import json
import sys
import os
import base64
from optparse import OptionParser
import hashlib
import uuid

# Local imports

from rpc import rpc_common
from rpc import rpcclient
from rpc import rpcrequest
from rpc import rpcresponse

PROGRAM = 'cumdist'

USAGESTRING="""usage: %(_progname)s [options] [args]"""

_progname=sys.argv[0]
_shortname, _extension = os.path.splitext(_progname)


class Cumdist(rpc_common.RPC_Base_Object):
    """ ipgrep anonymization client. """

    NAME = 'Cumdist'

    def __init__(self, *args, **kwargs):
        rpc_common.RPC_Base_Object.__init__(self, *args, **kwargs)


parser = OptionParser(usage=USAGESTRING % vars())
parser.add_option("-d", "--debug",
    action="store_true", dest="debug",
    help="Turn on debugging mode.")
parser.add_option("-r", "--file",
    action="store", dest="fname", metavar="FILE",
    default=None,
    help="File to be processed.")
parser.add_option("-u", "--usage",
    action="store_true", dest="usage",
    help="Print usage information.")
parser.add_option("-v", "--verbose",
    action="store_true", dest="verbose", default=False,
    help="Be verbose (on stdout) about what is happening.")
(options,args) = parser.parse_args()

if options.usage:
    usage = "true"
else:
    usage = "false"

if options.fname is None:
    data = ""
else:
    f = open(options.fname, "r")
    data = json.loads(f.read())
    f.close()

#rpc_common.rdb()

counts = data['matchingstats']
cumval = 0.0
cumpct = 0.0
parval = sorted(counts, key=counts.get, reverse=True)
parval.pop(0)
tot = float(counts['ALLSITES'])

for i in parval:
    n = counts[i]
    cumval += float(n)
    print "%s %d %.2f %.2f" % (\
            i,
            int(n),
            (float(n)/tot*100.0),
            (cumval/tot*100.0))
print "ALLSITES %d %.2f" % (tot,100)
sys.exit(0)
