#!/usr/local/bin/python
from pydrill.client import PyDrill
import json
import cgi
import sys
from bson.json_util import dumps
from bson import json_util
print "Content-type: application/json\n\n";
#print "Content-type: text/html\n\n";
#print """<p>hehy</p>"""
#drill = PyDrill(host='localhost', port=8047)
from pymongo import MongoClient
drill = PyDrill(host='localhost', port=8047)

if not drill.is_active():
    raise ImproperlyConfigured('Please run Drill first')

city=cgi.FieldStorage().value
#print [city]


city1=json.loads(city)

query="select sum(case when t.attributes.`Good For`.dessert='true' then 1 else 0 end) as Dessert,sum(case when t.attributes.`Good For`.latenight='true' then 1 else 0 end) as LateNight,sum(case when t.attributes.`Good For`.lunch='true' then 1 else 0 end) as Lunch,sum(case when t.attributes.`Good For`.dinner='true' then 1 else 0 end) as Dinner,sum(case when t.attributes.`Good For`.breakfast='true' then 1 else 0 end) as Breakfast,sum(case when t.attributes.`Good For`.brunch='true' then 1 else 0 end) as Brunch from `mongo.274_BI`.`yelp_dataset`t where  true=repeated_contains(categories,'"+city1["data"]+"')and t.stars>3.5 and t.city='"+city1["data2"]+"'"
#print query

yelp_reviews = drill.query(query)

print dumps(yelp_reviews)


# pandas dataframe
