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: reduce excessive API calls #810

Merged
merged 22 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
4 changes: 2 additions & 2 deletions cypress/fixtures/steps_5.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
"number": 2,
"name": "build",
"stage": "",
"status": "success",
"status": "running",
"error": "",
"exit_code": 2,
"created": 1572029883,
"started": 1572029928,
"finished": 1572029935,
"finished": 0,
"host": "",
"runtime": "docker",
"distribution": "linux"
Expand Down
7 changes: 6 additions & 1 deletion src/elm/Effect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Effect exposing
, sendCmd, sendMsg
, pushRoute, replaceRoute, loadExternalUrl
, map, toCmd
, addAlertError, addAlertSuccess, addDeployment, addFavorites, addOrgSecret, addRepoSchedule, addRepoSecret, addSharedSecret, alertsUpdate, approveBuild, cancelBuild, chownRepo, clearRedirect, deleteOrgSecret, deleteRepoSchedule, deleteRepoSecret, deleteSharedSecret, disableRepo, downloadFile, enableRepo, expandPipelineConfig, finishAuthentication, focusOn, getBuild, getBuildGraph, getBuildServiceLog, getBuildServices, getBuildStepLog, getBuildSteps, getCurrentUser, getCurrentUserShared, getDashboard, getOrgBuilds, getOrgRepos, getOrgSecret, getOrgSecrets, getPipelineConfig, getPipelineTemplates, getRepo, getRepoBuilds, getRepoBuildsShared, getRepoDeployments, getRepoHooks, getRepoHooksShared, getRepoSchedule, getRepoSchedules, getRepoSecret, getRepoSecrets, getSettings, getSharedSecret, getSharedSecrets, getWorkers, handleHttpError, logout, pushPath, redeliverHook, repairRepo, replacePath, replaceRouteRemoveTabHistorySkipDomFocus, restartBuild, setRedirect, setTheme, updateFavicon, updateFavorite, updateOrgSecret, updateRepo, updateRepoSchedule, updateRepoSecret, updateSettings, updateSharedSecret, updateSourceReposShared
, addAlertError, addAlertSuccess, addDeployment, addFavorites, addOrgSecret, addRepoSchedule, addRepoSecret, addSharedSecret, alertsUpdate, approveBuild, cancelBuild, chownRepo, clearRedirect, deleteOrgSecret, deleteRepoSchedule, deleteRepoSecret, deleteSharedSecret, disableRepo, downloadFile, enableRepo, expandPipelineConfig, finishAuthentication, focusOn, getBuild, getBuildGraph, getBuildServiceLog, getBuildServices, getBuildStepLog, getBuildSteps, getCurrentUser, getCurrentUserShared, getDashboard, getOrgBuilds, getOrgRepos, getOrgSecret, getOrgSecrets, getPipelineConfig, getPipelineTemplates, getRepo, getRepoBuilds, getRepoBuildsShared, getRepoDeployments, getRepoHooks, getRepoHooksShared, getRepoSchedule, getRepoSchedules, getRepoSecret, getRepoSecrets, getSettings, getSharedSecret, getSharedSecrets, getWorkers, handleHttpError, logout, pushPath, redeliverHook, repairRepo, replacePath, replaceRouteRemoveTabHistorySkipDomFocus, restartBuild, setRedirect, setTheme, updateFavicon, updateFavorite, updateOrgSecret, updateRepo, updateRepoHooksShared, updateRepoSchedule, updateRepoSecret, updateSettings, updateSharedSecret, updateSourceReposShared
)

