Commit cb19b030 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

Merge branch 'fnordmetric2' of github.com:paulasmuth/fnordmetric-dev into adminui_fixes

parents 974b2781 399bed20
Loading
Loading
Loading
Loading
+45 −6
Original line number Diff line number Diff line
@@ -224,11 +224,15 @@ void HTTPAPI::executeQuery(
    http::HTTPRequest* request,
    http::HTTPResponse* response,
    util::URI* uri) {
  response->setStatus(http::kStatusOK);
  response->addHeader("Content-Type", "application/json; charset=utf-8");
  auto params = uri->queryParams();

  std::shared_ptr<util::InputStream> input_stream =
      request->getBodyInputStream();
  std::shared_ptr<util::InputStream> input_stream;
  std::string get_query;
  if (util::URI::getParam(params, "q", &get_query)) {
    input_stream.reset(new util::StringInputStream(get_query));
  } else {
    input_stream = request->getBodyInputStream();
  }

  std::shared_ptr<util::OutputStream> output_stream =
      response->getBodyOutputStream();
@@ -237,12 +241,47 @@ void HTTPAPI::executeQuery(
  std::unique_ptr<query::TableRepository> table_repo(
      new MetricTableRepository(metric_repo_));

  query::QueryService::kFormat resp_format = query::QueryService::FORMAT_JSON;
  std::string format_param;
  if (util::URI::getParam(params, "format", &format_param)) {
    if (format_param == "svg") {
      resp_format = query::QueryService::FORMAT_SVG;
    }
  }

  response->setStatus(http::kStatusOK);

  switch (resp_format) {
    case query::QueryService::FORMAT_JSON:
      response->addHeader("Content-Type", "application/json; charset=utf-8");
      break;
    case query::QueryService::FORMAT_SVG:
      response->addHeader("Content-Type", "text/html; charset=utf-8");
      break;
    default:
      break;
  }

  int width = -1;
  std::string width_param;
  if (util::URI::getParam(params, "width", &width_param)) {
    width = std::stoi(width_param);
  }

  int height = -1;
  std::string height_param;
  if (util::URI::getParam(params, "height", &height_param)) {
    height = std::stoi(height_param);
  }

  try {
    query_service.executeQuery(
        input_stream,
        query::QueryService::FORMAT_JSON,
        resp_format,
        output_stream,
        std::move(table_repo));
        std::move(table_repo),
        width,
        height);

  } catch (util::RuntimeException e) {
    response->clearBody();
+21 −9
Original line number Diff line number Diff line
@@ -38,7 +38,9 @@ void QueryService::executeQuery(
    std::shared_ptr<util::InputStream> input_stream,
    kFormat output_format,
    std::shared_ptr<util::OutputStream> output_stream,
    std::unique_ptr<TableRepository> table_repo) {
    std::unique_ptr<TableRepository> table_repo,
    int width /* = -1 */,
    int height /* = -1 */) {
  std::string query_string;
  input_stream->readUntilEOF(&query_string);

@@ -54,16 +56,15 @@ void QueryService::executeQuery(
    query.execute();

    switch (output_format) {

      case FORMAT_SVG: {
        ui::SVGTarget target(output_stream.get());
        renderCharts(&query, &target);
        renderCharts(&query, &target, width, height);
        break;
      }

      case FORMAT_JSON: {
        util::JSONOutputStream target(output_stream);
        renderJSON(&query, &target);
        renderJSON(&query, &target, width, height);
        break;
      }

@@ -86,14 +87,23 @@ void QueryService::registerBackend(std::unique_ptr<Backend>&& backend) {
  runtime_.addBackend(std::move(backend));
}

void QueryService::renderCharts(Query* query, ui::RenderTarget* target) const {
void QueryService::renderCharts(
    Query* query,
    ui::RenderTarget* target,
    int width,
    int height) const {
  for (int i = 0; i < query->getNumCharts(); ++i) {
    query->getChart(i)->render(target);
    auto chart = query->getChart(i);
    chart->setDimensions(width, height);
    chart->render(target);
  }
}

void QueryService::renderJSON(Query* query, util::JSONOutputStream* target)
    const {
void QueryService::renderJSON(
    Query* query,
    util::JSONOutputStream* target,
    int width,
    int height) const {
  target->beginObject();

  if (query->getNumResultLists() > 0) {
@@ -153,7 +163,9 @@ void QueryService::renderJSON(Query* query, util::JSONOutputStream* target)
      std::string svg_data;
      auto string_stream = util::StringOutputStream::fromString(&svg_data);
      ui::SVGTarget svg_target(string_stream.get());
      query->getChart(i)->render(&svg_target);
      auto chart = query->getChart(i);
      chart->setDimensions(width, height);
      chart->render(&svg_target);

      target->beginObject();
      target->addObjectEntry("svg");
+15 −3
Original line number Diff line number Diff line
@@ -65,7 +65,9 @@ public:
      std::shared_ptr<util::InputStream> input_stream,
      kFormat output_format,
      std::shared_ptr<util::OutputStream> output_stream,
      std::unique_ptr<TableRepository> table_repo);
      std::unique_ptr<TableRepository> table_repo,
      int width = -1,
      int height = -1);

  /**
   * Register a query backend
@@ -74,8 +76,18 @@ public:

protected:

  void renderCharts(Query* query, ui::RenderTarget* target) const;
  void renderJSON(Query* query, util::JSONOutputStream* target) const;
  void renderCharts(
      Query* query,
      ui::RenderTarget* target,
      int width,
      int height) const;

  void renderJSON(
      Query* query,
      util::JSONOutputStream* target,
      int width,
      int height) const;

  void renderTables(Query* query, util::OutputStream* out) const;

  DefaultRuntime runtime_;
+10 −0
Original line number Diff line number Diff line
@@ -29,6 +29,16 @@ void Canvas::setSubtitle(const std::string& subtitle) {
  subtitle_ = subtitle;
}

void Canvas::setDimensions(int width, int height) {
  if (width > 0) {
    width_ = width;
  }

  if (height > 0) {
    height_ = height;
  }
}

void Canvas::render(RenderTarget* target) const {
  // FIXPAUL: initialize from rendertarget
  Viewport viewport(width_, height_);
+2 −0
Original line number Diff line number Diff line
@@ -53,6 +53,8 @@ public:
   */
  void setSubtitle(const std::string& subtitle);

  void setDimensions(int width, int height);

  /**
   * FIXPAUL overcomplicated, just accept a ptr
   *
Loading