Skip to content

Commit 2640583

Browse files
committed
feat: add state field at CostReportConfig list request model
Signed-off-by: ImMin5 <[email protected]>
1 parent 814710a commit 2640583

File tree

4 files changed

+6
-155
lines changed

4 files changed

+6
-155
lines changed

src/spaceone/cost_analysis/model/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@
1010
from spaceone.cost_analysis.model.cost_report_config.database import CostReportConfig
1111
from spaceone.cost_analysis.model.cost_report_data.database import CostReportData
1212
from spaceone.cost_analysis.model.cost_report.database import CostReport
13-
14-
# from spaceone.cost_analysis.model.daily_cost_record.database import DailyCostRecord

src/spaceone/cost_analysis/model/cost_report_config/request.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Union, Literal
22

33
from pydantic import BaseModel
44

@@ -15,6 +15,8 @@
1515
"CostReportConfigStatQueryRequest",
1616
]
1717

18+
State = Literal["ENABLED", "DISABLED"]
19+
1820

1921
class CostReportConfigCreateRequest(BaseModel):
2022
issue_day: Union[int, None] = None
@@ -70,6 +72,7 @@ class CostReportConfigGetRequest(BaseModel):
7072
class CostReportConfigSearchQueryRequest(BaseModel):
7173
query: Union[dict, None] = None
7274
cost_report_config_id: Union[str, None] = None
75+
state: Union[State, None] = None
7376
domain_id: str
7477

7578

src/spaceone/cost_analysis/service/cost_report_config_service.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ def list(
264264
params (CostReportConfigSearchQueryRequest): {
265265
'query': 'dict',
266266
'cost_report_config_id': 'str',
267-
'domain_id': 'str' # injected from auth (required)
267+
'state": 'str',
268268
'workspace_id': 'str'
269+
'domain_id': 'str' # injected from auth (required)
269270
}
270271
271272
Returns:

src/spaceone/cost_analysis/service/cost_report_serivce.py

-151
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from datetime import datetime
66
from typing import Tuple, Union
77

8-
import pandas as pd
98
from mongoengine import QuerySet
109
from spaceone.core import config
1110
from spaceone.core.service import *
@@ -352,156 +351,6 @@ def create_cost_report(self, params: dict):
352351
cost_report_created_at,
353352
)
354353

