rodloboz

rodloboz

Masu (枡, the squares of a grid) is tables for Phoenix. It is a data grid for LiveView that draws only the rows on the screen, so a hundred rows and a hundred thousand scroll the same way.

You give it a list, or an Ecto query and your repo. With Ecto, a sort is an ORDER BY, a filter and the search a WHERE, and the grid reads only the rows the person sees, a block of a hundred at a time.

<.data_grid id="customers" source={Customer} repo={Repo} searchable>
  <:col field={:name} />
  <:col field={:plan} type={:enum} />
  <:col field={:revenue} type={:decimal} />
  <:col field={:signed_up} type={:date} />
</.data_grid>

Filters, sorting and search

A filter menu on each column, with the filters of its type. An enum lists its values with their counts, from one GROUP BY. The filters show as chips above the grid. Shift+click sorts by several columns.

Editing, as in a spreadsheet

Give the grid your changeset and the cells edit: type on a cell, Enter and Tab, paste from a spreadsheet, fill down, undo. The edits save through Repo.update, and the changeset’s errors go on their cells.

Live, and together

live={{MyApp.PubSub, "orders"}} shows the rows that change on a topic as they change, in each person’s order and filters. Add Presence and the people who have the grid open see each other: their avatars, their cells in their colors, and a lock on the cell someone types in.

Groups, trees and details

group_by groups the rows with counts and totals, counted by the database. tree={:parent_id} shows rows as a tree. A :detail slot opens a row on any HEEx under it, another grid too.

And more

A menu on each cell with your own items, the keyboard of a spreadsheet, ⌘C that copies a range as a spreadsheet pastes it, CSV and XLSX export, saved views, locale formats from Intl, dark mode, and the WAI-ARIA grid pattern. Masu.Test reads, sorts and filters a grid in Phoenix.LiveViewTest with no browser.

Install

Masu needs Elixir 1.18, Phoenix 1.7.14 or 1.8, and LiveView 1.1. The
JavaScript is a prebuilt bundle in the Hex package, so your app does not
need Node.js for it.

{:masu, "~> 0.1"}
mix deps.get
mix masu.install

To try every feature, clone the repository and run mix dev.

Feedback and issues are welcome.

Showing Posts 1 to 6

brightball

brightball

Could this work to browse a very large data set, like something from Snowflake (via SnowflakeEx — snowflake_elixir v0.2.0)

Also, for the use of ORDER BY that you mentioned, that can get dangerous on big tables. LIMIT, OFFSET and ORDER BY all require pulling all of the data in scope into database memory for it to perform the sort, then get the data back. The bigger the table, the more intense this operation gets. If you hit an amount of data that is bigger than the DB will allow in memory, it will write a temp table to perform the operation and spike your disk I/O.

This write up gives a solid explanation of how to solve it, which should actually be pretty easy to implement in the structure that you’re using here. You essentially let the WHERE clause specify exactly where to start from and approximately where to end so that the amount of data being sorted is closer to what’s actually displayed and most importantly…consistent. If you’re sorting by column C then when you scroll to load the next set of data you know exactly the last data point in column C that was displayed. You’ll have to guess a little on the end point but you get the idea.

rodloboz

rodloboz OP

1/ Thanks for this, and for the link! It pushed me to look hard at how Masu pages, and to benchmark it to its limits. Masu 0.2.0 is out with what we found.

2/ Masu already used the seek method from that article: the next block starts after the sort values of the last row, with the primary key as a tiebreak. But the benchmarks showed that our WHERE kept Postgres from using the index. Fixed in 0.2.

3/ At 10M rows, a block in the middle now takes 0.24 ms (it was 450 ms), and the same block sorted by an indexed date takes 4 ms (it was 12.9 s). Numbers: masu/bench/RESULTS.md at main · hattoriai/masu · GitHub

4/ On ORDER BY: with an index on the sort column and the primary key, ORDER BY … LIMIT doesn’t sort the table. The database walks the index and stops. A deep OFFSET is the costly part, and that’s what keyset paging avoids. Without an index, a sort still scans, so index what people sort by.

5/ Snowflake: yes, with a source of your own (Masu.Source): run the query once and page through its result. The Ecto adapter for SnowflakeEx is archived, so I’d go through the driver or Snowflake’s SQL API.

6/ Or a controlled grid (on_query): your app runs the queries, and Masu is only the view. That’s how Lotus uses it, behind a flag: GitHub - elixir-lotus/lotus_web at try-masu · GitHub

7/ Also new in 0.2: count_limit for “10,000+” counts, and read={…} to page an Ecto source your own way.

pjode

pjode

looks great. Out of curiosity, is this something one would use over Flop if that is what they were using Flop for? Or in conjunction with it? And admittedly I only glanced at the docs but if that answer is the former, is there a means for more complex columns, such as tags, or in other words a many-to-1 relationship with the row?

rodloboz

rodloboz OP

Thanks! To be honest, I did not know about Flop when I built Masu. I wanted a grid that matches our other open source libraries: Kotoba, Jumon and Koyomi. They all have a similar visual style, and I use all of them in Lotus BI. I wanted to make something that is useful for the community, and that I can also change quickly when I need something new.

Instead of Flop or with Flop?

For a table in a LiveView, Masu does the work that you use Flop for, so you can use it in place of Flop. With an Ecto source (source={query} repo={Repo}), Masu makes the query itself. A sort becomes an ORDER BY. A filter or the search becomes a WHERE. Each block of rows is a LIMIT, and Masu reads it by key, not by OFFSET. Masu checks each filter against the column’s type on the server. It can also keep the view (sorts, filters, search) in the URL.

You can also use Masu together with Flop. If you already have Flop queries and want to keep them, use a controlled grid (on_query). The grid then does not sort, filter or page the rows. It sends you a %Masu.Query{} with the sorts and filters of the person, and an offset and a limit. You run your own query, for example with Flop, and give the grid the rows and the total. Masu has no adapter for Flop, so you write the small mapping from Masu.Query to Flop params.

Tags and other “has many” columns

You can show them now. Put a preload in the source query, and use a :col with your own HEEx, for example a chip for each tag. But in the Ecto source, only a column that names a field of the query’s first source can sort and filter in the database. A column of preloaded or joined data shows its values, but it does not sort or filter.

Thus “filter by tag” is not built in. To be honest, I did not think about it yet. Today you can do it in these ways:

  • Filter the source query in your LiveView, for example from a tag picker above the grid, and give the grid the new query.
  • Use on_query and run the tag filter in your own query. Flop can do this part.
  • Write your own Masu.Source.

All ideas are welcome. If you open an issue with your case, I will be happy to look at it. A PR is also welcome.

xNikhil001

xNikhil001

Thanks for the great library. Fits right when and where we needed it.

effinbanjos

effinbanjos

This looks very promising, well done.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
danschultzer
I’ve moved the strategies out of PowAssent and created a multi-provider framework for Elixir. This way the strategies can be used in any ...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New
danschultzer
I needed something like premailer for rails, and after searching, I decided to make an easy-to-use library to handle inline styles and co...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews