Skip to content

Commit

Permalink
Remove control diagrams from analytic dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
altjohndev committed Dec 8, 2020
1 parent 95fecdf commit b03093b
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 200 deletions.
9 changes: 7 additions & 2 deletions lib/health_board_web/live/components/card/card_header_menu.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule HealthBoardWeb.LiveComponents.CardHeaderMenu do
use Surface.Component, slot: "header"

alias HealthBoardWeb.Router.Helpers, as: Routes
alias Phoenix.LiveView

prop card_id, :atom, required: true
Expand All @@ -17,14 +18,14 @@ defmodule HealthBoardWeb.LiveComponents.CardHeaderMenu do
@spec render(map()) :: LiveView.Rendered.t()
def render(assigns) do
~H"""
<div id={{ @card_id }} class={{ "uk-card-header", "uk-visible-toggle", "show-when-not-hover-container", "uk-transition-toggle", "hb-border": @border_color, "hb-border-bottom": @border_color, "hb-border-#{@border_color}": @border_color }}>
<div id={{ "to_#{@card_id}" }} class={{ "uk-card-header", "uk-visible-toggle", "show-when-not-hover-container", "uk-transition-toggle", "hb-border": @border_color, "hb-border-bottom": @border_color, "hb-border-#{@border_color}": @border_color }}>
<h3 class={{"uk-card-title", "show-when-not-hover"}}>
{{ @card.name }}
</h3>
<div class={{ "uk-hidden-hover", "uk-transition-slide-top", "uk-flex", "uk-flex-middle", "uk-flex-between", "hb-card-menu"}}>
<div :if={{ @card[:link] }}>
<a href={{ @card.link }} uk-tooltip="Ver painel">
<a href={{ dashboard_path(@socket, @card.link, @card.filters) }} uk-tooltip="Ver painel">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="black" width="24px" height="24px"><path d="M0 0h24v24H0z" fill="none"/><path d="M19 4H5c-1.11 0-2 .9-2 2v12c0 1.1.89 2 2 2h4v-2H5V8h14v10h-4v2h4c1.1 0 2-.9 2-2V6c0-1.1-.89-2-2-2zm-7 6l-4 4h3v6h2v-6h3l-4-4z"/></svg>
</a>
</div>
Expand Down Expand Up @@ -62,4 +63,8 @@ defmodule HealthBoardWeb.LiveComponents.CardHeaderMenu do
</div>
"""
end

defp dashboard_path(socket, dashboard_id, filters) do
Routes.dashboard_path(socket, :index, dashboard_id, Enum.to_list(filters))
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ defmodule HealthBoardWeb.DashboardLive.DashboardData do
@spec assign(map) :: map
def assign(%{dashboard: dashboard, data: data, filters: filters, root_pid: root_pid}) do
dashboard.sections
|> Task.async_stream(&fetch_section_data(&1, data, filters, root_pid), timeout: 60_000)
|> Enum.reduce(%{}, fn {:ok, {k, v}}, map -> Map.put(map, k, v) end)
|> Enum.map(&fetch_section_data(&1, data, filters, root_pid))
|> Enum.into(%{})
rescue
_error -> []
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ defmodule HealthBoardWeb.DashboardLive.DashboardData.Analytic do
alias HealthBoard.Contexts
alias HealthBoard.Contexts.Demographic.YearlyPopulations
alias HealthBoard.Contexts.Info.DataPeriods
alias HealthBoard.Contexts.Morbidities.{WeeklyMorbidities, YearlyMorbidities}
alias HealthBoard.Contexts.Mortalities.{WeeklyDeaths, YearlyDeaths}
alias HealthBoard.Contexts.Morbidities.YearlyMorbidities
alias HealthBoard.Contexts.Mortalities.YearlyDeaths
alias HealthBoardWeb.DashboardLive.CommonData

@spec fetch(map()) :: map()
def fetch(%{data: data, filters: filters} = dashboard_data) do
filters = fetch_default_filters(filters)
data = fetch_location_data(data, filters)
{data, filters} = fetch_location_data(data, fetch_default_filters(filters))

