Skip to content
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

Display total table relation size in the drawer #257

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

desssai
Copy link

@desssai desssai commented Jul 5, 2024

Changes

  • new global boolean flag g:db_ui_show_size.
  • added 'schemes_tables_size_query' for postrgesql and sqlserver schemes.
  • displays total table relation size for each table in the right side of the db_ui drawer. It reserves 8 characters (1 space and 7 characters for size description e.g. min: 0.0B and max: 999.9GB).

The rendering takes into consideration drawer's width and calculates the necessary amount of spaces with repeat() so as to provide the drawer with the label in the format of {table-name}-{whitespace}-{table-size} that would fit with both nerd fonts enabled and different drawer width options. Does not break (throws no errors) if width is less than 8.

@desssai desssai marked this pull request as draft July 5, 2024 11:01
@kristijanhusak
Copy link
Owner

Thanks for the PR! This is a very nice feature.
What do you think of calculating it always (no need for db_ui_show_size), and putting it inside the details as they are shown on the database lines when you press H ? That way, it would be shown only when requested, and hidden by default.

@desssai
Copy link
Author

desssai commented Jul 6, 2024

Thanks for the PR! This is a very nice feature. What do you think of calculating it always (no need for db_ui_show_size), and putting it inside the details as they are shown on the database lines when you press H ? That way, it would be shown only when requested, and hidden by default.

I think it is a great idea to provide people with this flag as making queries for not only tables' names but sizes as well makes the delay slightly longer when opening the connection. For example, I tested it on a postgres database with over 11000 tables and the request took approximately 6 seconds for the first time and 4 seconds each call after, while the original took only 2 seconds. There is also pretty little I can do regarding optimization of size calculation as it is my first experience with vimscript. I will do something regarding it with bitwise shifts instead of divisions soon. Yet nothing can be done on the db's part. As it feels less straining on smaller databases, I would like to leave some options for people to turn off querying and calculating table size completely, for those of users who wish to retain their workflow speed.

What about making it g:db_ui_show_size = 1 by default and provide people with a way to turn it off.

@desssai
Copy link
Author

desssai commented Jul 6, 2024

But I will take details toggle into development as I think it is a great idea.

@kristijanhusak
Copy link
Owner

Ok, we can leave it as a togglable variable with the default off in that case. I thought there is no performance hit with it.

@desssai desssai marked this pull request as ready for review July 6, 2024 17:26
@desssai
Copy link
Author

desssai commented Jul 6, 2024

  • I reworked the code for size calculations with smaller amount of operations. Cannot do more as it turns out vimscript does not support bit shifts.

  • Added support for TB values (never seen one in a db table myself but who knows someone may need them).

  • Made the size parser universal (if there is no size then I populate the variable to an empty string in the request's return list) so as to make the code more clean and readable while it almost doesn't affect performance.

  • Removed an error when I try to access a size variable that doesn't exist.

  • Changed description to provide details on the db_ui_show_size flag and toggling via H (show_details).

@desssai
Copy link
Author

desssai commented Jul 6, 2024

Here's a preview

image

Copy link
Owner

@kristijanhusak kristijanhusak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks solid, but we just need to address few more things.

else
let icon = self.get_toggle_icon('table', a:tables.items[a:table])
let label = repeat(' ', shiftwidth() * a:level).icon.(!empty(icon) ? " " : "")
if strchars(label) + strchars(a:table) > g:db_ui_winwidth - 8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not rely on db_ui_winwidth variable for this. Table names can be longer than the width.
We should find the longest table name and use that as a reference for padding.
We also need to put the values in parenthesis so it's picked up by highlighter.

This should be enough for the current else block:

  let longest = max(map(copy(a:tables.list), 'strchars(v:val)'))
  return printf('%-'.longest.'s (%s)', a:table, a:tables.items[a:table].size)

Comment on lines +462 to +464
if !g:db_ui_show_size || !self.show_details
let name = a:table
else
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just return early here and avoid having an else block:

Suggested change
if !g:db_ui_show_size || !self.show_details
let name = a:table
else
if !g:db_ui_show_size || !self.show_details
return a:table
endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants