Skip to content

Commit

Permalink
Support format_d3_locale in the metrics YAML (#5974)
Browse files Browse the repository at this point in the history
  • Loading branch information
begelundmuller authored Nov 13, 2024
1 parent 90f2079 commit 6c18b46
Show file tree
Hide file tree
Showing 9 changed files with 923 additions and 844 deletions.
1 change: 1 addition & 0 deletions docs/docs/reference/project-files/metrics-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ In your Rill project directory, create a metrics view, `<metrics_view>.yaml`, fi
- **`format_d3`** — controls the formatting of this measure using a [d3-format string](https://d3js.org/d3-format). If an invalid format string is supplied, measures will be formatted with `format_preset: humanize` (described below). Measures <u>cannot</u> have both `format_preset` and `format_d3` entries. _(optional; if neither `format_preset` nor `format_d3` is supplied, measures will be formatted with the `humanize` preset)_
- **Example**: to show a measure using fixed point formatting with 2 digits after the decimal point, your measure specification would include: `format_d3: ".2f"`.
- **Example**: to show a measure using grouped thousands with two significant digits, your measure specification would include: `format_d3: ",.2r"`.
- **`format_d3_locale`** — locale configuration passed through to D3, enabling changing the currency symbol among other things. For details, see the docs for D3's [`formatLocale`](https://d3js.org/d3-format#formatLocale). _(optional)_
- **`format_preset`** — controls the formatting of this measure according to option specified below. Measures <u>cannot</u> have both `format_preset` and `format_d3` entries. _(optional; if neither `format_preset` nor `format_d3` is supplied, measures will be formatted with the `humanize` preset)_
- `humanize` — round off numbers in an opinionated way to thousands (K), millions (M), billions (B), etc.
- `none` — raw output
Expand Down
1,531 changes: 772 additions & 759 deletions proto/gen/rill/runtime/v1/resources.pb.go

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions proto/gen/rill/runtime/v1/resources.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions proto/gen/rill/runtime/v1/runtime.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2995,6 +2995,8 @@ definitions:
type: string
formatD3:
type: string
formatD3Locale:
type: object
validPercentOfTotal:
type: boolean
title: Measures are aggregated computed values
Expand Down
1 change: 1 addition & 0 deletions proto/rill/runtime/v1/resources.proto
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ message MetricsViewSpec {
repeated string referenced_measures = 12;
string format_preset = 5;
string format_d3 = 7;
google.protobuf.Struct format_d3_locale = 13;
bool valid_percent_of_total = 6;
}
// Connector containing the table
Expand Down
23 changes: 19 additions & 4 deletions runtime/compilers/rillv1/parse_metrics_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/pkg/duration"
"google.golang.org/protobuf/types/known/structpb"
"gopkg.in/yaml.v3"

// Load IANA time zone data
Expand Down Expand Up @@ -50,10 +51,11 @@ type MetricsViewYAML struct {
Window *MetricsViewMeasureWindow
Per MetricsViewFieldSelectorsYAML
Requires MetricsViewFieldSelectorsYAML
FormatPreset string `yaml:"format_preset"`
FormatD3 string `yaml:"format_d3"`
Ignore bool `yaml:"ignore"` // Deprecated
ValidPercentOfTotal bool `yaml:"valid_percent_of_total"`
FormatPreset string `yaml:"format_preset"`
FormatD3 string `yaml:"format_d3"`
FormatD3Locale map[string]any `yaml:"format_d3_locale"`
Ignore bool `yaml:"ignore"` // Deprecated
ValidPercentOfTotal bool `yaml:"valid_percent_of_total"`
}
Security *SecurityPolicyYAML

Expand Down Expand Up @@ -571,6 +573,18 @@ func (p *Parser) parseMetricsView(node *Node) error {
return fmt.Errorf(`cannot set both "format_preset" and "format_d3" for a measure`)
}

var formatD3Locale *structpb.Struct
if measure.FormatD3Locale != nil {
if measure.FormatD3 == "" {
return fmt.Errorf(`"format_d3_locale" can only be set if "format_d3" is set`)
}

formatD3Locale, err = structpb.NewStruct(measure.FormatD3Locale)
if err != nil {
return fmt.Errorf(`invalid "format_d3_locale": %w`, err)
}
}

var perDimensions []*runtimev1.MetricsViewSpec_DimensionSelector
for _, per := range measure.Per {
typ, ok := names[strings.ToLower(per.Name)]
Expand Down Expand Up @@ -680,6 +694,7 @@ func (p *Parser) parseMetricsView(node *Node) error {
ReferencedMeasures: referencedMeasures,
FormatPreset: measure.FormatPreset,
FormatD3: measure.FormatD3,
FormatD3Locale: formatD3Locale,
ValidPercentOfTotal: measure.ValidPercentOfTotal,
})
}
Expand Down
11 changes: 10 additions & 1 deletion runtime/compilers/rillv1/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ dimensions:
measures:
- name: b
expression: count(*)
format_d3: "0,0"
format_d3_locale:
currency: ["£", ""]
first_day_of_week: 7
first_month_of_year: 3
`,
Expand Down Expand Up @@ -277,7 +280,13 @@ schema: default
{Name: "a", Column: "a"},
},
Measures: []*runtimev1.MetricsViewSpec_MeasureV2{
{Name: "b", Expression: "count(*)", Type: runtimev1.MetricsViewSpec_MEASURE_TYPE_SIMPLE},
{
Name: "b",
Expression: "count(*)",
Type: runtimev1.MetricsViewSpec_MEASURE_TYPE_SIMPLE,
FormatD3: "0,0",
FormatD3Locale: must(structpb.NewStruct(map[string]any{"currency": []any{"£", ""}})),
},
},
FirstDayOfWeek: 7,
FirstMonthOfYear: 3,
Expand Down
6 changes: 6 additions & 0 deletions web-common/src/proto/gen/rill/runtime/v1/resources_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,11 @@ export class MetricsViewSpec_MeasureV2 extends Message<MetricsViewSpec_MeasureV2
*/
formatD3 = "";

/**
* @generated from field: google.protobuf.Struct format_d3_locale = 13;
*/
formatD3Locale?: Struct;

/**
* @generated from field: bool valid_percent_of_total = 6;
*/
Expand All @@ -1640,6 +1645,7 @@ export class MetricsViewSpec_MeasureV2 extends Message<MetricsViewSpec_MeasureV2
{ no: 12, name: "referenced_measures", kind: "scalar", T: 9 /* ScalarType.STRING */, repeated: true },
{ no: 5, name: "format_preset", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 7, name: "format_d3", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 13, name: "format_d3_locale", kind: "message", T: Struct },
{ no: 6, name: "valid_percent_of_total", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
]);

Expand Down
Loading

1 comment on commit 6c18b46

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Please sign in to comment.