Mostly working sequence_usage_report + remove N+1 from sequence index

pull/789/head
Rick Carlino 2018-04-13 11:43:01 -05:00
parent a90a56d8e4
commit 86f27b6ca2
3 changed files with 19 additions and 13 deletions

View File

@ -33,10 +33,7 @@ module Api
end
def sequences
puts "TODO- maybe I need to do a raw SQL query here ? Hmmm..."
@sequences ||= Sequence
.includes(:primary_nodes, :edge_nodes)
.where(device: current_device)
@sequences ||= Sequence.with_usage_reports.where(device: current_device)
end
def sequence

View File

@ -86,11 +86,12 @@ module CeleryScript
def misc_fields
return {
id: sequence.id,
name: sequence.name,
color: sequence.color,
created_at: sequence.created_at,
updated_at: sequence.updated_at,
args: Sequence::DEFAULT_ARGS
args: Sequence::DEFAULT_ARGS,
color: sequence.color,
in_use: sequence.in_use?,
name: sequence.name,
}
end
@ -110,12 +111,6 @@ module CeleryScript
def execute
canonical_form = misc_fields.merge!(recurse_into_node(entry_node))
# N+1 AHEAD:
canonical_form[:in_use] = \
EdgeNode.where(kind: "sequence_id", value: sequence.id).exists? ||
RegimenItem.where(sequence_id: sequence.id).exists? ||
FarmEvent.where(executable: sequence).exists?
raise "Eager load sequence_usage_report" if !sequence.association(:sequence_usage_report).loaded?
s = canonical_form.with_indifferent_access
# HISTORICAL NOTE:
# When I prototyped the variables declaration stuff, a few (failed)

View File

@ -63,6 +63,20 @@ class Sequence < ApplicationRecord
false unless destroyed?
end
# Determines if the current sequence is used by any farmevents, regimens or
# sequences.
def in_use?
[sequence_usage_report.edge_node_count,
sequence_usage_report.farm_event_count,
sequence_usage_report.regimen_items_count].max != 0
end
# Eagerly load edge_node, primary_node and usage_report. This is a big deal
# for performance when serializing sequences.
def self.with_usage_reports
self.includes(:sequence_usage_report, :edge_nodes, :primary_nodes)
end
# THIS IS AN OVERRIDE - Special serialization required for auto sync.
# When a sequence is created, we save it to the database to create a primary
# key, then we iterate over `EdgeNode` and `PrimaryNode`s, assigning that