From 75b15a59b70b34094b6f601a9797204e5c25bf82 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 5 Jan 2020 12:55:10 -0500 Subject: [PATCH] add upload graph to stats page --- assets/css/common/base.scss | 1 + assets/css/views/stats.scss | 7 +++++ .../templates/stat/index.html.slime | 5 ++- lib/philomena_web/views/stat_view.ex | 31 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 assets/css/views/stats.scss diff --git a/assets/css/common/base.scss b/assets/css/common/base.scss index 270ee3f7..5153976b 100644 --- a/assets/css/common/base.scss +++ b/assets/css/common/base.scss @@ -398,6 +398,7 @@ span.stat { @import "views/posts"; @import "views/profiles"; @import "views/search"; +@import "views/stats"; @import "views/tags"; .no-overflow { diff --git a/assets/css/views/stats.scss b/assets/css/views/stats.scss new file mode 100644 index 00000000..3a8b87c9 --- /dev/null +++ b/assets/css/views/stats.scss @@ -0,0 +1,7 @@ +.upload-stats rect { + fill: $sparkline_color; +} + +.upload-stats rect:hover { + fill: red; +} diff --git a/lib/philomena_web/templates/stat/index.html.slime b/lib/philomena_web/templates/stat/index.html.slime index 9033bb4e..e066960c 100644 --- a/lib/philomena_web/templates/stat/index.html.slime +++ b/lib/philomena_web/templates/stat/index.html.slime @@ -115,4 +115,7 @@ elixir: ' On the last 250 reports we've received, it's taken us on average span.stat> = @response_time - ' hour(s) between a report being made and the report being resolved. \ No newline at end of file + ' hour(s) between a report being made and the report being resolved. + + h3 Upload History + = upload_graph(img_bucket["non_deleted"]["all_time"]["buckets"]) diff --git a/lib/philomena_web/views/stat_view.ex b/lib/philomena_web/views/stat_view.ex index 6ae6adc7..51dab742 100644 --- a/lib/philomena_web/views/stat_view.ex +++ b/lib/philomena_web/views/stat_view.ex @@ -1,3 +1,34 @@ defmodule PhilomenaWeb.StatView do use PhilomenaWeb, :view + + def upload_graph(data) do + data = Enum.sort_by(data, & &1["key"]) + n_buckets = length(data) + + {%{"key" => min_time}, %{"key" => max_time}} = Enum.min_max_by(data, & &1["key"], fn -> %{"key" => 0} end) + {%{"doc_count" => min_docs}, %{"doc_count" => max_docs}} = Enum.min_max_by(data, & &1["doc_count"], fn -> %{"doc_count" => 0} end) + + graph_width = 950 + graph_height = 475 + + bar_width = safe_div(graph_width, n_buckets) + max_bar_height = safe_div(graph_height, max_docs - min_docs) + + content_tag :svg, class: "upload-stats", viewBox: "0 0 #{graph_width} #{graph_height}" do + for {datum, i} <- Enum.with_index(data) do + bar_height = safe_div(datum["doc_count"], max_docs) * max_bar_height + + x = i * bar_width + y = graph_height-bar_height + height = bar_height + + content_tag :rect, width: bar_width, height: height, x: x, y: y, fill: "#000" do + content_tag :title, "#{datum["key_as_string"]} - #{datum["doc_count"]} uploads" + end + end + end + end + + defp safe_div(n, 0), do: 0 + defp safe_div(n, d), do: n / d end