Skip to content
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
77 changes: 69 additions & 8 deletions app/application.ml
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,26 @@ let select_destination state destination =
;;

let feed_day_limit = 7
let startup_boot_time = Unix.gettimeofday ()
let startup_feed_load_time = ref None
let startup_timeline_presented = ref false

let log_startup_stage ~stage ?(extra = "") () =
let now = Unix.gettimeofday () in
let since_boot_ms = (now -. startup_boot_time) *. 1000. in
let since_feed_ms =
match !startup_feed_load_time with
| None -> ""
| Some started -> Printf.sprintf ",\"since_feed_start_ms\":%.2f" ((now -. started) *. 1000.)
in
(try
Printf.eprintf
"journal-startup: {\"stage\":%S,\"since_boot_ms\":%.2f%s%s}\n%!"
stage since_boot_ms since_feed_ms extra
with Sys_error _ -> ())
;;

let initial_feed_root_limit = 30
let feed_root_limit = 30
let sync_error_card_lifetime = 5.

Expand Down Expand Up @@ -918,7 +938,11 @@ let apply_manager_state ~discard state (manager_state : Graph_service.state) =
;;

let worker_request generation = function
| Journal_timeline_state.Feed { cursor } ->
| Journal_timeline_state.Feed { cursor = None } ->
Journal_graph_request.Load_feed
{ cursor = None; root_limit = initial_feed_root_limit; day_limit = feed_day_limit
; request_generation = generation }
| Journal_timeline_state.Feed { cursor = Some _ as cursor } ->
Journal_graph_request.Load_feed
{ cursor; root_limit = feed_root_limit; day_limit = feed_day_limit
; request_generation = generation }
Expand Down Expand Up @@ -1441,9 +1465,23 @@ let rec apply_worker_response ?(settling=false) ~discard state (response : Journ
~generation:request_generation
(Feed { cursor = None })
in
let next_feed_loaded = state.feed_loaded || complete in
if (not state.feed_loaded) && next_feed_loaded
then (
let day_count = List.length feed.Journal_graph_projection.days in
let entry_count =
List.fold_left
(fun acc (day : Journal_graph_projection.day_feed) -> acc + List.length day.entries)
0
feed.days
in
log_startup_stage
~stage:"app.feed_loaded"
~extra:(Printf.sprintf ",\"days\":%d,\"entries\":%d" day_count entry_count)
());
{ state with
timeline
; feed_loaded = state.feed_loaded || complete
; feed_loaded = next_feed_loaded
; presented_feed_context = Some refresh.context
; feed_refresh = (if complete then None else state.feed_refresh)
})
Expand All @@ -1470,9 +1508,23 @@ let rec apply_worker_response ?(settling=false) ~discard state (response : Journ
~generation:request_generation
(Feed { cursor = None })
in
let next_feed_loaded = state.feed_loaded || (accepted_initial_feed && complete) in
if (not state.feed_loaded) && next_feed_loaded
then (
let day_count = List.length feed.Journal_graph_projection.days in
let entry_count =
List.fold_left
(fun acc (day : Journal_graph_projection.day_feed) -> acc + List.length day.entries)
0
feed.days
in
log_startup_stage
~stage:"app.feed_loaded"
~extra:(Printf.sprintf ",\"days\":%d,\"entries\":%d" day_count entry_count)
());
{ state with
timeline
; feed_loaded = state.feed_loaded || (accepted_initial_feed && complete)
; feed_loaded = next_feed_loaded
; presented_feed_context =
(if accepted_initial_feed
then Option.map feed_projection_context state.calendar
Expand Down Expand Up @@ -5228,6 +5280,8 @@ let start
then Effect.ignore
else (
started_graph_generation := Some (graph_key, graph_state.generation);
startup_feed_load_time := Some (Unix.gettimeofday ());
log_startup_stage ~stage:"app.graph_open.load_feed_start" ();
retire_graph_scope ();
let current = !state_ref in
let graph_info = Journal_graph_runtime.start (!graph_runtime_cell) in
Expand All @@ -5243,7 +5297,7 @@ let start
~tags:(Timeline_filter.tags criteria) ~statuses:(Timeline_filter.statuses criteria) ~days;
submit
(Journal_graph_request.Load_feed
{ cursor = None; root_limit = feed_root_limit
{ cursor = None; root_limit = initial_feed_root_limit
; day_limit = feed_day_limit
; request_generation = feed_generation
})
Expand Down Expand Up @@ -6146,7 +6200,7 @@ let start
let output =
submit
(Journal_graph_request.Load_feed
{ cursor = None; root_limit = feed_root_limit
{ cursor = None; root_limit = initial_feed_root_limit
; day_limit = feed_day_limit
; request_generation = generation
})
Expand Down Expand Up @@ -6405,7 +6459,10 @@ let start
let output=Journal_graph_runtime.resume_insertion_recovery (!graph_runtime_cell) ~root recovery in
let delivery=deliver_output output in
Effect.run(apply_graph_responses set_state_and_effect delivery.responses)));
let startup_loading_key state = Journal_startup.Presentation.loading_request state.startup_presentation in
let startup_loading_key state =
if platform_code = 2 && Journal_startup.Presentation.initial_attempt state.startup_presentation
then None else Journal_startup.Presentation.loading_request state.startup_presentation
in
let prev_startup_loading_key = ref None in
let arm_startup_loading_timer key =
let started = Mtime_clock.counter () in
Expand Down Expand Up @@ -8372,8 +8429,11 @@ let start
~store:timeline_store ~media_store ~dispatch state
|> fun content -> V.of_lui (Lui_elements.stack ~grow:1.
~on_appear:(fun _ -> Option.iter (fun scope ->
if model_key !state_ref=Some(owner_key scope) then
Effect.run (send_manager (Graph_service.Acknowledge_timeline_presented scope))) (timeline_scope state))
if model_key !state_ref=Some(owner_key scope) then (
if not !startup_timeline_presented then (
startup_timeline_presented := true;
log_startup_stage ~stage:"app.timeline_presented" ());
Effect.run (send_manager (Graph_service.Acknowledge_timeline_presented scope)))) (timeline_scope state))
[Ui.mount content]))
in
let capture_adapter =
Expand Down Expand Up @@ -8836,6 +8896,7 @@ let start
if model_key state=Some source then (
if action="presented" then Option.iter (fun scope->
Effect.run(send_manager(Graph_service.Acknowledge_timeline_presented scope))) (timeline_scope state)
else if action="choose-graph" && show_graphs state then dispatch.send Switch_graph
else let target=member "target" event |> to_string in
match action with
| "cancel" when !preview_owner=Some target -> dispatch.send Graph_swipe_cancel
Expand Down
109 changes: 87 additions & 22 deletions app/journal_graph_feed_plan.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ type root_batch =
; root_next : Projection.root_cursor option
; root_members : Projection.tree_member Rrbvec.t
; root_proofs : proofs
; root_todo : Projection.tree_member Rrbvec.t
; mutable root_pending_previews : int
; mutable root_preview_results : Projection.tree_member Rrbvec.t array
; mutable root_preview_queue : (int * Projection.tree_member) list
; mutable root_batch_aborted : bool
}

type root_preview =
{ preview_batch : root_batch
; preview_index : int
; preview_root : Projection.tree_member
; preview_members : Projection.tree_member Rrbvec.t
; preview_cursor : Worker_pure_data.child_member Paging.cursor option
Expand Down Expand Up @@ -218,7 +222,8 @@ let root_owner_current t = function
;;

let root_batch_current t batch =
root_owner_current t batch.root_owner
(not batch.root_batch_aborted)
&& root_owner_current t batch.root_owner
&& Hashtbl.find_opt t.root_page_epochs batch.root_page.Projection.id
= Some batch.root_epoch
;;
Expand All @@ -230,6 +235,7 @@ let scan_failure scan message =
;;

let root_failure batch message =
batch.root_batch_aborted <- true;
match batch.root_owner with
| Feed_roots scan -> scan_failure scan message
| Changed_roots _ -> reject message
Expand Down Expand Up @@ -258,7 +264,10 @@ let make_root_batch t ~ports owner ~page_uuid (page : Projection.page) cursor li
; root_next = None
; root_members = Rrbvec.empty
; root_proofs = Hashtbl.create 8
; root_todo = Rrbvec.empty
; root_pending_previews = 0
; root_preview_results = [||]
; root_preview_queue = []
; root_batch_aborted = false
}
;;

Expand Down Expand Up @@ -745,22 +754,44 @@ let read_root_preview t ~ports preview =
request
;;

let next_root_preview t ~ports batch =
let max_concurrent_root_previews = 8

let start_root_previews t ~ports batch todo =
if not (root_batch_current t batch)
then empty
else (
match Rrbvec.pop_front batch.root_todo with
| None -> check t ~ports (Assemble batch)
| Some (root, rest) ->
let preview =
{ preview_batch = { batch with root_todo = rest }
; preview_root = root
; preview_members = Rrbvec.empty
; preview_cursor = None
; preview_limit = 3
}
let count = Rrbvec.length todo in
if count = 0
then check t ~ports (Assemble batch)
else (
batch.root_pending_previews <- count;
batch.root_preview_results <- Array.make count Rrbvec.empty;
let indexed =
Rrbvec.to_seq todo |> Seq.mapi (fun index root -> index, root) |> List.of_seq
in
let rec split n acc = function
| rest when n <= 0 -> List.rev acc, rest
| [] -> List.rev acc, []
| head :: tail -> split (n - 1) (head :: acc) tail
in
let initial, queued = split max_concurrent_root_previews [] indexed in
batch.root_preview_queue <- queued;
let planned =
List.map
(fun (index, root) ->
read_root_preview
t
~ports
{ preview_batch = batch
; preview_index = index
; preview_root = root
; preview_members = Rrbvec.empty
; preview_cursor = None
; preview_limit = 3
})
initial
in
requests [ read_root_preview t ~ports preview ])
requests planned))
;;

