From 8028b387163c8ed0c214b5c7c824f8b2a10eb4d7 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Thu, 19 Dec 2024 17:54:27 -0500 Subject: [PATCH] add recommendation inference --- lib/console/cost/ingester.ex | 19 +++++++++++++++++-- .../deployments/cluster_mutations_test.exs | 12 +++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/console/cost/ingester.ex b/lib/console/cost/ingester.ex index cfd5ea045..3493a1fd5 100644 --- a/lib/console/cost/ingester.ex +++ b/lib/console/cost/ingester.ex @@ -2,10 +2,11 @@ defmodule Console.Cost.Ingester do alias Console.Repo import Console.Services.Base import Console.Cost.Utils, only: [batch_insert: 2] - alias Console.Schema.{Cluster, ClusterUsage, ClusterNamespaceUsage, ClusterScalingRecommendation} + alias Console.Deployments.Settings + alias Console.Schema.{DeploymentSettings, Cluster, ClusterUsage, ClusterNamespaceUsage, ClusterScalingRecommendation} def ingest(attrs, %Cluster{id: id}) do - IO.inspect(attrs) + settings = Settings.cached() start_transaction() |> add_operation(:cluster, fn _ -> case Repo.get_by(ClusterUsage, cluster_id: id) do @@ -35,6 +36,8 @@ defmodule Console.Cost.Ingester do |> add_operation(:scaling, fn _ -> (Map.get(attrs, :recommendations) || []) |> Stream.map(&cluster_timestamped(&1, id)) + |> Stream.map(&infer_recommendation(&1, settings)) + |> Stream.filter(& &1) |> batch_insert(ClusterScalingRecommendation) |> ok() end) @@ -49,4 +52,16 @@ defmodule Console.Cost.Ingester do timestamped(map) |> Map.put(:cluster_id, cluster_id) end + + defp infer_recommendation(map, %DeploymentSettings{cost: %DeploymentSettings.Cost{recommendation_cushion: cush}}) + when is_integer(cush) do + case map do + %{memory_request: mr, cpu_request: cr} = map when is_float(mr) and is_float(cr) -> + Map.merge(map, %{memory_recommendation: cushioned(mr, cush), cpu_recommendation: cushioned(mr, cush)}) + _ -> nil + end + end + defp infer_recommendation(_, _), do: nil + + defp cushioned(val, cushion), do: val * ((cushion + 100) / 100) end diff --git a/test/console/graphql/mutations/deployments/cluster_mutations_test.exs b/test/console/graphql/mutations/deployments/cluster_mutations_test.exs index a32978989..8ac2c6347 100644 --- a/test/console/graphql/mutations/deployments/cluster_mutations_test.exs +++ b/test/console/graphql/mutations/deployments/cluster_mutations_test.exs @@ -487,11 +487,21 @@ defmodule Console.GraphQl.Deployments.ClusterMutationsTest do describe "ingestClusterCost" do test "it can ingest cluster cost information in one transaction" do cluster = insert(:cluster) + deployment_settings(cost: %{recommendation_cushion: 10}) cost = %{"cpu" => 1.0, "memory" => 100.0, "gpu" => 0.0, "cpuUtil" => 0.5, "memoryUtil" => 20.0, "gpuUtil" => 0.0} ingest = %{ "cluster" => cost, "namespaces" => Enum.map([cost], &Map.put(&1, "namespace", "default")), - "recommendations" => [%{"type" => "DEPLOYMENT", "namespace" => "default", "name" => "default", "container" => "nginx"}] + "recommendations" => [ + %{ + "type" => "DEPLOYMENT", + "namespace" => "default", + "name" => "default", + "container" => "nginx", + "memoryRequest" => 10.0, + "cpuRequest" => 10.0 + } + ] } {:ok, %{data: %{"ingestClusterCost" => true}}} = run_query("""