-
Notifications
You must be signed in to change notification settings - Fork 31
feat: 🎸 Refactor session recording filters #2981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ZedLi
wants to merge
2
commits into
main
Choose a base branch
from
use-sqlite-for-filters-session-recording
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
*/ | ||
|
||
import { modelMapping } from 'api/services/sqlite'; | ||
import { searchTables } from 'api/services/sqlite'; | ||
import { typeOf } from '@ember/utils'; | ||
import { underscore } from '@ember/string'; | ||
|
||
|
@@ -35,17 +34,15 @@ export function generateSQLExpressions( | |
addFilterConditions({ filters, parameters, conditions }); | ||
addSearchConditions({ search, resource, tableName, parameters, conditions }); | ||
|
||
const selectClause = constructSelectClause(select, tableName); | ||
const orderByClause = constructOrderByClause(resource, sort); | ||
|
||
const whereClause = conditions.length ? `WHERE ${and(conditions)}` : ''; | ||
|
||
const paginationClause = page && pageSize ? `LIMIT ? OFFSET ?` : ''; | ||
if (paginationClause) { | ||
parameters.push(pageSize, (page - 1) * pageSize); | ||
} | ||
|
||
const selectClause = `SELECT ${select ? select.join(', ') : '*'} FROM "${tableName}"`; | ||
|
||
return { | ||
// Replace any empty newlines or leading whitespace on each line to be consistent with formatting | ||
// This is mainly to help us read and test the generated SQL as it has no effect on the actual SQL execution. | ||
|
@@ -142,32 +139,57 @@ function addSearchConditions({ | |
parameters, | ||
conditions, | ||
}) { | ||
if (!search) { | ||
if (!search || !modelMapping[resource]) { | ||
return; | ||
} | ||
|
||
if (searchTables.has(resource)) { | ||
// Use the special prefix indicator "*" for full-text search | ||
parameters.push(`"${search}"*`); | ||
// Use a subquery to match against the FTS table with rowids as SQLite is | ||
// much more efficient with FTS queries when using rowids or MATCH (or both). | ||
// We could have also used a join here but a subquery is simpler. | ||
conditions.push( | ||
`rowid IN (SELECT rowid FROM ${tableName}_fts WHERE ${tableName}_fts MATCH ?)`, | ||
// Use the special prefix indicator "*" for full-text search | ||
if (typeOf(search) === 'object') { | ||
if (!search?.text) { | ||
return; | ||
} | ||
|
||
parameters.push( | ||
or(search.fields.map((field) => `${field}:"${search.text}"*`)), | ||
); | ||
return; | ||
} else { | ||
parameters.push(`"${search}"*`); | ||
} | ||
|
||
const fields = Object.keys(modelMapping[resource]); | ||
const searchConditions = parenthetical( | ||
or( | ||
fields.map((field) => { | ||
parameters.push(`%${search}%`); | ||
return `${field}${OPERATORS['contains']}`; | ||
}), | ||
), | ||
// Use a subquery to match against the FTS table with rowids as SQLite is | ||
// much more efficient with FTS queries when using rowids or MATCH (or both). | ||
// We could have also used a join here but a subquery is simpler. | ||
conditions.push( | ||
`rowid IN (SELECT rowid FROM ${tableName}_fts WHERE ${tableName}_fts MATCH ?)`, | ||
); | ||
conditions.push(searchConditions); | ||
} | ||
function constructSelectClause(select = [{ field: '*' }], tableName) { | ||
const distinctColumns = select.filter(({ isDistinct }) => isDistinct); | ||
let selectColumns; | ||
|
||
// Special case for distinct columns as they must be grouped together. | ||
// We're only handling simple use cases as anything more complicated | ||
// like windows/CTEs can be custom SQL. | ||
if (distinctColumns.length > 0) { | ||
selectColumns = `DISTINCT ${distinctColumns.map(({ field }) => field).join(', ')}`; | ||
} else { | ||
Comment on lines
+173
to
+175
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it a valid case to have a mix of distinct and regular columns selected? If it isn't, maybe an error could be thrown? |
||
selectColumns = select | ||
.map(({ field, isCount, alias }) => { | ||
let column = field; | ||
|
||
if (isCount) { | ||
column = `count(${column})`; | ||
} | ||
if (alias) { | ||
column = `${column} as ${alias}`; | ||
} | ||
|
||
return column; | ||
}) | ||
.join(', '); | ||
} | ||
|
||
return `SELECT ${selectColumns} FROM "${tableName}"`; | ||
} | ||
|
||
function constructOrderByClause(resource, sort) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it easier to use the model mapping and have selects expressed in terms of the model attributes and mapped to the underlying column? It feels like the outer api is starting to blend between the model/attributes and row/columns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yeah you're right, this is a little leaky. I could make a reverse mapping of
modelMapping
(since it currently goes from column to JSON path) and use that so we don't have to be aware of the columns