Esempio n. 1
0
 def get(self):
     # List Cities
     citylist = {}
     cities = City.all()
     for city in cities:
         citylist[city.Name] = {0:abs(hash(city.Name)),1:[]}
         for station in city.station_set:
             data = {'Name':station.Name}
             query = Query(AQIData)
             query.filter('Station =', station.Code)
             query.order('-Date')
             query.run()
             aqi = query.get()
             data['AQI'] = aqi.AQI
             data['Level'] = aqi.AQILevel
             data['Assess'] = aqi.AQIAssess
             data['Majority'] = aqi.Majority
             data['Date'] = aqi.Date
             citylist[city.Name][1].append(data)
     # logging.info(str(citylist))
     #----generate parameter list----------------------------------------------------------------------
     template_values = {
         'citylist' : citylist,
         }
     path = os.path.join(os.path.dirname(__file__), './/template//citylist.html')
     #----end------------------------------------------------------------------------------------------
     self.response.out.write(template.render(path,template_values))
Esempio n. 2
0
    def get(self):
        # get city id
        city_id = self.request.get('city','')
        try:
            city_id = int(city_id)
        except:
            city_id = 0

        if city_id == 0:
            # no city? --> show city list
            citylist = {}
            cities = City.all()
            for city in cities:
                citylist[city.Name] = [city.Code,abs(hash(city.Name))]
            #----generate parameter list----------------------------------------------------------------------
            template_values = {
                'citylist' : citylist,
                'stationlist' : None
                }
            path = os.path.join(os.path.dirname(__file__), './/template//citylist_all.html')
            #----end------------------------------------------------------------------------------------------
            self.response.out.write(template.render(path,template_values))
        else:
            # show station plots
            stationlist = {}
            query = Query(City)
            query.filter('Code =', city_id)
            query.run()
            city = query.get()
            for station in city.station_set:
                data = []
                query = Query(AQIData)
                query.filter('Station =', station.Code)
                query.order('-Date')
                query.run()
                aqi = query.fetch(None)
                for entry in aqi:
                    data.append("['%s',%d]" % (str(entry.Date),entry.AQI))
                stationlist[station.Name] = ','.join(data)
            #----generate parameter list----------------------------------------------------------------------
            template_values = {
                'citylist' : None,
                'stationlist' : stationlist
                }
            path = os.path.join(os.path.dirname(__file__), './/template//citylist_all.html')
            #----end------------------------------------------------------------------------------------------
            self.response.out.write(template.render(path,template_values))
def BatchSitesFromIds(event):
    logging.debug("BatchSitesFromIds")
    """Given a string of ids, like "1,2,3", returns corresponding Site objects.
  If comma_separated_ids is empty, returns all sites.
  """
    q = Query(model_class=site_db.Site)
    q.filter('event = ', event)
    sites = q.run(limit=1000, offset=4000)

    return sites
Esempio n. 4
0
def BatchSitesFromIds(event):
  logging.debug("BatchSitesFromIds")
  """Given a string of ids, like "1,2,3", returns corresponding Site objects.
  If comma_separated_ids is empty, returns all sites.
  """
  q = Query(model_class = site_db.Site)
  q.filter('event = ', event)
  sites = q.run(limit=1000, offset = 4000)

  
  return sites
Esempio n. 5
0
def GetAllCached(event, ids = None):
  if ids == None:
    q = Query(model_class = Site, keys_only = True)
    q.filter("event =", event)
    ids = [key.id() for key in q.run(batch_size = 2000)]
  lookup_ids = [str(id) for id in ids]
  cache_results = memcache.get_multi(lookup_ids, key_prefix = cache_prefix)
  not_found = [id for id in ids if not str(id) in cache_results.keys()]
  data_store_results = []
  orgs = dict([(o.key(), o) for o in organization.GetAllCached()])
  events = dict([(e.key(), e) for e in event_db.GetAllCached()])
  if len(not_found):
    data_store_results = [(site, SiteToDict(site)) for site in
                          GetSitesAndSetReferences(not_found, events, orgs)]
    memcache.set_multi(dict([(str(site[0].key().id()), site)
                             for site in data_store_results]),
                       key_prefix = cache_prefix,
                       time = cache_time)

  sites = cache_results.values() + data_store_results
  return sites
Esempio n. 6
0
def GetAllCached(event, ids = None):
  if ids == None:
    if cache_ids:
      cache_key_for_ids = "SiteDictIds:" + event.key().id() + ":" + county 
      ids = memcache.get(cache_key_for_ids)
      if not ids:
        # Retrieve all matching keys. As a keys_only scan,
        # This should be more efficient than a full data scan.
        q = Query(model_class = Site, keys_only = True)
        q.filter("event =", event)
        ids = [key.id() for key in q]
        # Cache these for up to six minutes.
        # TODO(Jeremy): This may do more harm than
        # good, depending on how often
        # people reload the map.
        memcache.set(cache_key_for_ids, ids,
                     time = 360)
    else:
      q = Query(model_class = Site, keys_only = True)
      q.filter("event =", event)
    
      ids = [key.id() for key in q.run(batch_size = 2000)]
  lookup_ids = [str(id) for id in ids]
  cache_results = memcache.get_multi(lookup_ids, key_prefix = cache_prefix)
  not_found = [id for id in ids if not str(id) in cache_results.keys()]
  data_store_results = []
  orgs = dict([(o.key(), o) for o in organization.GetAllCached()])
  events = dict([(e.key(), e) for e in event_db.GetAllCached()])
  if len(not_found):
    data_store_results = [(site, SiteToDict(site)) for site in
                          GetSitesAndSetReferences(not_found, events, orgs)]
    memcache.set_multi(dict([(str(site[0].key().id()), site)
                             for site in data_store_results]),
                       key_prefix = cache_prefix,
                       time = cache_time)

  sites = cache_results.values() + data_store_results
  return sites
Esempio n. 7
0
def GetAllCached(event, ids=None):
    if ids == None:
        q = Query(model_class=Site, keys_only=True)
        q.filter("event =", event)
        ids = [key.id() for key in q.run(batch_size=2000)]
    lookup_ids = [str(id) for id in ids]
    cache_results = memcache.get_multi(lookup_ids, key_prefix=cache_prefix)
    not_found = [id for id in ids if not str(id) in cache_results.keys()]
    data_store_results = []
    orgs = dict([(o.key(), o) for o in organization.GetAllCached()])
    events = dict([(e.key(), e) for e in event_db.GetAllCached()])
    if len(not_found):
        data_store_results = [
            (site, SiteToDict(site))
            for site in GetSitesAndSetReferences(not_found, events, orgs)
        ]
        memcache.set_multi(dict([(str(site[0].key().id()), site)
                                 for site in data_store_results]),
                           key_prefix=cache_prefix,
                           time=cache_time)

    sites = cache_results.values() + data_store_results
    return sites