-
Notifications
You must be signed in to change notification settings - Fork 0
[CLEAN] Synthetic Benchmark PR #5893 - Refactor building the Query struct #85
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
base: base_pr_5893_20251204_9257
Are you sure you want to change the base?
Conversation
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
|||||||||||||||||||||
User description
Benchmark PR plausible#5893
Type: Clean (correct implementation)
Original PR Title: Refactor building the Query struct
Original PR Description: ### Changes
This PR is a refactor and doesn't introduce any new behaviour.
In the upcoming dashboard migration from React to LiveView, we want to be able to create
Querystructs from native data structures. E.g.:Query.buildfunction, not thesite_idDateTimestructs instead of iso8601 stringsThe biggest refactor in this PR is changing the current
Query.buildfunction intoQuery.parse_and_buildwhich calls two separate modules (QueryParserandQueryBuilder(new) for the respective actions).It also introduces an intermediate data structure that represents the parsed state between those two phases -
ParsedQueryParams.There was a lot of query "building" logic and validations incorporated into
QueryParser. The test file (with more than 3k lines of code) got turned intoQueryParseAndBuildTest, now asserting on a Query struct outcome rather than an arbitrary map.This PR doesn't tackle the
date_rangeinput aspect yet. For now,QueryBuilderwill expect a cleanutc_time_rangeinput (via theParsedQueryParamsstruct). It shouldn't block this PR though. I can tackle it later. E.g.: we'll likely want to build queries like:With that, we can also migrate any existing ad-hoc
Queryconstruction (e.g. in email reports orquery_24h_stats) to use this new version ofQuery.build.Tests
Changelog
Documentation
Dark mode
Original PR URL: plausible#5893
PR Type
Enhancement, Tests
Description
Refactored
Query.buildtoQuery.parse_and_buildimplementing a two-phase architecture (parsing and building) for better separation of concernsCreated new
QueryBuildermodule to handleQuerystruct construction with comprehensive validation logic for metrics, dimensions, filters, and special constraintsCreated new
ParsedQueryParamsstruct as an intermediate data structure between parsing and building phasesRefactored
QueryParsermodule (relocated fromPlausible.Stats.Filters) to focus solely on parsing, removing validation and query building logicUpdated 20+ files across the codebase to use the new
Query.parse_and_buildAPI and relocatedQueryParserAdded comprehensive test suite (3000+ lines) for
Query.parse_and_buildfunctionality covering metrics, filters, dimensions, date ranges, pagination, and special casesReorganized test modules under
Plausible.Stats.Querynamespace for better structureRenamed
StatsAPIFilterParsertoLegacyStatsAPIFilterParserto clarify legacy API supportDiagram Walkthrough
File Walkthrough
6 files
query_parse_and_build_test.exs
Comprehensive test suite for Query parse and buildtest/plausible/stats/query/query_parse_and_build_test.exs
Query.parse_and_buildfunctionalityand include parameters
segments, and special metric validations
error handling
query_optimizer_test.exs
Update QueryOptimizerTest module path and referencestest/plausible/stats/query/query_optimizer_test.exs
Plausible.Stats.QueryOptimizerTesttoPlausible.Stats.Query.QueryOptimizerTestParsedQueryParamsaliasQueryParser.default_include()calls withParsedQueryParams.default_include()QueryParseralias from test describe blockquery_result_test.exs
Update QueryResultTest module path and function namestest/plausible/stats/query/query_result_test.exs
Plausible.Stats.QueryResultTesttoPlausible.Stats.Query.QueryResultTestQuery.build!calls toQuery.parse_and_build!query_parser_test.exs
New QueryParserTest module with basic teststest/plausible/stats/query/query_parser_test.exs
QueryParserfunctionalityinvalid metric detection
query_from_test.exs
Update QueryFromTest module path and cleanuptest/plausible/stats/query/query_from_test.exs
Plausible.Stats.QueryTesttoPlausible.Stats.Query.QueryFromTestQueryParseralias importcomparisons_test.exs
Update comparisons test function calltest/plausible/stats/comparisons_test.exs
Query.build!call toQuery.parse_and_build!to match renamedfunction
1 files
query_parser.ex
Refactor QueryParser to focus on parsing onlylib/plausible/stats/query_parser.ex
Plausible.Stats.Filters.QueryParsertoPlausible.Stats.QueryParserparsefunction to returnParsedQueryParamsstruct insteadof full query map
QueryBuilder)builder phase)
ParsedQueryParamsdefaults instead of local constants17 files
send_email_report.ex
Update email report worker to use new Query APIlib/workers/send_email_report.ex
Query.build!toQuery.parse_and_build!stats_aggregates,pages,sources, andgoalsfunctionsquery_builder.ex
New QueryBuilder module for Query struct constructionlib/plausible/stats/query_builder.ex
Querystructs fromalready-parsed parameters
filters, and special query constraints
comparison time range setup
ID retrieval
parsed_query_params.ex
New ParsedQueryParams struct for parsed query statelib/plausible/stats/parsed_query_params.ex
ParsedQueryParamsstruct as intermediate datastructure between parsing and building phases
includeandpaginationoptionsnew!/1constructor for creating validated instances withrequired fields
query.ex
Refactor Query.build to parse_and_build with delegationlib/plausible/stats/query.ex
build/4function toparse_and_build/4to reflect two-phasearchitecture
build!/4function toparse_and_build!/4for consistencyQueryParser.parseandQueryBuilder.buildinstead of inline logic
includereference fromQueryParser.default_include()to
ParsedQueryParams.default_include()legacy_query_builder.ex
Update legacy builder to use new QueryBuilder and QueryParserlib/plausible/stats/legacy/legacy_query_builder.ex
Query.put_comparison_utc_time_range()toQueryBuilder.put_comparison_utc_time_range()Query.set_time_on_page_data()toQueryBuilder.set_time_on_page_data()preload_goals_and_revenuecall to useQueryBuilderinstead ofQueryParserparse_order_byandparse_includeto use newQueryParsermodule with improved error handling
json_decodeandunwraputilities
filters.ex
Update filter module aliases and legacy parser naminglib/plausible/stats/filters/filters.ex
Plausible.Stats.Filters.QueryParsertoPlausible.Stats.QueryParserStatsAPIFilterParsertoLegacyStatsAPIFilterParserin aliasand usage
legacy_stats_api_filter_parser.ex
Rename and document legacy stats API filter parserlib/plausible/stats/filters/legacy_stats_api_filter_parser.ex
Plausible.Stats.Filters.StatsAPIFilterParsertoPlausible.Stats.Filters.LegacyStatsAPIFilterParserformat
filters.ex
Update segment filters to use relocated QueryParserlib/plausible/segments/filters.ex
QueryParserfromPlausible.Statsinstead ofnested under
FiltersFilters.QueryParser.parse_filterstoQueryParser.parse_filtersexternal_query_api_controller.ex
Update external query API controller function calllib/plausible_web/controllers/api/external_query_api_controller.ex
Query.buildcall toQuery.parse_and_buildto match renamedfunction
segment.ex
Update segment module function calllib/plausible/segments/segment.ex
Query.buildcall toQuery.parse_and_buildto match renamedfunction
page_rules.ex
Update page rules shield function calllib/plausible_web/live/shields/page_rules.ex
Query.build!call toQuery.parse_and_build!to match renamedfunction
hostname_rules.ex
Update hostname rules shield function calllib/plausible_web/live/shields/hostname_rules.ex
Query.build!call toQuery.parse_and_build!to match renamedfunction
form.ex
Update goal settings form function calllib/plausible_web/live/goal_settings/form.ex
Query.build!call toQuery.parse_and_build!to match renamedfunction
goal_suggestions.ex
Update goal suggestions function calllib/plausible/stats/goal_suggestions.ex
Query.build!call toQuery.parse_and_build!to match renamedfunction
consolidated_view.ex
Update consolidated view function callsextra/lib/plausible/stats/consolidated_view.ex
Query.build!calls toQuery.parse_and_build!to matchrenamed function
form.ex
Update funnel settings form function callextra/lib/plausible_web/live/funnel_settings/form.ex
Query.build!call toQuery.parse_and_build!to match renamedfunction
traffic_change_notifier.ex
Update traffic change notifier function callslib/workers/traffic_change_notifier.ex
Query.build!calls toQuery.parse_and_build!to matchrenamed function
1 files