Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make all routes "refreshable" #82

Merged
merged 3 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/elm/Data/Token.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Data.Token
exposing
( Token
, toString
, fromString
, encode
)

import Json.Encode


type Token
= Token String


toString : Token -> String
toString (Token token) =
token


fromString : String -> Token
fromString =
Token


encode : Token -> Json.Encode.Value
encode =
Json.Encode.string << toString
3 changes: 2 additions & 1 deletion src/elm/FileUpload/Messages.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module FileUpload.Messages exposing (Msg(..))

import FileUpload.Models exposing (JsonFile)
import Data.Token exposing (Token)


type Msg
Expand All @@ -13,4 +14,4 @@ type Msg
| FileUploadStarted ()
| FileUploadComplete ()
| FileUploadFailed String
| FileUploadSuccess String
| FileUploadSuccess Token
15 changes: 1 addition & 14 deletions src/elm/FileUpload/Models.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module FileUpload.Models exposing (..)

import Errors exposing (Errors)
import Data.Token exposing (Token)


type Loaded
Expand All @@ -11,10 +12,6 @@ type Total
= Total Float


type Token
= Token String


type Bytes
= Bytes Int

Expand Down Expand Up @@ -82,16 +79,6 @@ progressToCompletionPercent progress =
Nothing


uploadToken : Upload -> Maybe String
uploadToken upload =
case upload.progress of
Succeeded (Token token) ->
Just token

_ ->
Nothing


jsonFileToFile : JsonFile -> File
jsonFileToFile { filename, filetype, size } =
File (FileName filename) (fileTypeToType filetype) (Bytes <| round size)
Expand Down
7 changes: 4 additions & 3 deletions src/elm/FileUpload/Update.elm
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module FileUpload.Update exposing (update, subscriptions)

import Routing exposing (Route(Terms), navigateTo)
import FileUpload.Models exposing (Upload, UploadProgress(..), Loaded(..), Total(..), UploadFailure(..), Token(..), jsonFileToFile)
import FileUpload.Models exposing (Upload, UploadProgress(..), Loaded(..), Total(..), UploadFailure(..), jsonFileToFile)
import FileUpload.Messages exposing (Msg(..))
import FileUpload.Ports exposing (..)
import Data.Token as Token


subscriptions : Sub Msg
Expand All @@ -15,7 +16,7 @@ subscriptions =
, fileUploadStarted FileUploadStarted
, fileUploadComplete FileUploadComplete
, fileUploadFailed FileUploadFailed
, fileUploadSuccess FileUploadSuccess
, fileUploadSuccess (FileUploadSuccess << Token.fromString)
]


Expand Down Expand Up @@ -49,7 +50,7 @@ update msg upload =
)

