import os
import httplib
import json
import re

from django.core.management.base import BaseCommand
from django.db.models import Q
from pricing.models import AWS
from pricing.models import GCP

class Command(BaseCommand):

  def add_arguments(self, parser):
    parser.add_argument('command', nargs = 1)


  def handle(self, *args, **options):

    print options['command'][0]

    cmd = options['command'][0]

    if cmd == 'compare':
        # Compare AWS and GCS instances. Try to find matches.

        # apr is AWS queryset
        apr = AWS.objects;
        apr = apr.filter(offer_code = 'AmazonEC2', product_family = 'Compute Instance',
                         term_type = 'OnDemand', tenancy = 'Shared',
                         operating_system = 'Linux')

        gpr = GCP.objects
        gpr = gpr.filter(ptype = 'COMPUTEENGINE', psubtype = 'VMIMAGE', preemptible = False)

        print apr.count(), gpr.count()

        lm = 0.0
        for m in [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0]:
            print "==== memory <", m
            a_1g = apr.filter(memory__lte = m, memory__gt = lm).order_by('instance_type')
            g_1g = gpr.filter(memory__lte = m, memory__gt = lm)
            print a_1g.count(), g_1g.count()

            for a in a_1g:
                print a.instance_type, a.memory, a.vcpu,  a.price_per_unit, a.location

            for g in g_1g:
                print g.pargs, g.memory, g.cores, g.us, g.asia, g.europe

            lm = m

        return

#    # foreach instance_type
#    #   forech location
#    #    get Windows "Bring your own licence
#    #    get Windows "No License required
#    #    get Wiindows with pre_installed SW required
#    bq = AWS.objects.filter(Q(offer_code = 'AmazonEC2'),
#                            Q(product_family = 'Compute Instance'),
#                            Q(term_type = 'OnDemand'),
#                            Q(tenancy = 'Shared'),
#                            Q(instance_family = 'General purpose'),
#                            Q(current_generation = 'Yes'),
#                            Q(price_per_unit__gt = 0.0))
#    istq = bq.values('instance_type', 'current_generation').order_by('instance_type').distinct()
#    locq = bq.values('location').order_by('location').distinct()
#    
#    for l in locq:
#        for i in istq:
#            base = bq.filter(Q(instance_type = i['instance_type']),
#                             Q(location = l['location']),
#                             Q(license_model = 'Bring your own license'))
#
#            if len(base) == 0:
#                continue
#
#
#            win = bq.filter(Q(instance_type = i['instance_type']),
#                             Q(location = l['location']),
#                             Q(pre_installed_sw = 'NA'),
#                             Q(license_model = 'License Included'))
#            if len(win) == 0:
#                continue
#
#            b = base.first().price_per_unit
#            w = win.first().price_per_unit
#            c = base.first().vcpu
#            m = base.first().memory
#            print '%s,%s,%f,%f,%f,%f,%f' % (l['location'], i['instance_type'], c, m, b, w, (w - b)/b)

@@ -93,16 +93,82 @@
 Command%0A
+from django.db.models import Min%0Afrom django.db.models import Max%0A
 from dja
@@ -1071,16 +1071,23 @@
 0, 256.0
+, 512.0
 %5D:%0A     
@@ -1113,16 +1113,17 @@
 memory %3C
+ 
 %22, m%0A   
@@ -1209,16 +1209,17 @@
 _type')%0A
+%0A
         
@@ -1226,118 +1226,1085 @@
     
-g_1g = gpr.filter(memory__lte = m, memory__gt = lm)%0A            print a_1g.count(), g_1g.count()%0A%0A            
+for name in a_1g.values('instance_type', 'memory', 'vcpu').distinct():%0A                x = a_1g.filter(instance_type = name%5B'instance_type'%5D)%0A                regions = x.values('location', 'price_per_unit').distinct().order_by('location')%0A                minprice = x.values('price_per_unit').aggregate(Min('price_per_unit'))%0A                maxprice = x.values('price_per_unit').aggregate(Max('price_per_unit'))%0A                print name%5B'instance_type'%5D, name%5B'memory'%5D, name%5B'vcpu'%5D, minprice, maxprice%0A                for r in regions:%0A                    print '    %25s  %25f' %25 (r%5B'location'%5D, r%5B'price_per_unit'%5D)%0A                %0A%0A            #a_minprice = a_1g.values('price_per_unit').aggregate(Min('price_per_unit'))%0A            #a_maxprice = a_1g.values('price_per_unit').aggregate(Max('price_per_unit'))%0A            #a_instance = a_1g.values('instance_type').distinct()%0A            #a_vcpu = a_1g.values('vcpu').distinct()%0A            #a_memory = a_1g.values('memory').distinct()%0A%0A            #print a_instance, a_memory, a_vcpu, a_minprice, a_maxprice%0A            #
 for 
@@ -2318,32 +2318,33 @@
 1g:%0A            
+#
     print a.inst
@@ -2403,16 +2403,80 @@
 cation%0A%0A
+            g_1g = gpr.filter(memory__lte = m, memory__gt = lm)%0A
         

Tweak experimental price comparison.