Esempio n. 1
0
def submit(environ, start_response):
    setupStderr(environ["wsgi.errors"])
    config = get_config()

    # Check that this is a POST request
    if environ["REQUEST_METHOD"] != "POST":
        return common.show_error("Unsupported request method", start_response)

    # Parse the submitted JSON
    try:
        data = json.loads(environ["wsgi.input"].read(
            int(environ["CONTENT_LENGTH"])))
    except (KeyError, IOError, ValueError):
        return common.show_error("Error while parsing JSON data.",
                                 start_response)

    # Make sure the submitted data was contained within an object at least
    if not isinstance(data, dict):
        return common.show_error(
            "Error, data must be contained within an object.", start_response)

    # Log the data to a file
    log_dir = config.get("filterhitstats", "log_dir")
    try:
        log_file = log_filterhits(data, log_dir,
                                  environ.get("QUERY_STRING", ""))
    except (OSError, IOError):
        traceback.print_exc()
        return common.show_error("Failed to write data to log file!",
                                 start_response, "500 Logging error")

    # Update the geometrical_mean aggregations in the database
    interval = config.get("filterhitstats", "interval")
    try:
        db_connection = db.connect()
        try:
            db.write(db_connection, geometrical_mean.update(interval, data))
        finally:
            db_connection.close()
    except:
        # Updating the aggregations in the database failed for whatever reason,
        # log the details but continue to return 204 to the client to avoid the
        # re-transmission of data.
        processing_error_log = os.path.join(
            config.get("filterhitstats", "log_dir"), "processing-errors.log")
        with open(processing_error_log, "a+") as f:
            message = "Problem processing data file %s:\n%s" % (
                log_file, traceback.format_exc())
            print >> f, "[%s] %s" % (
                datetime.now().strftime("%d/%b/%Y:%H:%M:%S %z"), message)

    # Send back a 204 No Content
    start_response("204 No Content", [])
    return []
Esempio n. 2
0
def submit(environ, start_response):
  setupStderr(environ["wsgi.errors"])
  config = get_config()

  # Check that this is a POST request
  if environ["REQUEST_METHOD"] != "POST":
    return common.show_error("Unsupported request method", start_response)

  # Parse the submitted JSON
  try:
    data = json.loads(environ["wsgi.input"].read(int(environ["CONTENT_LENGTH"])))
  except (KeyError, IOError, ValueError):
    return common.show_error("Error while parsing JSON data.", start_response)

  # Make sure the submitted data was contained within an object at least
  if not isinstance(data, dict):
    return common.show_error("Error, data must be contained within an object.", start_response)

  # Log the data to a file
  log_dir = config.get("filterhitstats", "log_dir")
  try:
    log_file = log_filterhits(data, log_dir, environ.get("QUERY_STRING", ""))
  except (OSError, IOError):
    traceback.print_exc()
    return common.show_error("Failed to write data to log file!", start_response,
                             "500 Logging error")

  # Update the geometrical_mean aggregations in the database
  interval = config.get("filterhitstats", "interval")
  try:
    db_connection = db.connect()
    try:
      db.write(db_connection, geometrical_mean.update(interval, data))
    finally:
      db_connection.close()
  except:
    # Updating the aggregations in the database failed for whatever reason,
    # log the details but continue to return 204 to the client to avoid the
    # re-transmission of data.
    processing_error_log = os.path.join(config.get("filterhitstats", "log_dir"),
                                        "processing-errors.log")
    with open(processing_error_log, "a+") as f:
      message = "Problem processing data file %s:\n%s" % (
        log_file, traceback.format_exc()
      )
      print >> f, "[%s] %s" % (datetime.now().strftime("%d/%b/%Y:%H:%M:%S %z"), message)

  # Send back a 204 No Content
  start_response("204 No Content", [])
  return []
Esempio n. 3
0
def query_handler(environ, start_response):
  setupStderr(environ["wsgi.errors"])
  params = dict(parse_qsl(environ.get("QUERY_STRING", "")))

  try:
    db_connection = db.connect()
    try:
      results = db.query(db_connection, *query(**params), dict_result=True)
      total = db.query(db_connection, "SELECT FOUND_ROWS()")[0][0]
    finally:
      db_connection.close()
  except MySQLdb.Error:
    traceback.print_exc()
    return common.show_error("Failed to query database!", start_response,
                             "500 Database error")

  try:
    echo = int(params["echo"])
  except (ValueError, KeyError):
    echo = 0

  response_headers = [("Content-type", "application/json; charset=utf-8")]
  start_response("200 OK", response_headers)
  return [json.dumps({"results": results, "echo": echo,
                      "total": total, "count": len(results)},
                     ensure_ascii=False).encode("utf-8")]
Esempio n. 4
0
def query_handler(environ, start_response):
  setupStderr(environ["wsgi.errors"])
  params = dict(parse_qsl(environ.get("QUERY_STRING", "")))

  try:
    db_connection = db.connect()
    try:
      results = db.query(db_connection, *query(**params), dict_result=True)
      total = db.query(db_connection, "SELECT FOUND_ROWS()")[0][0]
    finally:
      db_connection.close()
  except MySQLdb.Error:
    traceback.print_exc()
    return common.show_error("Failed to query database!", start_response,
                             "500 Database error")

  try:
    echo = int(params["echo"])
  except (ValueError, KeyError):
    echo = 0

  response_headers = [("Content-type", "application/json; charset=utf-8")]
  start_response("200 OK", response_headers)
  return [json.dumps({"results": results, "echo": echo,
                      "total": total, "count": len(results)},
                     ensure_ascii=False).encode("utf-8")]