Skip to content

Commit 5a4b829

Browse files
authored
only display active pipelines by default (#6)
1 parent 7a8df21 commit 5a4b829

File tree

3 files changed

+101
-21
lines changed

3 files changed

+101
-21
lines changed

example/example.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ let main config mode =
243243
(Pipeline.v
244244
~source:{ src = Branch "main"; group = Project_B }
245245
~run:"b5233d0" ());
246+
Website.set_active_sources website
247+
(Current.return
248+
[
249+
{
250+
Metadata.Pipeline.Source.src = Pull_request 2;
251+
group = Project_A;
252+
};
253+
{
254+
Metadata.Pipeline.Source.src = Branch "main";
255+
group = Project_B;
256+
};
257+
]);
246258
])
247259
in
248260

src/web.ml

Lines changed: 85 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ module Make (R : Renderer) = struct
162162
String.compare (R.Pipeline.Source.id a) (R.Pipeline.Source.id b)
163163
end)
164164

165+
module SourceSet = Set.Make (struct
166+
type t = R.Pipeline.Source.t
167+
168+
let compare a b =
169+
String.compare (R.Pipeline.Source.id a) (R.Pipeline.Source.id b)
170+
end)
171+
165172
type pipeline_state =
166173
(R.Output.t, R.Node.t, R.Stage.t, R.Pipeline.t) State.pipeline
167174

@@ -178,7 +185,10 @@ module Make (R : Renderer) = struct
178185
pipeline_metadata )
179186
State.pipeline
180187

181-
type t = pipeline_state_internal StringMap.t SourceMap.t ref
188+
type t = {
189+
mutable runs : pipeline_state_internal StringMap.t SourceMap.t;
190+
mutable active : SourceSet.t;
191+
}
182192

183193
let unmarshal =
184194
State.unmarshal R.Output.unmarshal R.Node.unmarshal R.Stage.unmarshal
@@ -235,25 +245,25 @@ module Make (R : Renderer) = struct
235245
(k, List.to_seq pipelines |> StringMap.of_seq))
236246
|> List.to_seq |> SourceMap.of_seq
237247
in
238-
ref init_state
248+
{ runs = init_state; active = SourceSet.empty }
239249

240250
let get ~pipeline_source ~pipeline_id (state : t) =
241-
match SourceMap.find_opt pipeline_source !state with
251+
match SourceMap.find_opt pipeline_source state.runs with
242252
| None -> None
243253
| Some v -> StringMap.find_opt pipeline_id v
244254

245255
let set ~pipeline_source ~pipeline_id (state : t) new_state =
246-
match SourceMap.find_opt pipeline_source !state with
256+
match SourceMap.find_opt pipeline_source state.runs with
247257
| None ->
248-
state :=
258+
state.runs <-
249259
SourceMap.add pipeline_source
250260
(StringMap.singleton pipeline_id new_state)
251-
!state
261+
state.runs
252262
| Some v ->
253-
state :=
263+
state.runs <-
254264
SourceMap.add pipeline_source
255265
((StringMap.add pipeline_id new_state) v)
256-
!state
266+
state.runs
257267

258268
let update_state (state : t) (new_state : pipeline_state Current.t) =
259269
let open Current.Syntax in
@@ -286,6 +296,13 @@ module Make (R : Renderer) = struct
286296
{ v with metadata = { user_meta; run_time; creation_date } }
287297
in
288298
set ~pipeline_source ~pipeline_id state new_state
299+
300+
let set_active_sources (state : t)
301+
(active_sources : R.Pipeline.Source.t list Current.t) =
302+
let open Current.Syntax in
303+
let+ active_sources = active_sources in
304+
state.active <- SourceSet.of_list active_sources
305+
289306
(* RENDERING *)
290307

291308
let emoji_of_status =
@@ -313,8 +330,18 @@ module Make (R : Renderer) = struct
313330

314331
let node_map_status (node_meta, _) = R.Node.map_status node_meta
315332