{-|
Expand Down Expand Up @@ -626,6 +626,11 @@ getRepoHooksShared options =
SendSharedMsg <| Shared.Msg.GetRepoHooks options


updateRepoHooksShared : { hooks : WebData (List Vela.Hook) } -> Effect msg
updateRepoHooksShared options =
SendSharedMsg <| Shared.Msg.UpdateRepoHooks options


redeliverHook :
{ baseUrl : String
, session : Auth.Session.Session
Expand Down
52 changes: 35 additions & 17 deletions src/elm/Layouts/Default/Build.elm
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,42 @@ update props shared route msg model =

-- REFRESH
Tick options ->
let
isBuildRunning =
case model.build of
RemoteData.Success build ->
build.finished == 0

_ ->
True

getRepoBuildsEffect =
Effect.getRepoBuildsShared
{ pageNumber = Nothing
, perPage = Nothing
, maybeEvent = Nothing
, org = props.org
, repo = props.repo
}

runEffects =
if isBuildRunning then
[ getRepoBuildsEffect
, Effect.getBuild
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetBuildResponse
, org = props.org
, repo = props.repo
, build = props.build
}
]

else
[ getRepoBuildsEffect ]
in
( model
, Effect.batch
[ Effect.getRepoBuildsShared
{ pageNumber = Nothing
, perPage = Nothing
, maybeEvent = Nothing
, org = props.org
, repo = props.repo
}
, Effect.getBuild
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetBuildResponse
, org = props.org
, repo = props.repo
, build = props.build
}
]
, Effect.batch runEffects
)


Expand Down
37 changes: 20 additions & 17 deletions src/elm/Layouts/Default/Repo.elm
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,27 @@ update props route msg model =

-- REFRESH
Tick options ->
let
-- the hooks page has its own refresh call for hooks;
-- this is to prevent double calls
isNotOnHooksPage =
route.path /= Route.Path.Org__Repo__Hooks { org = props.org, repo = props.repo }

runEffect =
if isNotOnHooksPage then
Effect.getRepoHooksShared
{ pageNumber = Nothing
, perPage = Nothing
, maybeEvent = Nothing
, org = props.org
, repo = props.repo
}

else
Effect.none
in
( model
, Effect.batch
[ Effect.getCurrentUserShared {}
, Effect.getRepoBuildsShared
{ pageNumber = Nothing
, perPage = Nothing
, maybeEvent = Nothing
, org = props.org
, repo = props.repo
}
, Effect.getRepoHooksShared
{ pageNumber = Nothing
, perPage = Nothing
, maybeEvent = Nothing
, org = props.org
, repo = props.repo
}
]
, runEffect
)


Expand Down
24 changes: 1 addition & 23 deletions src/elm/Pages/Dash/Secrets/Engine_/Org/Org_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -216,29 +216,7 @@ update shared route msg model =

-- REFRESH
Tick options ->
( model
, Effect.batch
[ Effect.getOrgSecrets
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetOrgSecretsResponse
, pageNumber = Dict.get "page" route.query |> Maybe.andThen String.toInt
, perPage = Dict.get "perPage" route.query |> Maybe.andThen String.toInt
, engine = route.params.engine
, org = route.params.org
}
, Effect.getSharedSecrets
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetSharedSecretsResponse
, pageNumber = Nothing
, perPage = Nothing
, engine = route.params.engine
, org = route.params.org
, team = "*"
}
]
)
( model, Effect.none )



Expand Down
24 changes: 1 addition & 23 deletions src/elm/Pages/Dash/Secrets/Engine_/Repo/Org_/Repo_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,7 @@ update shared route msg model =

-- REFRESH
Tick options ->
( model
, Effect.batch
[ Effect.getRepoSecrets
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetRepoSecretsResponse
, pageNumber = Dict.get "page" route.query |> Maybe.andThen String.toInt
, perPage = Dict.get "perPage" route.query |> Maybe.andThen String.toInt
, engine = route.params.engine
, org = route.params.org
, repo = route.params.repo
}
, Effect.getOrgSecrets
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetOrgSecretsResponse
, pageNumber = Nothing
, perPage = Nothing
, engine = route.params.engine
, org = route.params.org
}
]
)
( model, Effect.none )



Expand Down
13 changes: 1 addition & 12 deletions src/elm/Pages/Dash/Secrets/Engine_/Shared/Org_/Team_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,7 @@ update shared route msg model =

-- REFRESH
Tick options ->
( model
, Effect.getSharedSecrets
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetSharedSecretsResponse
, pageNumber = Dict.get "page" route.query |> Maybe.andThen String.toInt
, perPage = Dict.get "perPage" route.query |> Maybe.andThen String.toInt
, engine = route.params.engine
, org = route.params.org
, team = route.params.team
}
)
( model, Effect.none )



Expand Down
2 changes: 1 addition & 1 deletion src/elm/Pages/Home_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ update msg model =
-- REFRESH
Tick options ->
( model
, Effect.getCurrentUserShared {}
, Effect.none
)


