Skip to content

Commit

Permalink
Remove search streams left overs
Browse files Browse the repository at this point in the history
It would kill the server in production environment
  • Loading branch information
lukaszreszke committed Jul 25, 2024
1 parent c653326 commit e470c8f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 217 deletions.
28 changes: 1 addition & 27 deletions ruby_event_store-browser/elm/src/Api.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Api exposing (Event, PaginatedList, PaginationLink, PaginationLinks, RemoteResource(..), SearchStream, Stream, emptyPaginatedList, eventDecoder, eventsDecoder, getEvent, getEvents, getSearchStreams, getStream, searchStreamsDecoder)
module Api exposing (Event, PaginatedList, PaginationLink, PaginationLinks, RemoteResource(..), Stream, emptyPaginatedList, eventDecoder, eventsDecoder, getEvent, getEvents, getStream)

import Flags exposing (Flags)
import Http
Expand Down Expand Up @@ -38,10 +38,6 @@ type alias Event =
}


type alias SearchStream =
{ streamId : String
}


type alias PaginatedList a =
{ pagination : Pagination.Specification
Expand Down Expand Up @@ -89,10 +85,6 @@ streamUrl flags streamId =
buildUrl (Url.toString flags.apiUrl ++ "/streams") streamId


searchStreamsUrl : Flags -> String -> String
searchStreamsUrl flags query =
buildUrl (Url.toString flags.apiUrl ++ "/search_streams") query


getEvent : (Result Http.Error Event -> msg) -> Flags -> String -> Cmd msg
getEvent msgBuilder flags eventId =
Expand All @@ -110,13 +102,6 @@ getStream msgBuilder flags streamId =
}


getSearchStreams : (Result Http.Error (List SearchStream) -> msg) -> Flags -> String -> Cmd msg
getSearchStreams msgBuilder flags query =
Http.get
{ url = searchStreamsUrl flags query
, expect = Http.expectJson msgBuilder searchStreamsDecoder
}


eventDecoder : Decoder Event
eventDecoder =
Expand Down Expand Up @@ -172,17 +157,6 @@ eventsDecoder pagination =
|> required "links" linksDecoder


searchStreamDecoder : Decoder SearchStream
searchStreamDecoder =
succeed SearchStream
|> required "id" string


searchStreamsDecoder : Decoder (List SearchStream)
searchStreamsDecoder =
list searchStreamDecoder
|> field "data"


linksDecoder : Decoder PaginationLinks
linksDecoder =
Expand Down
36 changes: 1 addition & 35 deletions ruby_event_store-browser/elm/src/Layout.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
port module Layout exposing (Model, Msg, buildModel, subscriptions, update, view, viewIncorrectConfig, viewNotFound)

import Api exposing (SearchStream, getSearchStreams)
import Browser exposing (Document)
import Browser.Navigation
import BrowserTime
Expand All @@ -25,9 +24,7 @@ type Msg
| SearchMsg Search.Msg
| ToggleBookmarksMenu
| ToggleDialog
| SearchedStreamsFetched (Result Http.Error (List SearchStream))
| OnSelect Search.Stream
| OnQueryChanged Search.Stream
| RequestSearch
| RequestSearchClose
| ToggleBookmark String
Expand Down Expand Up @@ -73,7 +70,7 @@ subscriptions =
buildModel : Model
buildModel =
{ search =
Search.init OnSelect OnQueryChanged
Search.init OnSelect
, displayBookmarksMenu = False
, bookmarks =
[ { itemType = "Stream", label = "Bookmark 1", link = "/" }
Expand All @@ -87,11 +84,6 @@ goToStream { key, flags } stream =
Browser.Navigation.pushUrl key (Route.streamUrl flags.rootUrl stream)


searchStreams : WrappedModel Model -> String -> Cmd Msg
searchStreams { flags } stream =
getSearchStreams SearchedStreamsFetched flags stream


update : Msg -> WrappedModel Model -> ( WrappedModel Model, Cmd Msg )
update msg model =
case msg of
Expand All @@ -110,9 +102,6 @@ update msg model =
]
)

OnQueryChanged streamName ->
( model, searchStreams model streamName )

TimeZoneSelected zoneName ->
let
defaultTimeZone =
Expand Down Expand Up @@ -162,29 +151,6 @@ update msg model =
ToggleDialog ->
( model, toggleDialog searchModalId )

SearchedStreamsFetched (Ok streams) ->
let
streams_ =
List.map .streamId streams

searchModel =
model.internal.search

newModel =
{ searchModel | streams = streams_ }
in
( { model | internal = Model newModel model.internal.displayBookmarksMenu model.internal.bookmarks }, Cmd.none )

SearchedStreamsFetched (Err _) ->
let
searchModel =
model.internal.search

