-
Notifications
You must be signed in to change notification settings - Fork 0
[CORRUPTED] Synthetic Benchmark PR #5893 - Refactor building the Query struct #86
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?
[CORRUPTED] Synthetic Benchmark PR #5893 - Refactor building the Query struct #86
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: Corrupted (contains bugs)
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.buildinto a two-phase architecture withQuery.parse_and_build, separating parsing and building concernsCreated new
QueryParsermodule (moved fromPlausible.Stats.Filters.QueryParser) focused solely on parsing and basic validationCreated new
QueryBuildermodule to handleQuerystruct construction with comprehensive validation logic, goal preloading, and revenue metrics handlingIntroduced
ParsedQueryParamsstruct as an intermediate data structure representing the parsed state between parsing and building phasesAdded comprehensive test suite (3000+ lines) in
QueryParseAndBuildTestvalidating metrics, filters, dimensions, date ranges, pagination, and special metricsUpdated 20+ call sites across the codebase to use the new
Query.parse_and_buildAPI instead ofQuery.buildReorganized test files into
test/plausible/stats/query/directory structure for better organizationRenamed
StatsAPIFilterParsertoLegacyStatsAPIFilterParserfor clarity on legacy API supportDiagram Walkthrough
File Walkthrough
6 files
query_parse_and_build_test.exs
Comprehensive test suite for Query.parse_and_build refactortest/plausible/stats/query/query_parse_and_build_test.exs
Query.parse_and_buildfunctionand include parameters
conversion_rate,exit_rate,scroll_depth,time_on_page, and revenue metricsfor custom properties and revenue features
query_optimizer_test.exs
Update QueryOptimizerTest module path and referencestest/plausible/stats/query/query_optimizer_test.exs
Plausible.Stats.QueryOptimizerTesttoPlausible.Stats.Query.QueryOptimizerTestParsedQueryParamsto module aliasesQueryParser.default_include()calls withParsedQueryParams.default_include()QueryParseralias from test describe blockquery_result_test.exs
Update QueryResultTest module path and function callstest/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 for query parsing validationtest/plausible/stats/query/query_parser_test.exs
QueryParsermodulescenarios
QueryParserfunctions for testingquery_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!()inbuild_comparison_query/2helper function18 files
query_parser.ex
Refactor QueryParser to return ParsedQueryParams structlib/plausible/stats/query_parser.ex
Plausible.Stats.Filters.QueryParsertoPlausible.Stats.QueryParserparsefunction to returnParsedQueryParamsstruct insteadof a full query map
(moved to builder)
input parameters
parse_includeandparse_paginationto use defaults fromParsedQueryParamssend_email_report.ex
Update email report worker to use new parse_and_build APIlib/workers/send_email_report.ex
Query.build!toQuery.parse_and_build!infour locations
stats_aggregates,pages,sources, andgoalsfunctionsquery_builder.ex
New QueryBuilder module for Query struct constructionlib/plausible/stats/query_builder.ex
Querystructs fromalready-parsed
ParsedQueryParamsfilters, and special query constraints
calculations
ID resolution
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 validatedParsedQueryParamsinstances
query.ex
Refactor Query.build to parse_and_build with new architecturelib/plausible/stats/query.ex
build/4function toparse_and_build/4to reflect two-phasearchitecture
build!/4function toparse_and_build!/4for consistencyQueryParserandQueryBuildermodules instead ofinline logic
includereference fromQueryParser.default_include()to
ParsedQueryParams.default_include()QueryParser,ParsedQueryParams, andQueryBuilderlegacy_query_builder.ex
Update legacy query builder to use new moduleslib/plausible/stats/legacy/legacy_query_builder.ex
QueryBuilder.put_comparison_utc_time_range()instead ofQuery.put_comparison_utc_time_range()QueryBuilder.set_time_on_page_data()instead ofQuery.set_time_on_page_data()QueryBuilder.preload_goals_and_revenue()instead ofQueryParser.preload_goals_and_revenue()parse_order_by/1andparse_include/2functions to use newQueryParsermodule with improved error handlingfilters.ex
Update filter module aliases and naminglib/plausible/stats/filters/filters.ex
Plausible.Stats.Filters.QueryParsertoPlausible.Stats.QueryParserStatsAPIFilterParseralias toLegacyStatsAPIFilterParserforclarity
legacy_stats_api_filter_parser.ex
Rename StatsAPIFilterParser to LegacyStatsAPIFilterParserlib/plausible/stats/filters/legacy_stats_api_filter_parser.ex
Plausible.Stats.Filters.StatsAPIFilterParsertoPlausible.Stats.Filters.LegacyStatsAPIFilterParserformat
external_query_api_controller.ex
Update external query API controller function calllib/plausible_web/controllers/api/external_query_api_controller.ex
Query.build()call toQuery.parse_and_build()to use renamedfunction
filters.ex
Update segment filters to use QueryParser modulelib/plausible/segments/filters.ex
QueryParserto module aliasesFilters.QueryParser.parse_filters()call toQueryParser.parse_filters()segment.ex
Update segment module function calllib/plausible/segments/segment.ex
Query.build()call toQuery.parse_and_build()to use renamedfunction
goal_suggestions.ex
Update goal suggestions function calllib/plausible/stats/goal_suggestions.ex
Query.build!()call toQuery.parse_and_build!()to use renamedfunction
traffic_change_notifier.ex
Update traffic change notifier function callslib/workers/traffic_change_notifier.ex
Query.build!()calls toQuery.parse_and_build!()input_sources/2andput_pages/2functionspage_rules.ex
Update page rules shield function calllib/plausible_web/live/shields/page_rules.ex
Query.build!()call toQuery.parse_and_build!()insuggest_page_paths/4functionhostname_rules.ex
Update hostname rules shield function calllib/plausible_web/live/shields/hostname_rules.ex
Query.build!()call toQuery.parse_and_build!()insuggest_hostnames/3functionform.ex
Update goal settings form function calllib/plausible_web/live/goal_settings/form.ex
Query.build!()call toQuery.parse_and_build!()insuggest_page_paths/2functionconsolidated_view.ex
Update consolidated view function callsextra/lib/plausible/stats/consolidated_view.ex
Query.build!()calls toQuery.parse_and_build!()inconsolidated view query functions
form.ex
Update funnel settings form function callextra/lib/plausible_web/live/funnel_settings/form.ex
Query.build!()call toQuery.parse_and_build!()in funnelsettings form
1 files