Skip to content

Commit a23bb50

Browse files
Validate docker intervals (#1465)
1 parent 5eac8bc commit a23bb50

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

apps/core/lib/core/services/metrics.ex

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,22 @@ defmodule Core.Services.Metrics do
3030
offset = Keyword.get(opts, :offset, @default_offset)
3131
precision = Keyword.get(opts, :precision, @default_precision)
3232

33-
Docker.repo_query(offset, precision)
34-
|> Influx.query(params: %{repository: repo})
35-
|> response()
33+
with {:ok, _} <- validate_intervals([offset, precision]) do
34+
Docker.repo_query(offset, precision)
35+
|> Influx.query(params: %{repository: repo})
36+
|> response()
37+
end
3638
end
3739

3840
def query_docker_pulls_for_tag(repo, tag, opts \\ []) do
3941
offset = Keyword.get(opts, :offset, @default_offset)
4042
precision = Keyword.get(opts, :precision, @default_precision)
4143

42-
Docker.tag_query(offset, precision)
43-
|> Influx.query(params: %{repository: repo, tag: tag})
44-
|> response()
44+
with {:ok, _} <- validate_intervals([offset, precision]) do
45+
Docker.tag_query(offset, precision)
46+
|> Influx.query(params: %{repository: repo, tag: tag})
47+
|> response()
48+
end
4549
end
4650

4751
def response(%{results: [%{series: series} | _]}) do
@@ -79,4 +83,13 @@ defmodule Core.Services.Metrics do
7983
end
8084

8185
defp parse_value([time, val]), do: %{time: Timex.parse!(time, "{ISO:Extended}"), value: val}
86+
87+
defp valid_interval?(interval), do: String.match?(interval, ~r/^[0-9]+[smhd]$/)
88+
89+
defp validate_intervals(intervals) do
90+
case Enum.find(intervals, &!valid_interval?(&1)) do
91+
interval when is_binary(interval) -> {:error, "Invalid interval format: #{interval}"}
92+
_ -> {:ok, intervals}
93+
end
94+
end
8295
end

0 commit comments

Comments
 (0)