# Path: data_model.py
# class PhoneLog(db.Model):
#   phone       = db.StringProperty()
#   to          = db.StringProperty(multiline=True)
#   date        = db.DateTimeProperty(auto_now_add=True)
#   body        = db.StringProperty(multiline=True,indexed=False)
#   smsID       = db.StringProperty(indexed=False)
#   outboundSMS = db.StringProperty(multiline=True,indexed=False)
# 
# Path: data_model.py
# class Caller(db.Model):
# 	# caller's phone number requests are sent from
# 	phone   = db.StringProperty(indexed=True)
#     # the SMB service number Caller has permission to use
# 	service = db.StringProperty()
# 	
# 	created = db.DateProperty(auto_now_add=True)
# 	expires = db.DateProperty()


import os
import logging
import webapp2
import twilio
import config
import paywall
from operator import itemgetter
from datetime import date
from datetime import timedelta
from google.appengine.api.labs.taskqueue import Task
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from data_model import PhoneLog
from data_model import Caller
      output = ''

      startDate = date.today() - timedelta(days=7)
      endDate = date.today()
      for i in range(1,51):
          week = 'week'+str(i)
          s = startDate.isoformat()
          e = endDate.isoformat()
          logging.debug('checking between '+s+' and '+e)
          q = db.GqlQuery("SELECT * FROM PhoneLog WHERE date > DATE(:1) and date <= DATE(:2)", s,e)
          #q = db.GqlQuery("SELECT * FROM PhoneLog WHERE date >= DATE(:1)", s)
          result = q.fetch(500)
          weeklyCount = len(result)
          histogram[week] = weeklyCount
          output += '<p>'+str(i)+':'+str(weeklyCount)+'</p>'
          logging.debug('week '+str(i)+' had '+str(weeklyCount)+' requests')

          # bump the dates backwards
          #runningTotal = len(result)
          endDate = startDate
          startDate = endDate - timedelta(days=7)

      self.response.out.write(output)
## end

class NormalizeLogHandler(webapp2.RequestHandler):
    def get(self):
      callers = {}
      cursor = None


# Path: data_model.py
# class PhoneLog(db.Model):
#   phone       = db.StringProperty()
#   to          = db.StringProperty(multiline=True)
#   date        = db.DateTimeProperty(auto_now_add=True)
#   body        = db.StringProperty(multiline=True,indexed=False)
#   smsID       = db.StringProperty(indexed=False)
#   outboundSMS = db.StringProperty(multiline=True,indexed=False)
# 
# Path: data_model.py
# class Caller(db.Model):
# 	# caller's phone number requests are sent from
# 	phone   = db.StringProperty(indexed=True)
#     # the SMB service number Caller has permission to use
# 	service = db.StringProperty()
# 	
# 	created = db.DateProperty(auto_now_add=True)
# 	expires = db.DateProperty()


# Path: admin.py
import os
import logging
import webapp2
import twilio
import config
import paywall
from operator import itemgetter
from datetime import date
from datetime import timedelta
from google.appengine.api.labs.taskqueue import Task
from google.appengine.ext import db
from google.appengine.ext.webapp import template
from data_model import PhoneLog
from data_model import Caller

      output = ''

      startDate = date.today() - timedelta(days=7)
      endDate = date.today()
      for i in range(1,51):
          week = 'week'+str(i)
          s = startDate.isoformat()
          e = endDate.isoformat()
          logging.debug('checking between '+s+' and '+e)
          q = db.GqlQuery("SELECT * FROM PhoneLog WHERE date > DATE(:1) and date <= DATE(:2)", s,e)
          #q = db.GqlQuery("SELECT * FROM PhoneLog WHERE date >= DATE(:1)", s)
          result = q.fetch(500)
          weeklyCount = len(result)
          histogram[week] = weeklyCount
          output += '<p>'+str(i)+':'+str(weeklyCount)+'</p>'
          logging.debug('week '+str(i)+' had '+str(weeklyCount)+' requests')

          # bump the dates backwards
          #runningTotal = len(result)
          endDate = startDate
          startDate = endDate - timedelta(days=7)

      self.response.out.write(output)
## end

class NormalizeLogHandler(webapp2.RequestHandler):
    def get(self):
      callers = {}
      cursor = None


      q = PhoneLog.all()