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

fix: stop double log fetch on steps/services #813

Merged
merged 8 commits into from
Aug 7, 2024
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
111 changes: 86 additions & 25 deletions src/elm/Pages/Org_/Repo_/Build_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ type Msg
| GetBuildStepLogResponse { step : Vela.Step, applyDomFocus : Bool, previousFocus : Maybe Focus.Focus } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Log ))
| GetBuildStepLogRefreshResponse { step : Vela.Step } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Log ))
| ClickStep { step : Vela.Step }
| ExpandStep { step : Vela.Step, applyDomFocus : Bool, previousFocus : Maybe Focus.Focus }
| ExpandStep { step : Vela.Step, applyDomFocus : Bool, previousFocus : Maybe Focus.Focus, triggeredFromClick : Bool }
| CollapseStep { step : Vela.Step }
| ExpandAll
| CollapseAll
Expand Down Expand Up @@ -265,7 +265,15 @@ update shared route msg model =
}
, RemoteData.withDefault [] model.steps
|> List.filter (\s -> Maybe.withDefault -1 focus.group == s.number)
|> List.map (\s -> ExpandStep { step = s, applyDomFocus = True, previousFocus = Just model.focus })
|> List.map
(\s ->
ExpandStep
{ step = s
, applyDomFocus = True
, previousFocus = Just model.focus
, triggeredFromClick = False
}
)
|> List.map Effect.sendMsg
|> Effect.batch
)
Expand Down Expand Up @@ -302,6 +310,7 @@ update shared route msg model =
{ step = step
, applyDomFocus = options.applyDomFocus
, previousFocus = Nothing
, triggeredFromClick = False
}
|> Effect.sendMsg
)
Expand Down Expand Up @@ -439,7 +448,12 @@ update shared route msg model =

else
Effect.batch
[ ExpandStep { step = options.step, applyDomFocus = False, previousFocus = Nothing }
[ ExpandStep
{ step = options.step
, applyDomFocus = False
, previousFocus = Nothing
, triggeredFromClick = True
}
|> Effect.sendMsg
, case model.focus.a of
Nothing ->
Expand All @@ -459,25 +473,57 @@ update shared route msg model =
)

ExpandStep options ->
( { model
| viewing = List.Extra.unique <| options.step.number :: model.viewing
}
, Effect.batch
[ Effect.getBuildStepLog
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse =
GetBuildStepLogResponse
{ step = options.step
, applyDomFocus = options.applyDomFocus
, previousFocus = options.previousFocus
}
, org = route.params.org
, repo = route.params.repo
, build = route.params.build
, stepNumber = String.fromInt options.step.number
}
, if options.applyDomFocus then
let
isFromHashChanged =
options.previousFocus /= Nothing

didFocusChange =
case options.previousFocus of
Just f ->
f.group /= model.focus.group

Nothing ->
False

-- hash will change when no line is selected and the selected group changes
-- this means expansion msg will double up on fetching logs unless instructed not to
willFocusChange =
case ( model.focus.group, model.focus.a, model.focus.b ) of
( Just g, Nothing, _ ) ->
g /= options.step.number

_ ->
False

isLogLoaded =
Dict.get options.step.id model.logs
|> Maybe.withDefault RemoteData.Loading
|> Util.isLoaded

-- fetch logs when expansion msg meets the criteria:
-- triggered by a click that will change the hash
-- the focus changes and the logs are not loaded
fetchLogs =
not (options.triggeredFromClick && willFocusChange)
&& ((didFocusChange && not isLogLoaded) || not isFromHashChanged)

getLogEffect =
Effect.getBuildStepLog
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse =
GetBuildStepLogResponse
{ step = options.step
, applyDomFocus = options.applyDomFocus
, previousFocus = options.previousFocus
}
, org = route.params.org
, repo = route.params.repo
, build = route.params.build
, stepNumber = String.fromInt options.step.number
}

applyDomFocusEffect =
case ( model.focus.group, model.focus.a, model.focus.b ) of
( Just g, Nothing, Nothing ) ->
FocusOn
Expand All @@ -493,9 +539,23 @@ update shared route msg model =
_ ->
Effect.none

else
Effect.none
]
runEffects =
[ if fetchLogs then
getLogEffect

else
Effect.none
, if options.applyDomFocus then
applyDomFocusEffect

else
Effect.none
]
in
( { model
| viewing = List.Extra.unique <| options.step.number :: model.viewing
}
, Effect.batch runEffects
)

