paper_trail_diff 0.16.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +566 -1
- data/README.md +42 -7
- data/docs/architecture.md +7 -5
- data/docs/performance.md +86 -38
- data/docs/reference.md +309 -92
- data/docs/testing.md +240 -32
- data/lib/paper_trail_diff/activity_belongs_to_event_applier.rb +2 -16
- data/lib/paper_trail_diff/activity_boundary.rb +55 -34
- data/lib/paper_trail_diff/activity_child_candidate_loader.rb +46 -57
- data/lib/paper_trail_diff/activity_collection_event_applier.rb +3 -13
- data/lib/paper_trail_diff/activity_collection_route_updater.rb +1 -1
- data/lib/paper_trail_diff/activity_event.rb +33 -21
- data/lib/paper_trail_diff/activity_event_record_resolver.rb +16 -116
- data/lib/paper_trail_diff/activity_event_route_finder.rb +29 -29
- data/lib/paper_trail_diff/activity_event_snapshot_refresher.rb +2 -7
- data/lib/paper_trail_diff/activity_history.rb +90 -49
- data/lib/paper_trail_diff/activity_range.rb +9 -35
- data/lib/paper_trail_diff/activity_root_snapshot_refresher.rb +74 -40
- data/lib/paper_trail_diff/activity_root_steps.rb +10 -13
- data/lib/paper_trail_diff/activity_scope_filter.rb +77 -0
- data/lib/paper_trail_diff/activity_snapshot_delta.rb +12 -15
- data/lib/paper_trail_diff/activity_snapshot_sequence.rb +37 -15
- data/lib/paper_trail_diff/activity_target_candidate_loader.rb +35 -43
- data/lib/paper_trail_diff/activity_timeline_builder.rb +194 -249
- data/lib/paper_trail_diff/activity_transaction_grouper.rb +3 -1
- data/lib/paper_trail_diff/activity_tree_filter.rb +54 -13
- data/lib/paper_trail_diff/activity_version_collector.rb +72 -138
- data/lib/paper_trail_diff/activity_view.rb +22 -11
- data/lib/paper_trail_diff/analysis.rb +19 -5
- data/lib/paper_trail_diff/analysis_batch.rb +16 -58
- data/lib/paper_trail_diff/approximate_paths.rb +4 -8
- data/lib/paper_trail_diff/association_activity_edge.rb +5 -24
- data/lib/paper_trail_diff/association_activity_selection.rb +10 -18
- data/lib/paper_trail_diff/association_discovery.rb +1 -6
- data/lib/paper_trail_diff/association_key.rb +9 -6
- data/lib/paper_trail_diff/association_traversal.rb +49 -6
- data/lib/paper_trail_diff/batch_boundary_resolver.rb +33 -42
- data/lib/paper_trail_diff/batch_preload.rb +2 -12
- data/lib/paper_trail_diff/batched_root_analyses.rb +72 -0
- data/lib/paper_trail_diff/batched_root_analyzer.rb +16 -14
- data/lib/paper_trail_diff/batched_root_versions.rb +25 -20
- data/lib/paper_trail_diff/branch_snapshot_refresher.rb +27 -31
- data/lib/paper_trail_diff/change_attribution.rb +314 -0
- data/lib/paper_trail_diff/change_row.rb +30 -10
- data/lib/paper_trail_diff/comparison_batch.rb +3 -4
- data/lib/paper_trail_diff/configuration.rb +1 -1
- data/lib/paper_trail_diff/deleted_analysis_batch.rb +22 -37
- data/lib/paper_trail_diff/deleted_root_filter.rb +3 -6
- data/lib/paper_trail_diff/deprecation.rb +4 -43
- data/lib/paper_trail_diff/diagnostics.rb +1 -2
- data/lib/paper_trail_diff/direct_record_state_loader.rb +8 -1
- data/lib/paper_trail_diff/endpoint.rb +78 -44
- data/lib/paper_trail_diff/errors.rb +2 -9
- data/lib/paper_trail_diff/fallback_paths.rb +2 -5
- data/lib/paper_trail_diff/habtm_transaction_owners.rb +5 -3
- data/lib/paper_trail_diff/historical_association_reifier.rb +3 -1
- data/lib/paper_trail_diff/historical_filter.rb +40 -15
- data/lib/paper_trail_diff/historical_snapshot_store.rb +80 -38
- data/lib/paper_trail_diff/instrumentation.rb +0 -8
- data/lib/paper_trail_diff/live_endpoint_batch_loader.rb +11 -7
- data/lib/paper_trail_diff/live_endpoint_provider.rb +7 -23
- data/lib/paper_trail_diff/live_graph_collector.rb +3 -3
- data/lib/paper_trail_diff/nested_comparator.rb +46 -19
- data/lib/paper_trail_diff/net_change.rb +276 -0
- data/lib/paper_trail_diff/own_window.rb +35 -0
- data/lib/paper_trail_diff/paper_trail_adapter.rb +40 -45
- data/lib/paper_trail_diff/prepared_activity_index.rb +170 -42
- data/lib/paper_trail_diff/prepared_child_candidates.rb +58 -0
- data/lib/paper_trail_diff/prepared_edge_loader.rb +43 -107
- data/lib/paper_trail_diff/prepared_history.rb +20 -31
- data/lib/paper_trail_diff/prepared_history_loader.rb +46 -9
- data/lib/paper_trail_diff/prepared_record_index.rb +137 -47
- data/lib/paper_trail_diff/prepared_snapshot_builder.rb +16 -0
- data/lib/paper_trail_diff/prepared_state_association_reader.rb +17 -19
- data/lib/paper_trail_diff/read_snapshot.rb +41 -19
- data/lib/paper_trail_diff/recorded_changes.rb +10 -11
- data/lib/paper_trail_diff/recorded_changes_reader.rb +60 -0
- data/lib/paper_trail_diff/recorded_class.rb +14 -19
- data/lib/paper_trail_diff/recorded_columns.rb +33 -0
- data/lib/paper_trail_diff/root_version_plan.rb +20 -22
- data/lib/paper_trail_diff/root_version_selection.rb +39 -29
- data/lib/paper_trail_diff/scope_condition.rb +7 -17
- data/lib/paper_trail_diff/scope_ranking.rb +16 -22
- data/lib/paper_trail_diff/scoped_analysis.rb +27 -4
- data/lib/paper_trail_diff/scoped_analysis_builder.rb +4 -3
- data/lib/paper_trail_diff/scoped_root_selection.rb +60 -41
- data/lib/paper_trail_diff/snapshot_normalizer.rb +18 -3
- data/lib/paper_trail_diff/stored_json_values.rb +52 -0
- data/lib/paper_trail_diff/stored_time_precision.rb +30 -18
- data/lib/paper_trail_diff/stored_values.rb +20 -0
- data/lib/paper_trail_diff/support.rb +185 -35
- data/lib/paper_trail_diff/time_of_day.rb +39 -0
- data/lib/paper_trail_diff/time_range.rb +37 -20
- data/lib/paper_trail_diff/time_version_range.rb +5 -10
- data/lib/paper_trail_diff/timeline_builder.rb +3 -3
- data/lib/paper_trail_diff/timeline_range.rb +18 -12
- data/lib/paper_trail_diff/timeline_snapshot_provider.rb +1 -1
- data/lib/paper_trail_diff/total_order.rb +55 -0
- data/lib/paper_trail_diff/transaction_cut.rb +192 -91
- data/lib/paper_trail_diff/traversal_preparer.rb +5 -8
- data/lib/paper_trail_diff/value_objects.rb +8 -4
- data/lib/paper_trail_diff/version.rb +1 -1
- data/lib/paper_trail_diff/version_association_candidate_scope.rb +1 -12
- data/lib/paper_trail_diff/version_boundary.rb +37 -21
- data/lib/paper_trail_diff/version_query.rb +25 -25
- data/lib/paper_trail_diff/version_range.rb +12 -16
- data/lib/paper_trail_diff/version_sequence_diagnostics.rb +32 -0
- data/lib/paper_trail_diff/version_table.rb +12 -8
- data/lib/paper_trail_diff.rb +28 -42
- data/sig/generated/paper_trail_diff/activity_belongs_to_event_applier.rbs +0 -8
- data/sig/generated/paper_trail_diff/activity_boundary.rbs +39 -17
- data/sig/generated/paper_trail_diff/activity_child_candidate_loader.rbs +19 -22
- data/sig/generated/paper_trail_diff/activity_collection_event_applier.rbs +0 -5
- data/sig/generated/paper_trail_diff/activity_event.rbs +25 -9
- data/sig/generated/paper_trail_diff/activity_event_record_resolver.rbs +6 -34
- data/sig/generated/paper_trail_diff/activity_event_route_finder.rbs +11 -4
- data/sig/generated/paper_trail_diff/activity_event_snapshot_refresher.rbs +0 -3
- data/sig/generated/paper_trail_diff/activity_history.rbs +54 -19
- data/sig/generated/paper_trail_diff/activity_range.rbs +6 -20
- data/sig/generated/paper_trail_diff/activity_root_snapshot_refresher.rbs +36 -2
- data/sig/generated/paper_trail_diff/activity_root_steps.rbs +6 -6
- data/sig/generated/paper_trail_diff/activity_scope_filter.rbs +51 -0
- data/sig/generated/paper_trail_diff/activity_snapshot_delta.rbs +5 -3
- data/sig/generated/paper_trail_diff/activity_snapshot_sequence.rbs +21 -3
- data/sig/generated/paper_trail_diff/activity_target_candidate_loader.rbs +19 -24
- data/sig/generated/paper_trail_diff/activity_timeline_builder.rbs +121 -127
- data/sig/generated/paper_trail_diff/activity_tree_filter.rbs +41 -8
- data/sig/generated/paper_trail_diff/activity_version_collector.rbs +42 -35
- data/sig/generated/paper_trail_diff/activity_view.rbs +16 -8
- data/sig/generated/paper_trail_diff/analysis.rbs +15 -6
- data/sig/generated/paper_trail_diff/analysis_batch.rbs +6 -24
- data/sig/generated/paper_trail_diff/association_activity_edge.rbs +0 -13
- data/sig/generated/paper_trail_diff/association_activity_selection.rbs +6 -9
- data/sig/generated/paper_trail_diff/association_discovery.rbs +0 -3
- data/sig/generated/paper_trail_diff/association_key.rbs +5 -0
- data/sig/generated/paper_trail_diff/association_traversal.rbs +26 -2
- data/sig/generated/paper_trail_diff/batch_boundary_resolver.rbs +17 -17
- data/sig/generated/paper_trail_diff/batch_preload.rbs +0 -4
- data/sig/generated/paper_trail_diff/batched_root_analyses.rbs +47 -0
- data/sig/generated/paper_trail_diff/batched_root_analyzer.rbs +9 -8
- data/sig/generated/paper_trail_diff/batched_root_versions.rbs +10 -6
- data/sig/generated/paper_trail_diff/branch_snapshot_refresher.rbs +11 -5
- data/sig/generated/paper_trail_diff/change_attribution.rbs +185 -0
- data/sig/generated/paper_trail_diff/change_row.rbs +22 -7
- data/sig/generated/paper_trail_diff/deleted_analysis_batch.rbs +11 -12
- data/sig/generated/paper_trail_diff/deleted_root_filter.rbs +0 -2
- data/sig/generated/paper_trail_diff/deprecation.rbs +4 -24
- data/sig/generated/paper_trail_diff/direct_record_state_loader.rbs +4 -0
- data/sig/generated/paper_trail_diff/endpoint.rbs +40 -16
- data/sig/generated/paper_trail_diff/errors.rbs +2 -11
- data/sig/generated/paper_trail_diff/habtm_transaction_owners.rbs +2 -1
- data/sig/generated/paper_trail_diff/historical_filter.rbs +20 -9
- data/sig/generated/paper_trail_diff/historical_snapshot_store.rbs +40 -10
- data/sig/generated/paper_trail_diff/instrumentation.rbs +0 -3
- data/sig/generated/paper_trail_diff/live_endpoint_batch_loader.rbs +3 -0
- data/sig/generated/paper_trail_diff/live_endpoint_provider.rbs +5 -8
- data/sig/generated/paper_trail_diff/live_graph_collector.rbs +2 -2
- data/sig/generated/paper_trail_diff/nested_comparator.rbs +20 -4
- data/sig/generated/paper_trail_diff/net_change.rbs +168 -0
- data/sig/generated/paper_trail_diff/own_window.rbs +22 -0
- data/sig/generated/paper_trail_diff/paper_trail_adapter.rbs +9 -6
- data/sig/generated/paper_trail_diff/prepared_activity_index.rbs +103 -14
- data/sig/generated/paper_trail_diff/prepared_child_candidates.rbs +39 -0
- data/sig/generated/paper_trail_diff/prepared_edge_loader.rbs +18 -35
- data/sig/generated/paper_trail_diff/prepared_history.rbs +9 -12
- data/sig/generated/paper_trail_diff/prepared_history_loader.rbs +28 -3
- data/sig/generated/paper_trail_diff/prepared_record_index.rbs +68 -14
- data/sig/generated/paper_trail_diff/prepared_snapshot_builder.rbs +12 -0
- data/sig/generated/paper_trail_diff/prepared_state_association_reader.rbs +6 -7
- data/sig/generated/paper_trail_diff/read_snapshot.rbs +27 -8
- data/sig/generated/paper_trail_diff/recorded_changes.rbs +4 -2
- data/sig/generated/paper_trail_diff/recorded_changes_reader.rbs +41 -0
- data/sig/generated/paper_trail_diff/recorded_class.rbs +7 -6
- data/sig/generated/paper_trail_diff/recorded_columns.rbs +21 -0
- data/sig/generated/paper_trail_diff/root_version_plan.rbs +8 -4
- data/sig/generated/paper_trail_diff/root_version_selection.rbs +27 -13
- data/sig/generated/paper_trail_diff/scope_condition.rbs +0 -2
- data/sig/generated/paper_trail_diff/scope_ranking.rbs +10 -10
- data/sig/generated/paper_trail_diff/scoped_analysis.rbs +19 -0
- data/sig/generated/paper_trail_diff/scoped_analysis_builder.rbs +2 -2
- data/sig/generated/paper_trail_diff/scoped_root_selection.rbs +37 -14
- data/sig/generated/paper_trail_diff/snapshot_normalizer.rbs +7 -0
- data/sig/generated/paper_trail_diff/stored_json_values.rbs +35 -0
- data/sig/generated/paper_trail_diff/stored_time_precision.rbs +19 -5
- data/sig/generated/paper_trail_diff/stored_values.rbs +13 -0
- data/sig/generated/paper_trail_diff/support.rbs +120 -23
- data/sig/generated/paper_trail_diff/time_of_day.rbs +21 -0
- data/sig/generated/paper_trail_diff/time_range.rbs +15 -8
- data/sig/generated/paper_trail_diff/time_version_range.rbs +2 -5
- data/sig/generated/paper_trail_diff/timeline_builder.rbs +2 -2
- data/sig/generated/paper_trail_diff/timeline_range.rbs +12 -7
- data/sig/generated/paper_trail_diff/total_order.rbs +32 -0
- data/sig/generated/paper_trail_diff/transaction_cut.rbs +111 -52
- data/sig/generated/paper_trail_diff/traversal_preparer.rbs +2 -2
- data/sig/generated/paper_trail_diff/value_objects.rbs +5 -1
- data/sig/generated/paper_trail_diff/version_association_candidate_scope.rbs +0 -7
- data/sig/generated/paper_trail_diff/version_boundary.rbs +15 -8
- data/sig/generated/paper_trail_diff/version_query.rbs +17 -10
- data/sig/generated/paper_trail_diff/version_range.rbs +6 -9
- data/sig/generated/paper_trail_diff/version_sequence_diagnostics.rbs +13 -0
- data/sig/generated/paper_trail_diff/version_table.rbs +3 -0
- data/sig/generated/paper_trail_diff.rbs +16 -16
- metadata +28 -6
- data/lib/paper_trail_diff/preloaded_endpoint_batch_loader.rb +0 -60
- data/sig/generated/paper_trail_diff/preloaded_endpoint_batch_loader.rbs +0 -32
|
@@ -15,19 +15,36 @@ module PaperTrailDiff
|
|
|
15
15
|
# PT-AT reads associations as they were before a transaction, so a record
|
|
16
16
|
# is judged by two snapshots: the one at its transaction's first event, and
|
|
17
17
|
# the one after its last, which is the next event's or what the range
|
|
18
|
-
# closes on. A
|
|
18
|
+
# closes on. A root version inside the transaction is the exception: it
|
|
19
|
+
# reads the has_and_belongs_to_many members it records, including a record
|
|
20
|
+
# the transaction created before it, so the snapshots at the transaction's
|
|
21
|
+
# root events are read too. A tag created, attached and detached again
|
|
22
|
+
# before the next root version is in the tree only there. A record in none
|
|
23
|
+
# of these was outside the tree throughout.
|
|
19
24
|
#
|
|
20
|
-
# The root's own events are always kept, and so
|
|
21
|
-
#
|
|
22
|
-
# step is
|
|
25
|
+
# The root's own events are always kept, and so is an event whose step is
|
|
26
|
+
# not reported, which only bounds one that is, unless its transaction has an
|
|
27
|
+
# event whose step is: the transaction crossed the window's end. A version
|
|
28
|
+
# it saved after the edge reads each record as it stood before the
|
|
29
|
+
# transaction, so a step ending there shows none of its changes. Judged
|
|
30
|
+
# like the rest, as the window after the edge judges it, the event bounds
|
|
31
|
+
# that step when the later window reports the transaction; left out, the
|
|
32
|
+
# step runs past it and reports the changes here instead (see
|
|
33
|
+
# ActivityHistoryBuilder#consume). A snapshot does not hold a through's join
|
|
34
|
+
# rows, so their versions are judged when they are collected, by the row's
|
|
35
|
+
# own state (PreparedActivityIndex#join_versions), and kept here.
|
|
23
36
|
class ActivityTreeFilter
|
|
24
|
-
# `
|
|
25
|
-
#
|
|
37
|
+
# `reported` says whether an event's step is reported. `closing` is the
|
|
38
|
+
# snapshot after the last event: the current record's, or nil when
|
|
39
|
+
# nothing follows, as after the root's destruction.
|
|
26
40
|
#: (Array[ActivityEvent], reported: untyped, closing: RecordSnapshot?) -> void
|
|
27
41
|
def initialize(events, reported:, closing:)
|
|
28
42
|
@reported = reported
|
|
29
43
|
@closing = closing
|
|
30
|
-
@
|
|
44
|
+
@keys = transaction_keys(events)
|
|
45
|
+
@starts, @ends = spans(@keys)
|
|
46
|
+
@roots = root_positions(events)
|
|
47
|
+
@judged = reported_transactions(events)
|
|
31
48
|
@types = {} #: Hash[String, Array[String?]]
|
|
32
49
|
end
|
|
33
50
|
|
|
@@ -45,30 +62,40 @@ module PaperTrailDiff
|
|
|
45
62
|
|
|
46
63
|
# @rbs @reported: untyped
|
|
47
64
|
# @rbs @closing: RecordSnapshot?
|
|
65
|
+
# @rbs @keys: Array[Array[untyped] | Integer]
|
|
48
66
|
# @rbs @starts: Array[Integer]
|
|
49
67
|
# @rbs @ends: Array[Integer]
|
|
68
|
+
# @rbs @roots: Hash[Array[untyped] | Integer, Array[Integer]]
|
|
69
|
+
# @rbs @judged: Set[Array[untyped] | Integer]
|
|
50
70
|
# @rbs @types: Hash[String, Array[String?]]
|
|
51
71
|
|
|
52
72
|
#: (ActivityEvent, Integer, Array[[ActivityEvent, RecordSnapshot?]]) -> bool
|
|
53
73
|
def keep?(event, index, pairs)
|
|
54
|
-
|
|
74
|
+
key = @keys.fetch(index)
|
|
75
|
+
return true if event.root? || event.join? || !@judged.include?(key)
|
|
55
76
|
|
|
77
|
+
judged_by(index, key, pairs).any? { |snapshot| holds?(snapshot, event) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The snapshots at the first event and the root events of the event's
|
|
81
|
+
# transaction, and the one after its last event.
|
|
82
|
+
#: (Integer, Array[untyped] | Integer, Array[[ActivityEvent, RecordSnapshot?]]) -> Array[RecordSnapshot?]
|
|
83
|
+
def judged_by(index, key, pairs)
|
|
56
84
|
position = @ends.fetch(index)
|
|
57
85
|
after = position < pairs.length ? pairs.fetch(position).last : @closing
|
|
58
|
-
|
|
86
|
+
([@starts.fetch(index)] + @roots.fetch(key, [])).map { |at| pairs.fetch(at).last } << after
|
|
59
87
|
end
|
|
60
88
|
|
|
61
89
|
#: (ActivityEvent) -> bool
|
|
62
90
|
def reported?(event)
|
|
63
|
-
|
|
91
|
+
@reported.call(event)
|
|
64
92
|
end
|
|
65
93
|
|
|
66
94
|
# Where each event's transaction starts, and the position just past its
|
|
67
95
|
# last event. Two transactions can interleave, so a transaction's events
|
|
68
96
|
# need not be adjacent.
|
|
69
|
-
#: (Array[
|
|
70
|
-
def spans(
|
|
71
|
-
keys = transaction_keys(events)
|
|
97
|
+
#: (Array[Array[untyped] | Integer]) -> [Array[Integer], Array[Integer]]
|
|
98
|
+
def spans(keys)
|
|
72
99
|
first = {} #: Hash[Array[untyped] | Integer, Integer]
|
|
73
100
|
last = {} #: Hash[Array[untyped] | Integer, Integer]
|
|
74
101
|
keys.each_with_index do |key, index|
|
|
@@ -78,6 +105,20 @@ module PaperTrailDiff
|
|
|
78
105
|
[keys.map { |key| first.fetch(key) }, keys.map { |key| last.fetch(key) + 1 }]
|
|
79
106
|
end
|
|
80
107
|
|
|
108
|
+
# Where each transaction's root events are.
|
|
109
|
+
#: (Array[ActivityEvent]) -> Hash[Array[untyped] | Integer, Array[Integer]]
|
|
110
|
+
def root_positions(events)
|
|
111
|
+
events.each_index.select { |index| events.fetch(index).root? }
|
|
112
|
+
.group_by { |index| @keys.fetch(index) }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# The transactions that have an event whose step is reported.
|
|
116
|
+
#: (Array[ActivityEvent]) -> Set[Array[untyped] | Integer]
|
|
117
|
+
def reported_transactions(events)
|
|
118
|
+
events.each_index.select { |index| reported?(events.fetch(index)) }
|
|
119
|
+
.to_set { |index| @keys.fetch(index) }
|
|
120
|
+
end
|
|
121
|
+
|
|
81
122
|
# A version saved outside a transaction is a transaction of its own,
|
|
82
123
|
# keyed by its position.
|
|
83
124
|
#: (Array[ActivityEvent]) -> Array[Array[untyped] | Integer]
|
|
@@ -3,21 +3,17 @@
|
|
|
3
3
|
|
|
4
4
|
module PaperTrailDiff
|
|
5
5
|
# Collects root and selected-descendant versions relevant to one root history range.
|
|
6
|
+
# Descendants are found in the root's prepared history.
|
|
6
7
|
class ActivityVersionCollector
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
range_end: root_versions.last,
|
|
14
|
-
prepared: nil
|
|
15
|
-
)
|
|
16
|
-
@prepared = prepared
|
|
8
|
+
# `traversal` holds the tree whose descendants are collected.
|
|
9
|
+
#: (untyped, root_versions: Array[untyped], traversal: AssociationTraversal, range_start: untyped, range_end: untyped, prepared: PreparedActivityIndex?) -> void
|
|
10
|
+
def initialize(record, root_versions:, traversal:, range_start:, range_end:, prepared:) # rubocop:disable Metrics/ParameterLists
|
|
11
|
+
# Only a tree's descendants are read from it, and a tree is always read
|
|
12
|
+
# with prepared history (see HistoricalSnapshotStore#prepare).
|
|
13
|
+
@prepared = prepared #: PreparedActivityIndex
|
|
17
14
|
@record = record
|
|
18
15
|
@root_versions = root_versions
|
|
19
|
-
@
|
|
20
|
-
@traversal = AssociationTraversal.new(tree)
|
|
16
|
+
@traversal = traversal
|
|
21
17
|
@range = ActivityRange.new(range_start, range_end)
|
|
22
18
|
@registry = ActivityEventRegistry.new
|
|
23
19
|
end
|
|
@@ -27,7 +23,10 @@ module PaperTrailDiff
|
|
|
27
23
|
#: () -> Array[ActivityEvent]
|
|
28
24
|
def call
|
|
29
25
|
@registry.add(@root_versions)
|
|
30
|
-
|
|
26
|
+
tree = @traversal.tree
|
|
27
|
+
unless tree.empty?
|
|
28
|
+
collect_tree(group(@record.class, [@record.id]), tree, path: '', branch: nil)
|
|
29
|
+
end
|
|
31
30
|
events = @registry.to_a.sort_by { |event| Support.chronological_version_key(event.version) }
|
|
32
31
|
pair = Support.ambiguous_pair(events.map(&:version))
|
|
33
32
|
pair ? raise(AmbiguousVersionOrderError, Support.ambiguous_message(pair)) : events.freeze
|
|
@@ -35,181 +34,116 @@ module PaperTrailDiff
|
|
|
35
34
|
|
|
36
35
|
private
|
|
37
36
|
|
|
38
|
-
# @rbs @prepared: PreparedActivityIndex
|
|
37
|
+
# @rbs @prepared: PreparedActivityIndex
|
|
39
38
|
# @rbs @record: untyped
|
|
40
39
|
# @rbs @root_versions: Array[untyped]
|
|
41
|
-
# @rbs @tree: AssociationTree
|
|
42
40
|
# @rbs @traversal: AssociationTraversal
|
|
43
41
|
# @rbs @range: ActivityRange
|
|
44
42
|
# @rbs @registry: ActivityEventRegistry
|
|
45
43
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
@traversal.reflections_for(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
selected_branch = branch || reflection.name.to_s
|
|
55
|
-
collect_reflection(parent, reflection, subtree, path, selected_branch)
|
|
44
|
+
# Each event is credited to the top-level association it was reached
|
|
45
|
+
# through.
|
|
46
|
+
#: (Hash[Symbol, untyped], AssociationTree, path: String, branch: String?) -> void
|
|
47
|
+
def collect_tree(parent, tree, path:, branch:)
|
|
48
|
+
@traversal.reflections_for(parent.fetch(:model), tree, path: path).each do |reflection|
|
|
49
|
+
# Each reflection is of a child the tree names.
|
|
50
|
+
subtree = tree.child(reflection.name) #: AssociationTree
|
|
51
|
+
collect_reflection(parent, reflection, subtree, path, branch || reflection.name.to_s)
|
|
56
52
|
end
|
|
57
53
|
end
|
|
58
54
|
|
|
59
55
|
#: (Hash[Symbol, untyped], untyped, AssociationTree, String, String) -> void
|
|
60
56
|
def collect_reflection(parent, reflection, subtree, path, branch)
|
|
61
|
-
|
|
62
|
-
groups.each_value do |group|
|
|
57
|
+
edge_groups(parent, reflection, branch).each do |group|
|
|
63
58
|
@registry.add(group.fetch(:versions), branch: branch)
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
child_path = Support.association_path(path, reflection.name)
|
|
67
|
-
collect_tree(group.fetch(:model), group.fetch(:ids), subtree,
|
|
68
|
-
path: child_path, branch: branch)
|
|
59
|
+
collect_tree(group, subtree, path: Support.association_path(path, reflection.name),
|
|
60
|
+
branch: branch)
|
|
69
61
|
end
|
|
70
62
|
end
|
|
71
63
|
|
|
72
|
-
|
|
64
|
+
# The groups of records an edge leads to, one for each class.
|
|
65
|
+
#: (Hash[Symbol, untyped], untyped, String) -> Array[Hash[Symbol, untyped]]
|
|
73
66
|
def edge_groups(parent, reflection, branch)
|
|
74
|
-
return through_groups(parent, reflection, branch) if reflection.
|
|
67
|
+
return through_groups(parent, reflection, branch) if reflection.through_reflection?
|
|
75
68
|
|
|
76
69
|
case reflection.macro
|
|
77
70
|
when :belongs_to
|
|
78
71
|
belongs_to_groups(parent.fetch(:model), parent.fetch(:ids), reflection)
|
|
79
|
-
when :has_one, :has_many
|
|
80
|
-
direct_child_groups(parent.fetch(:model), parent.fetch(:ids), reflection)
|
|
81
72
|
when :has_and_belongs_to_many
|
|
82
73
|
habtm_groups(parent.fetch(:model), parent.fetch(:versions), reflection)
|
|
83
74
|
else
|
|
84
|
-
|
|
75
|
+
direct_child_groups(parent.fetch(:model), parent.fetch(:ids), reflection)
|
|
85
76
|
end
|
|
86
77
|
end
|
|
87
78
|
|
|
88
|
-
|
|
79
|
+
# A through's join part is one class, never polymorphic, so one group of
|
|
80
|
+
# join rows leads to its targets.
|
|
81
|
+
#: (Hash[Symbol, untyped], untyped, String) -> Array[Hash[Symbol, untyped]]
|
|
89
82
|
def through_groups(parent, reflection, branch)
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
# A through is always read with prepared history, which knows the
|
|
95
|
-
# join row's state either side of each version.
|
|
96
|
-
@registry.add(@prepared&.join_versions(group, reflection) || group.fetch(:versions),
|
|
97
|
-
branch: branch, join: true)
|
|
98
|
-
source = reflection.source_reflection
|
|
99
|
-
children = edge_groups(group, source, branch)
|
|
100
|
-
merge_groups!(result, children)
|
|
83
|
+
targets = edge_groups(parent, reflection.through_reflection, branch).flat_map do |group|
|
|
84
|
+
joins = @prepared.join_versions(parent, group, reflection)
|
|
85
|
+
@registry.add(joins, branch: branch, join: true)
|
|
86
|
+
edge_groups(group, reflection.source_reflection, branch)
|
|
101
87
|
end
|
|
102
88
|
# Under `source_type:`, only targets of that type are members.
|
|
103
|
-
reflection.options[:source_type]
|
|
89
|
+
type = reflection.options[:source_type]
|
|
90
|
+
type ? targets.select { |group| type.to_s.eql?(group.fetch(:model).name) } : targets
|
|
104
91
|
end
|
|
105
92
|
|
|
106
93
|
# Every target the owners pointed at in the range, not only those their own
|
|
107
94
|
# changes there recorded: an author renamed under an article nobody edited
|
|
108
|
-
# is still that article's activity.
|
|
109
|
-
|
|
95
|
+
# is still that article's activity. Prepared history holds every target its
|
|
96
|
+
# snapshots can contain. One it did not load -- a key changed without a
|
|
97
|
+
# version -- has no state to compare.
|
|
98
|
+
#: (untyped, Array[untyped], untyped) -> Array[Hash[Symbol, untyped]]
|
|
110
99
|
def belongs_to_groups(parent_class, parent_ids, reflection)
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
# Prepared history holds every target its snapshots can contain. One it
|
|
115
|
-
# did not load -- a key changed without a version -- has no state to compare.
|
|
116
|
-
rows = prepared.target_rows(parent_class, parent_ids, reflection, @range)
|
|
117
|
-
identity_groups(rows, reflection) { |model_class, id| prepared.loaded?(model_class, id) }
|
|
100
|
+
rows = @prepared.target_rows(parent_class, parent_ids, reflection, @range)
|
|
101
|
+
identity_groups(rows, reflection) { |model_class, id| @prepared.loaded?(model_class, id) }
|
|
118
102
|
end
|
|
119
103
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
identity_groups(rows, reflection)
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
#: (untyped, Array[untyped], untyped) -> Hash[String, untyped]
|
|
104
|
+
# With no children, nothing below them is read: prepared history registers
|
|
105
|
+
# only the classes it loaded records of.
|
|
106
|
+
#: (untyped, Array[untyped], untyped) -> Array[Hash[Symbol, untyped]]
|
|
127
107
|
def direct_child_groups(parent_class, parent_ids, reflection)
|
|
128
|
-
item_ids =
|
|
129
|
-
|
|
130
|
-
else
|
|
131
|
-
ActivityChildCandidateLoader.new(parent_class, parent_ids, reflection,
|
|
132
|
-
@range).call
|
|
133
|
-
end
|
|
134
|
-
return {} if item_ids.empty?
|
|
135
|
-
|
|
136
|
-
{ reflection.klass.name => group(reflection.klass, item_ids) }
|
|
108
|
+
item_ids = @prepared.child_ids(parent_class, parent_ids, reflection, @range)
|
|
109
|
+
item_ids.empty? ? [] : [group(reflection.klass, item_ids)]
|
|
137
110
|
end
|
|
138
111
|
|
|
139
|
-
#: (untyped, Array[untyped], untyped) -> Hash[
|
|
112
|
+
#: (untyped, Array[untyped], untyped) -> Array[Hash[Symbol, untyped]]
|
|
140
113
|
def habtm_groups(parent_class, parent_versions, reflection)
|
|
141
|
-
|
|
142
|
-
rows = prepared.habtm_rows(parent_class, parent_versions, reflection)
|
|
143
|
-
return identity_groups(rows, reflection)
|
|
144
|
-
end
|
|
145
|
-
transaction_ids = parent_versions.filter_map do |version|
|
|
146
|
-
VersionBoundary.transaction_id(version)
|
|
147
|
-
end
|
|
148
|
-
rows = parent_class.paper_trail.version_association_class.where(
|
|
149
|
-
version_id: transaction_ids,
|
|
150
|
-
foreign_key_name: reflection.name.to_s
|
|
151
|
-
).to_a
|
|
152
|
-
identity_groups(rows, reflection)
|
|
114
|
+
identity_groups(@prepared.habtm_rows(parent_class, parent_versions, reflection), reflection)
|
|
153
115
|
end
|
|
154
116
|
|
|
155
|
-
|
|
117
|
+
# A type a polymorphic row stored that names no class has no versions to
|
|
118
|
+
# read.
|
|
119
|
+
#: (Array[untyped], untyped) ?{ (untyped, untyped) -> bool } -> Array[Hash[Symbol, untyped]]
|
|
156
120
|
def identity_groups(rows, reflection)
|
|
157
|
-
identities = rows.group_by
|
|
158
|
-
|
|
159
|
-
identities.each do |model_class, class_rows|
|
|
160
|
-
next unless model_class
|
|
161
|
-
|
|
162
|
-
ids = class_rows.filter_map(&:foreign_key_id).uniq
|
|
163
|
-
ids = ids.select { |id| yield(model_class, id) } if block_given?
|
|
164
|
-
groups[model_class.name] = group(model_class, ids)
|
|
121
|
+
identities = rows.group_by do |row|
|
|
122
|
+
AssociationTraversal.stored_target(reflection, row.foreign_type)
|
|
165
123
|
end
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
#: (untyped, Array[untyped]) -> Hash[Symbol, untyped]
|
|
170
|
-
def group(model_class, ids)
|
|
171
|
-
ids = @prepared&.member_ids(model_class, ids) || ids
|
|
172
|
-
{ model: model_class, ids: ids, versions: versions_for(model_class, ids) }
|
|
173
|
-
end
|
|
124
|
+
identities.filter_map do |model_class, class_rows|
|
|
125
|
+
next unless model_class
|
|
174
126
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
source.each do |name, incoming|
|
|
178
|
-
existing = destination[name]
|
|
179
|
-
unless existing
|
|
180
|
-
destination[name] = incoming
|
|
181
|
-
next
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
ids = (existing.fetch(:ids) | incoming.fetch(:ids))
|
|
185
|
-
destination[name] = group(existing.fetch(:model), ids)
|
|
127
|
+
ids = target_ids(class_rows)
|
|
128
|
+
group(model_class, block_given? ? ids.select { |id| yield(model_class, id) } : ids)
|
|
186
129
|
end
|
|
187
130
|
end
|
|
188
131
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
version_class = model_class.paper_trail.version_class
|
|
198
|
-
@range.scope(
|
|
199
|
-
version_class.where(
|
|
200
|
-
item_type: model_class.base_class.name,
|
|
201
|
-
item_id: ids
|
|
202
|
-
)
|
|
203
|
-
).to_a.select { |version| @range.include?(version) }
|
|
132
|
+
# Each target once, however many rows name it, and none for a row that
|
|
133
|
+
# names none. The registry keeps a version once, and a missing target has
|
|
134
|
+
# no versions, so this only saves reading them again, which is why mutant
|
|
135
|
+
# leaves it alone.
|
|
136
|
+
# mutant:disable
|
|
137
|
+
#: (Array[untyped]) -> Array[untyped]
|
|
138
|
+
def target_ids(rows)
|
|
139
|
+
rows.filter_map(&:foreign_key_id).uniq
|
|
204
140
|
end
|
|
205
141
|
|
|
206
|
-
#: (untyped, untyped) -> untyped
|
|
207
|
-
def
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
rescue NameError
|
|
212
|
-
nil
|
|
142
|
+
#: (untyped, Array[untyped]) -> Hash[Symbol, untyped]
|
|
143
|
+
def group(model_class, ids)
|
|
144
|
+
ids = @prepared.member_ids(model_class, ids)
|
|
145
|
+
{ model: model_class, ids: ids,
|
|
146
|
+
versions: @prepared.member_versions(model_class, ids, @range) }
|
|
213
147
|
end
|
|
214
148
|
end
|
|
215
149
|
end
|
|
@@ -9,9 +9,10 @@ module PaperTrailDiff
|
|
|
9
9
|
# history would read differently depending on how it was asked for.
|
|
10
10
|
class ActivityView
|
|
11
11
|
# Options only an activity view uses are refused without one.
|
|
12
|
-
#: (activity: bool, snapshots: bool, group: Symbol?, recorded: IgnorePolicy?) -> ActivityView
|
|
13
|
-
def self.build(activity:, snapshots:, group:, recorded:)
|
|
14
|
-
view = new(snapshots: snapshots, group: group, recorded: recorded
|
|
12
|
+
#: (activity: bool, snapshots: bool, group: Symbol?, recorded: IgnorePolicy?, ?version_scope: untyped) -> ActivityView
|
|
13
|
+
def self.build(activity:, snapshots:, group:, recorded:, version_scope: nil)
|
|
14
|
+
view = new(snapshots: snapshots, group: group, recorded: recorded,
|
|
15
|
+
version_scope: version_scope)
|
|
15
16
|
return view if activity
|
|
16
17
|
|
|
17
18
|
{ snapshots: snapshots, recorded_changes: !recorded.nil?, group: !group.nil? }
|
|
@@ -21,18 +22,25 @@ module PaperTrailDiff
|
|
|
21
22
|
view
|
|
22
23
|
end
|
|
23
24
|
|
|
24
|
-
#: (?snapshots: bool, ?group: Symbol?, ?recorded: IgnorePolicy?) -> void
|
|
25
|
-
def initialize(snapshots: false, group: nil, recorded: nil)
|
|
25
|
+
#: (?snapshots: bool, ?group: Symbol?, ?recorded: IgnorePolicy?, ?version_scope: untyped) -> void
|
|
26
|
+
def initialize(snapshots: false, group: nil, recorded: nil, version_scope: nil)
|
|
26
27
|
unless group.nil? || group == :transaction
|
|
27
28
|
raise ConfigurationError, 'group: must be :transaction or nil'
|
|
28
29
|
end
|
|
29
30
|
|
|
30
31
|
@snapshots = snapshots
|
|
31
32
|
@group = group
|
|
32
|
-
@
|
|
33
|
+
@readers = recorded && [true, false].to_h do |root|
|
|
34
|
+
[root, RecordedChangesReader.new(recorded.recorded_attributes(root: root))]
|
|
35
|
+
end
|
|
36
|
+
@version_filter = version_scope && ActivityScopeFilter.new(version_scope)
|
|
33
37
|
freeze
|
|
34
38
|
end
|
|
35
39
|
|
|
40
|
+
# Which events of activity on associations a `version_scope:` reports,
|
|
41
|
+
# answered once for the whole request; nil without one.
|
|
42
|
+
attr_reader :version_filter #: ActivityScopeFilter?
|
|
43
|
+
|
|
36
44
|
# Merging a group compares its outer states, so the snapshots must survive
|
|
37
45
|
# the build even when the caller did not ask to keep them.
|
|
38
46
|
#: () -> bool
|
|
@@ -40,10 +48,12 @@ module PaperTrailDiff
|
|
|
40
48
|
@snapshots || grouping?
|
|
41
49
|
end
|
|
42
50
|
|
|
43
|
-
#
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
# Reads what a boundary's version recorded, once per version for the whole
|
|
52
|
+
# request, when they were asked for. The root's own versions leave out the
|
|
53
|
+
# names ignored on its path as well.
|
|
54
|
+
#: (root: bool) -> RecordedChangesReader?
|
|
55
|
+
def recorded_reader(root:)
|
|
56
|
+
@readers&.fetch(root)
|
|
47
57
|
end
|
|
48
58
|
|
|
49
59
|
# Applied last, to finished steps, so grouping sees the same timeline the
|
|
@@ -59,7 +69,8 @@ module PaperTrailDiff
|
|
|
59
69
|
|
|
60
70
|
# @rbs @snapshots: bool
|
|
61
71
|
# @rbs @group: Symbol?
|
|
62
|
-
# @rbs @
|
|
72
|
+
# @rbs @readers: Hash[bool, RecordedChangesReader]?
|
|
73
|
+
# @rbs @version_filter: ActivityScopeFilter?
|
|
63
74
|
|
|
64
75
|
#: () -> bool
|
|
65
76
|
def grouping?
|
|
@@ -9,10 +9,10 @@ module PaperTrailDiff
|
|
|
9
9
|
attr_reader :activity_timeline #: Array[ActivityStep]?
|
|
10
10
|
attr_reader :from_snapshot #: RecordSnapshot?
|
|
11
11
|
attr_reader :to_snapshot #: RecordSnapshot?
|
|
12
|
-
# The time window the analysis covers, or nil without one
|
|
13
|
-
# window asked for unless a transaction crossed an edge,
|
|
14
|
-
# edge back to where that transaction began.
|
|
15
|
-
attr_reader :window #: Range[Time]?
|
|
12
|
+
# The time window the analysis covers, or nil without one; a report's has
|
|
13
|
+
# no end. It is the window asked for unless a transaction crossed an edge,
|
|
14
|
+
# which moves the edge back to where that transaction began.
|
|
15
|
+
attr_reader :window #: Range[Time?]?
|
|
16
16
|
|
|
17
17
|
# The result for a record whose requested history contains no versions,
|
|
18
18
|
# which is an empty history rather than a failed request. Its snapshots
|
|
@@ -29,7 +29,7 @@ module PaperTrailDiff
|
|
|
29
29
|
|
|
30
30
|
# The reconstructed states the diff was taken between. A report that has to
|
|
31
31
|
# render unchanged columns needs the whole final state, not only what moved.
|
|
32
|
-
#: (diff: Diff, timeline: Array[Step], ?activity_timeline: Array[ActivityStep]?, ?from_snapshot: RecordSnapshot?, ?to_snapshot: RecordSnapshot?, ?window: Range[Time]?) -> void
|
|
32
|
+
#: (diff: Diff, timeline: Array[Step], ?activity_timeline: Array[ActivityStep]?, ?from_snapshot: RecordSnapshot?, ?to_snapshot: RecordSnapshot?, ?window: Range[Time?]?) -> void
|
|
33
33
|
def initialize( # rubocop:disable Metrics/ParameterLists
|
|
34
34
|
diff:, timeline:, activity_timeline: nil, from_snapshot: nil, to_snapshot: nil, window: nil
|
|
35
35
|
)
|
|
@@ -62,6 +62,20 @@ module PaperTrailDiff
|
|
|
62
62
|
steps.flat_map(&:change_rows).freeze
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
+
# One entry for each record the activity's change rows are about: its
|
|
66
|
+
# state before them, and that state with only the kept changes made (see
|
|
67
|
+
# NetChange). The block picks the rows to keep, such as those with an
|
|
68
|
+
# author; without one, every row is kept. The states come from the
|
|
69
|
+
# steps' snapshots, so this needs `snapshots: true` as well as
|
|
70
|
+
# `activity: true`.
|
|
71
|
+
#: () ?{ (ChangeRow) -> boolish } -> Array[NetChange]
|
|
72
|
+
def net_changes(&keep)
|
|
73
|
+
steps = activity_timeline
|
|
74
|
+
raise ConfigurationError, 'net_changes requires activity: true' unless steps
|
|
75
|
+
|
|
76
|
+
NetChanges.call(steps, keep)
|
|
77
|
+
end
|
|
78
|
+
|
|
65
79
|
# Reconstructed endpoint states are opt-in, because they carry the whole
|
|
66
80
|
# selected graph whether or not anything changed, which dwarfs the rest of
|
|
67
81
|
# the payload for a wide graph.
|
|
@@ -6,12 +6,15 @@ module PaperTrailDiff
|
|
|
6
6
|
# versions and preparing their association history once for the whole batch
|
|
7
7
|
# rather than once per root.
|
|
8
8
|
class AnalysisBatch
|
|
9
|
-
|
|
9
|
+
# `traversal` names the requested paths, which decide where a window's
|
|
10
|
+
# edges move (see TransactionCut).
|
|
11
|
+
#: (Array[untyped], time_range: TimeRange?, live_loader: untyped, history_preparer: untyped, analyzer: untyped, version_scope: untyped, context_required: bool, traversal: AssociationTraversal?) -> void
|
|
10
12
|
def initialize( # rubocop:disable Metrics/ParameterLists
|
|
11
|
-
records, time_range:, live_loader:, history_preparer:, analyzer:, version_scope
|
|
12
|
-
context_required:
|
|
13
|
+
records, time_range:, live_loader:, history_preparer:, analyzer:, version_scope:,
|
|
14
|
+
context_required:, traversal:
|
|
13
15
|
)
|
|
14
16
|
@records = records
|
|
17
|
+
@traversal = traversal
|
|
15
18
|
@context_required = context_required
|
|
16
19
|
@time_range = time_range
|
|
17
20
|
# Checked up front rather than failing partway through a batch.
|
|
@@ -25,23 +28,23 @@ module PaperTrailDiff
|
|
|
25
28
|
def call
|
|
26
29
|
records = validated_records
|
|
27
30
|
loaded = @live_loader.call(records)
|
|
28
|
-
windows = TransactionCut.batch_windows(records, @time_range)
|
|
29
|
-
|
|
31
|
+
windows = TransactionCut.batch_windows(records, @time_range, traversal: @traversal)
|
|
32
|
+
# Activity on associations both takes a context and replays whole plans.
|
|
33
|
+
plans = BatchedRootVersions.new(
|
|
30
34
|
records, time_range: @time_range, version_scope: @version_scope,
|
|
31
|
-
live_endpoints: loaded,
|
|
32
|
-
|
|
35
|
+
live_endpoints: loaded, context_required: @context_required,
|
|
36
|
+
whole: @context_required, windows: windows
|
|
33
37
|
).call
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
[Support.immutable_copy(key), analysis_for(record, plan, windows.fetch(key, @time_range))]
|
|
39
|
-
end.freeze
|
|
38
|
+
BatchedRootAnalyses.new(
|
|
39
|
+
time_range: @time_range, windows: windows, history_preparer: @history_preparer,
|
|
40
|
+
analyzer: @analyzer
|
|
41
|
+
).call(records, plans, loaded: loaded).freeze
|
|
40
42
|
end
|
|
41
43
|
|
|
42
44
|
private
|
|
43
45
|
|
|
44
46
|
# @rbs @records: Array[untyped]
|
|
47
|
+
# @rbs @traversal: AssociationTraversal?
|
|
45
48
|
# @rbs @time_range: TimeRange?
|
|
46
49
|
# @rbs @version_scope: untyped
|
|
47
50
|
# @rbs @context_required: bool
|
|
@@ -62,50 +65,5 @@ module PaperTrailDiff
|
|
|
62
65
|
|
|
63
66
|
raise ConfigurationError, 'records: identities must be unique'
|
|
64
67
|
end
|
|
65
|
-
|
|
66
|
-
# Association history is prepared per model class across every selected root
|
|
67
|
-
# version, which is the work that would otherwise repeat for each record.
|
|
68
|
-
# The roots are preloaded first, because preparation reads their current
|
|
69
|
-
# association state as a fallback and would otherwise walk it one root at a
|
|
70
|
-
# time.
|
|
71
|
-
#: (Array[untyped], Hash[Array[String], RootVersionPlan], Hash[Array[String], untyped], Hash[Array[String], TimeRange]) -> void
|
|
72
|
-
def prepare(records, selected, loaded, windows)
|
|
73
|
-
records.group_by(&:class).each_value do |grouped|
|
|
74
|
-
versions = grouped.flat_map do |record|
|
|
75
|
-
selected.fetch(Endpoint.identity(record), RootVersionPlan.empty).reconstruction_versions
|
|
76
|
-
end
|
|
77
|
-
next if versions.empty?
|
|
78
|
-
|
|
79
|
-
roots = grouped.map { |record| loaded.fetch(Endpoint.identity(record), record) }
|
|
80
|
-
live = closes_live?(grouped, selected)
|
|
81
|
-
@history_preparer.call(roots, versions, **preparation_range(versions, windows, live: live))
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
#: (Array[untyped], Hash[Array[String], RootVersionPlan]) -> bool
|
|
86
|
-
def closes_live?(records, selected)
|
|
87
|
-
records.any? do |record|
|
|
88
|
-
!selected.fetch(Endpoint.identity(record), RootVersionPlan.empty).closing_record.nil?
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
# A plan closing on the live record reads history up to now; the others
|
|
93
|
-
# stop at the window, so a past window does not load everything since.
|
|
94
|
-
#: (Array[untyped], Hash[Array[String], TimeRange], live: bool) -> Hash[Symbol, untyped]
|
|
95
|
-
def preparation_range(versions, windows, live:)
|
|
96
|
-
times = versions.map(&:created_at)
|
|
97
|
-
end_at = [times.max, @time_range&.end_time, (Time.now.utc if live)].compact.max
|
|
98
|
-
{ start_at: TransactionCut.earliest_start(@time_range, windows) || times.min, end_at: end_at }
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
# A root with no versions in range has nothing to report, which is an empty
|
|
102
|
-
# result rather than a failed request -- unless an activity view closes it
|
|
103
|
-
# on current state, where its descendants may still have moved.
|
|
104
|
-
#: (untyped, RootVersionPlan, TimeRange?) -> Analysis
|
|
105
|
-
def analysis_for(record, plan, window)
|
|
106
|
-
return @analyzer.empty.with_window(window) if plan.empty? && !plan.closing_record
|
|
107
|
-
|
|
108
|
-
@analyzer.call(record, plan, window).with_window(window)
|
|
109
|
-
end
|
|
110
68
|
end
|
|
111
69
|
end
|
|
@@ -19,14 +19,10 @@ module PaperTrailDiff
|
|
|
19
19
|
# are checked for conditions alone.
|
|
20
20
|
#: (untyped, ranked: bool) -> String?
|
|
21
21
|
def self.fallback_reason(reflection, ranked:)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
part_reason = fallback_reason(part, ranked: false)
|
|
27
|
-
return part_reason if part_reason
|
|
28
|
-
end
|
|
29
|
-
nil
|
|
22
|
+
reason_for(reflection, ranked) ||
|
|
23
|
+
AssociationTraversal.through_parts(reflection).filter_map do |part|
|
|
24
|
+
reason_for(part, false)
|
|
25
|
+
end.first
|
|
30
26
|
end
|
|
31
27
|
private_class_method :fallback_reason
|
|
32
28
|
|