diff --git a/lib/dash.ex b/lib/dash.ex index fa7d033f..ac0db281 100644 --- a/lib/dash.ex +++ b/lib/dash.ex @@ -314,4 +314,39 @@ defmodule Dash do def subdomain_wait(), do: Application.get_env(:dash, __MODULE__)[:subdomain_wait_time] + + + defp clean_date_string(date) do + date + |> String.trim_leading("~U[") + |> String.trim_trailing("]") + end + + def get_hubs_by_date(start_date,end_date) do + + clean_start_date = start_date |> + clean_date_string() + + clean_end_date = end_date |> + clean_date_string() + + # query = + # from hub in Hub, + # join: stat in assoc(hub, :hub_stats), # Correctly using the association name + # where: stat.measured_at == ^clean_date, + # select: hub + + query = + from hub in Hub, + join: stat in assoc(hub, :hub_stats), + where: stat.measured_at >= ^clean_start_date and stat.measured_at <= ^clean_end_date, + select: hub + + Repo.all(query) + end + end + + + + diff --git a/lib/dash/hub.ex b/lib/dash/hub.ex index 10b8c447..d9a5ea46 100644 --- a/lib/dash/hub.ex +++ b/lib/dash/hub.ex @@ -1,7 +1,7 @@ defmodule Dash.Hub do use Ecto.Schema - alias Dash.{HubDeployment, Repo, RetClient, SubdomainDenial, TaskSupervisor} + alias Dash.{HubDeployment, Repo, RetClient, SubdomainDenial, TaskSupervisor, HubStat} import Dash.Utils, only: [rand_string: 1] import Ecto.Changeset import Ecto.Query @@ -9,6 +9,8 @@ defmodule Dash.Hub do @type id :: pos_integer @type t :: %__MODULE__{account_id: id} + @derive {Jason.Encoder, only: [:tier,:hub_id]} + @personal_ccu_limit 20 @personal_storage_limit_mb 2_000 @@ -24,7 +26,7 @@ defmodule Dash.Hub do belongs_to :account, Dash.Account, references: :account_id has_one :deployment, HubDeployment, foreign_key: :hub_id - + has_many :hub_stats, HubStat, foreign_key: :hub_id timestamps() end diff --git a/lib/dash/hub_stat.ex b/lib/dash/hub_stat.ex index f5f1156e..ad6ccb41 100644 --- a/lib/dash/hub_stat.ex +++ b/lib/dash/hub_stat.ex @@ -2,11 +2,12 @@ defmodule Dash.HubStat do use Ecto.Schema alias Dash.{HubStat, Repo, Hub, RetClient} + @primary_key false schema "hub_stats" do field :measured_at, :utc_datetime field :storage_mb, :integer - field :hub_id, :integer + belongs_to :hub, Hub, foreign_key: :hub_id end defp hub_stat_for_hub_id(hub_id, measured_at) do diff --git a/lib/dash_web/controllers/api/v1/analytics_controller.ex b/lib/dash_web/controllers/api/v1/analytics_controller.ex new file mode 100644 index 00000000..6520249e --- /dev/null +++ b/lib/dash_web/controllers/api/v1/analytics_controller.ex @@ -0,0 +1,21 @@ +defmodule DashWeb.Api.V1.AnalyticsController do + use DashWeb, :controller + + require Logger + + def show(conn, %{"start_date" => start_date, "end_date" => end_date} = params) do + + + hubs = Dash.get_hubs_by_date(start_date,end_date) + + conn + |> put_resp_header("access-control-allow-origin", "http://localhost:3000") + |> json(%{hubs: hubs}) + end + + + + + + +end diff --git a/lib/dash_web/router.ex b/lib/dash_web/router.ex index 1a68dc05..2217812f 100644 --- a/lib/dash_web/router.ex +++ b/lib/dash_web/router.ex @@ -26,6 +26,8 @@ defmodule DashWeb.Router do resources "/region", Api.V1.RegionController, only: [:show], singleton: true end + + scope "/api/v1", DashWeb do pipe_through :basic_auth resources "/logout", LogoutController, only: [:index] @@ -37,8 +39,11 @@ defmodule DashWeb.Router do resources "/account", Api.V1.AccountController, only: [:show], singleton: true resources "/plans", Api.V1.PlanController, only: [:create] resources "/subscription", Api.V1.SubscriptionController, only: [:show], singleton: true + resources "/analytics", Api.V1.AnalyticsController, only: [:show], singleton: true end + + scope "/api/v1", DashWeb do pipe_through [:api, :basic_auth, :jwt_authenticated, :approved_email_auth] @@ -52,6 +57,9 @@ defmodule DashWeb.Router do post "/hubs/validate_subdomain", Api.V1.HubController, :validate_subdomain end + + + scope "/api/v1", DashWeb do pipe_through :fxa_events_parser # TODO decode JWT tokens from FxA with a new plug diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..f991e334 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "turkey-portal", + "lockfileVersion": 2, + "requires": true, + "packages": {} +}