Skip to content

Commit

Permalink
Add total events counter to the debug page
Browse files Browse the repository at this point in the history
Co-authored-by: Łukasz Reszke <[email protected]>
  • Loading branch information
pjurewicz and lukaszreszke committed Mar 21, 2024
1 parent 95250e6 commit 4376e1f
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 17 deletions.
25 changes: 23 additions & 2 deletions ruby_event_store-browser/elm/src/Api.elm
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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(..), SearchStream, Stream, emptyPaginatedList, eventDecoder, eventsDecoder, getEvent, getEvents, getSearchStreams, getStream, searchStreamsDecoder, Stats, getStats)

import Flags exposing (Flags)
import Http
import Iso8601
import Json.Decode exposing (Decoder, field, list, maybe, string, succeed, value)
import Json.Decode exposing (Decoder, field, list, maybe, string, succeed, value, int)
import Json.Decode.Pipeline exposing (optional, optionalAt, required, requiredAt)
import Json.Encode exposing (encode)
import Maybe.Extra
Expand Down Expand Up @@ -42,6 +42,9 @@ type alias SearchStream =
{ streamId : String
}

type alias Stats =
{ eventsInTotal: Int
}

type alias PaginatedList a =
{ pagination : Pagination.Specification
Expand Down Expand Up @@ -93,6 +96,9 @@ searchStreamsUrl : Flags -> String -> String
searchStreamsUrl flags query =
buildUrl (Url.toString flags.apiUrl ++ "/search_streams") query

getStatsUrl : Flags -> String
getStatsUrl flags =
Url.toString flags.apiUrl ++ "/stats"

getEvent : (Result Http.Error Event -> msg) -> Flags -> String -> Cmd msg
getEvent msgBuilder flags eventId =
Expand All @@ -117,6 +123,12 @@ getSearchStreams msgBuilder flags query =
, expect = Http.expectJson msgBuilder searchStreamsDecoder
}

getStats : (Result Http.Error (Stats) -> msg) -> Flags -> Cmd msg
getStats msgBuilder flags =
Http.get
{ url = getStatsUrl flags
, expect = Http.expectJson msgBuilder statsDecoder
}

eventDecoder : Decoder Event
eventDecoder =
Expand Down Expand Up @@ -183,6 +195,15 @@ searchStreamsDecoder =
list searchStreamDecoder
|> field "data"

statsDecoder : Decoder Stats
statsDecoder =
statsDecoder_
|> field "meta"

statsDecoder_ : Decoder Stats
statsDecoder_ =
succeed Stats
|> required "events_in_total" int

linksDecoder : Decoder PaginationLinks
linksDecoder =
Expand Down
21 changes: 18 additions & 3 deletions ruby_event_store-browser/elm/src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import LinkedTimezones exposing (mapLinkedTimeZone)
import Page.Debug
import Page.ShowEvent
import Page.ShowStream
import Page.Debug
import Route
import Task
import Time
Expand Down Expand Up @@ -51,7 +52,7 @@ type Msg
| GotShowEventMsg Page.ShowEvent.Msg
| GotShowStreamMsg Page.ShowStream.Msg
| ReceiveTimeZone (Result String Time.ZoneName)

| GotDebugMsg Page.Debug.Msg

type Page
= NotFound
Expand Down Expand Up @@ -131,6 +132,16 @@ update msg model =
, Cmd.map GotShowEventMsg subCmd
)

( GotDebugMsg gotDebugMsg, Debug debugModel ) ->
let
(subModel, subCmd) =
Page.Debug.update gotDebugMsg debugModel
in
({model | page = Debug subModel}
, Cmd.map GotDebugMsg subCmd
)


( GotLayoutMsg layoutMsg, _ ) ->
case model.flags of
Nothing ->
Expand Down Expand Up @@ -215,8 +226,12 @@ navigate model location =
Nothing ->
( { model | page = NotFound }, Cmd.none )

Just Route.Debug ->
( { model | page = Debug (Page.Debug.init flags) }, Cmd.none )
Just (Route.Debug) ->
let
page = Page.Debug.init flags
cmd = Page.Debug.initCmd flags
in
( { model | page = Debug page }, Cmd.map GotDebugMsg cmd )

Nothing ->
( { model | page = NotFound }, Cmd.none )
Expand Down
38 changes: 27 additions & 11 deletions ruby_event_store-browser/elm/src/Page/Debug.elm
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
module Page.Debug exposing (..)

import Flags exposing (Flags)
import Html exposing (..)
import Flags exposing (Flags)
import Api exposing (getStats, Stats)
import Http

type alias Model = { resVersion : String, repositoryAdapter : String, eventsInTotal : String }

type alias Model =
{ resVersion : String, repositoryAdapter : String }

type Msg
= GotStats (Result Http.Error Stats)

init : Flags -> Model
init flags =
{ resVersion = flags.resVersion, repositoryAdapter = flags.repositoryAdapter }
init flags =
{ resVersion = flags.resVersion, repositoryAdapter = flags.repositoryAdapter, eventsInTotal = "" }

initCmd : Flags -> Cmd Msg
initCmd flags =
getStats GotStats flags

view : Model -> Html a
view: Model -> Html a
view model =
div []
[ p [] [ text ("RubyEventStore version: " ++ model.resVersion) ]
, p [] [ text ("RubyEventStore adapter: " ++ model.repositoryAdapter) ]
]
div [] [
p [] [text ("RubyEventStore version: " ++ model.resVersion)],
p [] [text ("RubyEventStore adapter: " ++ model.repositoryAdapter)],
p [] [text ("Events in total: " ++ model.eventsInTotal)]
]

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
GotStats result ->
case result of
Ok stats ->
({ model | eventsInTotal = String.fromInt stats.eventsInTotal }, Cmd.none)
Err _ ->
(model, Cmd.none)
1 change: 1 addition & 0 deletions ruby_event_store-browser/lib/ruby_event_store/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ module Browser
require_relative "browser/urls"
require_relative "browser/gem_source"
require_relative "browser/router"
require_relative "browser/get_stats"
6 changes: 5 additions & 1 deletion ruby_event_store-browser/lib/ruby_event_store/browser/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ def call(env)
stream_name: params.fetch("stream_name"),
)
end

router.add_route("GET", "/api/stats") do
json GetStats.new(
event_store: event_store
)
end
%w[/ /events/:event_id /streams/:stream_name /debug].each do |starting_route|
router.add_route("GET", starting_route) do |_, urls|
erb bootstrap_html,
Expand Down
27 changes: 27 additions & 0 deletions ruby_event_store-browser/lib/ruby_event_store/browser/get_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module RubyEventStore
module Browser
class GetStats
def initialize(event_store:)
@event_store = event_store
end

def to_h
{
meta: {
events_in_total: events
}
}
end

private

def events
event_store.read.count
end

attr_reader :event_store
end
end
end
16 changes: 16 additions & 0 deletions ruby_event_store-browser/spec/api/stats_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require "spec_helper"

module RubyEventStore
::RSpec.describe Browser do
include Browser::IntegrationHelpers

specify "not existing" do
api_client.get "/api/stats"
expect(api_client.parsed_body["meta"]).to match({
"events_in_total" => 0
})
end
end
end

0 comments on commit 4376e1f

Please sign in to comment.