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
3 changes: 2 additions & 1 deletion .agents/skills/journal-build/scripts/journal_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,8 @@ def interrupted(signum, frame):
void journal_ocaml_platform_response(const char *d, int32_t l) { (void)d; (void)l; }
void journal_ocaml_platform_failure(const char *d, int32_t l) { (void)d; (void)l; }
void journal_ocaml_set_wakeup_callback(wakeup_cb cb) { (void)cb; }
void journal_ocaml_set_platform_request_callback(platform_request_cb cb) { (void)cb; }"""
void journal_ocaml_set_platform_request_callback(platform_request_cb cb) { (void)cb; }
void journal_ocaml_set_filter_commit_callback(int32_t (*cb)(const char *, int32_t)) { (void)cb; }"""


if __name__ == "__main__":
Expand Down
397 changes: 142 additions & 255 deletions app/application.ml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions app/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
journal_validation
journal_calendar
journal_time
journal_filter_date
timeline_filter_preferences
journal_model
journal_environment
journal_graph_projection
journal_graph_request
journal_pure_copy)
(libraries
logseq_db_types
journal_timeline_filter
mldoc
ppx_deriving_yojson.runtime
threads
Expand Down Expand Up @@ -86,6 +89,7 @@
journal_worker_coordination_impl
logseq_journal.timeline_cursor
journal_timeline_cursor_impl
journal_timeline_filter_impl
threads
unix
ppx_deriving_yojson.runtime
Expand Down
5 changes: 5 additions & 0 deletions app/impl/timeline_filter/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name journal_timeline_filter_impl)
(implements journal_timeline_filter)
(modules timeline_filter)
(libraries logseq_db_types))
186 changes: 186 additions & 0 deletions app/impl/timeline_filter/timeline_filter.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
module Uuid = Logseq_db_types.Graph_types.Uuid

type tag_id = Uuid.t
type status_id = Uuid.t
type unit_ = Days | Weeks
type preset = Past_14_days | Past_3_months
type relative_time = [ `Preset of preset | `Custom of int * unit_ ]
type time_error = Invalid_count

let preset value = `Preset value
let default_time = preset Past_3_months

let custom ~count ~unit_ =
let multiplier = match unit_ with Days -> 1 | Weeks -> 7 in
if count <= 0 || count > max_int / multiplier
then Error Invalid_count
else Ok (`Custom (count, unit_))
;;

let time_choice time = time

type criteria = { tags : tag_id list; statuses : status_id list; time : relative_time }

let criteria ~tags ~statuses ~time =
{ tags = List.sort_uniq Uuid.compare tags; statuses = List.sort_uniq Uuid.compare statuses; time }
;;

let all = criteria ~tags:[] ~statuses:[] ~time:default_time
let tags criteria = criteria.tags
let statuses criteria = criteria.statuses
let time criteria = criteria.time

let active_count criteria =
Bool.to_int (criteria.tags <> [])
+ Bool.to_int (criteria.statuses <> [])
+ Bool.to_int (criteria.time <> default_time)
;;

type time_control = Preset of preset | Custom of { count_text : string; unit_ : unit_ }
type sheet = { draft_tags : tag_id list; draft_statuses : status_id list; draft_time : time_control }

type preferences = { definitions : (string * criteria) list; selected_name : string option }
type preference_error = Empty_name | Duplicate_name | Name_too_long | Too_many_filters | Too_many_selections | Unknown_selection

let empty_preferences = { definitions = []; selected_name = None }
let definitions preferences = preferences.definitions
let selected_name preferences = preferences.selected_name

let preferences ~definitions ~selected_name =
let rec validate seen = function
| [] ->
let definitions = List.rev seen in
if Option.exists (fun name -> not (List.mem_assoc name definitions)) selected_name
then Error Unknown_selection
else Ok { definitions; selected_name }
| (name, criteria) :: rest ->
let name = String.trim name in
if List.length seen = 5 then Error Too_many_filters
else if name = "" then Error Empty_name
else if String.length name > 128 || not (String.is_valid_utf_8 name) then Error Name_too_long
else if List.mem_assoc name seen then Error Duplicate_name
else if List.length criteria.tags > 100 || List.length criteria.statuses > 100
then Error Too_many_selections
else validate ((name, criteria) :: seen) rest
in
validate [] definitions
;;

