#!/usr/bin/python
	
""" List the AMIs uploaded by fedimg in the last N days.
	
 
	
Deps:  $ sudo yum install python-requests
	
 
	
Author:     Ralph Bean <rbean@redhat.com>
	
License:    LGPLv2+
	
"""
	
 
	
from __future__ import print_function
	
 
	
import argparse
	
import collections
	
import datetime
	
import functools
	
import logging
	
 
	
import requests
	
 
	
log = logging.getLogger()
	
 
	
base_url = 'https://apps.fedoraproject.org/datagrepper/raw'
	
topic = "org.fedoraproject.prod.fedimg.image.publish"
	
 
	
 
	
def get_page(page, pages, delta):
	
    """ Retrieve the JSON for a particular page of datagrepper results """
	
    log.debug("Getting page %i of %s", page, pages)
	
    response = requests.get(base_url, params=dict(
	
        topic=topic,
	
        delta=delta,
	
        page=page,
	
    ))
	
    return response.json()
	
 
	
 
	
 
	
def get_messages(days):
	
    """ Generator that yields messages from datagrepper """
	
 
	
    delta = int(datetime.timedelta(days=days).total_seconds())
	
 
	
    # Get the first page
	
    data = get_page(1, 'unknown', delta)
	
    for message in data['raw_messages']:
	
         yield message
	
 
	
    more = functools.partial(get_page, pages=data['pages'], delta=delta)
	
 
	
    # Get all subsequent pages (if there are any...)
	
    for page in range(1, data['pages']):
	
        data = more(page + 1)
	
 
	
        for message in data['raw_messages']:
	
            yield message
	
 
	
 
	
def parse_args():
	
    parser = argparse.ArgumentParser(__doc__.strip())
	
    parser.add_argument(
	
        '-d', '--days', default=3, type=int,
	
        help="Number of days of history to search.  Default: 3")
	
    parser.add_argument(
	
        '-v', '--verbose', action='store_true',
	
        help="Produce lots of output")
	
 
	
    return parser.parse_args()
	
 
	
 
	
if __name__ == '__main__':
	
    # 1 - Initialize
	
    args = parse_args()
	
    if args.verbose:
	
        logging.basicConfig(level=logging.DEBUG)
	
    else:
	
        logging.basicConfig(level=logging.WARNING)
	
 
	
    # 2 - Build a results dict
	
    results = collections.OrderedDict()
	
    messages = get_messages(args.days)
	
    for message in messages:
	
        key = message['msg']['image_name']
	
        if not key in results:
	
            results[key] = []
	
        results[key].append(message['msg'])
	
 
	
    # 3 - Print it out and format it
	
    for key, uploads in results.items():
	
        for upload in uploads:
	
            extra = upload['extra']
	
            print(
	
                key.ljust(45),
	
                upload.get('destination', '').ljust(20),
	
                extra.get('id', '').ljust(15),
	
                extra.get('virt_type', '').ljust(13),
	
                extra.get('vol_type', '').ljust(15),
	
            )

