Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/oc_stat_view.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
unit :: oc_stat_measure:unit(),
subscribed = false :: boolean(),
description = "" :: description() | '_',
ctags = #{} :: oc_tags:tags() | '_',
tags = [] :: [oc_tags:key()] | '_',
aggregation :: aggregation() | '_',
aggregation_options = [] :: aggregation_options() | '_'}).
Expand All @@ -86,7 +85,6 @@
-type description() :: binary() | string().
-type view_data() :: #{name := name(),
description := description(),
ctags := oc_tags:tags(),
tags := [oc_tags:key()],
data := oc_stat_aggregation:data()}.
-type view() :: #view{}.
Expand All @@ -95,6 +93,12 @@
%% @doc
%% Creates a View from a map.
%% @end
-spec new(#{name => name(),
measure => measure_name() | oc_stat_measure:measure(),
description => description(),
unit := oc_stat_measure:unit(),
tags := [oc_tags:key()],
aggregation => aggregation()}) -> view().
new(Map) when is_map(Map) ->
new(maps:get(name, Map), maps:get(measure, Map), maps:get(unit, Map, undefined),
maps:get(description, Map), maps:get(tags, Map, []), maps:get(aggregation, Map)).
Expand All @@ -103,18 +107,27 @@ new(Map) when is_map(Map) ->
%% Creates a View. This view needs to be registered and subscribed to a measure
%% in order to start aggregating data.
%% @end
-spec new(name(),
measure_name() | oc_stat_measure:measure(),
description(),
[oc_tags:key()],
aggregation()) -> view().
new(Name, Measure, Description, Tags, Aggregation) ->
new(Name, Measure, undefined, Description, Tags, Aggregation).

-spec new(name(),
measure_name() | oc_stat_measure:measure(),
oc_stat_measure:unit(),
description(),
[oc_tags:key()],
aggregation()) -> view().
new(Name, Measure, Unit, Description, Tags, Aggregation) ->
{CTags, Keys} = normalize_tags(Tags),
{AggregationModule, AggregationOptions} = normalize_aggregation(Aggregation),
#view{name=Name,
measure=Measure,
unit=Unit,
description=Description,
ctags=CTags,
tags=Keys,
tags=Tags,
aggregation=AggregationModule,
aggregation_options=AggregationOptions}.

Expand Down Expand Up @@ -243,15 +256,14 @@ is_subscribed(Name) ->
-spec export(view()) -> view_data().
export(#view{name=Name, description=Description,
unit=VUnit, measure=Measure,
ctags=CTags, tags=Keys,
tags=Keys,
aggregation=AggregationModule,
aggregation_options=AggregationOptions}) ->
%% TODO: maybe just store multiplier as unit measure??
MUnit = oc_stat_measure:unit(Measure),
Data = AggregationModule:export(Name, AggregationOptions),
#{name => Name,
description => Description,
ctags => CTags,
tags => lists:reverse(Keys),
data => oc_stat_aggregation:convert(Data, MUnit, VUnit)}.

Expand Down Expand Up @@ -440,15 +452,3 @@ normalize_aggregation({Module, Options}) ->
{Module, Options};
normalize_aggregation(Module) when is_atom(Module) ->
{Module, []}.

normalize_tags([]) ->
{#{}, []};
normalize_tags(Tags) ->
normalize_tags(Tags, {#{}, []}).

normalize_tags([], {Map, List}) ->
{Map, lists:reverse(List)};
normalize_tags([First|Rest], {Map, List}) when is_map(First) ->
normalize_tags(Rest, {maps:merge(Map, First), List});
normalize_tags([First|Rest], {Map, List}) when is_atom(First) ->
normalize_tags(Rest, {Map, [First | List]}).
30 changes: 12 additions & 18 deletions test/oc_stat_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@

-define(VD, [#{name := "last_video_size",
description := "last processed video size",
ctags := #{ctag := value},
tags := [],
data := #{type := latest,
rows := [#{tags := [],
value := 1024}]}},
#{name := "video_size",
description :=
"number of videos processed processed over time",
ctags := #{ctag := value},
tags := [],
data := #{type := distribution,
rows :=
Expand All @@ -29,23 +27,21 @@
count := 6,
mean := 1024.5,
sum := 6147}}]}},
#{name := "video_count",
description :=
"number of videos processed processed over time",
ctags := #{ctag := value},
tags := [type],
data := #{type := count,
rows := [#{tags := ["mpeg"],
value := 6}]}},
#{name := "video_sum",
description := "video_size_sum",
ctags := #{sum_tag := value},
tags := [category, type],
data := #{type := sum,
rows := [#{tags := ["category1", "mpeg"],
value := #{count := 6,
mean := 1024.5,
sum := 6147}}]}}]).
sum := 6147}}]}},
#{name := "video_count",
description :=
"number of videos processed processed over time",
tags := [type],
data := #{type := count,
rows := [#{tags := ["mpeg"],
value := 6}]}}]).

all() ->
[
Expand Down Expand Up @@ -199,7 +195,7 @@ full(_Config) ->
#{
name => "video_size",
description => "number of videos processed processed over time",
tags => [#{ctag => value}],
tags => [],
measure => 'my.org/measures/video_size',
aggregation => {oc_stat_aggregation_distribution, [{buckets, [0, 1 bsl 16, 1 bsl 32]}]}
}),
Expand All @@ -208,23 +204,21 @@ full(_Config) ->
"video_count",
'my.org/measures/video_size',
"number of videos processed processed over time",
[#{ctag => value},
type],
[type],
oc_stat_aggregation_count),

{ok, _} = oc_stat_view:subscribe(
"video_sum",
'my.org/measures/video_size',
"video_size_sum",
[#{sum_tag => value},
type, category],
[type, category],
oc_stat_aggregation_sum),

{ok, _} = oc_stat_view:subscribe(
"last_video_size",
'my.org/measures/video_size',
"last processed video size",
[#{ctag => value}],
[],
oc_stat_aggregation_latest),

Tags = #{type => "mpeg",
Expand Down
2 changes: 0 additions & 2 deletions test/oc_stat_unit_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ conversion(_Config) ->
?assertMatch([#{name := http_request_duration_seconds,
description := "desc",
tags := [],
ctags := #{},
data := #{rows := [#{tags := [],
value := #{count := 2,
mean := 0.6006,
Expand All @@ -54,7 +53,6 @@ no_conversion(_Config) ->
?assertMatch([#{name := http_request_duration_microsecond,
description := "desc",
tags := [],
ctags := #{},
data := #{rows := [#{tags := [],
value := #{count := 2,
mean := 6.006e8,
Expand Down