Beispiel #1
0
  def get_analytics(self, query):
    # key names
    mck_analytics_json = str(query.key()) + '.analytics-json'
    mck_analytics_json_last_update = str(query.key()) + '.analytics-json-last-update'
    mck_metric_last_update = str(query.key()) + '.last-update'

    # values
    metric_last_update = memcache.get(mck_metric_last_update)
    analytics_json_last_update = memcache.get(mck_analytics_json_last_update)
    analytics_json = memcache.get(mck_analytics_json)

    analytics = None

    try:
      analytics = json.loads(analytics_json)
    except TypeError:
      analytics = None
    except valueError:
      analytics = None

    # cache miss condition 
    if not (analytics is not None and metric_last_update is not None and analytics_json_last_update is not None and int(metric_last_update) < int(analytics_json_last_update)):
     
      # cache miss, so calculate the analytics
      analytics = analyze_query_data(query)
      analytics_json = json.dumps(analytics)

      # update the relevant keys
      memcache.set(
        key=mck_analytics_json, 
        value=analytics_json, 
      )

      memcache.set(
        key=mck_analytics_json_last_update, 
        value=datetime.now().strftime('%s'), 
      )

      # must have fallen out of memcache
      if metric_last_update is None: 
        memcache.set(
          key=mck_metric_last_update,
          value=datetime.now().strftime('%s'),
        )

    analytics_html = self.generate_analysis_view(analytics)

    return analytics_html
Beispiel #2
0
  def get_analytics(self, query):
    # key names
    mck_analytics_json = str(query.key()) + '.analytics-json'
    mck_analytics_json_last_update = str(query.key()) + '.analytics-json-last-update'
    mck_metric_last_update = str(query.key()) + '.last-update'


    # key values
    metric_last_update = memcache.get(mck_metric_last_update)
    analytics_json_last_update = memcache.get(mck_analytics_json_last_update)
    analytics_json = memcache.get(mck_analytics_json)


    # cache miss condition
    if not (analytics_json is not None and metric_last_update is not None and analytics_json_last_update is not None and int(metric_last_update) < int(analytics_json_last_update)):
     
      # cache miss, so calculate the analytics
      analytics_json = json.dumps(analyze_query_data(query))

      # update the relevant keys
      memcache.set( 
        key=mck_analytics_json, 
        value=analytics_json, 
      )
   
      memcache.set(
        key=mck_analytics_json_last_update, 
        value=datetime.now().strftime('%s'), 
      )

      # must have fallen out of memcache
      if metric_last_update is None: 
        memcache.set(
          key=mck_metric_last_update,
          value=datetime.now().strftime('%s'),
        )

    return analytics_json