Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Grato committed Nov 2, 2023
1 parent 8cdd063 commit 56c9a6e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
35 changes: 35 additions & 0 deletions lib/dash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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




6 changes: 4 additions & 2 deletions lib/dash/hub.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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
require Logger

@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
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/dash/hub_stat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/dash_web/controllers/api/v1/analytics_controller.ex
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/dash_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 56c9a6e

Please sign in to comment.