Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
f14bfd4a
Unverified
Commit
f14bfd4a
authored
5 months ago
by
Oleksandr Yakushev
Committed by
GitHub
5 months ago
Browse files
Options
Downloads
Patches
Plain Diff
perf: Do fewer moderation_reviews queries when fetching recent views (#48169)
parent
46a91cc4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/api/activity.clj
+23
-26
23 additions, 26 deletions
src/metabase/api/activity.clj
with
23 additions
and
26 deletions
src/metabase/api/activity.clj
+
23
−
26
View file @
f14bfd4a
...
...
@@ -35,31 +35,28 @@
[
:metabase_database.name
:database-name
]])
(
let
[
model-symb
(
symbol
(
str/capitalize
model
))
self-qualify
#
(
mdb.query/qualify
model-symb
%
)]
(
cond->
{
:where
[
:in
(
self-qualify
:id
)
ids
]}
(
not=
model
"table"
)
(
merge
{
:left-join
[
:collection
[
:=
:collection.id
(
self-qualify
:collection_id
)]]})
(
=
model
"table"
)
(
merge
{
:left-join
[
:metabase_database
[
:=
:metabase_database.id
(
self-qualify
:db_id
)]]})))))
(
defn-
select-items!
[
model
ids
]
(
when
(
seq
ids
)
(
for
[
model
(
t2/hydrate
(
models-query
model
ids
)
:moderation_reviews
)
:let
[
reviews
(
:moderation_reviews
model
)
status
(
->>
reviews
(
filter
:most_recent
)
first
:status
)]]
(
assoc
model
:moderated_status
status
))))
{
:where
[
:in
(
self-qualify
:id
)
ids
]
:left-join
(
if
(
=
model
"table"
)
[
:metabase_database
[
:=
:metabase_database.id
(
self-qualify
:db_id
)]]
[
:collection
[
:=
:collection.id
(
self-qualify
:collection_id
)]])})))
(
defn-
models-for-views
"Returns a map of {model {id instance}} for activity views suitable for looking up by model and id to get a model."
[
views
]
(
into
{}
(
map
(
fn
[[
model
models
]]
[
model
(
->>
models
(
map
:model_id
)
(
select-items!
model
)
(
m/index-by
:id
))]))
(
group-by
:model
views
)))
(
let
[
grouped
(
group-by
:model
views
)
;; We perform selects for each model type separately, but then bring them back into a flat list to hydrate
;; with moderation_reviews data all at once.
items
(
mapcat
(
fn
[[
model
views
'
]]
(
when
(
seq
views
'
)
(
->>
(
models-query
model
(
map
:model_id
views
'
))
(
mapv
#
(
assoc
%
:model
model
)))))
grouped
)
items
(
->>
(
t2/hydrate
items
:moderation_reviews
)
(
map
(
fn
[{
:keys
[
moderation_reviews
]
:as
item
}]
(
let
[
status
(
some
#
(
when
(
:most_recent
%
)
(
:status
%
))
moderation_reviews
)]
(
assoc
item
:moderated_status
status
)))))]
;; Now group the flat list of items into a map.
(
update-vals
(
group-by
:model
items
)
#
(
m/index-by
:id
%
))))
(
defn-
views-and-runs
"Query implementation for `popular_items`. Tables and Dashboards have a query limit of `views-limit`.
...
...
@@ -103,11 +100,11 @@
[
:=
:context
(
h2x/literal
:question
)]]
:order-by
[[
:max_ts
:desc
]]
:limit
card-runs-limit
})
(
map
#
(
dissoc
%
:row_count
))
(
map
#
(
assoc
%
:model
"card"
)))]
(
->>
(
concat
card-runs
dashboard-and-table-views
)
(
sort-by
:max_t
s
)
reverse
)))
(
map
v
#
(
->
%
(
dissoc
:row_count
)
(
assoc
:model
"card"
))))]
(
->>
(
into
card-runs
dashboard-and-table-view
s
)
(
sort-by
:max_ts
#
(
compare
%2
%1
))
)))
(
def
^
:private
views-limit
8
)
(
def
^
:private
card-runs-limit
8
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment