Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 60ad23e

Browse files
committed
Show statistics in a query result meta
Fixed resulting_object_cnt counter in case of a filter by 1:1 connection. Part of #71.
1 parent 69040ca commit 60ad23e

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

graphql/accessor_general.lua

+25-1
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,12 @@ local function process_tuple(self, state, tuple, opts)
878878
return true -- skip pivot item too
879879
end
880880

881+
-- Don't count subrequest resulting objects (needed for filtering) into
882+
-- count of object we show to an user as a result.
883+
-- XXX: It is better to have an option to control whether selected objects
884+
-- will be counted as resulting ones.
885+
local saved_resulting_object_cnt = qstats.resulting_object_cnt
886+
881887
-- make subrequests if needed
882888
for k, v in pairs(filter) do
883889
if obj[k] == nil then
@@ -891,6 +897,8 @@ local function process_tuple(self, state, tuple, opts)
891897
end
892898
end
893899

900+
qstats.resulting_object_cnt = saved_resulting_object_cnt
901+
894902
-- filter out non-matching objects
895903
local match = utils.is_subtable(obj, filter) and
896904
match_using_re(obj, pcre)
@@ -1060,6 +1068,13 @@ local function select_internal(self, collection_name, from, filter, args, extra)
10601068
-- fullscan
10611069
local primary_index = self.funcs.get_primary_index(self,
10621070
collection_name)
1071+
1072+
-- count full scan select request
1073+
extra.qcontext.statistics.select_requests_cnt =
1074+
extra.qcontext.statistics.select_requests_cnt + 1
1075+
extra.qcontext.statistics.full_scan_select_requests_cnt =
1076+
extra.qcontext.statistics.full_scan_select_requests_cnt + 1
1077+
10631078
for _, tuple in primary_index:pairs() do
10641079
assert(pivot == nil,
10651080
'offset for top-level objects must use a primary index')
@@ -1102,6 +1117,12 @@ local function select_internal(self, collection_name, from, filter, args, extra)
11021117

11031118
local tuple_count = 0
11041119

1120+
-- count index select request
1121+
extra.qcontext.statistics.select_requests_cnt =
1122+
extra.qcontext.statistics.select_requests_cnt + 1
1123+
extra.qcontext.statistics.index_select_requests_cnt =
1124+
extra.qcontext.statistics.index_select_requests_cnt + 1
1125+
11051126
for _, tuple in index:pairs(index_value, iterator_opts) do
11061127
tuple_count = tuple_count + 1
11071128
-- check full match constraint
@@ -1287,7 +1308,10 @@ local function init_qcontext(accessor, qcontext)
12871308
local settings = accessor.settings
12881309
qcontext.statistics = {
12891310
resulting_object_cnt = 0,
1290-
fetched_object_cnt = 0
1311+
fetched_object_cnt = 0,
1312+
select_requests_cnt = 0,
1313+
full_scan_select_requests_cnt = 0,
1314+
index_select_requests_cnt = 0,
12911315
}
12921316
qcontext.deadline_clock = clock.monotonic64() +
12931317
settings.timeout_ms * 1000 * 1000

graphql/core/execute.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ evaluateSelections = function(objectType, object, selections, context)
125125
return result
126126
end
127127

128-
return function(schema, tree, rootValue, variables, operationName)
128+
return function(schema, tree, rootValue, variables, operationName, opts)
129+
local opts = opts or {}
129130
local context = query_util.buildContext(schema, tree, rootValue, variables, operationName)
130131
-- The field is passed to resolve function within info attribute.
131132
-- Can be used to store any data within one query.
132-
context.qcontext = {}
133+
context.qcontext = opts.qcontext or {}
133134
local rootType = schema[context.operation.operation]
134135

135136
if not rootType then

graphql/impl.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ local function gql_execute(qstate, variables, operation_name)
4040
check(operation_name, 'operation_name', 'string', 'nil')
4141

4242
local root_value = {}
43+
local qcontext = {}
4344

4445
local traceback
4546
local ok, data = xpcall(function()
4647
return execute(state.schema, qstate.ast, root_value, variables,
47-
operation_name)
48+
operation_name, {qcontext = qcontext})
4849
end, function(err)
4950
traceback = debug.traceback()
5051
return err
@@ -55,6 +56,9 @@ local function gql_execute(qstate, variables, operation_name)
5556
end
5657
return {
5758
data = data,
59+
meta = {
60+
statistics = qcontext.statistics,
61+
}
5862
}
5963
end
6064

0 commit comments

Comments
 (0)