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
16812c4d
Unverified
Commit
16812c4d
authored
2 years ago
by
Alexander Polyankin
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Move PartialQueryBuilder MBQL manipulation to metabase-lib (#25857)
parent
ec1d935e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
frontend/src/metabase-lib/lib/queries/utils/segments.js
+24
-0
24 additions, 0 deletions
frontend/src/metabase-lib/lib/queries/utils/segments.js
frontend/src/metabase/admin/datamodel/components/PartialQueryBuilder.jsx
+15
-41
15 additions, 41 deletions
...tabase/admin/datamodel/components/PartialQueryBuilder.jsx
with
39 additions
and
41 deletions
frontend/src/metabase-lib/lib/queries/utils/segments.js
0 → 100644
+
24
−
0
View file @
16812c4d
import
Question
from
"
metabase-lib/lib/Question
"
;
export
function
getSegmentOrMetricQuestion
(
query
,
table
,
metadata
)
{
return
table
?
metadata
.
table
(
table
.
id
).
query
(
query
).
question
()
:
Question
.
create
({
metadata
});
}
export
function
getDefaultSegmentOrMetricQuestion
(
table
,
metadata
)
{
const
question
=
metadata
.
table
(
table
.
id
).
question
();
if
(
table
.
entity_type
===
"
entity/GoogleAnalyticsTable
"
)
{
const
dateField
=
table
.
fields
.
find
(
f
=>
f
.
name
===
"
ga:date
"
);
if
(
dateField
)
{
return
question
.
filter
([
"
time-interval
"
,
[
"
field
"
,
dateField
.
id
,
null
],
-
365
,
"
day
"
])
.
aggregate
([
"
metric
"
,
"
ga:users
"
]);
}
}
else
{
return
question
.
aggregate
([
"
count
"
]);
}
return
null
;
}
This diff is collapsed.
Click to expand it.
frontend/src/metabase/admin/datamodel/components/PartialQueryBuilder.jsx
+
15
−
41
View file @
16812c4d
...
...
@@ -10,8 +10,11 @@ import { getMetadata } from "metabase/selectors/metadata";
import
Tables
from
"
metabase/entities/tables
"
;
import
GuiQueryEditor
from
"
metabase/query_builder/components/GuiQueryEditor
"
;
import
*
as
Urls
from
"
metabase/lib/urls
"
;
import
Question
from
"
metabase-lib/lib/Question
"
;
import
Query
from
"
metabase-lib/lib/queries/Query
"
;
import
{
getSegmentOrMetricQuestion
,
getDefaultSegmentOrMetricQuestion
,
}
from
"
metabase-lib/lib/queries/utils/segments
"
;
import
withTableMetadataLoaded
from
"
../hoc/withTableMetadataLoaded
"
;
...
...
@@ -45,33 +48,21 @@ class PartialQueryBuilder extends Component {
maybeSetDefaultQuery
()
{
const
{
metadata
,
table
,
value
}
=
this
.
props
;
if
(
value
!=
null
&&
!
_
.
isEqual
(
Object
.
keys
(
value
),
[
"
source-table
"
]))
{
// only set the query if it doesn't already have an aggregation or filter
return
;
}
// we need metadata and a table to generate a default query
if
(
!
metadata
||
!
table
)
{
// we need metadata and a table to generate a default question
return
;
}
const
{
id
:
tableId
,
db_id
:
databaseId
}
=
table
;
const
query
=
Question
.
create
({
databaseId
,
tableId
,
metadata
}).
query
();
// const table = query.table();
let
queryWithFilters
;
if
(
table
.
entity_type
===
"
entity/GoogleAnalyticsTable
"
)
{
const
dateField
=
table
.
fields
.
find
(
f
=>
f
.
name
===
"
ga:date
"
);
if
(
dateField
)
{
queryWithFilters
=
query
.
filter
([
"
time-interval
"
,
[
"
field
"
,
dateField
.
id
,
null
],
-
365
,
"
day
"
])
.
aggregate
([
"
metric
"
,
"
ga:users
"
]);
}
}
else
{
queryWithFilters
=
query
.
aggregate
([
"
count
"
]);
// only set the query if it doesn't already have an aggregation or filter
const
question
=
getSegmentOrMetricQuestion
(
value
,
table
,
metadata
);
if
(
!
question
.
query
().
isRaw
())
{
return
;
}
if
(
queryWithFilters
)
{
this
.
setDatasetQuery
(
queryWithFilters
.
datasetQuery
());
const
defaultQuestion
=
getDefaultSegmentOrMetricQuestion
(
table
,
metadata
);
if
(
defaultQuestion
)
{
this
.
setDatasetQuery
(
defaultQuestion
.
datasetQuery
());
}
}
...
...
@@ -87,26 +78,9 @@ class PartialQueryBuilder extends Component {
render
()
{
const
{
features
,
value
,
metadata
,
table
,
previewSummary
}
=
this
.
props
;
const
datasetQuery
=
table
?
{
type
:
"
query
"
,
database
:
table
.
db_id
,
query
:
value
,
}
:
{
type
:
"
query
"
,
query
:
{},
};
const
query
=
new
Question
(
{
dataset_query
:
datasetQuery
},
metadata
,
).
query
();
const
previewCard
=
{
dataset_query
:
datasetQuery
,
};
const
previewUrl
=
Urls
.
serializedQuestion
(
previewCard
);
const
question
=
getSegmentOrMetricQuestion
(
value
,
table
,
metadata
);
const
query
=
question
.
query
();
const
previewUrl
=
Urls
.
serializedQuestion
(
question
.
card
());
return
(
<
div
className
=
"py1"
>
...
...
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