let start_feed_scan t ~ports ~cursor ~root_limit ~day_limit ~request_generation ?(reuse=false) ?after () =
Expand Down Expand Up @@ -857,8 +888,10 @@ let start_feed_scan t ~ports ~cursor ~root_limit ~day_limit ~request_generation
;;
let superseded_root_batch t ~ports batch =
match batch.root_owner with
| Feed_roots scan when root_owner_current t batch.root_owner
| Feed_roots scan when (not batch.root_batch_aborted)
&& root_owner_current t batch.root_owner
&& Hashtbl.mem t.root_page_limits batch.root_page.id ->
batch.root_batch_aborted <- true;
(match scan.root_start_day with
| None ->
(* A canonical local completion can invalidate a page while startup is
Expand Down Expand Up @@ -921,8 +954,8 @@ let receive_root_tree t ~ports batch (packet : Worker_pure_data.tree_member Pagi
Hashtbl.replace t.root_page_limits id (max batch.root_limit
(Option.value ~default:1 (Hashtbl.find_opt t.root_page_limits id)));
ports.retain_root_interest batch.root_page ~minimum_limit:batch.root_limit);
next_root_preview t ~ports
{batch with root_members=members;root_todo=todo;root_next=next_cursor})
start_root_previews t ~ports
{batch with root_members=members;root_next=next_cursor} todo)
;;

