Skip to content

Commit acf76de

Browse files
committed
Refactor: Move usage lifecycle metadata into model classes
1 parent 1a031fc commit acf76de

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

app/models/runtime/app_usage_event.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ class AppUsageEvent < Sequel::Model
99
:buildpack_guid, :buildpack_name,
1010
:package_state, :previous_package_state, :parent_app_guid,
1111
:parent_app_name, :process_type, :task_name, :task_guid
12+
13+
def self.usage_lifecycle
14+
{
15+
beginning_state: ProcessModel::STARTED,
16+
ending_state: ProcessModel::STOPPED,
17+
guid_column: :app_guid
18+
}.freeze
19+
end
20+
1221
AppUsageEvent.dataset_module do
1322
def supports_window_functions?
1423
false

app/models/services/service_usage_event.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@ class ServiceUsageEvent < Sequel::Model
77
:service_plan_guid, :service_plan_name,
88
:service_guid, :service_label,
99
:service_broker_name, :service_broker_guid
10+
11+
def self.usage_lifecycle
12+
{
13+
beginning_state: Repositories::ServiceUsageEventRepository::CREATED_EVENT_STATE,
14+
ending_state: Repositories::ServiceUsageEventRepository::DELETED_EVENT_STATE,
15+
guid_column: :service_instance_guid
16+
}.freeze
17+
end
1018
end
1119
end

lib/database/old_record_cleanup.rb

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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
10389
end

0 commit comments

Comments
 (0)