Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetMetricData implementation #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
49 changes: 49 additions & 0 deletions lib/ex_aws/cloudwatch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,37 @@ defmodule ExAws.Cloudwatch do
|> build_request(:get_metric_statistics)
end



@type get_metric_data_query :: [
id: binary,
metric_stat: get_metric_data_query_stat
]
@type get_metric_data_query_stat :: [
metric: get_metric_data_query_stat_metric,
period: integer,
stat: binary,
unit: binary
]
@type get_metric_data_query_stat_metric :: [
namespace: binary,
metric_name: binary,
dimensions: [dimension]
]
@spec get_metric_data(
start_time :: %DateTime{},
end_time :: %DateTime{},
queries :: [get_metric_data_query]
) :: ExAws.Operation.Query.t()
def get_metric_data(start_time, end_time, queries) do
[
{:start_time, start_time},
{:end_time, end_time},
{:metric_data_queries, queries}
]
|> build_request(:get_metric_data)
end

@doc """
Returns a list of the dashboards for your account.

Expand Down Expand Up @@ -660,10 +691,28 @@ defmodule ExAws.Cloudwatch do
|> format(prefix: "MetricData.member")
end

defp format_param({:metric_data_queries, metric_data_queries}) do
metric_data_queries
|> Enum.map(fn(metric_data_query) -> format_param({:metric_data_query, metric_data_query}) end)
|> format(prefix: "MetricDataQueries.member")
end

defp format_param({:metric_data_query, metric_data_query}) do
metric_data_query |> Enum.flat_map(&format_param/1)
end

defp format_param({:metric_datum, metric_datum}) do
metric_datum |> Enum.flat_map(&format_param/1)
end

defp format_param({:metric_stat, metric_stat}) do
metric_stat |> Enum.flat_map(&format_param/1) |> format(prefix: "MetricStat")
end

defp format_param({:metric, metric}) do
metric |> Enum.flat_map(&format_param/1) |> format(prefix: "Metric")
end

defp format_param({:ok_actions, ok_actions}) do
ok_actions |> format(prefix: "OKActions.member")
end
Expand Down
29 changes: 29 additions & 0 deletions lib/ex_aws/cloudwatch/parsers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ if Code.ensure_loaded?(SweetXml) do
{:ok, Map.put(resp, :body, parsed_body)}
end

def parse({:ok, %{body: xml} = resp}, :get_metric_data) do
parsed_body =
xml
|> SweetXml.xpath(
~x"//GetMetricDataResponse",
metric_data: metric_data_description(),
request_id: ~x"./ResponseMetadata/RequestId/text()"s
)

{:ok, Map.put(resp, :body, parsed_body)}
end

def parse({:ok, %{body: xml} = resp}, :list_dashboards) do
parsed_body =
xml
Expand Down Expand Up @@ -174,6 +186,16 @@ if Code.ensure_loaded?(SweetXml) do
]
end

defp metric_data_description do
[
~x"./GetMetricDataResult/MetricDataResults/member"l,
timestamps: ~x"./Timestamps/member/text()"ls |> SweetXml.transform_by(&to_timestamp/1),
values: ~x"./Values/member/text()"lf,
label: ~x"./Label/text()"s,
id: ~x"./Id/text()"s,
]
end

defp dashboards_description() do
[
~x"./ListDashboardsResult/DashboardEntries/member"l,
Expand Down Expand Up @@ -208,6 +230,13 @@ if Code.ensure_loaded?(SweetXml) do
defp to_boolean(xpath) do
xpath |> SweetXml.transform_by(&(&1 == "true"))
end

defp to_timestamp(list) when is_list(list), do: Enum.map(list, &to_timestamp/1)
defp to_timestamp(xpath) do
xpath
|> DateTime.from_iso8601()
|> elem(1)
end
end
else
defmodule ExAws.Cloudwatch.Parsers do
Expand Down