Skip to content

Commit

Permalink
Merge pull request #725 from learningtapestry/ctdl/fix-total-count
Browse files Browse the repository at this point in the history
Fix total count in CTDL queries with zero results taken
  • Loading branch information
excelsior authored Oct 1, 2024
2 parents 812d753 + 198b6b5 commit ef61bef
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
72 changes: 42 additions & 30 deletions app/services/ctdl_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,34 @@ def self.find_dictionary(locale)
DICTIONARIES.fetch(language, 'english')
end

def execute
IndexedEnvelopeResource.connection.execute(to_sql)
def count_query
@count_query ||= Arel::SelectManager.new
.from(relation.as('t'))
.project(Arel.star.count.as('total_count'))
end

def data_query
@data_query ||= begin
query = relation.dup
query = query.order(order) if order_by
query = query.skip(skip) if skip
query = query.take(take) if take
query = query
.project(table['ceterms:ctid'], table[:payload], fts_rank.as(FTS_RANK))

if with_metadata
query = query
.project(
table[:'search:recordCreated'],
table[:'search:recordOwnedBy'],
table[:'search:recordPublishedBy'],
table[:'search:resourcePublishType'],
table[:'search:recordUpdated']
)
end

query
end
end

def fts_rank
Expand Down Expand Up @@ -157,32 +183,10 @@ def relation
fts_rank.as(FTS_RANK)
)
else
relation = relation.where(table[:'ceterms:ctid'].not_eq(nil))

relation = relation.where(
table[:envelope_community_id].eq(envelope_community.id)
)

relation = relation.order(order) if order_by
relation = relation.skip(skip) if skip
relation = relation.take(take) if take

relation = relation.project(*[
*project,
fts_rank.as(FTS_RANK),
Arel.sql('COUNT(*) OVER()').as('total_count')
])

if with_metadata
relation = relation
.project(
table[:'search:recordCreated'],
table[:'search:recordOwnedBy'],
table[:'search:recordPublishedBy'],
table[:'search:resourcePublishType'],
table[:'search:recordUpdated']
)
end
relation = relation
.where(table[:'ceterms:ctid'].not_eq(nil))
.where(table[:envelope_community_id].eq(envelope_community.id))
.project(table[:'@id'], fts_rank.as(FTS_RANK))
end

subquery_ctes = subqueries.map do |subquery|
Expand Down Expand Up @@ -224,12 +228,20 @@ def resource_column
reverse_ref ? :subresource_uri : :resource_uri
end

def rows
IndexedEnvelopeResource.connection.execute(data_query.to_sql)
end

def subresource_column
reverse_ref ? :resource_uri : :subresource_uri
end

def to_sql
@sql ||= relation.to_sql
def total_count
IndexedEnvelopeResource
.connection
.execute(count_query.to_sql)
.first
.fetch('total_count')
end

private
Expand Down
11 changes: 5 additions & 6 deletions app/services/run_ctdl_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ def self.call(
with_metadata: include_results_metadata
)

rows = query.execute
rows = query.rows

result = {
data: rows.map { |r| JSON(r.fetch('payload')) },
total: rows.first&.fetch('total_count') || 0
data: rows.map { JSON(_1.fetch('payload')) },
total: query.total_count
}

ctids = rows.map { |r| r.fetch('ceterms:ctid') }.compact

result.merge!(sql: query.to_sql) if debug
result.merge!(sql: query.data_query.to_sql ) if debug

if include_description_set_resources || include_description_sets
description_set_data = FetchDescriptionSetData.call(
Expand Down Expand Up @@ -94,7 +93,7 @@ def self.call(
)
end

query_log&.update(query: query.to_sql)
query_log&.update(query: query.data_query.to_sql)
query_log&.complete(result)
OpenStruct.new(result: result, status: 200)
rescue => e
Expand Down

0 comments on commit ef61bef

Please sign in to comment.