@@ -473,6 +473,18 @@ def populate_monthly_cost_sql(self, cost_type, rate_type, rate, start_date, end_
473
473
distribution: Choice of monthly distribution ex. memory
474
474
provider_uuid (str): The str of the provider UUID
475
475
"""
476
+ cost_type_file_mapping = {
477
+ "Node" : "monthly_cost_cluster_and_node.sql" ,
478
+ "Node_Core_Month" : "monthly_cost_cluster_and_node.sql" ,
479
+ "Cluster" : "monthly_cost_cluster_and_node.sql" ,
480
+ "PVC" : "monthly_cost_persistentvolumeclaim.sql" ,
481
+ "OCP_VM" : "monthly_cost_virtual_machine.sql" ,
482
+ }
483
+ cost_type_file = cost_type_file_mapping .get (cost_type )
484
+ if not cost_type_file :
485
+ LOG .error (f"Invalid cost_type: { cost_type } for OCP provider. Skipping populate_monthly_cost_sql update" )
486
+ return
487
+
476
488
table_name = self ._table_map ["line_item_daily_summary" ]
477
489
report_period = self .report_periods_for_provider_uuid (provider_uuid , start_date )
478
490
ctx = {
@@ -503,11 +515,8 @@ def populate_monthly_cost_sql(self, cost_type, rate_type, rate, start_date, end_
503
515
)
504
516
# We cleared out existing data, but there is no new to calculate.
505
517
return
506
- if cost_type in ("Node" , "Node_Core_Month" , "Cluster" ):
507
- sql = pkgutil .get_data ("masu.database" , "sql/openshift/cost_model/monthly_cost_cluster_and_node.sql" )
508
- elif cost_type == "PVC" :
509
- sql = pkgutil .get_data ("masu.database" , "sql/openshift/cost_model/monthly_cost_persistentvolumeclaim.sql" )
510
518
519
+ sql = pkgutil .get_data ("masu.database" , f"sql/openshift/cost_model/{ cost_type_file } " )
511
520
sql = sql .decode ("utf-8" )
512
521
sql_params = {
513
522
"start_date" : start_date ,
@@ -642,9 +651,75 @@ def populate_usage_costs(self, rate_type, rates, start_date, end_date, provider_
642
651
"volume_request_rate" : rates .get (metric_constants .OCP_METRIC_STORAGE_GB_REQUEST_MONTH , 0 ),
643
652
"rate_type" : rate_type ,
644
653
}
654
+
645
655
LOG .info (log_json (msg = f"populating { rate_type } usage costs" , context = ctx ))
646
656
self ._prepare_and_execute_raw_sql_query (table_name , sql , sql_params , operation = "INSERT" )
647
657
658
+ if ocp_vm_hour_rate := rates .get (metric_constants .OCP_VM_HOUR ):
659
+ self .populate_vm_hourly_usage_costs (
660
+ rate_type , ocp_vm_hour_rate , start_date , end_date , provider_uuid , report_period_id
661
+ )
662
+
663
+ def populate_vm_hourly_usage_costs (
664
+ self , rate_type , ocp_vm_hour_rate , start_date , end_date , provider_uuid , report_period_id
665
+ ):
666
+ """Populate virtual machine hourly usage costs"""
667
+
668
+ ctx = {
669
+ "schema" : self .schema ,
670
+ "provider_uuid" : str (provider_uuid ),
671
+ "start_date" : start_date ,
672
+ "end_date" : end_date ,
673
+ "report_period" : report_period_id ,
674
+ }
675
+
676
+ table_name = OCP_REPORT_TABLE_MAP ["line_item_daily_summary" ]
677
+ LOG .info (
678
+ log_json (
679
+ msg = f"removing virtual machine cost model { rate_type } hourly costs from daily summary" , context = ctx
680
+ )
681
+ )
682
+
683
+ tmp_sql = """
684
+ DELETE FROM {{schema | sqlsafe}}.{{table | sqlsafe}}
685
+ WHERE usage_start >= {{start_date}}
686
+ AND usage_start <= {{end_date}}
687
+ AND report_period_id = {{report_period_id}}
688
+ AND cost_model_rate_type = {{rate_type}}
689
+ AND source_uuid = {{source_uuid}}::uuid
690
+ AND monthly_cost_type IS NULL
691
+ AND all_labels ? 'vm_kubevirt_io_name'
692
+ """
693
+ tmp_sql_params = {
694
+ "schema" : self .schema ,
695
+ "table" : table_name ,
696
+ "start_date" : start_date ,
697
+ "end_date" : end_date ,
698
+ "report_period_id" : report_period_id ,
699
+ "rate_type" : rate_type ,
700
+ "source_uuid" : str (provider_uuid ),
701
+ }
702
+
703
+ self ._prepare_and_execute_raw_sql_query (table_name , tmp_sql , tmp_sql_params , operation = "DELETE" )
704
+
705
+ sql = pkgutil .get_data ("masu.database" , "trino_sql/openshift/cost_model/hourly_cost_virtual_machine.sql" )
706
+ sql = sql .decode ("utf-8" )
707
+ sql_params = {
708
+ "start_date" : str (start_date ),
709
+ "end_date" : str (end_date ),
710
+ "schema" : self .schema ,
711
+ "source_uuid" : str (provider_uuid ),
712
+ "report_period_id" : report_period_id ,
713
+ "vm_cost_per_hour" : ocp_vm_hour_rate ,
714
+ "rate_type" : rate_type ,
715
+ }
716
+ start_date = DateHelper ().parse_to_date (start_date )
717
+ sql_params ["year" ] = start_date .strftime ("%Y" )
718
+ sql_params ["month" ] = start_date .strftime ("%m" )
719
+
720
+ LOG .info (log_json (msg = f"populating virtual machine { rate_type } hourly costs" , context = ctx ))
721
+ self ._execute_trino_multipart_sql_query (sql , bind_params = sql_params )
722
+
648
723
def populate_tag_usage_costs ( # noqa: C901
649
724
self , infrastructure_rates , supplementary_rates , start_date , end_date , cluster_id
650
725
):
0 commit comments