316-
let list_pipelines ~(state : t) =
333+
let list_pipelines ctx ~(state : t) =
317334
let open Tyxml_html in
335+
let list_all_query_param =
336+
(* this query parameter controls if we list all the pipeline sources for a given group *)
337+
let uri = Current_web.Context.request ctx |> Cohttp.Request.uri in
338+
Uri.get_query_param uri "list_all"
339+
in
340+
let list_all =
341+
match list_all_query_param with
342+
| None -> fun _ -> false
343+
| Some v -> fun group_id -> R.Pipeline.Group.id group_id = v
344+
in
318345
let show_pipeline (pipeline : pipeline_state_internal) =
319346
let { user_meta; run_time; _ } = pipeline.metadata in
320347
let id = R.Pipeline.id user_meta in
@@ -342,25 +369,61 @@ module Make (R : Renderer) = struct
342369
show_pipeline latest
343370
in
344371
[ div [ R.render_index () ]; h2 [ txt "Pipelines" ] ]
345-
@ (SourceMap.bindings !state
372+
@ (SourceMap.bindings state.runs
346373
|> List.sort (fun (a, _) (b, _) -> R.Pipeline.Source.compare a b)
347374
|> List.map (fun (source, pipelines) ->
348375
(R.Pipeline.Source.group source, (source, pipelines)))
349376
|> group_by_key R.Pipeline.Group.id
350377
|> List.map (fun (group, sources) ->
351-
div
352-
[
353-
h3 [ txt (R.Pipeline.Group.to_string group) ];
354-
ul
355-
(List.map
356-
(fun (_, pipelines) -> li (render_source pipelines))
357-
sources);
358-
]))
378+
if list_all group then
379+
div
380+
[
381+
h3 [ txt (R.Pipeline.Group.to_string group) ];
382+
h4 [ i [ txt "active pipelines" ] ];
383+
ul
384+
(sources
385+
|> List.filter (fun (source, _) ->
386+
SourceSet.mem source state.active)
387+
|> List.map (fun (_, pipelines) ->
388+
li (render_source pipelines)));
389+
h4 [ i [ a ~a:[ a_href "?" ] [ txt "inactive pipelines" ] ] ];
390+
ul
391+
(sources
392+
|> List.filter (fun (source, _) ->
393+
not (SourceSet.mem source state.active))
394+
|> List.map (fun (_, pipelines) ->
395+
li (render_source pipelines)));
396+
]
397+
else
398+
div
399+
[
400+
h3 [ txt (R.Pipeline.Group.to_string group) ];
401+
h4 [ i [ txt "active pipelines" ] ];
402+
ul
403+
(sources
404+
|> List.filter (fun (source, _) ->
405+
SourceSet.mem source state.active)
406+
|> List.map (fun (_, pipelines) ->
407+
li (render_source pipelines)));
408+
h4
409+
[
410+
i
411+
[
412+
a
413+
~a:
414+
[
415+
a_href
416+
("?list_all=" ^ R.Pipeline.Group.id group);
417+
]
418+
[ txt "show inactive pipelines" ];
419+
];
420+
];
421+
]))
359422

360423
(* SHOW PIPELINES *)
361424
let find_pipeline ~(state : t) pipeline_source_id pipeline_id =
362425
let src, pipelines =
363-
SourceMap.bindings !state
426+
SourceMap.bindings state.runs
364427
|> List.find (fun (source, _) ->
365428
R.Pipeline.Source.id source = pipeline_source_id)
366429
in
@@ -442,7 +505,8 @@ module Make (R : Renderer) = struct
442505
br ();
443506
h2 [ txt "History:" ];
444507
ul
445-
(SourceMap.find src !state |> StringMap.bindings |> List.rev_map snd
508+
(SourceMap.find src state.runs
509+
|> StringMap.bindings |> List.rev_map snd
446510
|> List.sort
447511
(fun
448512
({ metadata = { creation_date = a; _ }; _ } :
@@ -683,7 +747,7 @@ module Make (R : Renderer) = struct
683747
let internal_get_routes ctx ~state =
684748
Routes.
685749
[
686-
nil @--> list_pipelines ~state;
750+
nil @--> list_pipelines ctx ~state;
687751
(str / str /? nil) @--> show_pipeline ctx ~state;
688752
(str / str / str /? nil) @--> show_pipeline_task ~state;
689753
(str / str / str /? wildcard) @--> show_pipeline_task_job ~state;

src/web.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ module Make (R : Renderer) : sig
7171

7272
val make : unit -> t
7373
val update_state : t -> pipeline_state Current.t -> unit Current.t
74+
75+
val set_active_sources :
76+
t -> R.Pipeline.Source.t list Current.t -> unit Current.t
77+
7478
val routes : t -> Current.Engine.t -> Current_web.Resource.t Routes.route list
7579
val pipeline_page_url : R.Pipeline.t -> string
7680
val pipeline_stage_url : R.Pipeline.t -> R.Stage.t -> string

0 commit comments

Comments
 (0)