FileUploadSuccess token ->
( { upload | progress = Succeeded (Token token) }
( { upload | progress = Succeeded token }
, navigateTo <| Terms token
)

Expand Down
3 changes: 2 additions & 1 deletion src/elm/FileUpload/View.elm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import Json.Decode as JD
import Material.Progress as Loading
import View.Layout exposing (contentWrapper, buttonStyles)
import I18n exposing (Translation(..))
import FileUpload.Models exposing (Upload, File, Token(..), Bytes(..), UploadProgress(..), progressToCompletionPercent)
import FileUpload.Models exposing (Upload, File, Bytes(..), UploadProgress(..), progressToCompletionPercent)
import FileUpload.Messages exposing (Msg(..))
import Data.Token exposing (Token)


view : Upload -> Html Msg
Expand Down
15 changes: 2 additions & 13 deletions src/elm/Main.elm
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
module Main exposing (..)

import Subscriptions exposing (subscriptions)
import Models exposing (Model, Flags, initModel)
import Models exposing (Model, Flags)
import Messages exposing (Msg(..))
import Update exposing (update)
import Update exposing (init, update)
import View exposing (view)
import Navigation exposing (Location)
import Routing exposing (Route)
import FileUpload.Ports as FUP


init : Flags -> Location -> ( Model, Cmd Msg )
init flags location =
let
currentRoute =
Routing.parseLocation location
in
( initModel flags currentRoute, FUP.isUploadSupported () )


main : Program Flags Model Msg
Expand Down
5 changes: 2 additions & 3 deletions src/elm/Models.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import Terms.Models exposing (Terms, initTerms)
import Target.Models exposing (Target, initTarget)
import Resolver.Models exposing (Resolver, initResolver)
import Errors exposing (Errors)
import Data.Token exposing (Token)


type alias Model =
{ route : Routing.Route
, resolverUrl : String
, localDomain : String
, token : Maybe String
, upload : Upload
, terms : Terms
, target : Target
Expand All @@ -37,7 +37,6 @@ initModel flags route =
Model route
flags.resolverUrl
flags.localDomain
Nothing
initUpload
initTerms
(initTarget flags.dataSourcesIds)
Expand All @@ -47,7 +46,7 @@ initModel flags route =
Material.model


currentToken : Model -> Maybe String
currentToken : Model -> Maybe Token
currentToken { route } =
case route of
FileUpload ->
Expand Down
11 changes: 6 additions & 5 deletions src/elm/Resolver/Api.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,30 @@ import Json.Decode exposing (null)
import Resolver.Messages exposing (Msg(..))
import Resolver.Encoder as RE
import Resolver.Decoder exposing (statsAndErrorsDecoder)
import Data.Token as Token exposing (Token)


queryResolutionProgress : String -> Cmd Msg
queryResolutionProgress : Token -> Cmd Msg
queryResolutionProgress token =
let
url =
"/stats/" ++ token
"/stats/" ++ Token.toString token
in
Http.send ResolutionProgress
(Http.get url statsAndErrorsDecoder)


startResolution : String -> Cmd Msg
startResolution : Token -> Cmd Msg
startResolution token =
let
url =
"/resolver/" ++ token
"/resolver/" ++ Token.toString token
in
Http.send LaunchResolution
(Http.get url (null ()))


sendStopResolution : String -> Cmd Msg
sendStopResolution : Token -> Cmd Msg
sendStopResolution token =
let
url =
Expand Down
13 changes: 13 additions & 0 deletions src/elm/Resolver/Decoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ import TimeDuration.Model exposing (..)

statsAndErrorsDecoder : Decoder ( Stats, Errors )
statsAndErrorsDecoder =
oneOf
[ notStartedDecoder
, existingStatsAndErrorsDecoder
]


existingStatsAndErrorsDecoder : Decoder ( Stats, Errors )
existingStatsAndErrorsDecoder =
map2 (,) statsDecoder errors


notStartedDecoder : Decoder ( Stats, Errors )
notStartedDecoder =
null ( NotStarted, Nothing )


statsDecoder : Decoder Stats
statsDecoder =
field "status" string
Expand Down
5 changes: 3 additions & 2 deletions src/elm/Resolver/Encoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ module Resolver.Encoder exposing (body)

import Json.Encode exposing (..)
import Http
import Data.Token as Token exposing (Token)


body : String -> Http.Body
body : Token -> Http.Body
body token =
Http.jsonBody <|
object
[ ( "token", string token )
[ ( "token", Token.encode token )
, ( "stop_trigger", bool True )
]
6 changes: 6 additions & 0 deletions src/elm/Resolver/Helper.elm
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ ingestionResolverProgress { stats } =
NoStatsReceived ->
Pending

NotStarted ->
Pending

PendingResolution _ ->
Pending

Expand All @@ -162,6 +165,9 @@ resolutionResolverProgress { stats } =
NoStatsReceived ->
Pending

NotStarted ->
Pending

PendingResolution _ ->
Pending

Expand Down
4 changes: 4 additions & 0 deletions src/elm/Resolver/Models.elm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ metadataFromStats stats =
NoStatsReceived ->
Nothing

NotStarted ->
Nothing

PendingResolution m ->
Just m

Expand All @@ -69,6 +72,7 @@ metadataFromStats stats =

type Stats
= Unknown
| NotStarted
| NoStatsReceived
| PendingResolution ProgressMetadata
| Ingesting ProgressMetadata Ingestion
Expand Down
17 changes: 14 additions & 3 deletions src/elm/Resolver/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Resolver.Update exposing (subscriptions, update)
import Time exposing (millisecond)
import Resolver.Models exposing (..)
import Resolver.Messages exposing (Msg(..))
import Resolver.Api exposing (queryResolutionProgress, sendStopResolution)
import Resolver.Api exposing (queryResolutionProgress, startResolution, sendStopResolution)
import Data.Token exposing (Token)


subscriptions : Resolver -> Sub Msg
Expand All @@ -16,7 +17,7 @@ subscriptions { stats } =
Time.every (millisecond * 1000) QueryResolutionProgress


update : Msg -> Resolver -> String -> ( Resolver, Cmd Msg )
update : Msg -> Resolver -> Token -> ( Resolver, Cmd Msg )
update msg resolver token =
case msg of
LaunchResolution (Ok _) ->
Expand All @@ -33,7 +34,7 @@ update msg resolver token =
| stats = stats
, errors = errors
}
, Cmd.none
, resolutionProgressCmd token stats
)

ResolutionProgress (Err _) ->
Expand All @@ -50,3 +51,13 @@ update msg resolver token =

EmptyErrors ->
( { resolver | errors = Nothing }, Cmd.none )


resolutionProgressCmd : Token -> Stats -> Cmd Msg
resolutionProgressCmd token stats =
case stats of
NotStarted ->
startResolution token

_ ->
Cmd.none
19 changes: 8 additions & 11 deletions src/elm/Routing.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module Routing exposing (Token, Route(..), navigateTo, parseLocation)
module Routing exposing (Route(..), navigateTo, parseLocation)

import Navigation exposing (Location, newUrl)
import UrlParser exposing (..)


type alias Token =
String
import Data.Token as Token exposing (Token)


type Route
Expand All @@ -28,13 +25,13 @@ urlFor r =
"/"

Terms token ->
"/#terms/" ++ token
"/#terms/" ++ Token.toString token

Target token ->
"/#target/" ++ token
"/#target/" ++ Token.toString token

Resolver token ->
"/#resolver/" ++ token
"/#resolver/" ++ Token.toString token

NotFoundRoute ->
"/#404"
Expand All @@ -54,7 +51,7 @@ matchers : Parser (Route -> a) a
matchers =
oneOf
[ map FileUpload top
, map Terms (s "terms" </> string)
, map Target (s "target" </> string)
, map Resolver (s "resolver" </> string)
, map (Terms << Token.fromString) (s "terms" </> string)
, map (Target << Token.fromString) (s "target" </> string)
, map (Resolver << Token.fromString) (s "resolver" </> string)
]
5 changes: 3 additions & 2 deletions src/elm/Target/Encoder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ module Target.Encoder exposing (body)

import Json.Encode exposing (..)
import Http
import Data.Token as Token exposing (Token)


body : String -> Int -> Http.Body
body : Token -> Int -> Http.Body
body token targetId =
Http.jsonBody <|
object
[ ( "token", string token )
[ ( "token", Token.encode token )
, ( "data_source_id", int targetId )
]
Loading