Skip to content

Commit

Permalink
Add search/filter queries for cost data
Browse files Browse the repository at this point in the history
Will (hopefully) meet current specs for cost analysis views
  • Loading branch information
michaeljguarino committed Dec 18, 2024
1 parent ca124bb commit 271396c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
6 changes: 6 additions & 0 deletions assets/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,7 @@ export type ClusterUsageNamespacesArgs = {
before?: InputMaybe<Scalars['String']['input']>;
first?: InputMaybe<Scalars['Int']['input']>;
last?: InputMaybe<Scalars['Int']['input']>;
q?: InputMaybe<Scalars['String']['input']>;
};


Expand All @@ -1684,6 +1685,8 @@ export type ClusterUsageRecommendationsArgs = {
before?: InputMaybe<Scalars['String']['input']>;
first?: InputMaybe<Scalars['Int']['input']>;
last?: InputMaybe<Scalars['Int']['input']>;
q?: InputMaybe<Scalars['String']['input']>;
type?: InputMaybe<ScalingRecommendationType>;
};

export type ClusterUsageConnection = {
Expand Down Expand Up @@ -7189,6 +7192,9 @@ export type RootQueryTypeClusterUsagesArgs = {
before?: InputMaybe<Scalars['String']['input']>;
first?: InputMaybe<Scalars['Int']['input']>;
last?: InputMaybe<Scalars['Int']['input']>;
projectId?: InputMaybe<Scalars['ID']['input']>;
q?: InputMaybe<Scalars['String']['input']>;
tagQuery?: InputMaybe<TagQuery>;
};


Expand Down
6 changes: 6 additions & 0 deletions lib/console/graphql/deployments/cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,13 @@ defmodule Console.GraphQl.Deployments.Cluster do
field :cluster, :cluster, resolve: dataloader(Deployments)

connection field :namespaces, node_type: :cluster_namespace_usage do
arg :q, :string
resolve &Deployments.list_namespace_usage/3
end

connection field :recommendations, node_type: :cluster_scaling_recommendation do
arg :type, :scaling_recommendation_type
arg :q, :string
resolve &Deployments.list_scaling_recommendations/3
end

Expand Down Expand Up @@ -984,6 +987,9 @@ defmodule Console.GraphQl.Deployments.Cluster do

connection field :cluster_usages, node_type: :cluster_usage do
middleware Authenticated
arg :q, :string
arg :tag_query, :tag_query
arg :project_id, :id

resolve &Deployments.list_cluster_usage/2
end
Expand Down
16 changes: 15 additions & 1 deletion lib/console/graphql/resolvers/deployments/cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,28 @@ defmodule Console.GraphQl.Resolvers.Deployments.Cluster do
end

def list_cluster_usage(args, %{context: %{current_user: user}}) do
ClusterUsage.for_user(user)
clusters = Cluster.for_user(user)
|> maybe_search(Cluster, args)
|> cluster_filters(args)

ClusterUsage.for_clusters(clusters)
|> ClusterUsage.ordered()
|> ClusterUsage.preloaded()
|> paginate(args)
end

def list_namespace_usage(%ClusterUsage{cluster_id: cluster_id}, args, _) do
ClusterNamespaceUsage.for_cluster(cluster_id)
|> maybe_search(ClusterNamespaceUsage, args)
|> ClusterNamespaceUsage.ordered()
|> ClusterNamespaceUsage.preloaded()
|> paginate(args)
end

def list_scaling_recommendations(%ClusterUsage{cluster_id: cluster_id}, args, _) do
ClusterScalingRecommendation.for_cluster(cluster_id)
|> recommendation_filters(args)
|> maybe_search(ClusterScalingRecommendation, args)
|> ClusterScalingRecommendation.ordered()
|> ClusterScalingRecommendation.preloaded()
|> paginate(args)
Expand Down Expand Up @@ -236,4 +243,11 @@ defmodule Console.GraphQl.Resolvers.Deployments.Cluster do
_, q -> q
end)
end

defp recommendation_filters(query, args) do
Enum.reduce(args, query, fn
{:type, t}, q -> ClusterScalingRecommendation.for_type(q, t)
_, q -> q
end)
end
end
28 changes: 16 additions & 12 deletions lib/console/schema/cluster_namespace_usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ defmodule Console.Schema.ClusterNamespaceUsage do
timestamps()
end

def search(query \\ __MODULE__, q) do
from(cu in query, where: like(cu.namespace, ^"%#{q}%"))
end

def preloaded(query \\ __MODULE__, preloads \\ [:cluster]) do
from(cu in query, preload: ^preloads)
end

def for_cluster(query \\ __MODULE__, cid) do
from(cn in query, where: cn.cluster_id == ^cid)
end

def ordered(query \\ __MODULE__, order \\ [asc: :namespace]) do
from(cn in query, order_by: ^order)
end

@valid ~w(
memory
cpu
Expand All @@ -43,18 +59,6 @@ defmodule Console.Schema.ClusterNamespaceUsage do
egress_cost
)a

def preloaded(query \\ __MODULE__, preloads \\ [:cluster]) do
from(cu in query, preload: ^preloads)
end

def for_cluster(query \\ __MODULE__, cid) do
from(cn in query, where: cn.cluster_id == ^cid)
end

def ordered(query \\ __MODULE__, order \\ [asc: :namespace]) do
from(cn in query, order_by: ^order)
end

def changeset(model, attrs \\ %{}) do
model
|> cast(attrs, @valid)
Expand Down
9 changes: 9 additions & 0 deletions lib/console/schema/cluster_scaling_recommendation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ defmodule Console.Schema.ClusterScalingRecommendation do
timestamps()
end

def search(query \\ __MODULE__, q) do
wildcard = "%#{q}%"
from(csr in query, where: like(csr.namespace, ^wildcard) or like(csr.name, ^wildcard) or like(csr.container, ^wildcard))
end

def for_type(query \\ __MODULE__, t) do
from(csr in query, where: csr.type == ^t)
end

def for_cluster(query \\ __MODULE__, cid) do
from(csr in query, where: csr.cluster_id == ^cid)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/console/schema/cluster_usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ defmodule Console.Schema.ClusterUsage do
timestamps()
end

def for_clusters(query \\ __MODULE__, cluster_q) do
from(cu in query,
join: c in subquery(cluster_q),
on: c.id == cu.cluster_id,
as: :clusters,
distinct: true
)
end

def for_user(query \\ __MODULE__, user) do
from(cu in query,
join: c in ^Cluster.for_user(user),
Expand Down
6 changes: 3 additions & 3 deletions schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ type RootQueryType {
"fetch an individual runtime service for more thorough detail views"
runtimeService(id: ID!): RuntimeService

clusterUsages(after: String, first: Int, before: String, last: Int): ClusterUsageConnection
clusterUsages(after: String, first: Int, before: String, last: Int, q: String, tagQuery: TagQuery, projectId: ID): ClusterUsageConnection

clusterUsage(id: ID!): ClusterUsage

Expand Down Expand Up @@ -5320,9 +5320,9 @@ type ClusterUsage {

cluster: Cluster

namespaces(after: String, first: Int, before: String, last: Int): ClusterNamespaceUsageConnection
namespaces(after: String, first: Int, before: String, last: Int, q: String): ClusterNamespaceUsageConnection

recommendations(after: String, first: Int, before: String, last: Int): ClusterScalingRecommendationConnection
recommendations(after: String, first: Int, before: String, last: Int, type: ScalingRecommendationType, q: String): ClusterScalingRecommendationConnection

insertedAt: DateTime

Expand Down

0 comments on commit 271396c

Please sign in to comment.