Skip to content

Commit

Permalink
feat: add idle cost
Browse files Browse the repository at this point in the history
Signed-off-by: Youngjin Jo <[email protected]>
  • Loading branch information
yjinjo committed May 8, 2024
1 parent e5da9b4 commit 01c33ce
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/plugin/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def get_data(
prometheus_query_range_endpoint = (
f"{secret_data['mimir_endpoint']}/api/v1/query_range"
)

promql_response = self.mimir_connector.get_promql_response(
prometheus_query_range_endpoint,
start,
Expand All @@ -92,9 +93,9 @@ def get_data(
)

yield from self._process_response_stream(
promql_response_stream,
cluster_info,
service_account_id,
promql_response_stream=promql_response_stream,
)
else:
_LOGGER.error(
Expand Down Expand Up @@ -136,12 +137,13 @@ def _has_agent(

def _process_response_stream(
self,
promql_response_stream: Generator,
cluster_info: dict,
service_account_id: str,
promql_response_stream: Generator,
) -> Generator[dict, None, None]:
for results in promql_response_stream:
yield self._make_cost_data(results, cluster_info, service_account_id)

yield {"results": []}

def _make_cost_data(
Expand All @@ -156,15 +158,17 @@ def _make_cost_data(
costs_data = []
for result in results:
for i in range(len(result["values"])):
additional_info = self._make_additional_info(result, x_scope_orgid)

data = {}
if usage_type := result["metric"].get("type"):
data["usage_type"] = usage_type

result["cost"] = float(result["values"][i][1])
result["billed_date"] = pd.to_datetime(
result["values"][i][0], unit="s"
).strftime("%Y-%m-%d")

if type := result["metric"].get("type"):
data["usage_type"] = type
additional_info = self._make_additional_info(result, x_scope_orgid)
try:
data.update(
{
Expand Down Expand Up @@ -213,23 +217,23 @@ def _check_required_fields(result: dict):
@staticmethod
def _make_additional_info(result: dict, service_account_id: str) -> dict:
additional_info = {
"Cluster": result["metric"].get("cluster", ""),
"Cluster": result["metric"].get("cluster", "__idle__"),
"X-Scope-OrgID": service_account_id,
}

if node := result["metric"].get("node", "Persistent Volumes"):
if node := result["metric"].get("node", "__idle__"):
additional_info["Node"] = node

if namespace := result["metric"].get("namespace", "__idle__ + Unmounted PVs"):
if namespace := result["metric"].get("namespace", "__idle__"):
additional_info["Namespace"] = namespace

if pod := result["metric"].get("pod", "__idle__ + Unmounted PVs"):
if pod := result["metric"].get("pod", "__idle__"):
additional_info["Pod"] = pod

if container := result["metric"].get("container", "__idle__ + Unmounted PVs"):
if container := result["metric"].get("container", "__idle__"):
additional_info["Container"] = container

if pv := result["metric"].get("persistentvolume", "__idle__ + Unmounted PVs"):
if pv := result["metric"].get("persistentvolume", ""):
additional_info["PV"] = pv

return additional_info
Expand Down

0 comments on commit 01c33ce

Please sign in to comment.