@@ -40,30 +40,28 @@ def logger
4040 end
4141
4242 def exclude_running_records ( old_records )
43- return old_records unless has_duration ?( model )
43+ return old_records unless has_usage_lifecycle ?( model )
4444
45- beginning_string = beginning_string ( model )
46- ending_string = ending_string ( model )
47- guid_symbol = guid_symbol ( model )
48-
49- raise "Invalid duration model: #{ model } " if beginning_string . nil? || ending_string . nil? || guid_symbol . nil?
45+ beginning_state = beginning_state ( model )
46+ ending_state = ending_state ( model )
47+ guid_column = guid_column ( model )
5048
5149 # Create subqueries for START and STOP records within the old records set
5250 # Using from_self creates a subquery, allowing us to reference these in complex joins
53- initial_records = old_records . where ( state : beginning_string ) . from_self ( alias : :initial_records )
54- final_records = old_records . where ( state : ending_string ) . from_self ( alias : :final_records )
51+ initial_records = old_records . where ( state : beginning_state ) . from_self ( alias : :initial_records )
52+ final_records = old_records . where ( state : ending_state ) . from_self ( alias : :final_records )
5553
5654 # For each START record, check if there exists a STOP record that:
5755 # 1. Has the same resource GUID (app_guid or service_instance_guid)
5856 # 2. Was created AFTER the START record (higher ID implies later creation)
59- exists_condition = final_records . where ( Sequel [ :final_records ] [ guid_symbol ] => Sequel [ :initial_records ] [ guid_symbol ] ) . where do
57+ exists_condition = final_records . where ( Sequel [ :final_records ] [ guid_column ] => Sequel [ :initial_records ] [ guid_column ] ) . where do
6058 Sequel [ :final_records ] [ :id ] > Sequel [ :initial_records ] [ :id ]
6159 end . select ( 1 ) . exists
6260
6361 prunable_initial_records = initial_records . where ( exists_condition )
6462
6563 # Include records with states other than START/STOP
66- other_records = old_records . exclude ( state : [ beginning_string , ending_string ] )
64+ other_records = old_records . exclude ( state : [ beginning_state , ending_state ] )
6765
6866 # Return the UNION of:
6967 # 1. START records that have a matching STOP (safe to delete)
@@ -72,32 +70,20 @@ def exclude_running_records(old_records)
7270 prunable_initial_records . union ( final_records , all : true ) . union ( other_records , all : true )
7371 end
7472
75- def has_duration? ( model )
76- return true if model == VCAP ::CloudController ::AppUsageEvent
77- return true if model == VCAP ::CloudController ::ServiceUsageEvent
78-
79- false
73+ def has_usage_lifecycle? ( model )
74+ model . respond_to? ( :usage_lifecycle )
8075 end
8176
82- def beginning_string ( model )
83- return VCAP ::CloudController ::ProcessModel ::STARTED if model == VCAP ::CloudController ::AppUsageEvent
84- return VCAP ::CloudController ::Repositories ::ServiceUsageEventRepository ::CREATED_EVENT_STATE if model == VCAP ::CloudController ::ServiceUsageEvent
85-
86- nil
77+ def beginning_state ( model )
78+ model . usage_lifecycle [ :beginning_state ]
8779 end
8880
89- def ending_string ( model )
90- return VCAP ::CloudController ::ProcessModel ::STOPPED if model == VCAP ::CloudController ::AppUsageEvent
91- return VCAP ::CloudController ::Repositories ::ServiceUsageEventRepository ::DELETED_EVENT_STATE if model == VCAP ::CloudController ::ServiceUsageEvent
92-
93- nil
81+ def ending_state ( model )
82+ model . usage_lifecycle [ :ending_state ]
9483 end
9584
96- def guid_symbol ( model )
97- return :app_guid if model == VCAP ::CloudController ::AppUsageEvent
98- return :service_instance_guid if model == VCAP ::CloudController ::ServiceUsageEvent
99-
100- nil
85+ def guid_column ( model )
86+ model . usage_lifecycle [ :guid_column ]
10187 end
10288 end
10389end
0 commit comments