type t = { applied : criteria; sheet : sheet option; preferences : preferences }

let restore preferences =
let applied = Option.bind preferences.selected_name (fun name -> List.assoc_opt name preferences.definitions) in
{ applied = Option.value applied ~default:all; sheet = None; preferences }
;;

type entry = Configure_directly | Native_menu of { names : string list; active_name : string option }

let entry state =
match state.preferences.definitions with
| [] -> Configure_directly
| definitions -> Native_menu { names = List.map fst definitions; active_name = state.preferences.selected_name }
;;

let applied state = state.applied
let sheet state = state.sheet
let draft_tags sheet = sheet.draft_tags
let draft_statuses sheet = sheet.draft_statuses
let draft_time sheet = sheet.draft_time

let validate_draft_time sheet =
match sheet.draft_time with
| Preset value -> Ok (preset value)
| Custom { count_text; unit_ } ->
if not (String.for_all (function '0' .. '9' -> true | _ -> false) count_text)
then Error Invalid_count
else (
match int_of_string_opt count_text with
| None -> Error Invalid_count
| Some count -> custom ~count ~unit_)
;;

let draft criteria =
let draft_time =
match time_choice criteria.time with
| `Preset value -> Preset value
| `Custom (count, unit_) -> Custom { count_text = string_of_int count; unit_ }
in
{ draft_tags = criteria.tags; draft_statuses = criteria.statuses; draft_time }
;;

let open_sheet state =
match state.sheet with Some _ -> state | None -> { state with sheet = Some (draft state.applied) }
;;

type edit = Toggle_tag of tag_id | Toggle_status of status_id | Set_time of time_control | Reset

let toggle id ids =
if List.exists (Uuid.equal id) ids
then List.filter (fun candidate -> not (Uuid.equal id candidate)) ids
else id :: ids
;;

let edit state change =
match state.sheet with
| None -> state
| Some sheet ->
let sheet =
match change with
| Toggle_tag id -> { sheet with draft_tags = toggle id sheet.draft_tags }
| Toggle_status id -> { sheet with draft_statuses = toggle id sheet.draft_statuses }
| Set_time draft_time -> { sheet with draft_time }
| Reset -> draft all
in
{ state with sheet = Some sheet }
;;

let cancel state = { state with sheet = None }

type command = Apply | Save_as of string | Select of string | Clear | Delete of string
type error = Invalid_time of time_error | Invalid_preferences of preference_error | No_open_sheet | Unknown_name
type transition = { next : t; replacement : preferences option }