data
|> async_stream(filters)
|> Enum.reduce(data, fn {:ok, {key, value}}, data -> Map.put(data, key, value) end)
|> fetch_yearly_deaths(filters)
|> fetch_yearly_morbidities(filters)
|> fetch_yearly_populations(filters)
|> fetch_locations_year_deaths(filters)
|> fetch_locations_year_morbidities(filters)
|> fetch_locations_year_populations(filters)
|> fetch_data_periods()
|> update(dashboard_data)
|> Map.put(:filters, filters)
end
Expand All @@ -28,9 +32,14 @@ defmodule HealthBoardWeb.DashboardLive.DashboardData.Analytic do
end

defp fetch_location_data(data, filters) do
data
|> Map.put(:location, CommonData.location(filters))
|> fetch_locations()
location = CommonData.location(filters)

data =
data
|> Map.put(:location, CommonData.location(filters))
|> fetch_locations()

{data, Map.put(filters, :location, location.name)}
end

defp fetch_locations(%{location: location} = data) do
Expand All @@ -44,102 +53,66 @@ defmodule HealthBoardWeb.DashboardLive.DashboardData.Analytic do
|> Map.put(:locations_ids, Enum.map(locations, & &1.id))
end

defp async_stream(data, filters) do
[
fn -> {:yearly_deaths, fetch_yearly_deaths(data, filters)} end,
fn -> {:yearly_morbidities, fetch_yearly_morbidities(data, filters)} end,
fn -> {:yearly_populations, fetch_yearly_populations(data, filters)} end,
fn -> {:locations_year_deaths, fetch_locations_year_deaths(data, filters)} end,
fn -> {:locations_year_morbidities, fetch_locations_year_morbidities(data, filters)} end,
fn -> {:locations_year_populations, fetch_locations_year_populations(data, filters)} end,
fn -> {:weekly_deaths, fetch_weekly_deaths(data, filters)} end,
fn -> {:weekly_morbidities, fetch_weekly_morbidities(data, filters)} end,
fn -> {:data_periods, fetch_data_periods(data)} end
]
|> Task.async_stream(& &1.(), timeout: 10_000)
end

defp fetch_yearly_deaths(%{location: %{id: location_id}}, filters) do
defp fetch_yearly_deaths(%{location: %{id: location_id}} = data, filters) do
%{from_year: from_year, to_year: to_year} = filters

[location_id: location_id, from_year: from_year, to_year: to_year]
|> YearlyDeaths.list_by()
|> Enum.map(&Map.take(&1, [:context, :location_id, :year, :total]))
|> update(:yearly_deaths, data)
end

defp fetch_yearly_morbidities(%{location: %{id: location_id}}, filters) do
defp fetch_yearly_morbidities(%{location: %{id: location_id}} = data, filters) do
%{from_year: from_year, to_year: to_year} = filters

[location_id: location_id, from_year: from_year, to_year: to_year]
|> YearlyMorbidities.list_by()
|> Enum.map(&Map.take(&1, [:context, :location_id, :year, :total]))
|> update(:yearly_morbidities, data)
end

defp fetch_yearly_populations(%{location: %{id: location_id}}, filters) do
defp fetch_yearly_populations(%{location: %{id: location_id}} = data, filters) do
%{from_year: from_year, to_year: to_year} = filters

[location_id: location_id, from_year: from_year, to_year: to_year]
|> YearlyPopulations.list_by()
|> Enum.map(&Map.take(&1, [:location_id, :year, :total]))
|> update(:yearly_populations, data)
end

defp fetch_locations_year_deaths(%{locations_ids: locations_ids}, filters) do
defp fetch_locations_year_deaths(%{locations_ids: locations_ids} = data, filters) do
%{year: year} = filters

[year: year, locations_ids: locations_ids]
|> YearlyDeaths.list_by()
|> Enum.map(&Map.take(&1, [:context, :location_id, :year, :total]))
|> update(:locations_year_deaths, data)
end

defp fetch_locations_year_morbidities(%{locations_ids: locations_ids}, filters) do
defp fetch_locations_year_morbidities(%{locations_ids: locations_ids} = data, filters) do
%{year: year} = filters

[year: year, locations_ids: locations_ids]
|> YearlyMorbidities.list_by()
|> Enum.map(&Map.take(&1, [:context, :location_id, :year, :total]))
|> update(:locations_year_morbidities, data)
end

defp fetch_locations_year_populations(%{locations_ids: locations_ids}, filters) do
defp fetch_locations_year_populations(%{locations_ids: locations_ids} = data, filters) do
%{year: year} = filters