newModel =
{ searchModel | streams = [] }
in
( { model | internal = Model newModel model.internal.displayBookmarksMenu model.internal.bookmarks }, Cmd.none )

ToggleBookmark id ->
( model, toggleBookmark id )

Expand Down
91 changes: 5 additions & 86 deletions ruby_event_store-browser/elm/src/Search.elm
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ type alias Stream =


type alias Model a =
{ streams : List Stream
, searchedStream : Stream
{ searchedStream : Stream
, onSelectMsg : Stream -> a
, onQueryMsg : Stream -> a
}


Expand All @@ -35,12 +33,10 @@ emptyStreams =
[]


init : (Stream -> a) -> (Stream -> a) -> Model a
init onSelectMsg onQueryMsg =
{ streams = emptyStreams
, searchedStream = emptyStreamName
init : (Stream -> a) -> Model a
init onSelectMsg =
{ searchedStream = emptyStreamName
, onSelectMsg = onSelectMsg
, onQueryMsg = onQueryMsg
}


Expand All @@ -49,65 +45,20 @@ onSelectCmd onSelectMsg stream =
Task.perform onSelectMsg (Task.succeed stream)


onQueryChangedCmd : (Stream -> a) -> Stream -> Cmd a
onQueryChangedCmd onQueryMsg stream =
Task.perform onQueryMsg (Task.succeed stream)


hasAtLeastThreeChars : Stream -> Bool
hasAtLeastThreeChars stream =
String.length stream >= 3


update : Msg -> Model a -> ( Model a, Cmd a )
update msg model =
case msg of
StreamChanged stream ->
if hasAtLeastThreeChars stream then
( { model | searchedStream = stream }
, onQueryChangedCmd model.onQueryMsg stream
)

else
( { model | searchedStream = stream }, Cmd.none )
( { model | searchedStream = stream }, Cmd.none )

GoToStream stream ->
( { model | searchedStream = emptyStreamName }
, onSelectCmd model.onSelectMsg stream
)


caseInsensitiveContains : Stream -> Stream -> Maybe Stream
caseInsensitiveContains needle haystack =
let
needleLower =
String.toLower needle

haystackLower =
String.toLower haystack
in
if String.contains needleLower haystackLower then
Just haystack

else
Nothing


filterStreams : Stream -> List Stream -> List Stream
filterStreams stream streams =
if String.isEmpty stream then
emptyStreams

else
List.filterMap (caseInsensitiveContains stream) streams


view : Model a -> Html Msg
view model =
let
streams_ =
filterStreams model.searchedStream model.streams
in
form [ onSubmit (GoToStream model.searchedStream) ]
[ div [ class "relative" ]
[ FeatherIcons.search
Expand All @@ -125,36 +76,4 @@ view model =
[ span [ class "text-gray-500 bg-gray-50 font-bold block p-1 border border-gray-300 rounded " ] [ text "ESC" ]
]
]
, viewStreamList streams_
]


streamsPresent : List Stream -> Bool
streamsPresent streams =
not <| List.isEmpty streams


viewStreamList : List Stream -> Html Msg
viewStreamList streams =
if streams |> streamsPresent then
div
[]
[ ul
[ class "mt-4 overflow-auto space-y-2 w-full" ]
(List.map viewStreamListItem streams)
]

else
text ""


viewStreamListItem : Stream -> Html Msg
viewStreamListItem stream =
li []
[ a
[ class "p-3 block rounded hover:bg-red-200 w-full bg-gray-100 break-words text-xs font-bold font-mono"
, href ("/streams/" ++ stream)
, onClick (GoToStream stream)
]
[ text stream ]
]
32 changes: 1 addition & 31 deletions ruby_event_store-browser/elm/tests/DecodersTest.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module DecodersTest exposing (suite)

import Api exposing (eventDecoder, eventsDecoder, searchStreamsDecoder)
import Api exposing (eventDecoder, eventsDecoder)
import Expect
import Json.Decode
import Pagination
Expand Down Expand Up @@ -143,35 +143,5 @@ suite =
, validAt = Time.millisToPosix 1513813785273
}
)
, test "search decoder" <|
\_ ->
let
input =
"""
{
"data": [
{
"id": "$by_tenant_id_133",
"type": "streams"
},
{
"id": "$by_tenant_id_444",
"type": "streams"
}
]
}
"""

output =
Json.Decode.decodeString searchStreamsDecoder input
in
Expect.equal output
(Ok
[ { streamId = "$by_tenant_id_133"
}
, { streamId = "$by_tenant_id_444"
}
]
)
]
]
38 changes: 0 additions & 38 deletions ruby_event_store-browser/elm/tests/SearchTest.elm

This file was deleted.

0 comments on commit e470c8f

Please sign in to comment.