Skip to content

Commit

Permalink
add recommendation inference
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino committed Dec 19, 2024
1 parent 49b3834 commit 8028b38
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 17 additions & 2 deletions lib/console/cost/ingester.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
Expand Down

0 comments on commit 8028b38

Please sign in to comment.