Skip to content

Commit

Permalink
Linkify alert annotations (#946)
Browse files Browse the repository at this point in the history
* Make links in annotations clickable

* Update test script and bindata.go

* Add target = _blank
  • Loading branch information
w0rm authored and mxinden committed Aug 13, 2017
1 parent 09bc5dd commit 250bd35
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/ha/send_alerts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ alerts1='[
},
"annotations": {
"info": "The disk sda2 is running full",
"summary": "please check the instance example1"
"summary": "please check the instance example1",
"runbook": "the following link http://test-url should be clickable"
}
},
{
Expand Down
45 changes: 44 additions & 1 deletion ui/app/src/Utils/String.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Utils.String exposing (capitalizeFirst)
module Utils.String exposing (capitalizeFirst, linkify)

import String
import Char
Expand All @@ -12,3 +12,46 @@ capitalizeFirst string =

Just ( char, rest ) ->
String.cons (Char.toUpper char) rest


linkify : String -> List (Result String String)
linkify string =
List.reverse (linkifyHelp (String.words string) [])


linkifyHelp : List String -> List (Result String String) -> List (Result String String)
linkifyHelp words linkified =
case words of
[] ->
linkified

word :: restWords ->
if isUrl word then
case linkified of
(Err lastWord) :: restLinkified ->
-- append space to last word
linkifyHelp restWords (Ok word :: Err (lastWord ++ " ") :: restLinkified)

(Ok lastWord) :: restLinkified ->
-- insert space between two links
linkifyHelp restWords (Ok word :: Err " " :: linkified)

_ ->
linkifyHelp restWords (Ok word :: linkified)
else
case linkified of
(Err lastWord) :: restLinkified ->
-- concatenate with last word
linkifyHelp restWords (Err (lastWord ++ " " ++ word) :: restLinkified)

(Ok lastWord) :: restLinkified ->
-- insert space after the link
linkifyHelp restWords (Err (" " ++ word) :: linkified)

_ ->
linkifyHelp restWords (Err word :: linkified)


isUrl : String -> Bool
isUrl =
flip String.startsWith >> (flip List.any) [ "http://", "https://" ]
14 changes: 14 additions & 0 deletions ui/app/src/Utils/Views.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ labelButton maybeMsg labelText =
[ span [ class "text-muted" ] [ text labelText ] ]


linkifyText : String -> List (Html msg)
linkifyText str =
List.map
(\result ->
case result of
Ok link ->
a [ href link, target "_blank" ] [ text link ]

Err txt ->
text txt
)
(Utils.String.linkify str)


iconButtonMsg : String -> String -> msg -> Html msg
iconButtonMsg classString icon msg =
a [ class classString, onClick msg ]
Expand Down
3 changes: 2 additions & 1 deletion ui/app/src/Views/AlertList/AlertView.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Utils.Date
import Views.FilterBar.Types as FilterBarTypes
import Views.AlertList.Types exposing (AlertListMsg(MsgForFilterBar, SetActive))
import Utils.Filter
import Utils.Views


view : List ( String, String ) -> Maybe String -> Alert -> Html Msg
Expand Down Expand Up @@ -82,7 +83,7 @@ annotation : ( String, String ) -> Html Msg
annotation ( key, value ) =
tr []
[ th [ class "text-nowrap" ] [ text (key ++ ":") ]
, td [ class "w-100" ] [ text value ]
, td [ class "w-100" ] (Utils.Views.linkifyText value)
]


Expand Down
23 changes: 23 additions & 0 deletions ui/app/tests/StringUtils.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module StringUtils exposing (testLinkify)

import Utils.String exposing (linkify)
import Test exposing (..)
import Expect


testLinkify : Test
testLinkify =
describe "linkify"
[ test "should linkify a url in the middle" <|
\() ->
Expect.equal (linkify "word1 http://url word2")
[ Err "word1 ", Ok "http://url", Err " word2" ]
, test "should linkify a url in the beginning" <|
\() ->
Expect.equal (linkify "http://url word1 word2")
[ Ok "http://url", Err " word1 word2" ]
, test "should linkify a url in the end" <|
\() ->
Expect.equal (linkify "word1 word2 http://url")
[ Err "word1 word2 ", Ok "http://url" ]
]
4 changes: 2 additions & 2 deletions ui/bindata.go

Large diffs are not rendered by default.

0 comments on commit 250bd35

Please sign in to comment.