janela 0.16.1 → 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 +27 -0
- data/README.md +19 -3
- data/UPGRADING.md +29 -0
- data/app/assets/javascripts/janela/chart_controller.js +14 -2
- data/app/controllers/janela/panes_controller.rb +1 -1
- data/app/controllers/janela/queries_controller.rb +1 -0
- data/app/helpers/janela/frames_helper.rb +8 -3
- data/app/models/janela/pane.rb +1 -1
- data/app/models/janela/query.rb +27 -8
- data/app/views/janela/panes/_form.html.erb +6 -0
- data/app/views/janela/queries/_query.html.erb +1 -1
- data/config/locales/en.yml +3 -0
- data/db/migrate/20261123000001_add_show_under_min_rows_to_janela_panes.rb +8 -0
- data/docs/decisions/062-a-time-pane-can-accumulate-and-carry-more-series-on-two-axes.md +1 -1
- data/docs/decisions/063-a-pane-can-ignore-a-filter-its-model-cannot-apply.md +1 -1
- data/docs/decisions/064-a-measure-can-count-distinct-values-divide-two-sums-and-require-a-minimum-of-rows.md +193 -0
- data/docs/decisions/065-a-measure-can-carry-its-own-condition-so-one-pane-is-narrowed-and-not-the-frame.md +123 -0
- data/docs/decisions/INDEX.md +8 -6
- data/docs/roadmap.md +6 -4
- data/lib/janela/definition.rb +59 -11
- data/lib/janela/dimension.rb +8 -0
- data/lib/janela/doctor.rb +1 -1
- data/lib/janela/measure.rb +98 -15
- data/lib/janela/tools.rb +2 -1
- data/lib/janela/version.rb +1 -1
- metadata +21 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1ce5ee47a5b745fab7d6eeb91dde1580c2a90eae6579d66fa85d4b1db5e94927
|
|
4
|
+
data.tar.gz: ededf5d47e8685aca0b7498859a0f988ef493f1dd381758d929978e670706cb3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c12c8d343d7513c02b1211364a8d5925017b30ee75607aec902c20771c0b7956bd5cb193350bd2c5e129287b80146aeac4cdabe58ed1089d98c28544514fcca5
|
|
7
|
+
data.tar.gz: c7bd9d00cb05589c541a97831d013c3a6c11070bad22b223383f71e7c995b7574c291fd599914c94eace74b01517d4828b5a7da216e63f393c2bad72ee535d81
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.17.0] - 2026-11-23
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- **A measure can count distinct values.** `measure :stores_visited, count_distinct: :place_id` is `COUNT(DISTINCT place_id)`, read as a whole number and ordered like a count. It does not add up across buckets, so a running total of it is refused (#97, ADR 064).
|
|
13
|
+
- **A ratio can divide one sum by another.** `measure :to_target, ratio: :visits, over: :target` is the sum of visits over the sum of targets, read as a percentage and never clamped, so beating a target reads above 100%. Both columns must be numeric, checked at declaration. A group whose denominator sums to zero has no value, and sorts after those that have one (#97, ADR 064).
|
|
14
|
+
- **A measure can require a minimum of rows.** `min_rows: 50` on an average, a minimum, a maximum or a ratio hides a value worked out from too few rows. A category pane leaves those groups out before its sort and limit, so a bottom ten is ten groups that qualified. Over time a bucket under the minimum is a gap, so the axis does not change. A count, a sum or a count of distinct values refuses it, since they are true over any number of rows (#97, ADR 064).
|
|
15
|
+
- **A pane can list the groups under the minimum.** `show_under_min_rows: true` on `janela_pane`, the pane form, the pane URL and `Janela::Tools` lists them after every group that qualified, with a dash for their value, for a table that should show every group. Stored frames take a migration for it (#97, ADR 064, UPGRADING.md).
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- **No value reads as `–`.** A table cell or single figure with no value, such as an empty bucket of a ratio, was blank. An empty bucket and one under a minimum mean the same to a reader, and a blank cell reads as not loaded, so both are a dash now. A chart still draws a gap or no bar, and writes no label for one (ADR 064).
|
|
20
|
+
|
|
21
|
+
### Fixed
|
|
22
|
+
|
|
23
|
+
- **A single average, minimum or maximum over no rows reads as no value, not zero.** A single value pane showed `$0.00` for the average of nothing, a number nobody measured. A count or a sum over no rows is still zero (ADR 064).
|
|
24
|
+
|
|
25
|
+
## [0.16.2] - 2026-11-23
|
|
26
|
+
|
|
27
|
+
### Fixed
|
|
28
|
+
|
|
29
|
+
- **A value-labelled ratio's axis ends where the data does, not at 120%.** `value_labels` made room for the label above the tallest bar by adding 10% of grace to the value axis, so a ratio whose largest group was 95% drew an axis to 120%, a share no bar can reach. A ratio's labels now get their room from padding above the plot, so the axis ends at 100% (or wherever the data does). Every other measure keeps the grace, which also leaves room under a negative bar, and draws what it did (#96, ADR 054).
|
|
30
|
+
- **A pane is narrowed by a click on a dimension whose name starts with its own.** A pane sets aside the reader's filter on its own dimension, so the pane you clicked shows the alternatives. It recognised that filter by prefix, so a pane by `store` also set aside `store_type_in` and drew every store, unfiltered, after a click on a store type. Pairs like `category` and `category_group`, or `region` and `region_manager`, met the same thing. Only a key that is exactly the pane's own dimension and a Ransack predicate is set aside now. One visible difference: a misspelt predicate on the pane's own dimension (`store_inn` on a pane by store), which was quietly set aside with it, is now refused like a typo on any other key (#103, ADR 003, ADR 025).
|
|
31
|
+
- **A misspelt predicate on a key reached through an association is refused, not dropped, on a pane that ignores unknown filters.** On a pane with `ignore_unknown_filters`, `customer_region_inn` (a typo for `customer_region_in`) was dropped as if it belonged to another model, and the pane drew an unfiltered number with no error. A plain column with the same typo (`status_inn`) was already refused. A key reached through an association is now held to the same rule: refused with its reason when it starts with a dimension the model declares and ends with no predicate Ransack knows. Another model's key that only shares the prefix, such as `customer_region_code_in`, is still dropped (#94, ADR 063).
|
|
32
|
+
|
|
8
33
|
## [0.16.1] - 2026-11-02
|
|
9
34
|
|
|
10
35
|
### Added
|
|
@@ -326,6 +351,8 @@ First alpha, installed from GitHub for testing in a single host application.
|
|
|
326
351
|
- Only models that declare a `janela` block are addressable over HTTP.
|
|
327
352
|
- ADRs 001 to 004 in `docs/decisions/`, shipped inside the gem.
|
|
328
353
|
|
|
354
|
+
[0.17.0]: https://github.com/retail-tasker/janela/releases/tag/v0.17.0
|
|
355
|
+
[0.16.2]: https://github.com/retail-tasker/janela/releases/tag/v0.16.2
|
|
329
356
|
[0.16.1]: https://github.com/retail-tasker/janela/releases/tag/v0.16.1
|
|
330
357
|
[0.16.0]: https://github.com/retail-tasker/janela/releases/tag/v0.16.0
|
|
331
358
|
[0.15.0]: https://github.com/retail-tasker/janela/releases/tag/v0.15.0
|
data/README.md
CHANGED
|
@@ -27,7 +27,7 @@ Janela is an alpha on [rubygems.org](https://rubygems.org/gems/janela). It is a
|
|
|
27
27
|
|
|
28
28
|
```ruby
|
|
29
29
|
# Gemfile
|
|
30
|
-
gem "janela", "~> 0.
|
|
30
|
+
gem "janela", "~> 0.17"
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
```ruby
|
|
@@ -175,6 +175,22 @@ measure :expedited_rate, ratio: :expedited # 31.2%
|
|
|
175
175
|
|
|
176
176
|
It takes a boolean column, leaves rows where the column is null out of the average (as SQL's `AVG` does) rather than counting them as no, and orders, gap fills, cross-filters and snapshots like any other measure. The number it stores is the fraction, `0.3119`, and only the text a reader sees is a percentage, to one decimal place unless you declare `precision:`. It takes no `prefix:` or `suffix:`, since it is always a percentage, and it takes no condition (`ratio: { status: "paid" }`): where a column is not already a yes or no fact, a dimension gives you the split (ADR 038).
|
|
177
177
|
|
|
178
|
+
**Distinct values, one sum over another, and a minimum sample** are three more ways to declare a measure (ADR 064):
|
|
179
|
+
|
|
180
|
+
```ruby
|
|
181
|
+
measure :stores_visited, count_distinct: :place_id # 412
|
|
182
|
+
measure :to_target, ratio: :visits, over: :target # 104.5%
|
|
183
|
+
measure :in_stock_rate, ratio: :in_stock, min_rows: 50 # 87.3%, or – under 50 rows
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
`count_distinct:` counts distinct values of a column and reads as a whole number. Two months' distinct stores do not add up to the quarter's, so a running total of it is refused, as one of an average is.
|
|
187
|
+
|
|
188
|
+
`ratio:` with `over:` is the sum of one numeric column divided by the sum of another, read as a percentage like any ratio and never clamped: a store that beat its target reads above 100%, and the axis follows it. A group whose denominator sums to zero has no value.
|
|
189
|
+
|
|
190
|
+
`min_rows:` is how many rows a group needs before its value is shown, because a rate over three rows is noise that looks like a finding. It is for an average, a minimum, a maximum or a ratio, and refused on a count, a sum or a count of distinct values, which are true however few rows they are over. A category pane leaves a group under the minimum out before its sort and limit, so a bottom ten is ten groups that qualified. A pane that should list every group instead says `show_under_min_rows: true` (or ticks the box on the pane form, or adds `?show_under_min_rows=1` to the pane URL), and the groups under the minimum are listed after the rest with a dash. Over time, a bucket under the minimum is a gap on a line, no bar, and a dash in a table, so the axis is the same with or without it. A single figure under the minimum reads as a dash. A snapshot keeps what it was given and takes no `show_under_min_rows`.
|
|
191
|
+
|
|
192
|
+
Whatever a measure is, no value reads as `–`, whether the bucket was empty or under a minimum. Only a count or a sum over no rows reads as zero.
|
|
193
|
+
|
|
178
194
|
Then query them:
|
|
179
195
|
|
|
180
196
|
```ruby
|
|
@@ -390,7 +406,7 @@ A host with no tenancy defines nothing, gets a nil owner, and is correct: nothin
|
|
|
390
406
|
|
|
391
407
|
The dashboard's filters live in the page URL as the same `q[...]` parameters, so a reload keeps them and a filtered dashboard is a link you can send: `/reports/orders?q[status_in][]=paid` renders filtered before any JavaScript runs. A pane ignores filters on its own dimension, so clicking a value re-scopes the rest of the dashboard rather than collapsing the pane you clicked. Clicking a bucket on a time pane filters the others to the range it covers. Every selected value is marked `aria-pressed="true"` on tables and drawn solid against faded siblings on charts, so it can be styled and read.
|
|
392
408
|
|
|
393
|
-
**A frame over two models.** Every pane is handed the frame's filters, and a pane refuses a key its model does not declare, because Ransack would otherwise drop it and show an unfiltered number (ADR 025). On a frame that holds panes over two models that refusal reaches a click it should not: `paid` clicked on an orders pane is `q[status_in][]=paid` to a page views pane, which has no status and would answer "That request is not allowed on this pane" where its number was. A pane can say it ignores what it cannot apply: `janela_pane PageView, :views, by: :page, ignore_unknown_filters: true`, or the checkbox on the pane form, or `?ignore_unknown_filters=1` in the pane URL. The keys Ransack would drop are dropped before the query, a key the model does declare is applied as on any pane, a declared key with a predicate the dimension does not allow is still refused, and a host's `where:` and a frame's `default_where` are still refused when wrong. Unset, a pane is what it always was, so a typo on a pane that never meets another model still fails loudly. Two models share a click only when they spell the key the same, which for a dimension reached through an association is `<association>_<column>` (ADR 063).
|
|
409
|
+
**A frame over two models.** Every pane is handed the frame's filters, and a pane refuses a key its model does not declare, because Ransack would otherwise drop it and show an unfiltered number (ADR 025). On a frame that holds panes over two models that refusal reaches a click it should not: `paid` clicked on an orders pane is `q[status_in][]=paid` to a page views pane, which has no status and would answer "That request is not allowed on this pane" where its number was. A pane can say it ignores what it cannot apply: `janela_pane PageView, :views, by: :page, ignore_unknown_filters: true`, or the checkbox on the pane form, or `?ignore_unknown_filters=1` in the pane URL. The keys Ransack would drop are dropped before the query, a key the model does declare is applied as on any pane, a declared key with a predicate the dimension does not allow is still refused, including one reached through an association, and a host's `where:` and a frame's `default_where` are still refused when wrong. Unset, a pane is what it always was, so a typo on a pane that never meets another model still fails loudly. Two models share a click only when they spell the key the same, which for a dimension reached through an association is `<association>_<column>` (ADR 063).
|
|
394
410
|
|
|
395
411
|
**Selecting more than one.** Ctrl or Cmd click adds a value to the selection and takes it out again, leaving the rest alone, which is how every list in every operating system already behaves. A plain click selects one value and replaces whatever was selected, or clears the dimension if that value was the only one. It works the same on a chart. The (none) group, the rows that have nothing there, is a member of the selection like any value: Ctrl or Cmd click it beside a value and the panes show the rows that have that value or nothing (`q[channel_in][]=web&q[channel_null]=1`), where `not_in` and `not_null` together still mean neither (ADR 049). All of it works from the keyboard too: a value is a real `<button>`, so Enter is a click and Ctrl or Cmd with Enter adds. `Escape` clears the frame's filters, and those are the only two keys Janela binds, both only while focus is inside the frame, because a single letter belongs to your application and to any text field on the page (ADR 024). A pane with no matching rows renders a `.janela-empty` paragraph. A group whose dimension is null is labelled `(none)` and filters with Ransack's null predicate rather than an empty string. Only models that declare a `janela` block can be requested over HTTP.
|
|
396
412
|
|
|
@@ -581,7 +597,7 @@ Deliberately out of scope: natural-language query, a separate data warehouse, a
|
|
|
581
597
|
|
|
582
598
|
## Status
|
|
583
599
|
|
|
584
|
-
**v0.
|
|
600
|
+
**v0.17.0 alpha.** The measures/dimensions DSL, measures that count distinct values, divide two sums or need a minimum of rows, time dimensions, cross-filtering with multi-selection, bar, line, doughnut and pie charts, pane URLs, shareable dashboard URLs, snapshots, database-backed frames found by owner and key, panes that hold words or a host partial as well as a query, a host-fixed filter no click can remove and a frame's own permanent one beside it, STI subclasses, the engine's own pages for reading and editing them, bar charts that read in their measure's format and can show each bar's value, category panes that can be ordered either way by their measure or by their label, tools an agent can use to read and arrange a dashboard, and the optional vitral theme work and are covered by unit and real-browser tests, with the classes a theme may target documented in [Theming Janela](docs/theming.md). Not yet built: a visual editor, and narrowing a time pane's own granularity by clicking one of its buckets (a click on a bucket does filter the others). [Vista](docs/roadmap.md), the roadmap, says what 1.0 means and which of these are in it; open work is in [GitHub Issues](https://github.com/retail-tasker/janela/issues).
|
|
585
601
|
|
|
586
602
|
## Development
|
|
587
603
|
|
data/UPGRADING.md
CHANGED
|
@@ -12,6 +12,35 @@ bin/rails janela:doctor
|
|
|
12
12
|
|
|
13
13
|
It reads your application and lists what still needs changing.
|
|
14
14
|
|
|
15
|
+
## 0.16.2 to 0.17.0
|
|
16
|
+
|
|
17
|
+
One migration step, if you use stored frames, and one visible change.
|
|
18
|
+
|
|
19
|
+
**Take the pane migration.**
|
|
20
|
+
|
|
21
|
+
A category pane can list the groups under its measure's `min_rows` rather
|
|
22
|
+
than leaving them out (ADR 064). `janela_panes` gains a nullable boolean
|
|
23
|
+
`show_under_min_rows`:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
bin/rails janela:install:migrations
|
|
27
|
+
bin/rails db:migrate
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Every existing pane keeps it nil and is drawn exactly as before. If you never
|
|
31
|
+
use stored frames, there is nothing to do. `bin/rails janela:doctor` names the
|
|
32
|
+
column if a stored-frames host skips the step.
|
|
33
|
+
|
|
34
|
+
**No value reads as a dash.** Nothing to change. A table cell or a single
|
|
35
|
+
figure with no value, such as an empty bucket of a ratio or an average, was
|
|
36
|
+
blank and now reads `–`. A single average, minimum or maximum over no rows
|
|
37
|
+
read as zero (`$0.00`) and now reads `–` too, since there was no value to
|
|
38
|
+
show. A count or a sum over no rows still reads as zero. A host that asserts
|
|
39
|
+
on a blank cell, or screenshots one, will see the dash.
|
|
40
|
+
|
|
41
|
+
Nothing else changed: existing measures, panes, URLs and snapshots read as
|
|
42
|
+
they did.
|
|
43
|
+
|
|
15
44
|
## 0.15.0 to 0.16.0
|
|
16
45
|
|
|
17
46
|
One migration step, if you use stored frames.
|
|
@@ -35,6 +35,7 @@ export default class extends Controller {
|
|
|
35
35
|
// is decided here, when the chart is made: patched onto a chart built
|
|
36
36
|
// with it on, it draws at the wrong size.
|
|
37
37
|
maintainAspectRatio: !this.fixedHeightValue,
|
|
38
|
+
layout: this.labelRoomAbove() ? { padding: { top: Chart.defaults.font.size + 8 } } : undefined,
|
|
38
39
|
scales: this.scales(),
|
|
39
40
|
// A line is clicked anywhere along its x position rather than on
|
|
40
41
|
// the exact pixel of a point, which on a dense series is a few
|
|
@@ -125,7 +126,7 @@ export default class extends Controller {
|
|
|
125
126
|
const scales = { y: {
|
|
126
127
|
beginAtZero: true,
|
|
127
128
|
// Room above the tallest bar, and below the lowest, for its label (ADR 054).
|
|
128
|
-
grace: this.valueLabelsValue ? "10%" : undefined,
|
|
129
|
+
grace: this.valueLabelsValue && !this.labelRoomAbove() ? "10%" : undefined,
|
|
129
130
|
title: this.axisTitle(this.axesValue.left),
|
|
130
131
|
ticks
|
|
131
132
|
} }
|
|
@@ -146,6 +147,14 @@ export default class extends Controller {
|
|
|
146
147
|
return scales
|
|
147
148
|
}
|
|
148
149
|
|
|
150
|
+
// A ratio's labels get their room from padding above the plot rather than
|
|
151
|
+
// from grace on the scale, which took a share of 95% to an axis of 120%
|
|
152
|
+
// (#96). A ratio is never negative, so nothing is needed below. Other
|
|
153
|
+
// measures keep the grace, which also leaves room under a negative bar.
|
|
154
|
+
labelRoomAbove() {
|
|
155
|
+
return this.valueLabelsValue && this.tickFormatValue.ratio === true
|
|
156
|
+
}
|
|
157
|
+
|
|
149
158
|
axisTitle(text) {
|
|
150
159
|
return text ? { display: true, text } : undefined
|
|
151
160
|
}
|
|
@@ -172,7 +181,8 @@ export default class extends Controller {
|
|
|
172
181
|
const points = chart.getDatasetMeta(index).data
|
|
173
182
|
if (dataset.type === "line") {
|
|
174
183
|
const last = points.at(-1)
|
|
175
|
-
|
|
184
|
+
// A line that ends in a gap has no latest value to write (ADR 064).
|
|
185
|
+
if (!last || formatted.length === 0 || dataset.data.at(-1) == null) return
|
|
176
186
|
ctx.textAlign = "right"
|
|
177
187
|
ctx.textBaseline = "bottom"
|
|
178
188
|
ctx.fillText(formatted.at(-1), last.x, last.y - 6)
|
|
@@ -190,6 +200,8 @@ export default class extends Controller {
|
|
|
190
200
|
|
|
191
201
|
ctx.textAlign = "center"
|
|
192
202
|
bars.forEach((bar, index) => {
|
|
203
|
+
// No value is no bar, and so no label either (ADR 064).
|
|
204
|
+
if (values[index] == null) return
|
|
193
205
|
const negative = values[index] < 0
|
|
194
206
|
ctx.textBaseline = negative ? "top" : "bottom"
|
|
195
207
|
ctx.fillText(formatted[index], bar.x, negative ? bar.y + 4 : bar.y - 4)
|
|
@@ -76,7 +76,7 @@ module Janela
|
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
def pane_params
|
|
79
|
-
params.expect(pane: [ :kind, :model, :measure, :dimension, :renderer, :granularity, :limit, :sort, :height, :prominence, :value_labels, :cumulative, :ignore_unknown_filters, :span, :title,
|
|
79
|
+
params.expect(pane: [ :kind, :model, :measure, :dimension, :renderer, :granularity, :limit, :sort, :height, :prominence, :value_labels, :cumulative, :ignore_unknown_filters, :show_under_min_rows, :span, :title,
|
|
80
80
|
:heading, :body, :link, :partial, { companions: [], series: [ Janela::Query::SERIES_KEYS.map(&:to_sym) ] } ])
|
|
81
81
|
end
|
|
82
82
|
|
|
@@ -42,14 +42,19 @@ module Janela
|
|
|
42
42
|
# ignore_unknown_filters: drops the reader's filters this pane's model cannot
|
|
43
43
|
# apply rather than refusing them, for a pane beside panes over another
|
|
44
44
|
# model (ADR 063).
|
|
45
|
-
|
|
45
|
+
# show_under_min_rows: lists a category pane's groups under its measure's
|
|
46
|
+
# min_rows as dashes after every group that qualified, rather than leaving
|
|
47
|
+
# them out. It is part of the frame's id, since it is another answer
|
|
48
|
+
# (ADR 064).
|
|
49
|
+
def janela_pane(model, measure, by: nil, as: :table, granularity: nil, limit: nil, sort: nil, height: nil, prominence: nil, value_labels: nil, companions: nil, cumulative: nil, series: nil, ignore_unknown_filters: nil, show_under_min_rows: nil, id: nil)
|
|
46
50
|
query = { as: (as unless as.to_s == "table"), granularity: granularity, limit: limit, sort: sort, height: height, prominence: prominence,
|
|
47
51
|
value_labels: (1 if value_labels), companions: companions.presence, cumulative: janela_cumulative_param(cumulative), series: janela_series_params(series),
|
|
48
|
-
ignore_unknown_filters: (1 if ignore_unknown_filters),
|
|
52
|
+
ignore_unknown_filters: (1 if ignore_unknown_filters), show_under_min_rows: (1 if show_under_min_rows),
|
|
49
53
|
where: @janela_fixed_filters.presence }.compact
|
|
50
54
|
base = janela_routes.pane_path(model.model_name.route_key, measure, by, **query)
|
|
51
55
|
|
|
52
|
-
turbo_frame_tag id || Query.turbo_frame_id(model: model, measure: measure, by: by, as: as, granularity: granularity, limit: limit, sort: sort, cumulative: cumulative, series: series
|
|
56
|
+
turbo_frame_tag id || Query.turbo_frame_id(model: model, measure: measure, by: by, as: as, granularity: granularity, limit: limit, sort: sort, cumulative: cumulative, series: series,
|
|
57
|
+
show_under_min_rows: show_under_min_rows),
|
|
53
58
|
src: janela_with_page_filters(base),
|
|
54
59
|
loading: :lazy,
|
|
55
60
|
data: { janela__frame_target: "pane", janela_src: base }
|
data/app/models/janela/pane.rb
CHANGED
|
@@ -94,7 +94,7 @@ module Janela
|
|
|
94
94
|
def query(filters: {}, fixed: {}, renderer: self.renderer)
|
|
95
95
|
Query.new(definition: definition, measure: measure.to_sym, dimension: dimension.presence&.to_sym,
|
|
96
96
|
renderer: renderer, granularity: granularity, limit: limit, sort: sort, height: height, prominence: prominence, value_labels: value_labels, companions: companions,
|
|
97
|
-
cumulative: cumulative, series: series, ignore_unknown_filters: ignore_unknown_filters, filters: filters, fixed: fixed,
|
|
97
|
+
cumulative: cumulative, series: series, ignore_unknown_filters: ignore_unknown_filters, show_under_min_rows: show_under_min_rows, filters: filters, fixed: fixed,
|
|
98
98
|
default: frame.default_for(definition.model), title: title)
|
|
99
99
|
end
|
|
100
100
|
|
data/app/models/janela/query.rb
CHANGED
|
@@ -46,7 +46,7 @@ module Janela
|
|
|
46
46
|
MAX_BUCKETS = 10_000
|
|
47
47
|
Series = Struct.new(:measure, :dimension, :cumulative, :renderer, :axis, :title, :values, keyword_init: true)
|
|
48
48
|
|
|
49
|
-
attr_reader :definition, :measure, :dimension, :renderer, :limit, :sort, :height, :prominence, :value_labels, :companions, :cumulative, :series, :ignore_unknown_filters, :filters, :fixed, :default, :snapshot
|
|
49
|
+
attr_reader :definition, :measure, :dimension, :renderer, :limit, :sort, :height, :prominence, :value_labels, :companions, :cumulative, :series, :ignore_unknown_filters, :show_under_min_rows, :filters, :fixed, :default, :snapshot
|
|
50
50
|
|
|
51
51
|
# The helper renders the turbo frame and the controller renders its
|
|
52
52
|
# replacement, so both derive the id the same way from the same parameters.
|
|
@@ -55,11 +55,14 @@ module Janela
|
|
|
55
55
|
# sort is in ADR 055). Two panes that differ only in their series are two
|
|
56
56
|
# frames too, and a series is a hash, so it joins the id as a short digest
|
|
57
57
|
# of what was given.
|
|
58
|
-
|
|
58
|
+
#
|
|
59
|
+
# Kept groups under a minimum are another answer too, so a set
|
|
60
|
+
# show_under_min_rows joins the id (ADR 064).
|
|
61
|
+
def self.turbo_frame_id(model:, measure:, by: nil, as: :table, granularity: nil, limit: nil, sort: nil, cumulative: nil, series: nil, show_under_min_rows: nil, snapshot: nil)
|
|
59
62
|
parts = [ "janela", ("snapshot_#{snapshot.id}" if snapshot), model.model_name.route_key, measure, by,
|
|
60
63
|
(granularity if by), (as unless by.nil?), ("top#{limit}" if by && limit),
|
|
61
64
|
(sort if by && sort.present? && sort.to_s != "desc"), ("cumulative" if by && flag!("cumulative", cumulative)),
|
|
62
|
-
(series_digest(series) if by) ]
|
|
65
|
+
(series_digest(series) if by), ("kept" if by && flag!("show_under_min_rows", show_under_min_rows)) ]
|
|
63
66
|
parts.compact.join("_")
|
|
64
67
|
end
|
|
65
68
|
|
|
@@ -90,7 +93,7 @@ module Janela
|
|
|
90
93
|
end
|
|
91
94
|
end
|
|
92
95
|
|
|
93
|
-
def initialize(definition:, measure:, dimension: nil, renderer: "table", granularity: nil, limit: nil, sort: nil, height: nil, prominence: nil, value_labels: nil, companions: nil, cumulative: nil, series: nil, ignore_unknown_filters: nil, filters: {}, fixed: {}, default: {}, snapshot: nil, title: nil)
|
|
96
|
+
def initialize(definition:, measure:, dimension: nil, renderer: "table", granularity: nil, limit: nil, sort: nil, height: nil, prominence: nil, value_labels: nil, companions: nil, cumulative: nil, series: nil, ignore_unknown_filters: nil, show_under_min_rows: nil, filters: {}, fixed: {}, default: {}, snapshot: nil, title: nil)
|
|
94
97
|
@definition = definition
|
|
95
98
|
@title = title
|
|
96
99
|
@measure = measure
|
|
@@ -112,6 +115,9 @@ module Janela
|
|
|
112
115
|
@prominence = prominence!(prominence) if prominence.present?
|
|
113
116
|
@value_labels = flag!("value_labels", value_labels)
|
|
114
117
|
@ignore_unknown_filters = flag!("ignore_unknown_filters", ignore_unknown_filters)
|
|
118
|
+
# Only a category pane leaves groups out, and only under a measure with a
|
|
119
|
+
# minimum, so anywhere else the option has nothing to keep (ADR 064).
|
|
120
|
+
@show_under_min_rows = flag!("show_under_min_rows", show_under_min_rows) && keeps_groups?
|
|
115
121
|
@companions = companions!(companions)
|
|
116
122
|
@cumulative = cumulative!(cumulative)
|
|
117
123
|
@series = series!(series)
|
|
@@ -128,6 +134,13 @@ module Janela
|
|
|
128
134
|
definition.measure!(measure).format(value)
|
|
129
135
|
end
|
|
130
136
|
|
|
137
|
+
# A single figure over no rows: zero for a count or a sum, and no value
|
|
138
|
+
# for anything else. An average of nothing is not $0.00, and neither is
|
|
139
|
+
# one under its minimum (ADR 062, ADR 064).
|
|
140
|
+
def single(value)
|
|
141
|
+
value.nil? && definition.measure!(measure).additive? ? 0 : value
|
|
142
|
+
end
|
|
143
|
+
|
|
131
144
|
# What a chart's value axis needs to read like the measure's own numbers
|
|
132
145
|
# (ADR 054). The ticks are chosen in the browser and so cannot be formatted
|
|
133
146
|
# here; this is the part of the format a tick can use. Precision is left out
|
|
@@ -260,7 +273,8 @@ module Janela
|
|
|
260
273
|
|
|
261
274
|
def turbo_frame_id
|
|
262
275
|
self.class.turbo_frame_id(model: model, measure: measure, by: dimension, as: renderer,
|
|
263
|
-
granularity: @granularity, limit: limit, sort: sort, cumulative: cumulative, series: @series_given,
|
|
276
|
+
granularity: @granularity, limit: limit, sort: sort, cumulative: cumulative, series: @series_given,
|
|
277
|
+
show_under_min_rows: show_under_min_rows, snapshot: snapshot)
|
|
264
278
|
end
|
|
265
279
|
|
|
266
280
|
# A pane row may carry its own title, which is the first analyst authored
|
|
@@ -353,7 +367,8 @@ module Janela
|
|
|
353
367
|
# selection, which this pane shows the alternatives to (ADR 040, 043).
|
|
354
368
|
on = definition.narrow(on || model.all, default) if default.present?
|
|
355
369
|
on = definition.narrow(on || model.all, fixed) if fixed.present?
|
|
356
|
-
primary = time? ? time_result(on) : definition.query(measure, by: dimension, where: applicable_filters, on: on, granularity: granularity, limit: limit, sort: sort
|
|
370
|
+
primary = time? ? time_result(on) : definition.query(measure, by: dimension, where: applicable_filters, on: on, granularity: granularity, limit: limit, sort: sort,
|
|
371
|
+
show_under_min_rows: show_under_min_rows)
|
|
357
372
|
@companion_columns = companions? ? fetch_companions(primary, on) : []
|
|
358
373
|
primary
|
|
359
374
|
end
|
|
@@ -579,6 +594,10 @@ module Janela
|
|
|
579
594
|
self.class.flag!(name, value)
|
|
580
595
|
end
|
|
581
596
|
|
|
597
|
+
def keeps_groups?
|
|
598
|
+
dimension && !time? && definition.measure!(measure).min_rows.present?
|
|
599
|
+
end
|
|
600
|
+
|
|
582
601
|
# A running total is across buckets, so only a time pane has one. On a
|
|
583
602
|
# category pane or a single value it is refused with its reason rather
|
|
584
603
|
# than ignored: a row whose dimension is changed to a category is told on
|
|
@@ -593,7 +612,7 @@ module Janela
|
|
|
593
612
|
|
|
594
613
|
def unsummable(name)
|
|
595
614
|
"cumulative applies to a measure that adds up, a count or a sum, and #{name.inspect} is " \
|
|
596
|
-
"#{definition.measure!(name.to_sym).
|
|
615
|
+
"#{definition.measure!(name.to_sym).kind}"
|
|
597
616
|
end
|
|
598
617
|
|
|
599
618
|
def series!(value)
|
|
@@ -657,7 +676,7 @@ module Janela
|
|
|
657
676
|
def applicable_filters
|
|
658
677
|
@applicable_filters ||= begin
|
|
659
678
|
given = ignore_unknown_filters ? definition.applicable(filters) : filters
|
|
660
|
-
single_value? ? given : given.reject { |key, _| key
|
|
679
|
+
single_value? ? given : given.reject { |key, _| dimension_definition.key?(key) }
|
|
661
680
|
end
|
|
662
681
|
end
|
|
663
682
|
end
|
|
@@ -125,6 +125,12 @@
|
|
|
125
125
|
<span class="janela-hint"><%= t("janela.ignore_unknown_filters.hint") %></span>
|
|
126
126
|
</div>
|
|
127
127
|
|
|
128
|
+
<div class="janela-field">
|
|
129
|
+
<%= form.check_box :show_under_min_rows %>
|
|
130
|
+
<%= form.label :show_under_min_rows %>
|
|
131
|
+
<span class="janela-hint"><%= t("janela.show_under_min_rows.hint") %></span>
|
|
132
|
+
</div>
|
|
133
|
+
|
|
128
134
|
<div class="janela-field">
|
|
129
135
|
<%= form.label :span %>
|
|
130
136
|
<%= form.select :span, Janela::Pane::SPANS.to_a %>
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<% if query.single_value? %>
|
|
4
4
|
<%= tag.p class: [ "janela-pane", "janela-value", ("janela-prominence-#{query.prominence}" if query.prominent?) ] do %>
|
|
5
5
|
<span class="janela-value-label"><%= query.title %></span>
|
|
6
|
-
<strong class="janela-value-number"><%= query.format(result
|
|
6
|
+
<strong class="janela-value-number"><%= query.format(query.single(result)) %></strong>
|
|
7
7
|
<% end %>
|
|
8
8
|
<% elsif result.empty? %>
|
|
9
9
|
<p class="janela-pane janela-empty"><%= query.title %>: no data</p>
|
data/config/locales/en.yml
CHANGED
|
@@ -28,6 +28,7 @@ en:
|
|
|
28
28
|
cumulative: Running total
|
|
29
29
|
series: More series
|
|
30
30
|
ignore_unknown_filters: Ignore filters this model cannot apply
|
|
31
|
+
show_under_min_rows: List groups with too few rows
|
|
31
32
|
title: Title
|
|
32
33
|
janela:
|
|
33
34
|
errors:
|
|
@@ -81,6 +82,8 @@ en:
|
|
|
81
82
|
hint: Up to three, drawn as columns in a table. A dimension shows a value only where every row of its group shares one.
|
|
82
83
|
ignore_unknown_filters:
|
|
83
84
|
hint: For a pane beside panes over another model. A click there names a dimension this model does not have, and this pane stays as it is instead of saying the request is not allowed. A filter nobody's model declares is still a mistake on every other pane.
|
|
85
|
+
show_under_min_rows:
|
|
86
|
+
hint: For a measure with a minimum of rows. Groups under it are left out unless this is ticked, and then they are listed after the rest with a dash for their value. A chart over time already shows them as gaps.
|
|
84
87
|
value_labels:
|
|
85
88
|
hint: Draws the value at the end of each bar of a bar chart, if every one fits, and a line's latest value at its end. A ring ignores it.
|
|
86
89
|
series:
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
class AddShowUnderMinRowsToJanelaPanes < ActiveRecord::Migration[8.0]
|
|
2
|
+
def change
|
|
3
|
+
# Whether a category pane lists the groups under its measure's min_rows,
|
|
4
|
+
# without a value and after every group that qualified, rather than
|
|
5
|
+
# leaving them out (ADR 064). Null is what every existing pane is: unset.
|
|
6
|
+
add_column :janela_panes, :show_under_min_rows, :boolean
|
|
7
|
+
end
|
|
8
|
+
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
Date: 2026-10-29
|
|
3
|
-
Status:
|
|
3
|
+
Status: Accepted
|
|
4
4
|
Related: ADR 002, ADR 005, ADR 006, ADR 007, ADR 009, ADR 018, ADR 020, ADR 029, ADR 037, ADR 045, ADR 046, ADR 047, ADR 051, ADR 054, ADR 055
|
|
5
5
|
Triggers:
|
|
6
6
|
- a running total, a cumulative line, a to-date figure over time
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
---
|
|
2
|
+
Date: 2026-11-20
|
|
3
|
+
Status: Accepted
|
|
4
|
+
Related: ADR 002, ADR 007, ADR 009, ADR 020, ADR 038, ADR 054, ADR 055, ADR 062, ADR 063
|
|
5
|
+
Triggers:
|
|
6
|
+
- adding a measure kind, or a sixth aggregate to the DSL
|
|
7
|
+
- counting distinct values rather than rows
|
|
8
|
+
- dividing one sum by another, or a rate against a target
|
|
9
|
+
- hiding or dashing a value worked out from too few rows
|
|
10
|
+
- choosing whether a pane lists the groups a minimum suppressed
|
|
11
|
+
- a ratio that can be above 100%
|
|
12
|
+
Topics: DSL, query layer, measures, formatting, ordering
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
# ADR 064: A Measure Can Count Distinct Values, Divide Two Sums, and Require a Minimum of Rows
|
|
16
|
+
|
|
17
|
+
## Context
|
|
18
|
+
|
|
19
|
+
#97, from a host building reporting frames over fact views. A measure
|
|
20
|
+
today is one of five aggregates, or `ratio:` of one boolean column
|
|
21
|
+
(ADR 038). Three things a reporting page needs cannot be declared:
|
|
22
|
+
|
|
23
|
+
- **A count of distinct values.** "Stores visited" is not "visits".
|
|
24
|
+
- **One sum over another.** Visits against target, or ranged lines against
|
|
25
|
+
lines expected. It reads as a percentage, and it can rightly be above
|
|
26
|
+
100%: a store that beat its target did not make an error.
|
|
27
|
+
- **A minimum sample.** A ratio over three rows is noise that looks like a
|
|
28
|
+
finding. The host's own rule is fifty observations before a rate is
|
|
29
|
+
shown.
|
|
30
|
+
|
|
31
|
+
Measured against the demo on `3a41a64` (Rails main, SQLite), with
|
|
32
|
+
`Order` grouped by `status`:
|
|
33
|
+
|
|
34
|
+
- `relation.distinct.count(:customer_id)` answers a hash per group,
|
|
35
|
+
through Groupdate per month too, and its grouped alias is
|
|
36
|
+
`count_customer_id`, the same alias ADR 007 already orders a count by.
|
|
37
|
+
Nothing new is needed for grouping, ordering or gap filling.
|
|
38
|
+
- A ratio of two sums cannot be one `relation.<aggregate>(expression)`
|
|
39
|
+
call: `SUM(a) / SUM(b)` is not an argument to any one aggregate, and
|
|
40
|
+
wrapping it nests aggregates. Two sums read separately and divided per
|
|
41
|
+
key are right, and Groupdate fills both with zero, so an empty bucket is
|
|
42
|
+
`0 / 0`, which has to be no value rather than an error. Ordering by the
|
|
43
|
+
expression itself, `SUM(a) * 1.0 / NULLIF(SUM(b), 0)`, works in an
|
|
44
|
+
`ORDER BY` beside a `GROUP BY`, which is how ADR 038 already orders a
|
|
45
|
+
ratio.
|
|
46
|
+
- `HAVING COUNT(*) >= 2` on a category pane returns only the groups that
|
|
47
|
+
qualify, before `limit`. On a time pane the same `HAVING` through
|
|
48
|
+
Groupdate returned `{}` where every day had one row: Groupdate fills
|
|
49
|
+
between the first and last bucket it is given, so with none it draws
|
|
50
|
+
nothing, and with some it draws a shorter axis. A minimum on a time
|
|
51
|
+
pane cannot be a `HAVING`.
|
|
52
|
+
- Groups under the minimum can be kept and still sorted after every
|
|
53
|
+
group that qualifies. `ORDER BY CASE WHEN COUNT(*) >= 2 THEN 0 ELSE 1
|
|
54
|
+
END, SUM(amount)` with `LIMIT 2` on the demo's three statuses (`paid`
|
|
55
|
+
with two rows, `pending` and `refunded` with one) put `paid` first in
|
|
56
|
+
both directions, then `pending` ascending or `refunded` descending. The
|
|
57
|
+
`HAVING` version of the same query returned `paid` alone.
|
|
58
|
+
|
|
59
|
+
### What was considered
|
|
60
|
+
|
|
61
|
+
**A free SQL expression measure** (`measure :x, sql: "..."`) would cover
|
|
62
|
+
all three and every future ask. Rejected: it is a second query language
|
|
63
|
+
beside Ransack (ADR 001, ADR 002), it cannot be checked at declaration,
|
|
64
|
+
and a forker reading it learns nothing about what it means.
|
|
65
|
+
|
|
66
|
+
**A ratio of sums as a new key, `divide: [:a, :b]`.** Rejected for the
|
|
67
|
+
name. A boolean ratio already is a division, the true rows over the known
|
|
68
|
+
rows, and reading the two as one idea with one name is simpler than two
|
|
69
|
+
words for it.
|
|
70
|
+
|
|
71
|
+
**`min_rows` always drawing a dash in a category pane.** A table row
|
|
72
|
+
reading "–" was the first shape asked for. Rejected as the only
|
|
73
|
+
behaviour, for how it meets `limit` and `sort`: sorted among the real
|
|
74
|
+
values, the dashes either take the places of real ones in a bottom ten
|
|
75
|
+
or fall out of the limit unseen. Rejected as the default too, because a
|
|
76
|
+
ranking is the commoner category pane and leaving the groups out is what
|
|
77
|
+
a ranking wants. Kept as a pane option, sorted last, below.
|
|
78
|
+
|
|
79
|
+
**Leaving the groups out, always.** Rejected because the same measure
|
|
80
|
+
answers two questions on two panes. A bottom ten wants the low-sample
|
|
81
|
+
groups gone. A table of every store wants them listed with "–", because
|
|
82
|
+
which stores lack the data is itself the finding. The measure cannot know
|
|
83
|
+
which pane it is on, so the choice is the pane's. A BI tool makes the
|
|
84
|
+
same split per visual ("show items with no data").
|
|
85
|
+
|
|
86
|
+
**`min_rows` on the pane rather than the measure.** Rejected. Fifty rows
|
|
87
|
+
is the rule for when the rate means anything, and it has to hold on every
|
|
88
|
+
pane, snapshot and agent read of that measure. A pane that could lower it
|
|
89
|
+
would show the noise the rule exists to hide. A pane chooses only whether
|
|
90
|
+
to list what the measure suppressed.
|
|
91
|
+
|
|
92
|
+
**A setting for what an empty value reads as.** Rejected. Formatting
|
|
93
|
+
belongs to the measure (ADR 020), and a blank cell against a dash is not
|
|
94
|
+
a judgement any pane needs to make differently.
|
|
95
|
+
|
|
96
|
+
**Clamping a ratio of sums at 100%.** Rejected. Over 100% is a real
|
|
97
|
+
answer, and a clamped one is a wrong number that looks right.
|
|
98
|
+
|
|
99
|
+
## Decision
|
|
100
|
+
|
|
101
|
+
**A measure can be `count_distinct:` a column, `ratio:` one column `over:`
|
|
102
|
+
another, and any measure that does not add up can require `min_rows:`.**
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
janela do
|
|
106
|
+
measure :visits, count: true
|
|
107
|
+
measure :stores_visited, count_distinct: :place_id
|
|
108
|
+
measure :to_target, ratio: :visits, over: :target
|
|
109
|
+
measure :in_stock_rate, ratio: :in_stock, min_rows: 50
|
|
110
|
+
end
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**`count_distinct:`** is `COUNT(DISTINCT column)`, a sixth aggregate. It
|
|
114
|
+
reads as a count (no decimal places, ADR 020) and orders by its count
|
|
115
|
+
alias. It is not additive: two months' distinct stores do not sum to the
|
|
116
|
+
quarter's, so `cumulative` refuses it as it refuses an average (ADR 062),
|
|
117
|
+
and a bucket with no rows is zero.
|
|
118
|
+
|
|
119
|
+
**`ratio: :a, over: :b`** is `SUM(a) / SUM(b)`, read by two sums and
|
|
120
|
+
divided per group. Both columns must be numeric, checked at declaration
|
|
121
|
+
the way ADR 038 checks a boolean. Without `over:`, `ratio:` is what it
|
|
122
|
+
is today. It is stored as the fraction and read as a percentage, exactly
|
|
123
|
+
as ADR 038 says, and is never clamped: 1.25 reads `125.0%`, and the
|
|
124
|
+
axis follows the data (#96). A group whose denominator sums to zero has
|
|
125
|
+
no value, as an empty bucket of a boolean ratio has none today. It
|
|
126
|
+
orders by the expression. It does not add up, so `cumulative` refuses it.
|
|
127
|
+
|
|
128
|
+
**`min_rows:`** is a whole number of rows a group needs before its value
|
|
129
|
+
is shown. It is allowed on an average, a minimum, a maximum and either
|
|
130
|
+
ratio, and refused on a sum, a count or a count distinct, which are
|
|
131
|
+
true however few rows they are over. Rows are the group's rows after
|
|
132
|
+
every filter, `COUNT(*)`.
|
|
133
|
+
|
|
134
|
+
- On a category pane a group under the minimum is **left out before**
|
|
135
|
+
`sort` and `limit`, as `HAVING COUNT(*) >= n`, so a bottom ten is ten
|
|
136
|
+
groups that qualified.
|
|
137
|
+
- A category pane can ask to **keep them**, with `show_under_min_rows:
|
|
138
|
+
true` (`janela_pane Fact, :in_stock_rate, by: :store,
|
|
139
|
+
show_under_min_rows: true`), a checkbox on the pane form,
|
|
140
|
+
`?show_under_min_rows=1` in the pane URL and a key in `Janela::Tools`.
|
|
141
|
+
A kept group has no value, reads `–`, and **sorts after every group
|
|
142
|
+
that qualified, whichever way the pane sorts**, so `limit` fills with
|
|
143
|
+
real values first and a dash is seen only when fewer groups qualify
|
|
144
|
+
than the limit allows. Among themselves they sort by label. A time
|
|
145
|
+
pane, a series and a single value ignore the option, since they
|
|
146
|
+
already keep a bucket under the minimum as a gap, and so does a pane
|
|
147
|
+
whose measure has no `min_rows`.
|
|
148
|
+
- On a time pane, a series (ADR 062) and a single value, a bucket under
|
|
149
|
+
the minimum **has no value**: read beside a count of its rows and set to
|
|
150
|
+
none after the read, so the axis is the same with or without it. It is
|
|
151
|
+
a gap on a line, no bar, and `–` in a table or a single value.
|
|
152
|
+
- A snapshot stores what the query returned, so a frozen value under the
|
|
153
|
+
minimum stays without one (ADR 009).
|
|
154
|
+
|
|
155
|
+
**One pane option, and otherwise none.** All three are declarations on
|
|
156
|
+
the model, the same way every measure option is (ADR 020), and a stored
|
|
157
|
+
pane names the measure and gets them. `show_under_min_rows` is the one
|
|
158
|
+
setting, and it earns itself the way `ignore_unknown_filters` did (ADR
|
|
159
|
+
063): it is a judgement about what one pane is for, which neither the
|
|
160
|
+
measure nor a convention can make for it.
|
|
161
|
+
|
|
162
|
+
## Consequences
|
|
163
|
+
|
|
164
|
+
- A reporting host can declare the three it asked for without a view per
|
|
165
|
+
question or a second model.
|
|
166
|
+
- `Measure` grows from five aggregates and a ratio to six and a ratio
|
|
167
|
+
with an optional denominator, plus one option. `Definition#query` reads
|
|
168
|
+
a ratio of sums in two queries, and a time pane with `min_rows` in two.
|
|
169
|
+
Both are bounded: the second query has the same `GROUP BY` as the first.
|
|
170
|
+
- A category pane with `min_rows` can show fewer rows than its `limit`
|
|
171
|
+
with nothing on the page saying why. That is the honest cost of leaving
|
|
172
|
+
groups out, and `show_under_min_rows` is the answer where it matters. A
|
|
173
|
+
note under the pane saying how many were left out is a later question,
|
|
174
|
+
not decided here.
|
|
175
|
+
- Kept groups cost a category pane a second read: the `HAVING` cannot be
|
|
176
|
+
used, so the order carries the minimum instead, and a count beside the
|
|
177
|
+
read says which groups to set to no value.
|
|
178
|
+
- `show_under_min_rows` is a column on `janela_panes`, so a host with
|
|
179
|
+
stored frames runs a migration, which makes the release that ships it
|
|
180
|
+
a minor one. `UPGRADING.md` carries the step and the doctor's
|
|
181
|
+
`unmigrated-columns` check learns the column, as it did for
|
|
182
|
+
`ignore_unknown_filters`.
|
|
183
|
+
- **`–` for no value.** Today an empty bucket of a ratio formats as an
|
|
184
|
+
empty string. A suppressed value and an empty one should read the same,
|
|
185
|
+
so both become `–`. That is a visible change to every host with a ratio
|
|
186
|
+
over empty buckets, small, and listed in the changelog.
|
|
187
|
+
- `Janela::Tools` and `describe_vocabulary` need nothing new: they list
|
|
188
|
+
measures by name (ADR 053).
|
|
189
|
+
- What would change this decision: a host needing a running ratio of
|
|
190
|
+
sums, which is meaningful (the running numerator over the running
|
|
191
|
+
denominator) and could be allowed later without changing these names;
|
|
192
|
+
or a host whose panes nearly all set `show_under_min_rows`, which would
|
|
193
|
+
argue for keeping as the default.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
---
|
|
2
|
+
Date: 2026-11-20
|
|
3
|
+
Status: Proposed
|
|
4
|
+
Related: ADR 002, ADR 003, ADR 020, ADR 025, ADR 040, ADR 043, ADR 051, ADR 062, ADR 064
|
|
5
|
+
Triggers:
|
|
6
|
+
- narrowing one pane without narrowing the frame around it
|
|
7
|
+
- two panes over the same model where only one is filtered
|
|
8
|
+
- a measure of only some rows, such as new products or one channel
|
|
9
|
+
- adding a where to a pane, or a filter column to janela_panes
|
|
10
|
+
Topics: DSL, query layer, measures, cross-filtering, frames
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# ADR 065: A Measure Can Carry Its Own Condition, So One Pane Is Narrowed and Not the Frame
|
|
14
|
+
|
|
15
|
+
## Context
|
|
16
|
+
|
|
17
|
+
#98. Every narrowing a host can apply today is frame-wide. A host's
|
|
18
|
+
`where:` (ADR 040) narrows every pane a `janela_frame` draws, and a
|
|
19
|
+
frame's `default_where` (ADR 043) narrows every pane over its model. A
|
|
20
|
+
frame cannot hold "sales" beside "sales of new products" over the same
|
|
21
|
+
model. The only way today is a second model or a second database view per
|
|
22
|
+
condition, which turns a one-word difference into a schema change.
|
|
23
|
+
|
|
24
|
+
Read against the code on `ced8ea9`: `Definition#query` and
|
|
25
|
+
`Definition#series` both start from `filter(on || model.all, where)`, and
|
|
26
|
+
`Definition#narrow` already applies a fixed set of Ransack conditions
|
|
27
|
+
under ADR 025's bounds (declared dimensions, allowed predicates, the value
|
|
28
|
+
limit), raising with its reason. A condition has one obvious place to go
|
|
29
|
+
in: the relation, before the grouping, in the two methods every pane
|
|
30
|
+
passes through.
|
|
31
|
+
|
|
32
|
+
### What was considered
|
|
33
|
+
|
|
34
|
+
**A `where:` on the pane.** The obvious reading of "fix a filter on one
|
|
35
|
+
pane". Rejected for its cost and for what it says. It is a new column on
|
|
36
|
+
`janela_panes`, a new parameter in the pane URL (ADR 037 counts both as
|
|
37
|
+
public surface), a field on the pane form and an attribute in
|
|
38
|
+
`Janela::Tools`, and it has to be told how it composes with the reader's
|
|
39
|
+
`q[...]` on the same key. And it puts the meaning in the wrong place:
|
|
40
|
+
"sales of new products" is a thing being measured, which a host names once
|
|
41
|
+
on the model, not a property of where a pane happens to sit. Two pages
|
|
42
|
+
wanting it would each write it again.
|
|
43
|
+
|
|
44
|
+
**A `filter:` beside the frame's `where:` that names a pane.** Rejected:
|
|
45
|
+
a hand-written frame has no pane identities to name, and a stored one
|
|
46
|
+
would need its own column anyway.
|
|
47
|
+
|
|
48
|
+
**A conditional aggregate in SQL**, `SUM(CASE WHEN ... THEN x END)`.
|
|
49
|
+
Rejected: Ransack builds `WHERE` conditions, not `CASE` expressions, so
|
|
50
|
+
this would be a second condition language beside Ransack's, which ADR 002
|
|
51
|
+
exists to avoid. It would also keep every group in a category pane with a
|
|
52
|
+
zero where it has no matching rows, which reads as a measured zero rather
|
|
53
|
+
than an absence.
|
|
54
|
+
|
|
55
|
+
## Decision
|
|
56
|
+
|
|
57
|
+
**A measure may carry `where:`, a hash of Ransack conditions on the
|
|
58
|
+
model's declared dimensions, and it measures only the rows that meet
|
|
59
|
+
them.**
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
janela do
|
|
63
|
+
measure :sales, sum: :amount
|
|
64
|
+
measure :new_product_sales, sum: :amount, where: { new_product_eq: true }
|
|
65
|
+
measure :online_orders, count: true, where: { channel_in: %w[web app] }
|
|
66
|
+
|
|
67
|
+
dimension :new_product
|
|
68
|
+
dimension :channel
|
|
69
|
+
end
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```erb
|
|
73
|
+
<%= janela_frame do %>
|
|
74
|
+
<%= janela_pane Sale, :sales, by: :region, as: :bar %>
|
|
75
|
+
<%= janela_pane Sale, :new_product_sales, by: :region, as: :bar %>
|
|
76
|
+
<% end %>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**It is applied as one more narrowing, before the grouping,** through
|
|
80
|
+
`Definition#narrow`, in `query` and `series`. So it composes with every
|
|
81
|
+
other narrowing the same way they compose with each other: a frame's
|
|
82
|
+
default, a host's `where:`, the reader's selection and the measure's own
|
|
83
|
+
condition are all ANDed. A reader's click that contradicts it (`no` on
|
|
84
|
+
`new_product` for a pane of new product sales) leaves that pane empty,
|
|
85
|
+
which is the true answer to the question asked.
|
|
86
|
+
|
|
87
|
+
**It is checked when the `janela` block ends,** against the dimensions the
|
|
88
|
+
block declared and ADR 025's predicate bounds, and a wrong condition raises
|
|
89
|
+
at boot with the same sentence a bad request would get. The check waits for
|
|
90
|
+
the end of the block because a measure is usually declared before the
|
|
91
|
+
dimensions it names.
|
|
92
|
+
|
|
93
|
+
**It travels with the measure.** A pane, a series (ADR 062), a stored
|
|
94
|
+
pane, a snapshot, a companion column's facts (ADR 051) and
|
|
95
|
+
`Janela::Tools` name the measure and get its condition. Nothing about a
|
|
96
|
+
pane, its URL or its form changes.
|
|
97
|
+
|
|
98
|
+
**A pane's own dimension is not exempt.** A measure narrowed to two
|
|
99
|
+
channels drawn `by: :channel` shows those two. The cross-filtering rule
|
|
100
|
+
(ADR 003) sets aside the reader's selection on a pane's own dimension;
|
|
101
|
+
the measure's condition is part of what the pane measures, not a
|
|
102
|
+
selection.
|
|
103
|
+
|
|
104
|
+
## Consequences
|
|
105
|
+
|
|
106
|
+
- A frame can hold a measure and a narrowed version of it side by side
|
|
107
|
+
with no new model, view or pane setting.
|
|
108
|
+
- A category pane of a narrowed measure shows only groups that have
|
|
109
|
+
matching rows, not every group with a zero. A host wanting every group
|
|
110
|
+
listed puts the unnarrowed measure beside it.
|
|
111
|
+
- The condition is the host's own code, read at boot, so it is not part
|
|
112
|
+
of ADR 025's request bounds in the sense of arriving over HTTP. It is
|
|
113
|
+
still held to them, because a typo in it would otherwise be a silently
|
|
114
|
+
unfiltered measure, the failure this library is built to refuse.
|
|
115
|
+
- The measure's name is the only label a reader sees, so it has to say
|
|
116
|
+
what is narrowed ("New product sales", not "Sales 2"). That is the
|
|
117
|
+
host's naming, as it is for every measure (ADR 020).
|
|
118
|
+
- Works with ADR 064's measures unchanged: a ratio over a condition is a
|
|
119
|
+
ratio of the rows that meet it, and `min_rows` counts those rows.
|
|
120
|
+
- What would change this decision: a host needing the narrowing chosen
|
|
121
|
+
per page rather than per measure, such as an analyst picking a filter
|
|
122
|
+
for one stored pane at run time. That is a pane-level `where:`, and it
|
|
123
|
+
would need its own ADR on the URL and the form.
|
data/docs/decisions/INDEX.md
CHANGED
|
@@ -22,12 +22,12 @@ them when in doubt: `grep -l "Triggers:.*fork" docs/decisions/*.md`.
|
|
|
22
22
|
|-------|------|
|
|
23
23
|
| **Vision, scope, forkability** | 001, 010, 012, 037, 052 |
|
|
24
24
|
| **Open-source & host-decoupling** | 001, 022, 036, 041, 052, 060, 061 |
|
|
25
|
-
| **DSL & query layer** | 002, 006, 007, 020, 025, 038, 044, 049, 051, 055, 062 |
|
|
25
|
+
| **DSL & query layer** | 002, 006, 007, 020, 025, 038, 044, 049, 051, 055, 062, 064, 065 |
|
|
26
26
|
| **Dependencies** | 002, 003, 004, 006, 017, 025, 053 |
|
|
27
27
|
| **Authorisation** | 002, 003, 004, 009, 017, 019, 022, 032, 033, 034, 035, 039, 040, 048, 053 |
|
|
28
28
|
| **Performance & storage** | 007, 017, 025, 048, 051 |
|
|
29
|
-
| **Ordering & formatting** | 007, 020, 038, 054, 055, 061, 062 |
|
|
30
|
-
| **Cross-filtering & Hotwire** | 003, 004, 005, 008, 024, 025, 040, 043, 044, 045, 048, 049, 063 |
|
|
29
|
+
| **Ordering & formatting** | 007, 020, 038, 054, 055, 061, 062, 064 |
|
|
30
|
+
| **Cross-filtering & Hotwire** | 003, 004, 005, 008, 024, 025, 040, 043, 044, 045, 048, 049, 063, 065 |
|
|
31
31
|
| **Layouts & views** | 011, 012, 016, 018, 020, 027, 039, 047, 050, 059 |
|
|
32
32
|
| **CSS & styling** | 016, 018, 023, 026, 027, 036, 042, 046, 047, 050, 062 |
|
|
33
33
|
| **Frames, panes & persistence** | 012, 013, 014, 019, 029, 030, 033, 039, 040, 041, 043, 044, 047, 048, 050, 051, 053, 054, 055, 062, 063 |
|
|
@@ -109,9 +109,11 @@ them when in doubt: `grep -l "Triggers:.*fork" docs/decisions/*.md`.
|
|
|
109
109
|
| 059 | A Host Can Tell Janela's Own Pages That Its Layout Draws Charts | 2026-10-14 | Proposed |
|
|
110
110
|
| 060 | The Demo Tells Search Engines What It Is, and Shows Its Own Adoption | 2026-10-14 | Proposed |
|
|
111
111
|
| 061 | The Demo Speaks Brazilian Portuguese Under a Prefix, and the Gem Stays English | 2026-10-14 | Proposed |
|
|
112
|
-
| 062 | A Time Pane Can Accumulate, and Carry More Series on Two Axes | 2026-10-29 |
|
|
113
|
-
| 063 | A Pane Can Ignore a Filter Its Model Cannot Apply | 2026-10-29 |
|
|
112
|
+
| 062 | A Time Pane Can Accumulate, and Carry More Series on Two Axes | 2026-10-29 | Accepted |
|
|
113
|
+
| 063 | A Pane Can Ignore a Filter Its Model Cannot Apply | 2026-10-29 | Accepted |
|
|
114
|
+
| 064 | A Measure Can Count Distinct Values, Divide Two Sums, and Require a Minimum of Rows | 2026-11-20 | Accepted |
|
|
115
|
+
| 065 | A Measure Can Carry Its Own Condition, So One Pane Is Narrowed and Not the Frame | 2026-11-20 | Proposed |
|
|
114
116
|
|
|
115
117
|
## Next number
|
|
116
118
|
|
|
117
|
-
Next ADR:
|
|
119
|
+
Next ADR: 066
|
data/docs/roadmap.md
CHANGED
|
@@ -156,10 +156,12 @@ chart can show each bar's value (ADR 054). 0.15.0 followed that: a category
|
|
|
156
156
|
pane can be sorted by its measure either way or by its label, so a bottom five
|
|
157
157
|
and a chart in date order can be drawn (ADR 055). 0.16.0 followed that: a time
|
|
158
158
|
pane can be a running total and carry more series on two axes, and a pane can
|
|
159
|
-
ignore a filter its model cannot apply (ADR 062, ADR 063).
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
159
|
+
ignore a filter its model cannot apply (ADR 062, ADR 063). 0.17.0 followed
|
|
160
|
+
that: a measure can count distinct values, divide one sum by another and need
|
|
161
|
+
a minimum of rows, and a pane can list the groups under that minimum (ADR 064).
|
|
162
|
+
All five came from a real install, and the last four add a pane option and a
|
|
163
|
+
URL parameter, so the soak starts again from 0.17.0. What 1.0 waits
|
|
164
|
+
on now is a week of real installs of 0.17.0 ([#71](https://github.com/retail-tasker/janela/issues/71)),
|
|
163
165
|
since much of that surface was added in the last days and has only met this
|
|
164
166
|
repository. When nothing surprising comes back, the surface is frozen and 1.0
|
|
165
167
|
is tagged. The agent tools are the one new piece of surface: the read tools are
|
data/lib/janela/definition.rb
CHANGED
|
@@ -49,11 +49,14 @@ module Janela
|
|
|
49
49
|
# keys: narrows a categorical grouping to the labels a companion column is
|
|
50
50
|
# being fetched for (ADR 051), so it is the primary's rows and not the
|
|
51
51
|
# companion's own top 1000.
|
|
52
|
-
|
|
52
|
+
# show_under_min_rows: keeps a category pane's groups under the measure's
|
|
53
|
+
# min_rows, without a value and after every group that qualified, where
|
|
54
|
+
# they are otherwise left out (ADR 064).
|
|
55
|
+
def query(measure_name, by: nil, where: {}, on: nil, granularity: nil, limit: nil, keys: nil, sort: nil, show_under_min_rows: false)
|
|
53
56
|
measure = measure!(measure_name)
|
|
54
57
|
sort = sort!(sort)
|
|
55
58
|
relation = filter(on || model.all, where)
|
|
56
|
-
return measure
|
|
59
|
+
return single(measure, relation) if by.nil?
|
|
57
60
|
|
|
58
61
|
dimension = dimension!(by)
|
|
59
62
|
relation = relation.left_joins(dimension.through) if dimension.through
|
|
@@ -63,9 +66,13 @@ module Janela
|
|
|
63
66
|
bucketed(measure, dimension, relation, granularity).transform_keys { |bucket| dimension.label(bucket, granularity) }
|
|
64
67
|
else
|
|
65
68
|
relation = within_keys(relation, dimension, keys) if keys
|
|
66
|
-
|
|
69
|
+
keep = show_under_min_rows && measure.min_rows
|
|
70
|
+
grouped = relation.group(dimension.attribute).order(Arel.sql(ordering(measure, dimension, sort, keep: keep)))
|
|
71
|
+
grouped = grouped.having("COUNT(*) >= ?", measure.min_rows) if measure.min_rows && !keep
|
|
67
72
|
grouped = grouped.limit(limit ? limit!(limit) : MAXIMUM)
|
|
68
|
-
measure.apply(grouped)
|
|
73
|
+
result = measure.apply(grouped)
|
|
74
|
+
result = suppressed(result, relation.group(dimension.attribute).where(dimension.attribute.in(result.keys.compact).or(dimension.attribute.eq(nil))), measure) if keep
|
|
75
|
+
result.transform_keys { |value| value.nil? ? Dimension::NONE : value }
|
|
69
76
|
end
|
|
70
77
|
end
|
|
71
78
|
|
|
@@ -183,15 +190,48 @@ module Janela
|
|
|
183
190
|
# order and PostgreSQL last, and a label order would otherwise differ by
|
|
184
191
|
# host (ADR 055). The database sorts the column as its own type, so a
|
|
185
192
|
# number is in numeric order and a date in date order.
|
|
186
|
-
|
|
193
|
+
#
|
|
194
|
+
# A pane that keeps the groups under a measure's minimum puts them after
|
|
195
|
+
# every group that qualified, whichever way it sorts, so a limit fills
|
|
196
|
+
# with real values first. Their measure is left out of the order, so
|
|
197
|
+
# among themselves they fall to the label (ADR 064).
|
|
198
|
+
def ordering(measure, dimension, sort, keep: false)
|
|
187
199
|
label = dimension.quoted_column
|
|
188
200
|
by_label = "#{label} IS NULL, #{label}"
|
|
189
|
-
case sort
|
|
190
|
-
when "asc" then "#{measure
|
|
201
|
+
order = case sort
|
|
202
|
+
when "asc" then "#{measure_order(measure, 'ASC', keep)}, #{by_label} ASC"
|
|
191
203
|
when "label" then "#{by_label} ASC"
|
|
192
204
|
when "label_desc" then "#{by_label} DESC"
|
|
193
|
-
else "#{measure
|
|
205
|
+
else "#{measure_order(measure, 'DESC', keep)}, #{by_label} ASC"
|
|
194
206
|
end
|
|
207
|
+
keep ? "#{qualifies(measure)} DESC, #{order}" : order
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Kept, the measure is ordered only where the group qualified, and is
|
|
211
|
+
# otherwise none, so those under the minimum tie and fall to the label.
|
|
212
|
+
# An alias cannot sit inside a CASE, so this is the expression itself.
|
|
213
|
+
def measure_order(measure, direction, keep)
|
|
214
|
+
return measure.order_by(direction) unless keep
|
|
215
|
+
|
|
216
|
+
value = "CASE WHEN #{qualifies(measure)} = 1 THEN #{measure.expression} END"
|
|
217
|
+
"#{value} IS NULL, #{value} #{direction}"
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def qualifies(measure)
|
|
221
|
+
"(CASE WHEN COUNT(*) >= #{Integer(measure.min_rows)} THEN 1 ELSE 0 END)"
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# A single figure under the minimum has no value.
|
|
225
|
+
def single(measure, relation)
|
|
226
|
+
value = measure.apply(relation)
|
|
227
|
+
value unless measure.min_rows && relation.count < measure.min_rows
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# The groups or buckets whose rows are under the minimum, read beside the
|
|
231
|
+
# values and set to none, so what is kept is exactly what was read.
|
|
232
|
+
def suppressed(result, counted, measure)
|
|
233
|
+
rows = counted.count
|
|
234
|
+
result.to_h { |key, value| [ key, rows.fetch(key, 0) >= measure.min_rows ? value : nil ] }
|
|
195
235
|
end
|
|
196
236
|
|
|
197
237
|
def within_keys(relation, dimension, keys)
|
|
@@ -201,9 +241,15 @@ module Janela
|
|
|
201
241
|
relation.where(condition)
|
|
202
242
|
end
|
|
203
243
|
|
|
244
|
+
# A minimum on a time dimension cannot be a HAVING: Groupdate fills
|
|
245
|
+
# between the first and last bucket it is given, so the axis would
|
|
246
|
+
# shorten or vanish. The buckets are counted beside the read instead
|
|
247
|
+
# (ADR 064).
|
|
204
248
|
def bucketed(measure, dimension, relation, granularity)
|
|
205
249
|
options = granularity == "week" ? { week_start: Date.beginning_of_week } : {}
|
|
206
|
-
|
|
250
|
+
grouped = relation.group_by_period(granularity, dimension.qualified_column, **options)
|
|
251
|
+
result = measure.apply(grouped)
|
|
252
|
+
measure.min_rows ? suppressed(result, grouped, measure) : result
|
|
207
253
|
end
|
|
208
254
|
|
|
209
255
|
def filter(relation, params)
|
|
@@ -272,9 +318,11 @@ module Janela
|
|
|
272
318
|
|
|
273
319
|
# A key Ransack did not apply that is a declared dimension and a suffix
|
|
274
320
|
# that is not another attribute with a known predicate on it: path_inn is
|
|
275
|
-
# path with a typo, status_code_in is a different attribute entirely.
|
|
321
|
+
# path with a typo, status_code_in is a different attribute entirely. A
|
|
322
|
+
# dimension through an association is spelt <association>_<column> here as
|
|
323
|
+
# everywhere else, so it is held to the same rule (#94).
|
|
276
324
|
def misspelt_declared_key?(key)
|
|
277
|
-
dimensions.values.
|
|
325
|
+
dimensions.values.any? do |dimension|
|
|
278
326
|
rest = key.to_s.delete_prefix("#{dimension.ransack_name}_")
|
|
279
327
|
rest != key.to_s && Ransack.predicates.keys.none? { |predicate| rest.end_with?("_#{predicate}") }
|
|
280
328
|
end
|
data/lib/janela/dimension.rb
CHANGED
|
@@ -110,6 +110,14 @@ module Janela
|
|
|
110
110
|
through ? "#{through}_#{column}" : column.to_s
|
|
111
111
|
end
|
|
112
112
|
|
|
113
|
+
# Whether a filter key is this dimension's: its Ransack name and then a
|
|
114
|
+
# predicate Ransack knows, exactly. A prefix alone is not enough, since
|
|
115
|
+
# store_type_in starts with store and is another dimension's (#103).
|
|
116
|
+
def key?(key)
|
|
117
|
+
prefix = "#{ransack_name}_"
|
|
118
|
+
key.to_s.start_with?(prefix) && Ransack.predicates.has_key?(key.to_s.delete_prefix(prefix))
|
|
119
|
+
end
|
|
120
|
+
|
|
113
121
|
def label(bucket, granularity = self.granularity)
|
|
114
122
|
LABELS.fetch(granularity).call(bucket)
|
|
115
123
|
end
|
data/lib/janela/doctor.rb
CHANGED
|
@@ -157,7 +157,7 @@ module Janela
|
|
|
157
157
|
# the same thing twice.
|
|
158
158
|
ADDED_COLUMNS = {
|
|
159
159
|
"Janela::Frame" => %w[key default_model default_where],
|
|
160
|
-
"Janela::Pane" => %w[kind heading body link partial height prominence companions value_labels sort cumulative series ignore_unknown_filters],
|
|
160
|
+
"Janela::Pane" => %w[kind heading body link partial height prominence companions value_labels sort cumulative series ignore_unknown_filters show_under_min_rows],
|
|
161
161
|
"Janela::Snapshot" => %w[owner_type owner_id]
|
|
162
162
|
}.freeze
|
|
163
163
|
|
data/lib/janela/measure.rb
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
module Janela
|
|
2
2
|
class Measure
|
|
3
|
-
AGGREGATES = %i[sum count average minimum maximum].freeze
|
|
3
|
+
AGGREGATES = %i[sum count count_distinct average minimum maximum].freeze
|
|
4
4
|
# Aggregates whose answer is a number, so a boolean column would have its
|
|
5
5
|
# result cast back to true or false by ActiveRecord.
|
|
6
6
|
NUMERIC = %i[sum average].freeze
|
|
7
7
|
# Declared beside the aggregate, so they are taken out before the one
|
|
8
8
|
# remaining option is read as the aggregate.
|
|
9
9
|
FORMATS = %i[precision prefix suffix].freeze
|
|
10
|
+
# A count, a sum and a count of distinct values are true however few rows
|
|
11
|
+
# they are over. Only a measure worked out from its rows can be noise on
|
|
12
|
+
# too few of them (ADR 064).
|
|
13
|
+
SAMPLED = %i[average minimum maximum ratio].freeze
|
|
14
|
+
# What a measure reads as when it has no value: an empty bucket and one
|
|
15
|
+
# under its minimum are the same thing to a reader (ADR 064).
|
|
16
|
+
NO_VALUE = "–".freeze
|
|
10
17
|
# Where neither the aggregate nor the column says how many decimal places
|
|
11
18
|
# the measure means, two: enough for a rate or a currency, and short of
|
|
12
19
|
# the noise a float carries (issue #25).
|
|
@@ -16,10 +23,12 @@ module Janela
|
|
|
16
23
|
# and two is noise (ADR 038).
|
|
17
24
|
RATIO_PRECISION = 1
|
|
18
25
|
|
|
19
|
-
attr_reader :name, :aggregate, :column, :prefix, :suffix
|
|
26
|
+
attr_reader :name, :aggregate, :column, :over, :min_rows, :prefix, :suffix
|
|
20
27
|
|
|
21
28
|
def self.build(name, model: nil, **options)
|
|
22
29
|
format = options.extract!(*FORMATS)
|
|
30
|
+
over = options.delete(:over)
|
|
31
|
+
min_rows = options.delete(:min_rows)
|
|
23
32
|
aggregate, column = options.first
|
|
24
33
|
# A ratio sits beside the five aggregates in the same position rather
|
|
25
34
|
# than among them: it names a column and an intent, not an SQL function
|
|
@@ -28,24 +37,38 @@ module Janela
|
|
|
28
37
|
raise Error, "measure #{name.inspect} needs exactly one of #{AGGREGATES.join(', ')}, ratio"
|
|
29
38
|
end
|
|
30
39
|
|
|
31
|
-
new(name, aggregate, column == true ? nil : column, model: model, **format)
|
|
40
|
+
new(name, aggregate, column == true ? nil : column, model: model, over: over, min_rows: min_rows, **format)
|
|
32
41
|
end
|
|
33
42
|
|
|
34
|
-
def initialize(name, aggregate, column, model: nil, precision: nil, prefix: nil, suffix: nil)
|
|
43
|
+
def initialize(name, aggregate, column, model: nil, over: nil, min_rows: nil, precision: nil, prefix: nil, suffix: nil)
|
|
35
44
|
@name = name
|
|
36
45
|
@aggregate = aggregate
|
|
37
46
|
@column = column
|
|
47
|
+
@over = over
|
|
38
48
|
@model = model
|
|
39
49
|
@precision = precision!(precision)
|
|
40
50
|
@prefix = prefix
|
|
41
51
|
@suffix = suffix
|
|
52
|
+
raise Error, "measure #{name.inspect} takes over: only as a ratio, e.g. ratio: :visits, over: :target" if over && !ratio?
|
|
42
53
|
check_ratio! if ratio?
|
|
54
|
+
@min_rows = min_rows!(min_rows)
|
|
43
55
|
end
|
|
44
56
|
|
|
45
57
|
def ratio?
|
|
46
58
|
aggregate == :ratio
|
|
47
59
|
end
|
|
48
60
|
|
|
61
|
+
# One sum over another, rather than the share of rows where a boolean is
|
|
62
|
+
# true (ADR 064).
|
|
63
|
+
def ratio_of_sums?
|
|
64
|
+
ratio? && !over.nil?
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# How a reader names it in a refusal.
|
|
68
|
+
def kind
|
|
69
|
+
{ count_distinct: "a count of distinct values", average: "an average" }.fetch(aggregate) { "a #{aggregate}" }
|
|
70
|
+
end
|
|
71
|
+
|
|
49
72
|
# A count or a sum adds up across buckets: a running total of it means
|
|
50
73
|
# something, and a bucket with no rows is zero. An average, a minimum, a
|
|
51
74
|
# maximum or a ratio does not, and a bucket with no rows has no value
|
|
@@ -55,17 +78,23 @@ module Janela
|
|
|
55
78
|
end
|
|
56
79
|
|
|
57
80
|
def apply(relation)
|
|
58
|
-
return relation.
|
|
81
|
+
return divided(relation.sum(quoted(column)), relation.sum(quoted(over))) if ratio_of_sums?
|
|
82
|
+
return relation.average(boolean_share) if ratio?
|
|
83
|
+
return relation.distinct.count(column) if aggregate == :count_distinct
|
|
59
84
|
|
|
60
85
|
column ? relation.public_send(aggregate, column) : relation.public_send(aggregate)
|
|
61
86
|
end
|
|
62
87
|
|
|
63
|
-
# What a grouped relation is ordered by
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
|
|
68
|
-
|
|
88
|
+
# What a grouped relation is ordered by, in a direction. An aggregate has
|
|
89
|
+
# the column alias ActiveRecord gives a grouped calculation, and a ratio
|
|
90
|
+
# has no alias to name, so it orders by the expression itself (ADR 007,
|
|
91
|
+
# ADR 038). A ratio of sums with no denominator has no value, and sorts
|
|
92
|
+
# after every group that has one whichever way the pane sorts (ADR 064).
|
|
93
|
+
def order_by(direction = "DESC")
|
|
94
|
+
return "#{expression} #{direction}" if ratio? && !ratio_of_sums?
|
|
95
|
+
return "#{aggregate == :count_distinct ? 'count' : aggregate}_#{column || 'all'} #{direction}" unless ratio_of_sums?
|
|
96
|
+
|
|
97
|
+
"#{expression} IS NULL, #{expression} #{direction}"
|
|
69
98
|
end
|
|
70
99
|
|
|
71
100
|
# How many decimal places this measure means. Counting rows has none, and
|
|
@@ -80,7 +109,7 @@ module Janela
|
|
|
80
109
|
# format is declared later (ADR 009). Anything that is not a number is
|
|
81
110
|
# left alone, since minimum of a string is still that string.
|
|
82
111
|
def format(value)
|
|
83
|
-
return
|
|
112
|
+
return NO_VALUE if value.nil?
|
|
84
113
|
return value.to_s unless value.is_a?(Numeric)
|
|
85
114
|
# The number stays the fraction everywhere it is stored, ordered and
|
|
86
115
|
# compared; only this string is a percentage (ADR 038).
|
|
@@ -89,13 +118,39 @@ module Janela
|
|
|
89
118
|
"#{prefix}#{rounded(value)}#{suffix}"
|
|
90
119
|
end
|
|
91
120
|
|
|
121
|
+
# The measure as SQL, for ordering inside an expression where the alias
|
|
122
|
+
# cannot be named. Only a measure that can take a minimum needs it.
|
|
123
|
+
def expression
|
|
124
|
+
return "SUM(#{quoted(column)}) * 1.0 / NULLIF(SUM(#{quoted(over)}), 0)" if ratio_of_sums?
|
|
125
|
+
return "AVG(#{boolean_share})" if ratio?
|
|
126
|
+
|
|
127
|
+
"#{{ average: 'AVG', minimum: 'MIN', maximum: 'MAX' }.fetch(aggregate)}(#{quoted(column)})"
|
|
128
|
+
end
|
|
129
|
+
|
|
92
130
|
private
|
|
131
|
+
# Two sums keyed the same way, by a group or a bucket, or two numbers.
|
|
132
|
+
# Groupdate fills an empty bucket with zero in both, and no denominator
|
|
133
|
+
# is no value rather than an error or infinity.
|
|
134
|
+
def divided(numerators, denominators)
|
|
135
|
+
return quotient(numerators, denominators) unless numerators.is_a?(Hash)
|
|
136
|
+
|
|
137
|
+
numerators.to_h { |key, numerator| [ key, quotient(numerator, denominators[key]) ] }
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def quotient(numerator, denominator)
|
|
141
|
+
numerator.to_f / denominator.to_f unless denominator.nil? || denominator.zero?
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def quoted(name)
|
|
145
|
+
Arel.sql("#{@model.quoted_table_name}.#{@model.connection.quote_column_name(name)}")
|
|
146
|
+
end
|
|
147
|
+
|
|
93
148
|
# An unknown is not a failure: the null arm leaves it out of the average,
|
|
94
149
|
# which is exactly what AVG does with a null and what the naive CASE does
|
|
95
150
|
# not. The column is looked up in the model's schema at declaration and
|
|
96
151
|
# quoted here, never taken from anything that arrived over HTTP.
|
|
97
|
-
def
|
|
98
|
-
@
|
|
152
|
+
def boolean_share
|
|
153
|
+
@boolean_share ||= begin
|
|
99
154
|
field = "#{@model.quoted_table_name}.#{@model.connection.quote_column_name(column)}"
|
|
100
155
|
Arel.sql("CASE WHEN #{field} IS NULL THEN NULL WHEN #{field} THEN 1.0 ELSE 0.0 END")
|
|
101
156
|
end
|
|
@@ -109,6 +164,7 @@ module Janela
|
|
|
109
164
|
"no #{prefix ? 'prefix' : 'suffix'}. Declare a precision if one decimal place is not right."
|
|
110
165
|
end
|
|
111
166
|
return if @model.nil?
|
|
167
|
+
return check_sums! if ratio_of_sums?
|
|
112
168
|
|
|
113
169
|
type = @model.type_for_attribute(column.to_s).type
|
|
114
170
|
return if type == :boolean
|
|
@@ -120,6 +176,33 @@ module Janela
|
|
|
120
176
|
nil # no database to ask yet; a query will raise on its own if it cannot run
|
|
121
177
|
end
|
|
122
178
|
|
|
179
|
+
# Both sides of a ratio of sums are summed, so both must be numbers. A
|
|
180
|
+
# boolean would be cast back to true or false by ActiveRecord, the same
|
|
181
|
+
# reason a sum of one is refused.
|
|
182
|
+
def check_sums!
|
|
183
|
+
[ column, over ].each do |side|
|
|
184
|
+
type = @model.type_for_attribute(side.to_s).type
|
|
185
|
+
next if %i[integer decimal float].include?(type)
|
|
186
|
+
|
|
187
|
+
raise Error, "measure #{name.inspect} takes a ratio of #{column.inspect} over #{over.inspect}, and " \
|
|
188
|
+
"#{@model}##{side} is #{type ? "a #{type} column" : 'not a column'}. A ratio over another " \
|
|
189
|
+
"column divides two sums, so both must be numeric; ratio: alone is the share of a boolean."
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def min_rows!(value)
|
|
194
|
+
return if value.nil?
|
|
195
|
+
|
|
196
|
+
unless SAMPLED.include?(aggregate)
|
|
197
|
+
raise Error, "measure #{name.inspect} is #{kind}, which is true however few rows it is over, so it takes " \
|
|
198
|
+
"no min_rows. A minimum is for an average, a minimum, a maximum or a ratio."
|
|
199
|
+
end
|
|
200
|
+
rows = Integer(value, exception: false)
|
|
201
|
+
raise Error, "measure #{name.inspect} takes a min_rows of a whole number above zero, not #{value.inspect}" unless rows&.positive?
|
|
202
|
+
|
|
203
|
+
rows
|
|
204
|
+
end
|
|
205
|
+
|
|
123
206
|
def rounded(value)
|
|
124
207
|
ActiveSupport::NumberHelper.number_to_rounded(value, precision: precision,
|
|
125
208
|
delimiter: I18n.t("number.format.delimiter", default: ","))
|
|
@@ -128,7 +211,7 @@ module Janela
|
|
|
128
211
|
# Asking the schema rather than the value: one bucket of a measure can
|
|
129
212
|
# land on a whole number without the measure being a whole number.
|
|
130
213
|
def column_scale
|
|
131
|
-
return 0 if aggregate
|
|
214
|
+
return 0 if %i[count count_distinct].include?(aggregate) || column.nil?
|
|
132
215
|
|
|
133
216
|
type = @model&.type_for_attribute(column)
|
|
134
217
|
return if type.nil?
|
data/lib/janela/tools.rb
CHANGED
|
@@ -18,7 +18,7 @@ module Janela
|
|
|
18
18
|
# The attributes of a pane a caller may set: the ones the README names. The
|
|
19
19
|
# frame, the position and the timestamps are not an agent's to write.
|
|
20
20
|
PANE_ATTRIBUTES = %w[kind model measure dimension renderer granularity limit sort span title height prominence
|
|
21
|
-
value_labels companions cumulative series ignore_unknown_filters heading body link partial].freeze
|
|
21
|
+
value_labels companions cumulative series ignore_unknown_filters show_under_min_rows heading body link partial].freeze
|
|
22
22
|
# A frame's grid is the only part of a frame an agent may change (ADR 057).
|
|
23
23
|
# Its name, key and owner are the host's to say (ADR 033, ADR 041).
|
|
24
24
|
FRAME_ATTRIBUTES = %w[columns gap].freeze
|
|
@@ -244,6 +244,7 @@ module Janela
|
|
|
244
244
|
axis: { type: "string", enum: Query::AXES },
|
|
245
245
|
title: { type: "string" } } } },
|
|
246
246
|
ignore_unknown_filters: { type: "boolean", description: "Drop the reader's filters this pane's model cannot apply rather than refusing them, for a pane beside panes over another model" },
|
|
247
|
+
show_under_min_rows: { type: "boolean", description: "List a category pane's groups under its measure's min_rows with no value, after the rest, rather than leaving them out" },
|
|
247
248
|
heading: { type: "string" },
|
|
248
249
|
body: { type: "string" },
|
|
249
250
|
link: { type: "string", description: "A path on this site, starting with /" },
|
data/lib/janela/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: janela
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.17.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jay Killeen
|
|
@@ -150,6 +150,7 @@ files:
|
|
|
150
150
|
- db/migrate/20261013000001_add_sort_to_janela_panes.rb
|
|
151
151
|
- db/migrate/20261029000001_add_series_to_janela_panes.rb
|
|
152
152
|
- db/migrate/20261029000003_add_ignore_unknown_filters_to_janela_panes.rb
|
|
153
|
+
- db/migrate/20261123000001_add_show_under_min_rows_to_janela_panes.rb
|
|
153
154
|
- docs/agents.md
|
|
154
155
|
- docs/charts.md
|
|
155
156
|
- docs/composing.md
|
|
@@ -216,6 +217,8 @@ files:
|
|
|
216
217
|
- docs/decisions/061-the-demo-speaks-brazilian-portuguese-under-a-prefix-and-the-gem-stays-english.md
|
|
217
218
|
- docs/decisions/062-a-time-pane-can-accumulate-and-carry-more-series-on-two-axes.md
|
|
218
219
|
- docs/decisions/063-a-pane-can-ignore-a-filter-its-model-cannot-apply.md
|
|
220
|
+
- docs/decisions/064-a-measure-can-count-distinct-values-divide-two-sums-and-require-a-minimum-of-rows.md
|
|
221
|
+
- docs/decisions/065-a-measure-can-carry-its-own-condition-so-one-pane-is-narrowed-and-not-the-frame.md
|
|
219
222
|
- docs/decisions/INDEX.md
|
|
220
223
|
- docs/multi-tenancy.md
|
|
221
224
|
- docs/naming.md
|
|
@@ -243,6 +246,23 @@ metadata:
|
|
|
243
246
|
changelog_uri: https://github.com/retail-tasker/janela/blob/main/CHANGELOG.md
|
|
244
247
|
bug_tracker_uri: https://github.com/retail-tasker/janela/issues
|
|
245
248
|
rubygems_mfa_required: 'true'
|
|
249
|
+
post_install_message: |
|
|
250
|
+
Janela 0.17.0: one migration, if you use stored frames.
|
|
251
|
+
|
|
252
|
+
A measure can count distinct values, divide one sum by another and
|
|
253
|
+
need a minimum of rows, and a category pane can list the groups under
|
|
254
|
+
that minimum (ADR 064). That adds a nullable show_under_min_rows
|
|
255
|
+
column to janela_panes. Run bin/rails janela:install:migrations and
|
|
256
|
+
then bin/rails db:migrate. If you never use stored frames there is
|
|
257
|
+
nothing to do.
|
|
258
|
+
|
|
259
|
+
Also: no value now reads as a dash rather than a blank, and a single
|
|
260
|
+
average over no rows reads as a dash rather than zero.
|
|
261
|
+
|
|
262
|
+
Steps: UPGRADING.md in this gem, or
|
|
263
|
+
https://github.com/retail-tasker/janela/blob/main/UPGRADING.md
|
|
264
|
+
|
|
265
|
+
Then run: bin/rails janela:doctor
|
|
246
266
|
rdoc_options: []
|
|
247
267
|
require_paths:
|
|
248
268
|
- lib
|