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

analytics page #416

Merged
merged 14 commits into from
Nov 27, 2023
Merged

analytics page #416

merged 14 commits into from
Nov 27, 2023

Conversation

nickgrato
Copy link
Contributor

@nickgrato nickgrato commented Oct 2, 2023

Creating a route that can get data from the hubstat join hubs table to see all the active hubs in a given time period.

Screenshot 2023-11-02 at 7 48 31 AM

@@ -6,7 +6,7 @@ defmodule Dash.HubStat do
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have this weird error "warning: invalid association hub in schema Dash.HubStat: associated schema Dash.Hub does not have field id" which I know.. it has "hub_id" but am I not doing that right here? saying ":hub_id"?

@bryanenders @tanfarming

Copy link
Contributor

@bryanenders bryanenders Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don’t actually need to set the :foreign key option, since it defaults to the name of the association suffixed by _id. The option you want is :references, which sets the key on the other schema to be used for the association. That’s the one that defaults to :id.

Suggested change
belongs_to :hub, Hub, foreign_key: :hub_id
belongs_to :hub, Hub, references: :hub_id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bryanenders your fix worked but isn't the primary key on hubs :hub_id?
@primary_key {:hub_id, :id, autogenerate: true}

was thinking that overrides the :id?

thanks again for helping!

lib/dash.ex Outdated
from(hub in Hub,
join: stat in assoc(hub, :hub_stats),
where: stat.measured_at >= ^start_date and stat.measured_at <= ^end_date,
select: hub
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The :select may not be necessary

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]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this Elixir project isn’t long for this world, this is fine. If we expected it to live on, I’d instead suggest picking off the fields we want where they’re used: the controller/view.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be a great part to pair on if you feel like passing on some wisdom. I was trying it put that in the controller / view and just couldn't figure It this seems super heavy handed, and like you said is ok since we are out but I'd love to see how to do it properly if you have time.

@bryanenders
Copy link
Contributor

There are some things I’d recommend if this Elixir project were living on. Since it’s not, this looks like it should do the trick with that belongs_to change.

@nickgrato nickgrato merged commit 3f2202d into main Nov 27, 2023
12 checks passed
@nickgrato nickgrato deleted the feature/analytics branch November 27, 2023 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants