janela 0.16.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f90ccaae7d4191d3b5bfa3db12db772656808a9b15908d73bef299beda109eef
4
- data.tar.gz: a3cd040d84114900904349d9bde09ad89fb264624a7997c3a9961e9947f8b266
3
+ metadata.gz: 1ce5ee47a5b745fab7d6eeb91dde1580c2a90eae6579d66fa85d4b1db5e94927
4
+ data.tar.gz: ededf5d47e8685aca0b7498859a0f988ef493f1dd381758d929978e670706cb3
5
5
  SHA512:
6
- metadata.gz: 6cdcc597e34ed611a8c9c2f448875ca9e5cd487f888b5bed8fe0c1e9f5e22728ad6f3b743da7c3b868b80b85a0c6565630e6604629fbe7d316c5d00a338213cb
7
- data.tar.gz: 0a96deb8421cc49ce7048cfe148ef2516fd2edd4bb91b24635ec172935db2d728ae4e3be871abfb346a6870b577b854505057909d35cabdfed7737e49ad722c4
6
+ metadata.gz: c12c8d343d7513c02b1211364a8d5925017b30ee75607aec902c20771c0b7956bd5cb193350bd2c5e129287b80146aeac4cdabe58ed1089d98c28544514fcca5
7
+ data.tar.gz: c7bd9d00cb05589c541a97831d013c3a6c11070bad22b223383f71e7c995b7574c291fd599914c94eace74b01517d4828b5a7da216e63f393c2bad72ee535d81
data/CHANGELOG.md CHANGED
@@ -5,6 +5,23 @@ 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
+
8
25
  ## [0.16.2] - 2026-11-23
9
26
 
10
27
  ### Fixed
@@ -334,6 +351,7 @@ First alpha, installed from GitHub for testing in a single host application.
334
351
  - Only models that declare a `janela` block are addressable over HTTP.
335
352
  - ADRs 001 to 004 in `docs/decisions/`, shipped inside the gem.
336
353
 
354
+ [0.17.0]: https://github.com/retail-tasker/janela/releases/tag/v0.17.0
337
355
  [0.16.2]: https://github.com/retail-tasker/janela/releases/tag/v0.16.2
338
356
  [0.16.1]: https://github.com/retail-tasker/janela/releases/tag/v0.16.1
339
357
  [0.16.0]: https://github.com/retail-tasker/janela/releases/tag/v0.16.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.16"
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
@@ -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.16.2 alpha.** The measures/dimensions DSL, 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).
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.
@@ -181,7 +181,8 @@ export default class extends Controller {
181
181
  const points = chart.getDatasetMeta(index).data
182
182
  if (dataset.type === "line") {
183
183
  const last = points.at(-1)
184
- if (!last || formatted.length === 0) return
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
185
186
  ctx.textAlign = "right"
186
187
  ctx.textBaseline = "bottom"
187
188
  ctx.fillText(formatted.at(-1), last.x, last.y - 6)
@@ -199,6 +200,8 @@ export default class extends Controller {
199
200
 
200
201
  ctx.textAlign = "center"
201
202
  bars.forEach((bar, index) => {
203
+ // No value is no bar, and so no label either (ADR 064).
204
+ if (values[index] == null) return
202
205
  const negative = values[index] < 0
203
206
  ctx.textBaseline = negative ? "top" : "bottom"
204
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
 
@@ -16,6 +16,7 @@ module Janela
16
16
  cumulative: params[:cumulative],
17
17
  series: series_params,
18
18
  ignore_unknown_filters: params[:ignore_unknown_filters],
19
+ show_under_min_rows: params[:show_under_min_rows],
19
20
  filters: filters,
20
21
  fixed: fixed_filters
21
22
  )
@@ -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
- 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, id: nil)
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 }
@@ -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
 
@@ -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
- def self.turbo_frame_id(model:, measure:, by: nil, as: :table, granularity: nil, limit: nil, sort: nil, cumulative: nil, series: nil, snapshot: nil)
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, snapshot: snapshot)
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).aggregate == :ratio ? 'a ratio' : "an #{definition.measure!(name.to_sym).aggregate}"}"
615
+ "#{definition.measure!(name.to_sym).kind}"
597
616
  end
598
617
 
599
618
  def series!(value)
@@ -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 || 0) %></strong>
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>
@@ -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,12 +1,13 @@
1
1
  ---
2
2
  Date: 2026-11-20
3
- Status: Proposed
4
- Related: ADR 002, ADR 007, ADR 009, ADR 020, ADR 038, ADR 054, ADR 055, ADR 062
3
+ Status: Accepted
4
+ Related: ADR 002, ADR 007, ADR 009, ADR 020, ADR 038, ADR 054, ADR 055, ADR 062, ADR 063
5
5
  Triggers:
6
6
  - adding a measure kind, or a sixth aggregate to the DSL
7
7
  - counting distinct values rather than rows
8
8
  - dividing one sum by another, or a rate against a target
9
9
  - hiding or dashing a value worked out from too few rows
10
+ - choosing whether a pane lists the groups a minimum suppressed
10
11
  - a ratio that can be above 100%
11
12
  Topics: DSL, query layer, measures, formatting, ordering
12
13
  ---
@@ -48,6 +49,12 @@ Measured against the demo on `3a41a64` (Rails main, SQLite), with
48
49
  between the first and last bucket it is given, so with none it draws
49
50
  nothing, and with some it draws a shorter axis. A minimum on a time
50
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.
51
58
 
52
59
  ### What was considered