355-
# self._create_daily_cost_record(
356-
# domain_id, cost_report_config_id, current_date, currency
357-
# )
358-
359-
# def _create_daily_cost_record(
360-
# self,
361-
# domain_id: str,
362-
# cost_report_config_id: str,
363-
# current_date: datetime,
364-
# currency: str,
365-
# ):
366-
# # check if create today's daily cost report
367-
# cost_report_data_query = {
368-
# "group_by": [
369-
# "domain_id",
370-
# "workspace_id",
371-
# "project_id",
372-
# "data_source_id",
373-
# "product",
374-
# "workspace_name",
375-
# "project_name",
376-
# ],
377-
# "fields": {
378-
# "value_sum": {"key": f"cost.{currency}", "operator": "sum"},
379-
# },
380-
# "filter": [
381-
# {"k": "is_confirmed", "v": False, "o": "eq"},
382-
# {"k": "cost_report_config_id", "v": cost_report_config_id, "o": "eq"},
383-
# {"k": "domain_id", "v": domain_id, "o": "eq"},
384-
# {"k": "report_month", "v": current_date.strftime("%Y-%m"), "o": "eq"},
385-
# ],
386-
# }
387-
# results = self.cost_report_data_mgr.analyze_cost_reports_data(
388-
# query=cost_report_data_query
389-
# ).get("results", [])
390-
#
391-
# daily_cost_record_mgr = DailyCostRecordManager()
392-
# (
393-
# daily_cost_record_vos,
394-
# daily_cost_record_total_count,
395-
# ) = daily_cost_record_mgr.list_daily_cost_records(
396-
# query={
397-
# "filter": [
398-
# {"k": "domain_id", "v": domain_id, "o": "eq"},
399-
# {
400-
# "k": "record_month",
401-
# "v": current_date.strftime("%Y-%m"),
402-
# "o": "eq",
403-
# },
404-
# ]
405-
# }
406-
# )
407-
#
408-
# df1 = pd.DataFrame(results)
409-
#
410-
# daily_cost_record_created_at = datetime.utcnow()
411-
# record_month = current_date.strftime("%Y-%m")
412-
# record_date = current_date.strftime("%Y-%m-%d")
413-
# if daily_cost_record_total_count > 0:
414-
# if self._is_daily_cost_report_created_today(daily_cost_record_vos[0]):
415-
# return
416-
#
417-
# daily_cost_records_info = [
418-
# daily_cost_record_vo.to_dict()
419-
# for daily_cost_record_vo in daily_cost_record_vos
420-
# ]
421-
# df2 = pd.DataFrame(daily_cost_records_info)
422-
# df2 = df2.drop(["_id"], axis=1)
423-
#
424-
# joined_df = df1.merge(
425-
# df2,
426-
# on=[
427-
# "domain_id",
428-
# "workspace_id",
429-
# "project_id",
430-
# "data_source_id",
431-
# "product",
432-
# ],
433-
# how="left",
434-
# )
435-
#
436-
# for joined_data in joined_df.to_dict(orient="records"):
437-
# monthly_cost = joined_data.get("value_sum", 0)
438-
# before_monthly_cost = joined_data.get("monthly_cost", 0)
439-
#
440-
# daily_cost_diff_percent = 0.0
441-
# daily_cost_diff = 0.0
442-
#
443-
# if before_monthly_cost > 0:
444-
# daily_cost_diff = monthly_cost - before_monthly_cost
445-
# daily_cost_diff_percent = (
446-
# daily_cost_diff / before_monthly_cost
447-
# ) * 100
448-
#
449-
# create_params = {
450-
# "daily_cost_diff": daily_cost_diff,
451-
# "daily_cost_diff_percent": daily_cost_diff_percent,
452-
# "monthly_cost": monthly_cost,
453-
# "record_month": record_month,
454-
# "record_date": record_date,
455-
# "product": joined_data.get("product"),
456-
# "project_name": joined_data.get("project_name"),
457-
# "workspace_name": joined_data.get("workspace_name"),
458-
# "cost_report_config_id": cost_report_config_id,
459-
# "data_source_id": joined_data["data_source_id"],
460-
# "project_id": joined_data.get("project_id"),
461-
# "workspace_id": joined_data["workspace_id"],
462-
# "domain_id": joined_data["domain_id"],
463-
# }
464-
# daily_cost_record_mgr.create_daily_cost_record(create_params)
465-
#
466-
# _LOGGER.debug(
467-
# f"[create_daily_cost_record] delete previous daily cost record {record_month} ({len(daily_cost_record_vos)}"
468-
# )
469-
# daily_cost_record_vos.delete()
470-
#
471-
# else:
472-
# for result in results:
473-
# create_params = {
474-
# "daily_cost_diff": 0.0,
475-
# "daily_cost_diff_percent": 0.0,
476-
# "monthly_cost": result.get("value_sum", 0, 0),
477-
# "record_month": record_month,
478-
# "record_date": record_date,
479-
# "product": result.get("product"),
480-
# "project_name": result.get("project_name"),
481-
# "workspace_name": result.get("workspace_name"),
482-
# "data_source_id": result["data_source_id"],
483-
# "cost_report_config_id": cost_report_config_id,
484-
# "project_id": result.get("project_id"),
485-
# "workspace_id": result["workspace_id"],
486-
# "domain_id": result["domain_id"],
487-
# }
488-
# daily_cost_record_mgr.create_daily_cost_record(create_params)
489-
#
490-
# @staticmethod
491-
# def _is_daily_cost_report_created_today(
492-
# daily_cost_record_vo: DailyCostRecord,
493-
# ) -> bool:
494-
# daily_cost_record_created_at: datetime = daily_cost_record_vo.created_at
495-
# if daily_cost_record_created_at.strftime(
496-
# "%Y-%m-%d"
497-
# ) == datetime.utcnow().strftime("%Y-%m-%d"):
498-
# _LOGGER.debug(
499-
# f"[_is_daily_cost_report_created_today] This workspace ({daily_cost_record_vo.workspace_id}) -> SKIP"
500-
# )
501-
# return True
502-
#
503-
# return False
504-
505354
def _aggregate_monthly_cost_report(
506355
self,
507356
domain_id: str,

0 commit comments

Comments
 (0)