Skip to content

Commit

Permalink
Added Text Fields to Data Set Views
Browse files Browse the repository at this point in the history
I added text fields to the data set views to enable aggregation around
text values in charts. I also added a new index for each text field so
that we can perform full text search later. For now, we're doing nothing
with it, but later we can start using it as the indexes will be there
and populated moving forward.

This will require another migration to enable the Postgres Trigram
extension. We will also have to do manual migrations to rebuild the
views:

```elixir
ids =
  MetaActions.list(ready_only: true)
  |> Enum.map(& &1.id)

ids
|> Enum.map(&MetaActions.get/1)
|> Enum.each(&DataSetActions.down!/1)

ids
|> Enum.map(&MetaActions.get/1)
|> Enum.each(&DataSetActions.up!/1)

ids
|> Enum.map(&MetaActions.get/1)
|> Enum.each(&PlenarioEtl.import_data_set_on_demand/1)
```

Updates #264
  • Loading branch information
Vince Forgione committed Jul 11, 2018
1 parent 9630494 commit 897d5a7
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 13 deletions.
15 changes: 13 additions & 2 deletions lib/plenario/actions/data_set_actions.ex
Expand Up @@ -17,6 +17,7 @@ defmodule Plenario.Actions.DataSetActions do
@create_table "db-actions/up/create-table.sql.eex"
@create_view "db-actions/up/create-view.sql.eex"
@create_index "db-actions/up/create-index.sql.eex"
@create_trgm_index "db-actions/up/create-trgm-index.sql.eex"

def up!(%Meta{id: meta_id}), do: up!(meta_id)
def up!(meta_id) do
Expand All @@ -28,12 +29,13 @@ defmodule Plenario.Actions.DataSetActions do
table_name = meta.table_name
view_name = "#{table_name}_view"

text_fields = Enum.filter(fields, & &1.type == "text") |> Enum.map(& &1.name)
boolean_fields = Enum.filter(fields, & &1.type == "boolean") |> Enum.map(& &1.name)
integer_fields = Enum.filter(fields, & &1.type == "integer") |> Enum.map(& &1.name)
float_fields = Enum.filter(fields, & &1.type == "float") |> Enum.map(& &1.name)
timestamp_fields = Enum.filter(fields, & &1.type == "timestamp") |> Enum.map(& &1.name)

index_fields =
gin_index_fields =
boolean_fields ++
integer_fields ++
float_fields ++
Expand All @@ -42,6 +44,8 @@ defmodule Plenario.Actions.DataSetActions do

gist_index_fields = Enum.map(virtual_points, & &1.name)

tsvector_index_fields = text_fields

Repo.transaction fn ->
execute! @create_table,
table_name: table_name,
Expand All @@ -50,14 +54,15 @@ defmodule Plenario.Actions.DataSetActions do
execute! @create_view,
table_name: table_name,
view_name: view_name,
text_fields: text_fields,
boolean_fields: boolean_fields,
integer_fields: integer_fields,
float_fields: float_fields,
timestamp_fields: timestamp_fields,
virtual_dates: virtual_dates,
virtual_points: virtual_points