[year: year, locations_ids: locations_ids]
|> YearlyPopulations.list_by()
|> Enum.map(&Map.take(&1, [:location_id, :year, :total]))
|> update(:locations_year_populations, data)
end

defp fetch_weekly_deaths(%{location: %{id: location_id}}, filters) do
%{from_year: from_year, to_year: to_year} = filters

[
location_id: location_id,
from_year: from_year,
to_year: to_year,
order_by: [asc: :context, asc: :year, asc: :week]
]
|> WeeklyDeaths.list_by()
|> Enum.map(&Map.take(&1, [:context, :location_id, :year, :week, :total]))
|> Enum.group_by(& &1.context)
end

defp fetch_weekly_morbidities(%{location: %{id: location_id}}, filters) do
%{from_year: from_year, to_year: to_year} = filters

[
location_id: location_id,
from_year: from_year,
to_year: to_year,
order_by: [asc: :context, asc: :year, asc: :week]
]
|> WeeklyMorbidities.list_by()
|> Enum.map(&Map.take(&1, [:context, :location_id, :year, :week, :total]))
|> Enum.group_by(& &1.context)
end

defp fetch_data_periods(%{location: %{id: location_id}}) do
defp fetch_data_periods(%{location: %{id: location_id}} = data) do
[location_id: location_id, data_contexts: [Contexts.data_context!(:morbidity), Contexts.data_context!(:deaths)]]
|> DataPeriods.list_by()
|> Enum.group_by(& &1.data_context)
|> group_data_periods_by_context()
|> update(:data_periods, data)
end

defp group_data_periods_by_context(data_periods_per_data_context) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,176 @@
defmodule HealthBoardWeb.DashboardLive.DashboardData.Morbidity do
alias HealthBoard.Contexts
alias HealthBoard.Contexts.Demographic.YearlyPopulations
alias HealthBoard.Contexts.Info.DataPeriods
alias HealthBoard.Contexts.Morbidities.{WeeklyMorbidities, YearlyMorbidities}
alias HealthBoard.Contexts.Mortalities.{WeeklyDeaths, YearlyDeaths}
alias HealthBoardWeb.DashboardLive.CommonData

@spec fetch(map()) :: map()
def fetch(dashboard_data) do
dashboard_data
def fetch(%{data: data, filters: filters} = dashboard_data) do
{data, filters} = fetch_location_data(data, fetch_default_filters(filters))

data
|> fetch_yearly_deaths(filters)
|> fetch_yearly_morbidities(filters)
|> fetch_yearly_population(filters)
|> fetch_locations_year_deaths(filters)
|> fetch_locations_year_morbidities(filters)
|> fetch_locations_year_populations(filters)
|> fetch_weekly_deaths(filters)
|> fetch_weekly_morbidities(filters)
|> fetch_data_periods(filters)
|> update(dashboard_data)
|> Map.put(:filters, filters)
end

defp fetch_default_filters(filters) do
current_year = Date.utc_today().year

filters
|> Map.put(:year, current_year)
|> Map.put(:to_year, current_year)
|> Map.put(:from_year, 2000)
|> Map.put_new(:morbidity_context, 100_000)
end

defp fetch_location_data(data, filters) do
location = CommonData.location(filters)

data =
data
|> Map.put(:location, CommonData.location(filters))
|> fetch_locations()

{data, Map.put(filters, :location, location.name)}
end

defp fetch_locations(%{location: location} = data) do
locations =
location
|> CommonData.locations()
|> Enum.sort(&(&1.name <= &2.name))

data
|> Map.put(:locations, locations)
|> Map.put(:locations_ids, Enum.map(locations, & &1.id))
end

defp fetch_yearly_deaths(%{location: %{id: location_id}} = data, filters) do
%{from_year: from_year, to_year: to_year, morbidity_context: context} = filters

yearly_deaths =
[location_id: location_id, from_year: from_year, to_year: to_year, context: context]
|> YearlyDeaths.list_by()
|> Enum.map(&Map.take(&1, [:year, :total]))

%{total: year_deaths} = Enum.find(yearly_deaths, %{total: 0}, &(&1.year == to_year))

data
|> Map.put(:yearly_deaths, yearly_deaths)
|> Map.put(:year_deaths, year_deaths)
end

