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

Let the browser remember the creator of a silence #1112

Merged
merged 6 commits into from
Dec 8, 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
7 changes: 6 additions & 1 deletion ui/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
}
</script>
<script src="script.js"></script>
<script>Elm.Main.embed(document.body, { production: true })</script>
<script>
var app = Elm.Main.embed(document.body, { production: true, defaultCreator: window.localStorage.getItem('defaultCreator') });
app.ports.persistDefaultCreator.subscribe(function(name) {
window.localStorage.setItem('defaultCreator', name);
});
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions ui/app/src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ init flags location =
|> Json.decodeValue (Json.field "production" Json.bool)
|> Result.withDefault False

defaultCreator =
flags
|> Json.decodeValue (Json.field "defaultCreator" Json.string)
|> Result.withDefault ""

apiUrl =
if prod then
Api.makeApiUrl location.pathname
Expand All @@ -88,6 +93,7 @@ init flags location =
libUrl
Loading
Loading
defaultCreator
)


Expand Down
2 changes: 2 additions & 0 deletions ui/app/src/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type alias Model =
, libUrl : String
, bootstrapCSS : ApiData String
, fontAwesomeCSS : ApiData String
, defaultCreator : String
}


Expand All @@ -45,6 +46,7 @@ type Msg
| UpdateFilter String
| BootstrapCSSLoaded (ApiData String)
| FontAwesomeCSSLoaded (ApiData String)
| SetDefaultCreator String


type Route
Expand Down
11 changes: 7 additions & 4 deletions ui/app/src/Updates.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ update msg ({ basePath, apiUrl } as model) =
List.map (\( k, v ) -> Matcher False k v) labels

( silenceForm, cmd ) =
Views.SilenceForm.Updates.update (NewSilenceFromMatchers matchers) model.silenceForm basePath apiUrl
Views.SilenceForm.Updates.update (NewSilenceFromMatchers model.defaultCreator matchers) model.silenceForm basePath apiUrl
in
( { model | silenceForm = silenceForm }, Cmd.map MsgForSilenceForm cmd )
( { model | silenceForm = silenceForm }, cmd )

NavigateToAlerts filter ->
let
Expand Down Expand Up @@ -68,7 +68,7 @@ update msg ({ basePath, apiUrl } as model) =
, if keep then
Cmd.none
else
Task.perform (NewSilenceFromMatchers >> MsgForSilenceForm) (Task.succeed [])
Task.perform (NewSilenceFromMatchers model.defaultCreator >> MsgForSilenceForm) (Task.succeed [])
)

NavigateToSilenceFormEdit uuid ->
Expand Down Expand Up @@ -125,10 +125,13 @@ update msg ({ basePath, apiUrl } as model) =
( silenceForm, cmd ) =
Views.SilenceForm.Updates.update msg model.silenceForm basePath apiUrl
in
( { model | silenceForm = silenceForm }, Cmd.map MsgForSilenceForm cmd )
( { model | silenceForm = silenceForm }, cmd )

BootstrapCSSLoaded css ->
( { model | bootstrapCSS = css }, Cmd.none )

FontAwesomeCSSLoaded css ->
( { model | fontAwesomeCSS = css }, Cmd.none )

SetDefaultCreator name ->
( { model | defaultCreator = name }, Cmd.none )
4 changes: 2 additions & 2 deletions ui/app/src/Views.elm
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ currentView model =
SilenceList.view model.silenceList

SilenceFormNewRoute keep ->
SilenceForm.view Nothing model.silenceForm |> Html.map MsgForSilenceForm
SilenceForm.view Nothing model.defaultCreator model.silenceForm |> Html.map MsgForSilenceForm

SilenceFormEditRoute silenceId ->
SilenceForm.view (Just silenceId) model.silenceForm |> Html.map MsgForSilenceForm
SilenceForm.view (Just silenceId) "" model.silenceForm |> Html.map MsgForSilenceForm

TopLevelRoute ->
Utils.Views.loading
Expand Down
9 changes: 5 additions & 4 deletions ui/app/src/Views/SilenceForm/Types.elm
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ type SilenceFormMsg
| PreviewSilence
| AlertGroupsPreview (ApiData (List Alert))
| FetchSilence String
| NewSilenceFromMatchers (List Matcher)
| NewSilenceFromMatchersAndTime (List Matcher) Time
| NewSilenceFromMatchers String (List Matcher)
| NewSilenceFromMatchersAndTime String (List Matcher) Time
| SilenceFetch (ApiData Silence)
| SilenceCreate (ApiData SilenceId)

Expand Down Expand Up @@ -172,12 +172,13 @@ defaultDuration =
2 * Time.hour


