Commit a2ed5509 authored by André Espaze's avatar André Espaze
Browse files

Series selection with Elm for Plot.ly rendering

parent dbc965e8082e
all: delete rename
all: delete rename plot
delete:
elm make elm/Delete.elm --output tsview/tsview_static/delete_elm.js
......@@ -6,6 +6,9 @@ delete:
rename:
elm make elm/Rename.elm --output tsview/tsview_static/rename_elm.js
plot:
elm make elm/Plot.elm --output tsview/tsview_static/plot_elm.js
clean: cleanstuff cleanbuild
cleanstuff:
......@@ -14,3 +17,4 @@ cleanstuff:
cleanbuild:
rm tsview/tsview_static/delete_elm.js -f
rm tsview/tsview_static/rename_elm.js -f
rm tsview/tsview_static/plot_elm.js -f
port module Plot exposing (main)
import Browser
import Common exposing (classes)
import Dict
import Html.Styled exposing (..)
import Html.Styled.Events exposing (onClick)
import Http
import Json.Decode as Decode
import KeywordMultiSelector
import KeywordSelector
import Tachyons.Classes as T
import Time
import Url
import Url.Builder as UB
type alias Model =
{ urlPrefix : String
, series : List String
, searchString : String
, searchedSeries : List String
, selectedSeries : List String
, activeSelection : Bool
}
type alias SeriesCatalog =
Dict.Dict String String
type Msg
= CatalogReceived (Result Http.Error SeriesCatalog)
| ToggleSelection
| ToggleItem String
| SearchSeries String
| MakeSearch
| OnApply
| GotPlot (Result Http.Error String)
type alias RenderArgs =
{ plotlyResponse : String
, selectedSeries : List String
, permalinkQuery : String
}
port renderPlotly : RenderArgs -> Cmd msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
let
removeItem x xs =
List.filter ((/=) x) xs
toggleItem x xs =
if List.member x xs then
removeItem x xs
else
x :: xs
newModel x =
( x, Cmd.none )
keywordMatch xm xs =
if String.length xm < 2 then
[]
else
KeywordSelector.select xm xs |> List.take 20
plotUrl =
UB.crossOrigin model.urlPrefix
[ "tsplot" ]
(List.map (\x -> UB.string "series" x) model.selectedSeries)
in
case msg of
CatalogReceived (Ok x) ->
let
series =
Dict.keys x
in
newModel { model | series = series }
CatalogReceived (Err x) ->
let
_ =
Debug.log "Error on CatalogReceived" x
in
newModel model
ToggleSelection ->
newModel { model | activeSelection = not model.activeSelection }
ToggleItem x ->
newModel { model | selectedSeries = toggleItem x model.selectedSeries }
SearchSeries x ->
newModel { model | searchString = x }
MakeSearch ->
newModel { model | searchedSeries = keywordMatch model.searchString model.series }
OnApply ->
( model, Http.get { url = plotUrl, expect = Http.expectString GotPlot } )
GotPlot (Ok x) ->
let
validUrl =
Common.maybe
("http://dummy" ++ plotUrl)
(always plotUrl)
(Url.fromString plotUrl)
q =
validUrl
|> Url.fromString
|> Maybe.map (.query >> Maybe.withDefault "")
|> Maybe.withDefault ""
in
( model, renderPlotly <| RenderArgs x model.selectedSeries q )
GotPlot (Err x) ->
let
_ =
Debug.log "Error on GotPlot" x
in
newModel model
selectorConfig : KeywordMultiSelector.Config Msg
selectorConfig =
{ searchSelector =
{ action = Nothing
, defaultText =
text
"Type some keywords in input bar for selecting time series"
, toggleMsg = ToggleItem
}
, actionSelector =
{ action =
Just
{ attrs = [ classes [ T.white, T.bg_dark_blue ] ]
, html = text "Apply"
, clickMsg = OnApply
}
, defaultText = text ""
, toggleMsg = ToggleItem
}
, onInputMsg = SearchSeries
, divAttrs = [ classes [ T.mb4 ] ]
}
view : Model -> Html Msg
view model =
let
cls =
classes [ T.pb2, T.f4, T.fw6, T.db, T.navy, T.link, T.dim ]
children =
[ a [ cls, onClick ToggleSelection ] [ text "Series selection" ] ]
ctx =
KeywordMultiSelector.Context model.searchedSeries model.selectedSeries
in
div [ classes [ T.center, T.pt4, T.w_90 ] ]
(if model.activeSelection then
List.append children
[ KeywordMultiSelector.view selectorConfig ctx
]
else
children
)
main : Program String Model Msg
main =
let
initialGet urlPrefix =
Http.get
{ expect = Http.expectJson CatalogReceived (Decode.dict Decode.string)
, url =
UB.crossOrigin urlPrefix
[ "api", "series", "catalog" ]
[]
}
init urlPrefix =
( Model urlPrefix [] "" [] [] True, initialGet urlPrefix )
sub model =
if model.activeSelection then
Time.every 1000 (always MakeSearch)
else
Sub.none
in
Browser.element
{ init = init
, view = view >> toUnstyled
, update = update
, subscriptions = sub
}
......@@ -18,7 +18,7 @@ class ElmBuild(build_ext):
def run(self):
wdir = Path(__file__).resolve().parent
for edit_kind in ["Delete", "Rename"]:
for edit_kind in ["Delete", "Rename", "Plot"]:
compile_elm(wdir, edit_kind)
super().run()
......
......@@ -3,6 +3,7 @@
<head>
<title>Time Series View</title>
<link rel=stylesheet href=https://unpkg.com/tachyons@4.10.0/css/tachyons.min.css/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
......
......@@ -2,7 +2,27 @@
{% block body %}
{% include 'tsviewform.html' %}
<form id="tsviewform" class="form-inline">
<div id="series_selector"></div>
<script src="./tsview_static/plot_elm.js"></script>
<script>
var app = Elm.Plot.init({
node: document.getElementById("series_selector"),
flags: ""
});
app.ports.renderPlotly.subscribe(function(args) {
let $target = $('#output')
$target.html(args.plotlyResponse)
$target.append(`<a href="tsview?${args.permalinkQuery}">Permalink</a>`)
args.selectedSeries.forEach(function(name){
$target.append(`<br/> <a href="tshistory/${name}" target=_blank>View ${name} history</a>`)
});
const table = $('.dataframe').DataTable()
table.page.len(20);
table.draw();
});
</script>
</form>
<div id="output" style="margin-top: 1em; padding-bottom:2em;">
No data yet.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment