From 16801c08208007a8f363be071fd2c25014bd9bb5 Mon Sep 17 00:00:00 2001 From: Vinicius Brasil Date: Thu, 23 Feb 2023 13:08:45 -0300 Subject: [PATCH] Fix Credo warnings --- .../controllers/api/stats_controller.ex | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 48eb2cd91b6b..a79e68266c84 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -114,23 +114,7 @@ defmodule PlausibleWeb.Api.StatsController do present_index = present_index_for(site, query, labels) full_intervals = build_full_intervals(query, labels) - comparison_result = - case params["comparison"] do - "previous_period" -> - comparison_query = Query.shift_back(query, site) - Stats.timeseries(site, comparison_query, [selected_metric]) - - "last_year" -> - start_date = Date.add(query.date_range.first, -365) - end_date = Date.add(query.date_range.last, -365) - range = Date.range(start_date, end_date) - - comparison_query = %Query{query | date_range: range} - Stats.timeseries(site, comparison_query, [selected_metric]) - - _any -> - nil - end + comparison_result = query_comparison(params["comparison"], site, query, selected_metric) json(conn, %{ plot: plot_timeseries(timeseries_result, selected_metric), @@ -182,6 +166,25 @@ defmodule PlausibleWeb.Api.StatsController do nil end + defp query_comparison(comparison_mode, site, query, selected_metric) do + case comparison_mode do + "previous_period" -> + comparison_query = Query.shift_back(query, site) + Stats.timeseries(site, comparison_query, [selected_metric]) + + "last_year" -> + start_date = Date.add(query.date_range.first, -365) + end_date = Date.add(query.date_range.last, -365) + range = Date.range(start_date, end_date) + + comparison_query = %Query{query | date_range: range} + Stats.timeseries(site, comparison_query, [selected_metric]) + + _any -> + nil + end + end + def top_stats(conn, params) do site = conn.assigns[:site]