Skip to content

Commit

Permalink
feat: Trend and Value metrics should use formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
staylorwr committed Sep 16, 2020
1 parent 9299be5 commit 89797bd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
9 changes: 8 additions & 1 deletion assets/src/components/Metrics/TrendMetric.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { DateTime } from 'luxon';
import Duration from 'luxon/src/duration';
import Interval from 'luxon/src/interval';
import InteractsWithTheme from '@/mixins/InteractsWithTheme';
import numbro from 'numbro';
const FORMATS = {
minute: DateTime.TIME_SIMPLE,
Expand Down Expand Up @@ -130,14 +131,20 @@ export default {
yAxes: [ {
gridLines: {
drawBorder: false
},
ticks: {
callback: (value, index, values) => {
const elems = [ this.prefix, numbro(value).format(this.card.options.format) ].filter(v => v);
return elems.join('');
}
}
} ],
},
tooltips: {
mode: 'index',
callbacks: {
label: (item, data) => {
const elems = [ this.prefix, item.value, this.suffix ].filter(v => v);
const elems = [ this.prefix, numbro(item.value).format(this.card.options.format), this.suffix ].filter(v => v);
return `${data.datasets[item.datasetIndex].label}: ${elems.join('')}`;
},
labelColor: (item, chart) => {
Expand Down
8 changes: 5 additions & 3 deletions assets/src/components/Metrics/ValueMetric.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export default {
data: () => ({
loading: true,
format: '(0[.]00a)',
data: [],
prefix: '',
suffix: '',
Expand All @@ -86,6 +85,10 @@ export default {
ranges () {
return this.card.options.ranges;
},
format () {
return this.card.options.format;
}
},
watch: {
Expand Down Expand Up @@ -120,11 +123,10 @@ export default {
})).then(
({
data: {
metric: { data, prefix, suffix, format, multiple_results },
metric: { data, prefix, suffix, multiple_results },
},
}) => {
this.multiple_results = multiple_results;
this.format = format || this.format;
this.prefix = prefix || this.prefix;
this.suffix = suffix || this.suffix;
this.data = multiple_results ? data : [ { label: this.card.title, data } ];
Expand Down
7 changes: 4 additions & 3 deletions lib/ex_teal/metric/metric.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule ExTeal.Metric do

@doc """
A string representing how a value is formatted before being displayed by the
[numbro](https://numbrojs.com/) library. Defaults to `(0[.]00a)`
[numbro](https://numbrojs.com/) library. Defaults to `0,0`
"""
@callback format() :: String.t()

Expand Down Expand Up @@ -124,7 +124,7 @@ defmodule ExTeal.Metric do

def suffix, do: nil

def format, do: nil
def format, do: "0,0"

def component, do: "empty-metric"

Expand All @@ -135,7 +135,8 @@ defmodule ExTeal.Metric do
ranges: ranges(),
default_range: default_range(),
uri: uri(),
title: title()
title: title(),
format: format()
}

defoverridable(
Expand Down
6 changes: 6 additions & 0 deletions lib/ex_teal/metric/value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule ExTeal.Metric.Value do

alias ExTeal.Metric.{Request, Result, Value}

@impl true
def component, do: "value-metric"

@doc """
Expand Down Expand Up @@ -72,6 +73,11 @@ defmodule ExTeal.Metric.Value do
def sum(request, query, field) do
Value.aggregate(__MODULE__, request, query, :sum, field)
end

@impl true
def format, do: '(0[.]00a)'

defoverridable format: 0
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/ex_teal/metric/value_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule ExTeal.Metric.ValueTest do

describe "format/0" do
test "defaults to nil" do
assert TestExTeal.NewUserMetric.format() == nil
assert TestExTeal.NewUserMetric.format() == '(0[.]00a)'
end

test "can be overriden" do
Expand Down

0 comments on commit 89797bd

Please sign in to comment.