Enum.each(index_fields, fn field ->
Enum.each(gin_index_fields, fn field ->
execute! @create_index,
view_name: view_name,
field: field,
Expand All @@ -70,6 +75,12 @@ defmodule Plenario.Actions.DataSetActions do
field: field,
using: "GIST"
end)

Enum.each(tsvector_index_fields, fn field ->
execute! @create_trgm_index,
view_name: view_name,
field: field
end)
end

:ok
Expand Down
1 change: 0 additions & 1 deletion lib/plenario/model_registry.ex
Expand Up @@ -106,7 +106,6 @@ defmodule Plenario.ModelRegistry do
view = "#{meta.table_name}_view"
fields =
meta.fields
|> Enum.filter(& Enum.member?(["boolean", "integer", "float", "timestamp"], &1.type))
|> Enum.map(& {String.to_atom(&1.name), Map.fetch!(@type_map, &1.type)})

vpfs =
Expand Down
4 changes: 2 additions & 2 deletions lib/plenario/schemas/chart.ex
Expand Up @@ -19,15 +19,15 @@ defmodule Plenario.Schemas.Chart do
has_many :datasets, Plenario.Schemas.ChartDataset
end

@types ["line", "bar", "pie", "doughnut", "radar", "polar", "location", "heatmap"]
@types ["line", "bar", "pie", "doughnut", "radar", "polarArea", "location", "heatmap"]

@type_choices [
Line: "line",
Bar: "bar",
Pie: "pie",
Doughnut: "doughnut",
Radar: "radar",
Polar: "polar",
Polar: "polarArea",
"Locations on Map": "location",
Heatmap: "heatmap"
]
Expand Down
41 changes: 33 additions & 8 deletions lib/plenario_web/controllers/web/chart_controller.ex
Expand Up @@ -116,7 +116,7 @@ defmodule PlenarioWeb.Web.ChartController do

conn
|> put_flash(:success, "Deleted chart #{inspect(chart.title)}")
|> redirect(to: meta_path(conn, :show, meta_id))
|> redirect(to: data_set_path(conn, :show, meta_id))
end

def render_chart(conn, %{"id" => chart_id}) do
Expand Down Expand Up @@ -160,13 +160,38 @@ defmodule PlenarioWeb.Web.ChartController do
groups
|> Enum.map(& Keyword.get(data, &1, 0))

%{
label: d.label,
data: selected_data,
borderColor: "rgba(#{d.color},1)",
backgroundColor: "rgba(#{d.color},0.2)",
fill: d.fill?
}
obj =
%{
label: d.label,
data: selected_data
}
obj =
case chart.type == "polarArea" do
false ->
Map.merge(obj, %{
borderColor: "rgba(#{d.color},1)",
backgroundColor: "rgba(#{d.color},0.2)",
fill: d.fill?
})

true ->
borders =
ChartDataset.get_colors()
|> Enum.map(& "rgba(#{&1},1)")
|> Stream.cycle()
|> Enum.take(length(groups))
backgrounds =
ChartDataset.get_colors()
|> Enum.map(& "rgba(#{&1},0.2)")
|> Stream.cycle()
|> Enum.take(length(groups))
Map.merge(obj, %{
borderColor: borders,
backgroundColor: backgrounds
})
end

obj
end)

# make chart object
Expand Down
6 changes: 6 additions & 0 deletions lib/plenario_web/templates/web/chart/show.html.eex
Expand Up @@ -13,6 +13,12 @@
</div>
</div>

<div class="row" style="margin-bottom:50px">
<div class="col-lg-12">
<%= link "Delete This Chart", to: chart_path(@conn, :delete, @meta.id, @chart.id), method: :delete, class: "btn btn-danger float-right" %>
</div>
</div>

<div class="row">
<div class="col-lg-6">
<h3>
Expand Down
3 changes: 3 additions & 0 deletions priv/db-actions/up/create-trgm-index.sql.eex
@@ -0,0 +1,3 @@
CREATE INDEX "<%= view_name %>_<%= field %>_tsvector"
ON "<%= view_name %>"
USING GIN ( to_tsvector('english', "<%= field %>") )
5 changes: 5 additions & 0 deletions priv/db-actions/up/create-view.sql.eex
Expand Up @@ -2,6 +2,11 @@ CREATE MATERIALIZED VIEW "<%= view_name %>" AS
SELECT
row_id

-- text fields
<%= for f <- text_fields do %>
, "<%= f %>"
<% end %>

-- boolean fields
<%= for f <- boolean_fields do %>
, p_boolean_from_text("<%= f %>") AS "<%= f %>"
Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/000020_enable_pgtrgm.exs
@@ -0,0 +1,9 @@
defmodule Plenario.Repo.Migrations.EnablePgTrgm do
use Ecto.Migration

def up do
execute """
CREATE EXTENSION IF NOT EXISTS pg_trgm;
"""
end
end

0 comments on commit 897d5a7

Please sign in to comment.