let submit state command =
let previous = state.preferences in
let finish applied sheet preferences =
Ok { next = { applied; sheet; preferences }
; replacement = (if preferences = previous then None else Some preferences) }
in
match command with
| Clear -> finish all state.sheet { previous with selected_name = None }
| Select name ->
(match List.assoc_opt name previous.definitions with
| None -> Error Unknown_name
| Some applied -> finish applied state.sheet { previous with selected_name = Some name })
| Delete name ->
(match state.sheet with
| None -> Error No_open_sheet
| Some sheet ->
if not (List.mem_assoc name previous.definitions) then Error Unknown_name else
let definitions = List.remove_assoc name previous.definitions in
if previous.selected_name = Some name then
let unchanged =
List.sort Uuid.compare sheet.draft_tags = state.applied.tags
&& List.sort Uuid.compare sheet.draft_statuses = state.applied.statuses
&& sheet.draft_time = (draft state.applied).draft_time in
finish all (Some (if unchanged then draft all else sheet)) { definitions; selected_name = None }
else finish state.applied state.sheet { previous with definitions })
| Apply | Save_as _ ->
match state.sheet with
| None -> Error No_open_sheet
| Some sheet ->
match validate_draft_time sheet with
| Error error -> Error (Invalid_time error)
| Ok time ->
let applied = criteria ~tags:sheet.draft_tags ~statuses:sheet.draft_statuses ~time in
if List.length applied.tags > 100 || List.length applied.statuses > 100
then Error (Invalid_preferences Too_many_selections)
else match command with
| Apply -> finish applied None { previous with selected_name = None }
| Save_as name ->
let selected_name = Some (String.trim name) in
(match preferences ~definitions:(previous.definitions @ [name, applied]) ~selected_name with
| Ok preferences -> finish applied None preferences
| Error error -> Error (Invalid_preferences error))
| Clear | Select _ | Delete _ -> assert false
;;
1 change: 1 addition & 0 deletions app/impl/worker_coordination/worker_pure_coordinator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type _ kind =
let kind : type a. a Worker.query -> a kind = function
| Timeline_read{query=Worker.Timeline.Dates _;_}->Timeline_dates
| Timeline_read{query=Worker.Timeline.Roots _;_}->Timeline_roots
| Timeline_read{query=Worker.Timeline.Roots_through_grandchildren _;_}->Timeline_roots
| Timeline_read{query=Worker.Timeline.Preview_children _;_}->Timeline_preview
| Timeline_check _->Timeline_check
| Graph_info -> Info
Expand Down
2 changes: 2 additions & 0 deletions app/journal_bridge.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type hooks =
}

external wakeup : unit -> unit = "journal_ml_wakeup"
external commit_timeline_filter : string -> bool = "journal_ml_commit_timeline_filter"

external platform_request : string -> unit = "journal_ml_platform_request"

let current : hooks option ref = ref None
Expand Down
2 changes: 2 additions & 0 deletions app/journal_bridge.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ val picked : int -> string -> string
the app thread. *)
external wakeup : unit -> unit = "journal_ml_wakeup"

external commit_timeline_filter : string -> bool = "journal_ml_commit_timeline_filter"

(* Forwards one LJP2 request envelope to the host platform bridge (the host
answers asynchronously through [platform_response]). *)
external platform_request : string -> unit = "journal_ml_platform_request"
Expand Down
36 changes: 36 additions & 0 deletions app/journal_filter_date.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
open Journal_timeline_filter.Timeline_filter

let resolve_days criteria ~today =
let calendar_months () =
let month_index = (today / 10000) * 12 + today / 100 mod 100 - 1 - 3 in
if month_index < 12 then invalid_arg "Filter range starts before year 1";
let year = month_index / 12 and month = month_index mod 12 + 1 in
let last_day = match month with
| 2 -> if year mod 4 = 0 && (year mod 100 <> 0 || year mod 400 = 0) then 29 else 28
| 4 | 6 | 9 | 11 -> 30
| _ -> 31 in
Some (year * 10000 + month * 100 + min (today mod 100) last_day, today)
in
let days count =
let civil day =
let year = day / 10000 and month = day / 100 mod 100 in
let year = if month <= 2 then year - 1 else year in
let era = year / 400 in
let year_of_era = year mod 400 in
let month = if month <= 2 then month + 9 else month - 3 in
era * 146097 + year_of_era * 365 + year_of_era / 4 - year_of_era / 100
+ (153 * month + 2) / 5 + day mod 100 - 1 - 719468
in
let last = civil today in
let offset = count - 1 in
if offset > last - civil 10101 then invalid_arg "Filter range starts before year 1";
let earliest = last - offset in
let start = Unix.gmtime (float_of_int earliest *. 86400.) in
Some ((start.tm_year + 1900) * 10000 + (start.tm_mon + 1) * 100 + start.tm_mday, today)
in
match time_choice (time criteria) with
| `Preset Past_3_months -> calendar_months ()
| `Preset Past_14_days -> days 14
| `Custom (count, Days) -> days count
| `Custom (count, Weeks) -> days (7 * count)
;;
Loading
Loading