Expand Down
11 changes: 1 addition & 10 deletions src/elm/Pages/Org_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,7 @@ update shared route msg model =

-- REFRESH
Tick options ->
( model
, Effect.getOrgRepos
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetOrgReposResponse
, org = route.params.org
, pageNumber = Dict.get "page" route.query |> Maybe.andThen String.toInt
, perPage = Dict.get "perPage" route.query |> Maybe.andThen String.toInt
}
)
( model, Effect.none )



Expand Down
43 changes: 33 additions & 10 deletions src/elm/Pages/Org_/Repo_/Build_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ update shared route msg model =
( { model | steps = RemoteData.succeed <| List.sortBy .number steps }
, steps
|> List.filter (\step -> List.member step.number model.viewing)
-- note: it's possible that there are log updates in flight
-- even after the step has a status of finished, especially
-- for large logs. we get the most recent version of logs
-- on page load or when a step log is expanded, so potentially
-- seeing incomplete logs here is only a concern when someone
-- is following the logs live.
|> List.filter (\step -> step.finished == 0)
|> List.map
(\step ->
Effect.getBuildStepLog
Expand Down Expand Up @@ -546,17 +553,33 @@ update shared route msg model =

-- REFRESH
Tick options ->
let
isAnyStepRunning =
case model.steps of
RemoteData.Success steps ->
List.any (\s -> s.finished == 0) steps

_ ->
False

runEffect =
if isAnyStepRunning then
Effect.getBuildSteps
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetBuildStepsRefreshResponse
, pageNumber = Nothing
, perPage = Just 100
, org = route.params.org
, repo = route.params.repo
, build = route.params.build
}

else
Effect.none
in
( model
, Effect.getBuildSteps
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetBuildStepsRefreshResponse
, pageNumber = Nothing
, perPage = Just 100
, org = route.params.org
, repo = route.params.repo
, build = route.params.build
}
, runEffect
)


Expand Down
49 changes: 38 additions & 11 deletions src/elm/Pages/Org_/Repo_/Build_/Graph.elm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Http
import Http.Detailed
import Interop
import Layouts
import List.Extra
import Page exposing (Page)
import Pages.Account.Login exposing (Msg(..))
import RemoteData exposing (RemoteData(..), WebData)
Expand Down Expand Up @@ -237,6 +238,31 @@ update shared route msg model =
)

Refresh options ->
let
isBuildRunning =
case shared.builds of
RemoteData.Success builds ->
List.Extra.find (\b -> b.number == Maybe.withDefault 0 (String.toInt route.params.build)) builds
|> Maybe.map (\b -> b.finished == 0)
|> Maybe.withDefault False

_ ->
False

runEffect =
if isBuildRunning then
Effect.getBuildGraph
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetBuildGraphResponse { freshDraw = options.freshDraw }
, org = route.params.org
, repo = route.params.repo
, build = route.params.build
}

else
Effect.none
in
( { model
| graph =
if options.setToLoading then
Expand All @@ -246,14 +272,7 @@ update shared route msg model =
model.graph
}
, Effect.batch
[ Effect.getBuildGraph
{ baseUrl = shared.velaAPIBaseURL
, session = shared.session
, onResponse = GetBuildGraphResponse { freshDraw = options.freshDraw }
, org = route.params.org
, repo = route.params.repo
, build = route.params.build
}
[ runEffect
, if options.clear then
Effect.sendCmd clearBuildGraph

Expand Down Expand Up @@ -337,9 +356,16 @@ update shared route msg model =

-- REFRESH
Tick options ->
( model
, Effect.sendMsg <| Refresh { freshDraw = False, setToLoading = False, clear = False }
)
case options.interval of
Interval.FiveSeconds ->
( model
, Effect.sendMsg <| Refresh { freshDraw = False, setToLoading = False, clear = False }
)

Interval.OneSecond ->
( model
, Effect.sendCmd <| renderBuildGraph shared model { freshDraw = False }
)



Expand All @@ -357,6 +383,7 @@ subscriptions model =
-- on visiblity changed, same as shared
, Browser.Events.onVisibilityChange
(\visibility -> VisibilityChanged { visibility = visibility })
, Interval.tickEveryFiveSeconds Tick
, Interval.tickEveryOneSecond Tick
]

Expand Down
Loading
Loading