let receive_root_preview t ~ports preview (packet : Worker_pure_data.child_member Paging.packet) =
Expand Down Expand Up @@ -990,8 +1023,32 @@ let receive_root_preview t ~ports preview (packet : Worker_pure_data.child_membe
; preview_cursor = next_cursor
}
]
else
next_root_preview t ~ports { batch with root_members = Rrbvec.append batch.root_members selected }))
else (
batch.root_preview_results.(preview.preview_index) <- selected;
batch.root_pending_previews <- batch.root_pending_previews - 1;
if batch.root_pending_previews = 0
then (
let all_members =
Array.fold_left Rrbvec.append batch.root_members batch.root_preview_results
in
check t ~ports (Assemble { batch with root_members = all_members }))
else (
match batch.root_preview_queue with
| [] -> empty
| (next_index, next_root) :: rest ->
batch.root_preview_queue <- rest;
requests
[ read_root_preview
t
~ports
{ preview_batch = batch
; preview_index = next_index
; preview_root = next_root
; preview_members = Rrbvec.empty
; preview_cursor = None
; preview_limit = 3
}
]))))
;;

let stale_continuation scan =
Expand All @@ -1001,6 +1058,7 @@ let stale_continuation scan =
;;

let restart_batch t ~ports batch =
batch.root_batch_aborted <- true;
let proofs = owner_proofs batch.root_owner in
Hashtbl.filter_map_inplace (fun scope proof ->
if scope_page scope = Some batch.root_page.id then None else Some proof) proofs;
Expand Down Expand Up @@ -1183,7 +1241,12 @@ let failure_scope = function
| Root_dates (scan, _) | Root_today (scan, _) ->
(match scan.root_start_day with None -> Feed scan.root_generation
| Some day -> Day {day;request_generation=scan.root_generation})
| Root_tree batch | Root_preview {preview_batch=batch;_} ->
| Root_tree batch ->
(match batch.root_owner with Changed_roots _ -> Reconciliation
| Feed_roots scan -> match scan.root_start_day with None -> Feed scan.root_generation
| Some day -> Day {day;request_generation=scan.root_generation})
| Root_preview {preview_batch=batch;_} ->
batch.root_preview_queue <- [];
(match batch.root_owner with Changed_roots _ -> Reconciliation
| Feed_roots scan -> match scan.root_start_day with None -> Feed scan.root_generation
| Some day -> Day {day;request_generation=scan.root_generation})
Expand Down Expand Up @@ -1360,7 +1423,9 @@ let failed t ~ports operation ~code =
read_root_dates t ~ports scan
| Root_tree batch, Logseq_db_worker.Error.Response_too_large when batch.root_limit>1 ->
let limit=max 1 (batch.root_limit/2) in
(match batch.root_owner with Feed_roots scan->scan.root_page_size<-limit | _->());
(match batch.root_owner with
| Feed_roots scan -> scan.root_page_size <- limit
| _ -> ());
continue_root_batch t ~ports batch.root_owner batch.root_page batch.root_cursor limit
| Root_preview preview, Logseq_db_worker.Error.Response_too_large when preview.preview_limit>1 ->
requests [read_root_preview t ~ports {preview with preview_limit=max 1 (preview.preview_limit/2);
Expand Down
14 changes: 8 additions & 6 deletions app/journal_header.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ module Json = struct
type props =
{ mode : string; title : string option [@default None]; connecting : bool option [@default None]; controls : bool option [@default None]
; text : string option [@default None]; id : string option [@default None]; level : int option [@default None]
; loading : bool option [@default None]; initial : bool option [@default None]; active : bool option [@default None]; value : float option [@default None]; message : string option [@default None] }
; canChoose : bool option [@default None]; loading : bool option [@default None]; initial : bool option [@default None]; active : bool option [@default None]; value : float option [@default None]; message : string option [@default None] }
[@@deriving yojson]
end

let props ~mode ?title ?connecting ?controls ?text ?id ?level ?loading ?initial ?active ?value ?message () : Json.props =
{ mode; title; connecting; controls; text; id; level; loading; initial; active; value; message }
let props ~mode ?title ?connecting ?controls ?text ?id ?level ?canChoose ?loading ?initial ?active ?value ?message () : Json.props =
{ mode; title; connecting; controls; text; id; level; canChoose; loading; initial; active; value; message }
;;

let chrome =
Expand All @@ -39,6 +39,7 @@ let chrome =
match Journal_ids.Native_widget.Event_id.to_int event_id, Bytes.to_string bytes with
| 1, "journals" -> Ok `Journals
| 1, "favorites" -> Ok `Favorites
| 1, "choose-graph" -> Ok `Choose_graph
| 1, "capture" -> Ok `Capture
| _ -> Error "Unknown Journal chrome event")
()
Expand Down Expand Up @@ -80,11 +81,11 @@ let date_header ?relative_label ~title () =
()
;;

let startup ~loading ~initial ~active ?value ~message action =
let startup ?on_choose ~loading ~initial ~active ?value ~message action =
Ui.Native_widget.widget chrome
~key:(Ui.Key.string "journal-startup-surface")
~props:(props ~mode:"startup" ~loading ~initial ~active ~message ?value ())
~on_event:(fun _ -> ()) ~children:[V.column [action]] () |> V.Body.static
~props:(props ~mode:"startup" ~canChoose:(Option.is_some on_choose) ~loading ~initial ~active ~message ?value ())
~on_event:(function `Choose_graph -> Option.iter (fun choose -> choose ()) on_choose | _ -> ()) ~children:[V.column [action]] () |> V.Body.static
;;

let capture_control ~enabled ~on_capture =
Expand Down Expand Up @@ -480,6 +481,7 @@ let view_impl
in
match action with
| `Journals -> invoke on_journals
| `Choose_graph -> ()
| `Favorites -> invoke on_favorites
| `Capture ->
let current = Option.fold ~none:initial ~some:Signal.sample presentation_signal in
Expand Down
Loading
Loading