CollapseStep options ->
Expand Down Expand Up @@ -525,6 +585,7 @@ update shared route msg model =
{ step = step
, applyDomFocus = False
, previousFocus = Nothing
, triggeredFromClick = False
}
)
|> List.map Effect.sendMsg
Expand Down
112 changes: 87 additions & 25 deletions src/elm/Pages/Org_/Repo_/Build_/Services.elm
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ type Msg
| GetBuildServiceLogResponse { service : Vela.Service, applyDomFocus : Bool, previousFocus : Maybe Focus.Focus } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Log ))
| GetBuildServiceLogRefreshResponse { service : Vela.Service } (Result (Http.Detailed.Error String) ( Http.Metadata, Vela.Log ))
| ClickService { service : Vela.Service }
| ExpandService { service : Vela.Service, applyDomFocus : Bool, previousFocus : Maybe Focus.Focus }
| ExpandService { service : Vela.Service, applyDomFocus : Bool, previousFocus : Maybe Focus.Focus, triggeredFromClick : Bool }
| CollapseService { service : Vela.Service }
| ExpandAll
| CollapseAll
Expand Down Expand Up @@ -257,7 +257,15 @@ update shared route msg model =
}
, RemoteData.withDefault [] model.services
|> List.filter (\s -> Maybe.withDefault -1 focus.group == s.number)
|> List.map (\s -> ExpandService { service = s, applyDomFocus = True, previousFocus = Just model.focus })
|> List.map
(\s ->
ExpandService
{ service = s
, applyDomFocus = True
, previousFocus = Just model.focus
, triggeredFromClick = False
}
)
|> List.map Effect.sendMsg
|> Effect.batch
)
Expand Down Expand Up @@ -293,6 +301,7 @@ update shared route msg model =
{ service = service
, applyDomFocus = True
, previousFocus = Nothing
, triggeredFromClick = False
}
|> Effect.sendMsg
)
Expand Down Expand Up @@ -430,7 +439,12 @@ update shared route msg model =

else
Effect.batch
[ ExpandService { service = options.service, applyDomFocus = False, previousFocus = Nothing }
[ ExpandService
{ service = options.service
, applyDomFocus = False
, previousFocus = Nothing
, triggeredFromClick = True
}
|> Effect.sendMsg
, case model.focus.a of
Nothing ->
Expand All @@ -450,25 +464,57 @@ update shared route msg model =
)

ExpandService options ->
( { model
| viewing = List.Extra.unique <| options.service.number :: model.viewing
}
, Effect.batch
[ Effect.getBuildServiceLog
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse =
GetBuildServiceLogResponse
{ service = options.service
, applyDomFocus = options.applyDomFocus
, previousFocus = options.previousFocus
}
, org = route.params.org
, repo = route.params.repo
, build = route.params.build
, serviceNumber = String.fromInt options.service.number
}
, if options.applyDomFocus then
let
isFromHashChanged =
options.previousFocus /= Nothing

didFocusChange =
case options.previousFocus of
Just f ->
f.group /= model.focus.group

Nothing ->
False

-- hash will change when no line is selected and the selected group changes
-- this means the expansion msg will double up on fetching logs unless instructed not to
willFocusChange =
case ( model.focus.group, model.focus.a, model.focus.b ) of
( Just g, Nothing, _ ) ->
g /= options.service.number

_ ->
False

isLogLoaded =
Dict.get options.service.id model.logs
|> Maybe.withDefault RemoteData.Loading
|> Util.isLoaded

-- fetch logs when expansion msg meets the criteria:
-- triggered by a click that will change the hash
-- the focus changes and the logs are not loaded
fetchLogs =
not (options.triggeredFromClick && willFocusChange)
&& ((didFocusChange && not isLogLoaded) || not isFromHashChanged)

getLogEffect =
Effect.getBuildServiceLog
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse =
GetBuildServiceLogResponse
{ service = options.service
, applyDomFocus = options.applyDomFocus
, previousFocus = options.previousFocus
}
, org = route.params.org
, repo = route.params.repo
, build = route.params.build
, serviceNumber = String.fromInt options.service.number
}

applyDomFocusEffect =
case ( model.focus.group, model.focus.a, model.focus.b ) of
( Just g, Nothing, Nothing ) ->
FocusOn
Expand All @@ -484,9 +530,24 @@ update shared route msg model =
_ ->
Effect.none

else
Effect.none
]
runEffects =
[ if fetchLogs then
getLogEffect

else
Effect.none
, if options.applyDomFocus then
applyDomFocusEffect

else
Effect.none
]
in
( { model
| viewing = List.Extra.unique <| options.service.number :: model.viewing
}
, Effect.batch
runEffects
)

CollapseService options ->
Expand Down Expand Up @@ -516,6 +577,7 @@ update shared route msg model =
{ service = service
, applyDomFocus = False
, previousFocus = Nothing
, triggeredFromClick = False
}
)
|> List.map Effect.sendMsg
Expand Down
15 changes: 9 additions & 6 deletions src/elm/Utils/Helpers.elm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Utils.Helpers exposing
, humanReadableDateTimeFormatter
, humanReadableDateTimeWithDefault
, humanReadableDateWithDefault
, isLoading
, isLoaded
, isSuccess
, mergeListsById
, noBlanks
Expand Down Expand Up @@ -375,17 +375,20 @@ fiveSecondsMillis =
oneSecondMillis * 5


{-| isLoading : takes WebData and returns true if it is in a Loading state.
{-| isLoaded : takes WebData and returns true if it is in a 'loaded' state, meaning its anything but NotAsked or Loading.
-}
isLoading : WebData a -> Bool
isLoading status =
isLoaded : WebData a -> Bool
isLoaded status =
case status of
RemoteData.Loading ->
True
False

_ ->
RemoteData.NotAsked ->
False

_ ->
True


{-| isSuccess : takes WebData and returns true if it is in a Success state.
-}
Expand Down