Skip to content

Commit

Permalink
refactor: build graph implementation with auto refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Jan 25, 2024
1 parent 1da9ac1 commit 19de625
Show file tree
Hide file tree
Showing 9 changed files with 1,220 additions and 631 deletions.
17 changes: 9 additions & 8 deletions src/elm/Components/Logs.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type alias Props msg =
, buildNumber : String
, resourceType : String
, resourceNumber : String
, lineFocus : Maybe LogFocus
, focus : Focus.Focus
, follow : Int
}

Expand All @@ -41,10 +41,6 @@ type alias LogLine msg =
}


type alias LogFocus =
( Maybe Int, Maybe Int )


view : Shared.Model -> Props msg -> Html msg
view shared props =
case props.log of
Expand Down Expand Up @@ -146,16 +142,21 @@ viewLine shared props logLine lineNumber =
[ div
[ class "wrapper"
, Util.testAttribute <| String.join "-" [ "log", "line", props.resourceType, props.resourceNumber, String.fromInt lineNumber ]
, class <| Focus.lineFocusStyles props.lineFocus lineNumber
, class <| Focus.lineRangeStyles props.focus lineNumber
]
[ td []
[ button
[ Util.onClickPreventDefault <|
props.msgs.pushUrlHash
{ hash = Focus.resourceLineRangeId props.resourceType props.resourceNumber lineNumber props.lineFocus shared.shift
{ hash =
Focus.toString <| Focus.updateLineRange shared props.focus lineNumber
}
, Util.testAttribute <| String.join "-" [ "log", "line", "num", props.resourceType, props.resourceNumber, String.fromInt lineNumber ]
, id <| Focus.resourceAndLineToFocusId props.resourceType props.resourceNumber lineNumber
, Focus.toAttr
{ group = String.toInt props.resourceNumber
, a = Just lineNumber
, b = Nothing
}
, class "line-number"
, class "button"
, class "-link"
Expand Down
22 changes: 21 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, addOrgSecret, addRepoSecret, alertsUpdate, clearRedirect, downloadFile, enableRepo, expandPipelineConfig, finishAuthentication, focusOn, getBuild, getBuildServiceLog, getBuildServices, getBuildStepLog, getBuildSteps, getCurrentUser, getOrgBuilds, getOrgRepos, getOrgSecret, getOrgSecrets, getPipelineConfig, getPipelineTemplates, getRepo, getRepoBuilds, getRepoBuildsShared, getRepoDeployments, getRepoHooks, getRepoHooksShared, getRepoSchedules, getRepoSecret, getRepoSecrets, getSharedSecrets, handleHttpError, logout, pushPath, redeliverHook, sendSharedRepoBuildsResponse, sendSharedRepoHooksResponse, setRedirect, setTheme, updateFavorites, updateOrgSecret, updateRepoSecret
, addAlertError, addAlertSuccess, addDeployment, addOrgSecret, addRepoSecret, alertsUpdate, clearRedirect, downloadFile, enableRepo, expandPipelineConfig, finishAuthentication, focusOn, getBuild, getBuildGraph, getBuildServiceLog, getBuildServices, getBuildStepLog, getBuildSteps, getCurrentUser, getOrgBuilds, getOrgRepos, getOrgSecret, getOrgSecrets, getPipelineConfig, getPipelineTemplates, getRepo, getRepoBuilds, getRepoBuildsShared, getRepoDeployments, getRepoHooks, getRepoHooksShared, getRepoSchedules, getRepoSecret, getRepoSecrets, getSharedSecrets, handleHttpError, logout, pushPath, redeliverHook, sendSharedRepoBuildsResponse, sendSharedRepoHooksResponse, setRedirect, setTheme, updateFavorites, updateOrgSecret, updateRepoSecret
)

{-|
Expand Down Expand Up @@ -753,6 +753,26 @@ getBuildServiceLog options =
|> sendCmd


getBuildGraph :
{ baseUrl : String
, session : Auth.Session.Session
, onResponse : Result (Http.Detailed.Error String) ( Http.Metadata, Vela.BuildGraph ) -> msg
, org : String
, repo : String
, buildNumber : String
}
-> Effect msg
getBuildGraph options =
Api.try
options.onResponse
(Api.Operations.getBuildGraph
options.baseUrl
options.session
options
)
|> sendCmd


getPipelineConfig :
{ baseUrl : String
, session : Auth.Session.Session
Expand Down
103 changes: 52 additions & 51 deletions src/elm/Pages/Org_/Repo_/Build_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Http
import Http.Detailed
import Layouts
import List.Extra
import Maybe.Extra
import Page exposing (Page)
import RemoteData exposing (WebData)
import Route exposing (Route)
Expand Down Expand Up @@ -72,7 +73,7 @@ toLayout user route model =
type alias Model =
{ steps : WebData (List Vela.Step)
, logs : Dict Int (WebData Vela.Log)
, logLineFocus : ( Maybe Int, ( Maybe Int, Maybe Int ) )
, focus : Focus.Focus
, logFollow : Int
}

Expand All @@ -81,18 +82,15 @@ init : Shared.Model -> Route { org : String, repo : String, buildNumber : String
init shared route () =
( { steps = RemoteData.Loading
, logs = Dict.empty
, logLineFocus =
route.hash
|> Focus.parseResourceFocusTargetFromFragment
|> (\ft -> ( ft.resourceNumber, ( ft.lineA, ft.lineB ) ))
, focus = Focus.fromString route.hash
, logFollow = 0
}
, Effect.getBuildSteps
{ baseUrl = shared.velaAPI
, session = shared.session
, onResponse = GetBuildStepsResponse
, pageNumber = Nothing
, perPage = Nothing
, perPage = Just 100
, org = route.params.org
, repo = route.params.repo
, buildNumber = route.params.buildNumber
Expand Down Expand Up @@ -125,31 +123,21 @@ update : Shared.Model -> Route { org : String, repo : String, buildNumber : Stri
update shared route msg model =
case msg of
-- BROWSER
OnHashChanged _ ->
( { model
| logLineFocus =
route.hash
|> Focus.parseResourceFocusTargetFromFragment
|> (\ft -> ( ft.resourceNumber, ( ft.lineA, ft.lineB ) ))
}
, case model.steps of
RemoteData.Success steps ->
let
resourceNumber =
route.hash
|> Focus.parseResourceFocusTargetFromFragment
|> (\ft -> ( ft.resourceNumber, ( ft.lineA, ft.lineB ) ))
|> Tuple.first
|> Maybe.withDefault -1
in
steps
|> List.filter (\s -> resourceNumber == s.number)
|> List.map (\s -> ExpandStep { step = s, updateUrlHash = False })
|> List.map Effect.sendMsg
|> Effect.batch

_ ->
Effect.none
OnHashChanged options ->
let
focus =
Focus.fromString route.hash
in
( { model | focus = focus }
, model.steps
|> RemoteData.unwrap Effect.none
(\steps ->
steps
|> List.Extra.find (\s -> s.number == Maybe.withDefault -1 focus.group)
|> Maybe.map (\s -> ExpandStep { step = s, updateUrlHash = False })
|> Maybe.map Effect.sendMsg
|> Maybe.withDefault Effect.none
)
)

PushUrlHash options ->
Expand Down Expand Up @@ -178,12 +166,19 @@ update shared route msg model =
steps
|> List.map
(\step ->
case model.logLineFocus of
( Just resourceNumber, _ ) ->
case model.focus.group of
Just resourceNumber ->
if step.number == resourceNumber then
( { step | viewing = True }
, ExpandStep { step = step, updateUrlHash = False }
|> Effect.sendMsg
, Effect.batch
[ ExpandStep { step = step, updateUrlHash = False }
|> Effect.sendMsg
, FocusOn
{ target =
Focus.toDomTarget model.focus
}
|> Effect.sendMsg
]
)

else
Expand Down Expand Up @@ -211,17 +206,18 @@ update shared route msg model =
GetBuildStepLogResponse step response ->
case response of
Ok ( _, log ) ->
let
logs =
( { model
| logs =
Dict.update step.id
(Components.Logs.safeDecodeLogData shared.velaLogBytesLimit log)
model.logs
in
( { model | logs = logs }
, model.logLineFocus
|> Focus.resourceLineFocusToFocusId "step"
|> (\t -> FocusOn { target = t })
|> Effect.sendMsg
}
, if Focus.canTarget model.focus then
FocusOn { target = Focus.toDomTarget model.focus }
|> Effect.sendMsg

else
Effect.none
)

Err error ->
Expand Down Expand Up @@ -262,7 +258,13 @@ update shared route msg model =
, buildNumber = route.params.buildNumber
}
, query = route.query
, hash = Just <| Focus.resourceFocusId "step" (String.fromInt options.step.number)
, hash =
Just <|
Focus.toString
{ group = Just options.step.number
, a = Nothing
, b = Nothing
}
}

else
Expand Down Expand Up @@ -415,7 +417,11 @@ viewStep shared model route step =
[ class "summary"
, Util.testAttribute <| "step-header-" ++ stepNumber
, onClick <| clickStep { step = step, updateUrlHash = True }
, id <| "step-" ++ stepNumber
, Focus.toAttr
{ group = Just step.number
, a = Nothing
, b = Nothing
}
]
[ div
[ class "-info" ]
Expand Down Expand Up @@ -467,11 +473,6 @@ viewLogs shared model route step log =
, buildNumber = route.params.buildNumber
, resourceType = "step"
, resourceNumber = String.fromInt step.number
, lineFocus =
if step.number == Maybe.withDefault -1 (Tuple.first model.logLineFocus) then
Just <| Tuple.second model.logLineFocus

else
Nothing
, focus = model.focus
, follow = model.logFollow
}
Loading

0 comments on commit 19de625

Please sign in to comment.