-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bitemporal data structure visualizer #94
Conversation
module_function | ||
|
||
def visualize(record, height: 10, width: 40, highlight: true) | ||
histories = record.class.ignore_valid_datetime.within_deleted.bitemporal_for(record) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use ignore_bitemporal_datetime
instead of ignore_valid_datetime.within_deleted
.
histories = record.class.ignore_valid_datetime.within_deleted.bitemporal_for(record) | |
histories = record.class.ignore_bitemporal_datetime.bitemporal_for(record) |
valid_times = (histories.map(&:valid_from) + histories.map(&:valid_to)).sort.uniq | ||
transaction_times = (histories.map(&:transaction_from) + histories.map(&:transaction_to)).sort.uniq |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change it to use pluck(:valid_from)
?
because #pluck
is better performance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the histories
are already loaded, ActiveRecord::Calculations#pluck
is just the same as map(&:valid_from)
.
https://github.com/rails/rails/blob/v7.0.3/activerecord/lib/active_record/relation/calculations.rb#L194
https://github.com/rails/rails/blob/v7.0.3/activesupport/lib/active_support/core_ext/enumerable.rb#L201
I believe that map(&:valid_from)
is better in performance because it is much more efficient in this case to iterate the array than to execute SQL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okey, Thanks :)
d3e64d8
to
bc947fa
Compare
Thanks :) I have a few questions about a few things that have been bothering me.
|
Yes, but the reason I didn't do that was that the header part might look like part of a history. If you don't have this concern, we may implement this. |
Thanks for reply.
I don't see a problem. |
Fixed by 95a19ef |
UPDATE: Added This is useful for checking at what time it is valid between different histories such as associations. In the example below, it's easy to see that a department is valid in the employee's three histories.
|
body.print("#{record.transaction_from.strftime('%F %T.%3N')} ", line: line) | ||
if width > 0 | ||
if height > 0 | ||
body.print('|' + '-' * width + '|', line: line, column: column) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nits] Changing '|'
to '+'
will make it a little easier to see.
https://github.com/kufu/activerecord-bitemporal/pull/94/files#diff-69d7b91cda642b52f976ad3f279e8d9aeb47b167a299f8426a6fc5d535c3aecfR88
before
| 2019-01-10 00:00:00.000
| | 2019-01-15 00:00:00.000
| | | 2019-01-20 00:00:00.000
| | | | 9999-12-31 00:00:00.000
2019-01-10 00:00:00.000 |---------------------------------------|
| |
2019-01-15 00:00:00.000 |---------|-----------------------------|
| | |
| | |
2019-01-20 00:00:00.000 | |---------|-------------------|
| | |*******************|
| | |*******************|
| | |*******************|
| | |*******************|
9999-12-31 00:00:00.000 |---------|---------|-------------------|
after
| 2019-01-10 00:00:00.000
| | 2019-01-15 00:00:00.000
| | | 2019-01-20 00:00:00.000
| | | | 9999-12-31 00:00:00.000
2019-01-10 00:00:00.000 +---------------------------------------+
| |
2019-01-15 00:00:00.000 +---------+-----------------------------+
| | |
| | |
2019-01-20 00:00:00.000 | +---------+-------------------+
| | |*******************|
| | |*******************|
| | |*******************|
| | |*******************|
9999-12-31 00:00:00.000 +---------+---------+-------------------+
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 9bafa9e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Thanks!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great works for visualization 🎉
LGTM 👍
9bafa9e
to
d3d538d
Compare
Although the Bi-temporal data model is a good data structure, it has a difficult problem to intuitively understand the data structure that has two times, valid time and transaction time.
This PR introduces a visualizer that plots multiple histories on a two-dimensional figure to help you understand the data structure. This visualizer draws the following history as follows:
The area filled with
*
is the duration of the instance passed tovisualize
. This makes it easier to understand where the history in the overall history.Apart from
visualize
,visualize_records
is also provided as a lower-level interface. This method allows you to pass histories and another history to highlight. The following is example:In the above example, unlike
visualize
, you can only draw histories after 2019-01-12, not all history. Alternatively, you can draw the temporal relationships of different associations.