defp fetch_yearly_morbidities(%{location: %{id: location_id}} = data, filters) do
%{from_year: from_year, to_year: to_year, morbidity_context: context} = filters

yearly_morbidities =
[location_id: location_id, from_year: from_year, to_year: to_year, context: context]
|> YearlyMorbidities.list_by()
|> Enum.map(&Map.take(&1, [:year, :total]))

%{total: year_morbidity} = Enum.find(yearly_morbidities, %{total: 0}, &(&1.year == to_year))

data
|> Map.put(:yearly_morbidities, yearly_morbidities)
|> Map.put(:year_morbidity, year_morbidity)
end

defp fetch_yearly_population(%{location: %{id: location_id}} = data, filters) do
%{from_year: from_year, to_year: to_year} = filters

yearly_population =
[location_id: location_id, from_year: from_year, to_year: to_year]
|> YearlyPopulations.list_by()
|> Enum.map(&Map.take(&1, [:year, :total]))

%{total: year_population} = Enum.find(yearly_population, %{total: 0}, &(&1.year == to_year))

data
|> Map.put(:yearly_population, yearly_population)
|> Map.put(:year_population, year_population)
end

defp fetch_locations_year_deaths(%{locations_ids: locations_ids} = data, filters) do
%{year: year, morbidity_context: context} = filters

[year: year, locations_ids: locations_ids, context: context]
|> YearlyDeaths.list_by()
|> Enum.map(&Map.take(&1, [:location_id, :total]))
|> update(:locations_year_deaths, data)
end

defp fetch_locations_year_morbidities(%{locations_ids: locations_ids} = data, filters) do
%{year: year, morbidity_context: context} = filters

[year: year, locations_ids: locations_ids, context: context]
|> YearlyMorbidities.list_by()
|> Enum.map(&Map.take(&1, [:location_id, :total]))
|> update(:locations_year_morbidities, data)
end

defp fetch_locations_year_populations(%{locations_ids: locations_ids} = data, filters) do
%{year: year, morbidity_context: context} = filters

[year: year, locations_ids: locations_ids, context: context]
|> YearlyPopulations.list_by()
|> Enum.map(&Map.take(&1, [:location_id, :total]))
|> update(:locations_year_populations, data)
end

defp fetch_weekly_deaths(%{location: %{id: location_id}} = data, filters) do
%{from_year: from_year, to_year: to_year, morbidity_context: context} = filters

[
location_id: location_id,
from_year: from_year,
to_year: to_year,
context: context,
order_by: [asc: :context, asc: :year, asc: :week]
]
|> WeeklyDeaths.list_by()
|> Enum.map(&Map.take(&1, [:year, :week, :total]))
|> update(:weekly_deaths, data)
end

defp fetch_weekly_morbidities(%{location: %{id: location_id}} = data, filters) do
%{from_year: from_year, to_year: to_year, morbidity_context: context} = filters

[
location_id: location_id,
from_year: from_year,
to_year: to_year,
context: context,
order_by: [asc: :context, asc: :year, asc: :week]
]
|> WeeklyMorbidities.list_by()
|> Enum.map(&Map.take(&1, [:year, :week, :total]))
|> update(:weekly_morbidities, data)
end

defp fetch_data_periods(%{location: %{id: location_id}} = data, filters) do
%{morbidity_context: context} = filters

[
location_id: location_id,
data_contexts: [Contexts.data_context!(:morbidity), Contexts.data_context!(:deaths)],
context: context
]
|> DataPeriods.list_by()
|> update(:data_periods, data)
end

defp update(data, key \\ :data, dashboard_data) do
Map.put(dashboard_data, key, data)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ defmodule HealthBoardWeb.DashboardLive.SectionData do
@spec assign(map) :: map
def assign(%{section: %{cards: section_cards}, data: data, filters: filters, root_pid: root_pid}) do
section_cards
|> Task.async_stream(&fetch_card_data(&1, data, filters, root_pid), timeout: 60_000)
|> Enum.reduce(%{}, fn {:ok, {k, v}}, map -> Map.put(map, k, v) end)
|> Enum.map(&fetch_card_data(&1, data, filters, root_pid))
|> Enum.into(%{})
rescue
_error -> []
end
Expand Down
Loading

0 comments on commit b03093b

Please sign in to comment.