예제 #1
0
def UpdateCategory(request):
  category = request.REQUEST.get('category')
  user_agent_key = request.REQUEST.get('user_agent_key')
  if not category:
    logging.info('cron.UserAgentGroup: No category')
    return http.HttpResponse('No category')

  test_set = all_test_sets.GetTestSet(category)
  if not test_set:
    logging.info('cron.UserAgentGroup: Bad category: %s', category)
    return http.HttpResponse('Bad category: %s' % category)

  if not user_agent_key:
    logging.info('cron.UserAgentGroup: No key')
    return http.HttpResponse('No key')
  try:
    user_agent = UserAgent.get(db.Key(user_agent_key))
  except db.BadKeyError:
    logging.info('cron.UserAgentGroup: Invalid UserAgent key: %s', user_agent_key)
    return http.HttpResponse('Invalid UserAgent key: %s' % user_agent_key)
  if user_agent:
    logging.info('cron.UserAgentGroup: UpdateCategory(%s, %s)', category, user_agent_key)
    try:
      result_stats.UpdateCategory(category, user_agent)
      return http.HttpResponse('Done with UserAgent key=%s' % user_agent_key)
    except:
      return http.HttpResponse('Got a BadValueError. eff this one.')
  else:
    return http.HttpResponse('No user_agent with this key.')
예제 #2
0
def DataDump(request):
  """This is used by bin/data_dump.py to replicate the datastore."""
  model = request.REQUEST.get('model')
  key_prefix = request.REQUEST.get('key_prefix', '')
  keys_list = request.REQUEST.get('keys')
  time_limit = int(request.REQUEST.get('time_limit', 3))

  if keys_list:
    keys = ['%s%s' % (key_prefix, key) for key in keys_list.split(',')]
  else:
    return http.HttpResponseBadRequest('"keys" is a required parameter.')

  start_time = datetime.datetime.now()

  if model == 'ResultParent':
    query = pager.PagerQuery(ResultParent, keys_only=True)
  elif model == 'UserAgent':
    query = pager.PagerQuery(UserAgent)
  else:
    return http.HttpResponseBadRequest(
        'model must be one of "ResultParent", "UserAgent".')
  data = []
  error = None
  if model == 'ResultParent':
    result_time_query = ResultTime.gql('WHERE ANCESTOR IS :1')
    for result_parent_key in keys:
      if (datetime.datetime.now() - start_time).seconds > time_limit:
        error = 'Over time limit'
        break
      try:
        p = ResultParent.get(result_parent_key)
      except db.Timeout:
        error = 'db.Timeout: ResultParent'
        break
      if not p:
        data.append({
          'model_class': 'ResultParent',
          'lost_key': result_parent_key,
          })
        continue
      result_time_query.bind(p.key())
      try:
        result_times = result_time_query.fetch(1000)
      except db.Timeout:
        error = 'db.Timeout: ResultTime'
        break
      row_data = [{
          'model_class': 'ResultParent',
          'result_parent_key': result_parent_key,
          'category': p.category,
          'user_agent_key': str(
              ResultParent.user_agent.get_value_for_datastore(p)),
          'ip': p.ip,
          'user_id': p.user and p.user.user_id() or None,
          'created': p.created and p.created.isoformat() or None,
          'params_str': p.params_str,
          'loader_id': hasattr(p, 'loader_id') and p.loader_id or None,
          }]
      is_dirty = False
      for result_time in result_times:
        if result_time.dirty:
          is_dirty = True
          break
        row_data.append({
            'model_class': 'ResultTime',
            'result_time_key': str(result_time.key()),
            'result_parent_key': str(result_parent_key),
            'test': result_time.test,
            'score': result_time.score,
            })
      if is_dirty:
        data.append({'dirty_key': result_parent_key,})
      else:
        data.extend(row_data)
  elif model == 'UserAgent':
    try:
      user_agents = UserAgent.get(keys)
    except db.Timeout:
      error = 'db.Timeout: UserAgent'
    else:
      for key, ua in zip(keys, user_agents):
        if ua:
          data.append({
              'model_class': 'UserAgent',
              'user_agent_key': key,
              'string': ua.string,
              'family': ua.family,
              'v1': ua.v1,
              'v2': ua.v2,
              'v3': ua.v3,
              'confirmed': ua.confirmed,
              'created': ua.created and ua.created.isoformat() or None,
              'js_user_agent_string': (hasattr(ua, 'js_user_agent_string') and
                                       ua.js_user_agent_string or None),
              })
        else:
          data.append({
              'model_class': 'UserAgent',
              'lost_key': key,
              })
  response_params = {
      'data': data,
      }
  if error:
    response_params['error'] = error
  return http.HttpResponse(content=simplejson.dumps(response_params),
                           content_type='application/json')