fromMatchersAndTime : List Matcher -> Time -> SilenceForm
fromMatchersAndTime matchers now =
fromMatchersAndTime : String -> List Matcher -> Time -> SilenceForm
fromMatchersAndTime defaultCreator matchers now =
{ empty
| startsAt = initialField (timeToString now)
, endsAt = initialField (timeToString (now + defaultDuration))
, duration = initialField (durationFormat defaultDuration)
, createdBy = initialField defaultCreator
, matchers =
-- If no matchers were specified, add an empty row
if List.isEmpty matchers then
Expand Down
28 changes: 18 additions & 10 deletions ui/app/src/Views/SilenceForm/Updates.elm
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Views.SilenceForm.Updates exposing (update)
port module Views.SilenceForm.Updates exposing (update)

import Alerts.Api
import Silences.Api
import Task
import Time
import Types exposing (Msg(MsgForSilenceForm, SetDefaultCreator))
import Navigation
import Utils.Date exposing (timeFromString)
import Utils.List
Expand Down Expand Up @@ -169,14 +170,18 @@ updateForm msg form =
{ form | matchers = matchers }


update : SilenceFormMsg -> Model -> String -> String -> ( Model, Cmd SilenceFormMsg )
update : SilenceFormMsg -> Model -> String -> String -> ( Model, Cmd Msg )
update msg model basePath apiUrl =
case msg of
CreateSilence ->
case toSilence model.form of
Just silence ->
( { model | silenceId = Loading }
, Silences.Api.create apiUrl silence |> Cmd.map SilenceCreate
, Cmd.batch
[ Silences.Api.create apiUrl silence |> Cmd.map (SilenceCreate >> MsgForSilenceForm)
, persistDefaultCreator silence.createdBy
, Task.succeed silence.createdBy |> Task.perform SetDefaultCreator
]
)

Nothing ->
Expand All @@ -199,23 +204,23 @@ update msg model basePath apiUrl =
in
( { model | silenceId = silenceId }, cmd )

NewSilenceFromMatchers matchers ->
( model, Task.perform (NewSilenceFromMatchersAndTime matchers) Time.now )
NewSilenceFromMatchers defaultCreator matchers ->
( model, Task.perform (NewSilenceFromMatchersAndTime defaultCreator matchers >> MsgForSilenceForm) Time.now )

NewSilenceFromMatchersAndTime matchers time ->
( { form = fromMatchersAndTime matchers time
NewSilenceFromMatchersAndTime defaultCreator matchers time ->
( { form = fromMatchersAndTime defaultCreator matchers time
, alerts = Initial
, silenceId = Initial
}
, Cmd.none
)

FetchSilence silenceId ->
( model, Silences.Api.getSilence apiUrl silenceId SilenceFetch )
( model, Silences.Api.getSilence apiUrl silenceId (SilenceFetch >> MsgForSilenceForm) )

SilenceFetch (Success silence) ->
( { model | form = fromSilence silence }
, Task.perform identity (Task.succeed PreviewSilence)
, Task.perform identity (Task.succeed (MsgForSilenceForm PreviewSilence))
)

SilenceFetch _ ->
Expand All @@ -228,7 +233,7 @@ update msg model basePath apiUrl =
, Alerts.Api.fetchAlerts
apiUrl
{ nullFilter | text = Just (Utils.List.mjoin silence.matchers) }
|> Cmd.map AlertGroupsPreview
|> Cmd.map (AlertGroupsPreview >> MsgForSilenceForm)
)

Nothing ->
Expand All @@ -251,3 +256,6 @@ update msg model basePath apiUrl =
}
, Cmd.none
)


port persistDefaultCreator : String -> Cmd msg
6 changes: 3 additions & 3 deletions ui/app/src/Views/SilenceForm/Views.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import Utils.FormValidation exposing (ValidationState(..), ValidatedField)
import Views.SilenceForm.Types exposing (Model, SilenceFormMsg(..), SilenceFormFieldMsg(..), SilenceForm)


view : Maybe SilenceId -> Model -> Html SilenceFormMsg
view maybeId { form, silenceId, alerts } =
view : Maybe SilenceId -> String -> Model -> Html SilenceFormMsg
view maybeId defaultCreator { form, silenceId, alerts } =
let
( title, resetClick ) =
case maybeId of
Just silenceId ->
( "Edit Silence", FetchSilence silenceId )

Nothing ->
( "New Silence", NewSilenceFromMatchers [] )
( "New Silence", NewSilenceFromMatchers defaultCreator [] )
in
div []
[ h1 [] [ text title ]
Expand Down
2 changes: 1 addition & 1 deletion ui/app/src/Views/SilenceList/SilenceView.elm
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ editButton silence =
a
[ class "btn btn-outline-info border-0"
, href "#/silences/new?keep=1"
, onClick (NewSilenceFromMatchers silence.matchers |> MsgForSilenceForm)
, onClick (NewSilenceFromMatchers silence.createdBy silence.matchers |> MsgForSilenceForm)
]
[ text "Recreate"
]
Expand Down
8 changes: 4 additions & 4 deletions ui/bindata.go

Large diffs are not rendered by default.