53
60
 
@@ -61,13 +68,30 @@ name. A boolean ratio already is a division, the true rows over the known
61
68
  rows, and reading the two as one idea with one name is simpler than two
62
69
  words for it.
63
70
 
64
- **`min_rows` drawing a dash in a category pane.** A table row reading
65
- "–" was the first shape asked for. Rejected for how it meets `limit` and
66
- `sort`: on `sort: :asc, limit: 10`, the bottom ten would be chosen from
67
- groups that include the suppressed ones, so either the dashes take the
68
- places of real values or they are sorted out of the limit and never seen
69
- anyway. Leaving them out before the limit is the version that answers the
70
- question a bottom ten asks.
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.
71
95
 
72
96
  **Clamping a ratio of sums at 100%.** Rejected. Over 100% is a real
73
97
  answer, and a clamped one is a wrong number that looks right.
@@ -110,6 +134,17 @@ every filter, `COUNT(*)`.
110
134
  - On a category pane a group under the minimum is **left out before**
111
135
  `sort` and `limit`, as `HAVING COUNT(*) >= n`, so a bottom ten is ten
112
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`.
113
148
  - On a time pane, a series (ADR 062) and a single value, a bucket under
114
149
  the minimum **has no value**: read beside a count of its rows and set to
115
150
  none after the read, so the axis is the same with or without it. It is
@@ -117,9 +152,12 @@ every filter, `COUNT(*)`.
117
152
  - A snapshot stores what the query returned, so a frozen value under the
118
153
  minimum stays without one (ADR 009).
119
154
 
120
- **No pane or URL changes.** All three are declarations on the model, the
121
- same way every measure option is (ADR 020). A stored pane names the
122
- measure and gets them.
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.
123
161
 
124
162
  ## Consequences
125
163
 
@@ -131,8 +169,17 @@ measure and gets them.
131
169
  Both are bounded: the second query has the same `GROUP BY` as the first.
132
170
  - A category pane with `min_rows` can show fewer rows than its `limit`
133
171
  with nothing on the page saying why. That is the honest cost of leaving
134
- groups out. A note under the pane saying how many were left out is a
135
- later question, not decided here.
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`.
136
183
  - **`–` for no value.** Today an empty bucket of a ratio formats as an
137
184
  empty string. A suppressed value and an empty one should read the same,
138
185
  so both become `–`. That is a visible change to every host with a ratio
@@ -142,5 +189,5 @@ measure and gets them.
142
189
  - What would change this decision: a host needing a running ratio of
143
190
  sums, which is meaningful (the running numerator over the running
144
191
  denominator) and could be allowed later without changing these names;
145
- or one needing the left-out groups listed, which would argue for the
146
- dash again.
192
+ or a host whose panes nearly all set `show_under_min_rows`, which would
193
+ argue for keeping as the default.
@@ -111,7 +111,7 @@ them when in doubt: `grep -l "Triggers:.*fork" docs/decisions/*.md`.
111
111
  | 061 | The Demo Speaks Brazilian Portuguese Under a Prefix, and the Gem Stays English | 2026-10-14 | Proposed |
112
112
  | 062 | A Time Pane Can Accumulate, and Carry More Series on Two Axes | 2026-10-29 | Accepted |
113
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 | Proposed |
114
+ | 064 | A Measure Can Count Distinct Values, Divide Two Sums, and Require a Minimum of Rows | 2026-11-20 | Accepted |
115
115
  | 065 | A Measure Can Carry Its Own Condition, So One Pane Is Narrowed and Not the Frame | 2026-11-20 | Proposed |
116
116
 
117
117
  ## Next number
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). All four came from a
160
- real install, and the last three add a pane option and a URL parameter, so the
161
- soak starts again from 0.16.0. What 1.0 waits
162
- on now is a week of real installs of 0.16.0 ([#71](https://github.com/retail-tasker/janela/issues/71)),
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
@@ -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
- def query(measure_name, by: nil, where: {}, on: nil, granularity: nil, limit: nil, keys: nil, sort: nil)
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.apply(relation) if by.nil?
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
- grouped = relation.group(dimension.attribute).order(Arel.sql(ordering(measure, dimension, sort)))
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).transform_keys { |value| value.nil? ? Dimension::NONE : value }
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
- def ordering(measure, dimension, sort)
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.order_by} ASC, #{by_label} ASC"
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.order_by} DESC, #{by_label} ASC"
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
- measure.apply(relation.group_by_period(granularity, dimension.qualified_column, **options))
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)
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
 
@@ -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.average(ratio_expression) if ratio?
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 to put the measure's largest
64
- # first. An aggregate has the column alias ActiveRecord gives a grouped
65
- # calculation, and a ratio has no alias to name, so it orders by the
66
- # expression itself (ADR 007, ADR 038).
67
- def order_by
68
- ratio? ? "AVG(#{ratio_expression})" : "#{aggregate}_#{column || 'all'}"
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 "" if value.nil?
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 ratio_expression
98
- @ratio_expression ||= begin
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 == :count || column.nil?
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 /" },
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Janela
4
- VERSION = "0.16.2"
4
+ VERSION = "0.17.0"
5
5
  end
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.16.2
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
@@ -245,6 +246,23 @@ metadata:
245
246
  changelog_uri: https://github.com/retail-tasker/janela/blob/main/CHANGELOG.md
246
247
  bug_tracker_uri: https://github.com/retail-tasker/janela/issues
247
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
248
266
  rdoc_options: []
249
267
  require_paths:
250
268
  - lib