From 22d38c4cd83dd1d72ec435e1b8cf6fc3eb70d957 Mon Sep 17 00:00:00 2001 From: Vignesh Joglekar Date: Thu, 27 May 2021 03:47:19 -0500 Subject: [PATCH] 80% of auto-complete filters progress Still needs - countries and screen into new format - re-style dropdown and background - drop debounce time - tests --- assets/js/dashboard/constants.js | 253 ++++++++++++++++++ assets/js/dashboard/filters.js | 2 +- assets/js/dashboard/stats/modals/filter.js | 174 +++++++++++- lib/plausible/stats/clickhouse.ex | 95 +++++++ .../controllers/api/stats_controller.ex | 7 + lib/plausible_web/router.ex | 1 + 6 files changed, 518 insertions(+), 14 deletions(-) create mode 100644 assets/js/dashboard/constants.js diff --git a/assets/js/dashboard/constants.js b/assets/js/dashboard/constants.js new file mode 100644 index 000000000..07e3d10d5 --- /dev/null +++ b/assets/js/dashboard/constants.js @@ -0,0 +1,253 @@ +export const country_codes = { + "AFG": "Afghanistan", + "ALA": "Aland Islands", + "ALB": "Albania", + "DZA": "Algeria", + "ASM": "American Samoa", + "AND": "Andorra", + "AGO": "Angola", + "AIA": "Anguilla", + "ATA": "Antarctica", + "ATG": "Antigua And Barbuda", + "ARG": "Argentina", + "ARM": "Armenia", + "ABW": "Aruba", + "AUS": "Australia", + "AUT": "Austria", + "AZE": "Azerbaijan", + "BHS": "Bahamas", + "BHR": "Bahrain", + "BGD": "Bangladesh", + "BRB": "Barbados", + "BLR": "Belarus", + "BEL": "Belgium", + "BLZ": "Belize", + "BEN": "Benin", + "BMU": "Bermuda", + "BTN": "Bhutan", + "BOL": "Bolivia", + "BIH": "Bosnia And Herzegovina", + "BWA": "Botswana", + "BVT": "Bouvet Island", + "BRA": "Brazil", + "IOT": "British Indian Ocean Territory", + "BRN": "Brunei Darussalam", + "BGR": "Bulgaria", + "BFA": "Burkina Faso", + "BDI": "Burundi", + "KHM": "Cambodia", + "CMR": "Cameroon", + "CAN": "Canada", + "CPV": "Cape Verde", + "CYM": "Cayman Islands", + "CAF": "Central African Republic", + "TCD": "Chad", + "CHL": "Chile", + "CHN": "China", + "CXR": "Christmas Island", + "CCK": "Cocos (Keeling) Islands", + "COL": "Colombia", + "COM": "Comoros", + "COG": "Congo", + "COD": "Congo, Democratic Republic", + "COK": "Cook Islands", + "CRI": "Costa Rica", + "CIV": "Cote D'Ivoire", + "HRV": "Croatia", + "CUB": "Cuba", + "CYP": "Cyprus", + "CZE": "Czech Republic", + "DNK": "Denmark", + "DJI": "Djibouti", + "DMA": "Dominica", + "DOM": "Dominican Republic", + "ECU": "Ecuador", + "EGY": "Egypt", + "SLV": "El Salvador", + "GNQ": "Equatorial Guinea", + "ERI": "Eritrea", + "EST": "Estonia", + "ETH": "Ethiopia", + "FLK": "Falkland Islands (Malvinas)", + "FRO": "Faroe Islands", + "FJI": "Fiji", + "FIN": "Finland", + "FRA": "France", + "GUF": "French Guiana", + "PYF": "French Polynesia", + "ATF": "French Southern Territories", + "GAB": "Gabon", + "GMB": "Gambia", + "GEO": "Georgia", + "DEU": "Germany", + "GHA": "Ghana", + "GIB": "Gibraltar", + "GRC": "Greece", + "GRL": "Greenland", + "GRD": "Grenada", + "GLP": "Guadeloupe", + "GUM": "Guam", + "GTM": "Guatemala", + "GGY": "Guernsey", + "GIN": "Guinea", + "GNB": "Guinea-Bissau", + "GUY": "Guyana", + "HTI": "Haiti", + "HMD": "Heard Island & Mcdonald Islands", + "VAT": "Holy See (Vatican City State)", + "HND": "Honduras", + "HKG": "Hong Kong", + "HUN": "Hungary", + "ISL": "Iceland", + "IND": "India", + "IDN": "Indonesia", + "IRN": "Iran, Islamic Republic Of", + "IRQ": "Iraq", + "IRL": "Ireland", + "IMN": "Isle Of Man", + "ISR": "Israel", + "ITA": "Italy", + "JAM": "Jamaica", + "JPN": "Japan", + "JEY": "Jersey", + "JOR": "Jordan", + "KAZ": "Kazakhstan", + "KEN": "Kenya", + "KIR": "Kiribati", + "KOR": "Korea", + "KWT": "Kuwait", + "KGZ": "Kyrgyzstan", + "LAO": "Lao People's Democratic Republic", + "LVA": "Latvia", + "LBN": "Lebanon", + "LSO": "Lesotho", + "LBR": "Liberia", + "LBY": "Libyan Arab Jamahiriya", + "LIE": "Liechtenstein", + "LTU": "Lithuania", + "LUX": "Luxembourg", + "MAC": "Macao", + "MKD": "Macedonia", + "MDG": "Madagascar", + "MWI": "Malawi", + "MYS": "Malaysia", + "MDV": "Maldives", + "MLI": "Mali", + "MLT": "Malta", + "MHL": "Marshall Islands", + "MTQ": "Martinique", + "MRT": "Mauritania", + "MUS": "Mauritius", + "MYT": "Mayotte", + "MEX": "Mexico", + "FSM": "Micronesia, Federated States Of", + "MDA": "Moldova", + "MCO": "Monaco", + "MNG": "Mongolia", + "MNE": "Montenegro", + "MSR": "Montserrat", + "MAR": "Morocco", + "MOZ": "Mozambique", + "MMR": "Myanmar", + "NAM": "Namibia", + "NRU": "Nauru", + "NPL": "Nepal", + "NLD": "Netherlands", + "NCL": "New Caledonia", + "NZL": "New Zealand", + "NIC": "Nicaragua", + "NER": "Niger", + "NGA": "Nigeria", + "NIU": "Niue", + "NFK": "Norfolk Island", + "MNP": "Northern Mariana Islands", + "NOR": "Norway", + "OMN": "Oman", + "PAK": "Pakistan", + "PLW": "Palau", + "PSE": "Palestinian Territory, Occupied", + "PAN": "Panama", + "PNG": "Papua New Guinea", + "PRY": "Paraguay", + "PER": "Peru", + "PHL": "Philippines", + "PCN": "Pitcairn", + "POL": "Poland", + "PRT": "Portugal", + "PRI": "Puerto Rico", + "QAT": "Qatar", + "REU": "Reunion", + "ROU": "Romania", + "RUS": "Russian Federation", + "RWA": "Rwanda", + "BLM": "Saint Barthelemy", + "SHN": "Saint Helena", + "KNA": "Saint Kitts And Nevis", + "LCA": "Saint Lucia", + "MAF": "Saint Martin", + "SPM": "Saint Pierre And Miquelon", + "VCT": "Saint Vincent And Grenadines", + "WSM": "Samoa", + "SMR": "San Marino", + "STP": "Sao Tome And Principe", + "SAU": "Saudi Arabia", + "SEN": "Senegal", + "SRB": "Serbia", + "SYC": "Seychelles", + "SLE": "Sierra Leone", + "SGP": "Singapore", + "SVK": "Slovakia", + "SVN": "Slovenia", + "SLB": "Solomon Islands", + "SOM": "Somalia", + "ZAF": "South Africa", + "SGS": "South Georgia And Sandwich Isl.", + "ESP": "Spain", + "LKA": "Sri Lanka", + "SDN": "Sudan", + "SUR": "Suriname", + "SJM": "Svalbard And Jan Mayen", + "SWZ": "Swaziland", + "SWE": "Sweden", + "CHE": "Switzerland", + "SYR": "Syrian Arab Republic", + "TWN": "Taiwan", + "TJK": "Tajikistan", + "TZA": "Tanzania", + "THA": "Thailand", + "TLS": "Timor-Leste", + "TGO": "Togo", + "TKL": "Tokelau", + "TON": "Tonga", + "TTO": "Trinidad And Tobago", + "TUN": "Tunisia", + "TUR": "Turkey", + "TKM": "Turkmenistan", + "TCA": "Turks And Caicos Islands", + "TUV": "Tuvalu", + "UGA": "Uganda", + "UKR": "Ukraine", + "ARE": "United Arab Emirates", + "GBR": "United Kingdom", + "USA": "United States", + "UMI": "United States Outlying Islands", + "URY": "Uruguay", + "UZB": "Uzbekistan", + "VUT": "Vanuatu", + "VEN": "Venezuela", + "VNM": "Viet Nam", + "VGB": "Virgin Islands, British", + "VIR": "Virgin Islands, U.S.", + "WLF": "Wallis And Futuna", + "ESH": "Western Sahara", + "YEM": "Yemen", + "ZMB": "Zambia", + "ZWE": "Zimbabwe" +} + +export const screen_sizes = [ + 'Desktop', + 'Mobile', + 'Laptop', + 'Tablet' +] diff --git a/assets/js/dashboard/filters.js b/assets/js/dashboard/filters.js index 1fd179918..bf66a5a26 100644 --- a/assets/js/dashboard/filters.js +++ b/assets/js/dashboard/filters.js @@ -216,7 +216,7 @@ class Filters extends React.Component { )} this.removeFilter(key, history, query)}> - + ) diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index 84e513e24..dd434113f 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -3,6 +3,9 @@ import { withRouter, Redirect } from 'react-router-dom' import Modal from './modal' import { parseQuery, formattedFilters, navigateToQuery } from '../../query' +import { country_codes, screen_sizes } from "../../constants"; +import Transition from "../../../transition"; +import * as api from '../../api' class FilterModal extends React.Component { constructor(props) { @@ -12,31 +15,184 @@ class FilterModal extends React.Component { selectedFilter: "", negated: false, updatedValue: "", - filterSaved: false + filterSaved: false, + suggestions: [], + fetchSuggestionsTimeout: undefined, + initialSuggest: true, + focusedSuggestionIndex: -1 } this.editableGoals = Object.keys(this.state.query.filters).filter(filter => !['goal', 'props'].includes(filter)) + this.renderSelector = this.renderSelector.bind(this) + this.fetchSuggestions = this.fetchSuggestions.bind(this) + this.handleKeydown = this.handleKeydown.bind(this) } componentDidMount() { this.setState({ selectedFilter: this.props.match.params.field }) + + document.addEventListener("keydown", this.handleKeydown); } componentDidUpdate(prevProps, prevState) { - const { query, selectedFilter } = this.state + const { query, selectedFilter, updatedValue, preventSuggestions } = this.state if (prevState.selectedFilter !== selectedFilter) { const negated = query.filters[selectedFilter] && query.filters[selectedFilter][0] == '!' && this.negationSupported(selectedFilter) const updatedValue = negated ? query.filters[selectedFilter].slice(1) : (query.filters[selectedFilter] || "") - this.setState({ updatedValue, negated }) + clearTimeout(this.state.fetchSuggestionsTimeout) + + this.setState({ updatedValue, negated, suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 }) + } + + if (prevState.updatedValue !== updatedValue) { + if (!preventSuggestions || prevState.preventSuggestions) { + let fetchSuggestionsTimeout; + clearTimeout(this.state.fetchSuggestionsTimeout) + + if (!['country', 'screen'].includes(selectedFilter)) { + fetchSuggestionsTimeout = setTimeout(this.fetchSuggestions, 1000) + } + + this.setState({ suggestions: [], fetchSuggestionsTimeout, preventSuggestions: false, focusedSuggestionIndex: -1 }) + } else { + this.setState({ suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 }) + } + } + } + + componentWillUnmount() { + document.removeEventListener("keydown", this.handleKeydown); + } + + handleKeydown(e) { + const { suggestions } = this.state + let { focusedSuggestionIndex, updatedValue, preventSuggestions } = this.state + const navigationKeys = ['ArrowUp', undefined, 'ArrowDown'] + + if (e.target.id !== 'suggestions_input') return true + if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.isComposing || e.keyCode === 229) return + + if ((e.key === 'Enter' || e.key === 'Tab') && suggestions[focusedSuggestionIndex]) { + updatedValue = suggestions[focusedSuggestionIndex] + focusedSuggestionIndex = -1 + preventSuggestions = true + e.preventDefault() + } + + if (navigationKeys.includes(e.key)) { + focusedSuggestionIndex += navigationKeys.indexOf(e.key)-1 + if (focusedSuggestionIndex < 0) { + focusedSuggestionIndex = suggestions.length - 1 + } else if (focusedSuggestionIndex >= suggestions.length) { + focusedSuggestionIndex = 0 + } + e.preventDefault() } + + this.setState({ focusedSuggestionIndex, updatedValue, preventSuggestions }) } negationSupported(filter) { return ['page', 'entry_page', 'exit_page'].includes(filter) } + fetchSuggestions() { + const { query, updatedValue, selectedFilter } = this.state + + query.filters[selectedFilter] = null + + api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, query, { q: updatedValue }) + .then((res) => this.setState({ suggestions: res, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 })) + } + + renderSelector(filter) { + const { updatedValue, suggestions, initialSuggest, inputFocused, focusedSuggestionIndex } = this.state; + + if (!filter) { return null; } + + if (filter === 'country') { + return () + } + + if (filter === 'screen') { + return ( + <> + + + ) + } + + return
+ { + this.setState({ + updatedValue: e.target.value + }) + }} + onFocus={() => { + if (initialSuggest) { + this.setState({ initial: false }) + this.fetchSuggestions() + } + this.setState({ inputFocused: true }) + }} + onBlur={() => this.setState({ inputFocused: false })} + /> + +
+
+ {suggestions.map((suggestion, index) => ( + { + this.setState({ updatedValue: suggestion, preventSuggestions: true }) + }} + > + {suggestion} + ))} +
+
+
+
+ } + renderBody() { const { selectedFilter, negated, updatedValue, query } = this.state; @@ -81,15 +237,7 @@ class FilterModal extends React.Component { } - {selectedFilter && - { this.setState({ updatedValue: e.target.value }) }} - /> - } + {this.renderSelector(selectedFilter)} } diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex index 096b91024..bbc457013 100644 --- a/lib/plausible/stats/clickhouse.ex +++ b/lib/plausible/stats/clickhouse.ex @@ -1597,4 +1597,99 @@ defmodule Plausible.Stats.Clickhouse do db_query end end + + def make_suggestions(site, query, filter_name, filter_search) do + filter_query = "%#{filter_search}%" + + filter_name = case filter_name do + "page" -> "pathname" + "source" -> "referrer_source" + "os" -> "operating_system" + "os_version" -> "operating_system_version" + _ -> filter_name + end + + q = + if(filter_name == "pathname", + do: base_query(site, query), + else: base_session_query(site, query) + ) + |> from( + group_by: ^String.to_atom(filter_name), + order_by: [desc: fragment("count(*)")], + limit: 10 + ) + + q = + case filter_name do + "pathname" -> + from(e in q, + select: {e.pathname}, + where: fragment("? ilike ?", e.pathname, ^filter_query) + ) + + "entry_page" -> + from(e in q, + select: {e.entry_page}, + where: fragment("? ilike ?", e.entry_page, ^filter_query) + ) + + "exit_page" -> + from(e in q, + select: {e.exit_page}, + where: fragment("? ilike ?", e.exit_page, ^filter_query) + ) + + "referrer_source" -> + from(e in q, + select: {e.referrer_source}, + where: fragment("? ilike ?", e.referrer_source, ^filter_query) + ) + + "utm_medium" -> + from(e in q, + select: {e.utm_medium}, + where: fragment("? ilike ?", e.utm_medium, ^filter_query) + ) + + "utm_source" -> + from(e in q, + select: {e.utm_source}, + where: fragment("? ilike ?", e.utm_source, ^filter_query) + ) + + "utm_campaign" -> + from(e in q, + select: {e.utm_campaign}, + where: fragment("? ilike ?", e.utm_campaign, ^filter_query) + ) + + "referrer" -> + from(e in q, + select: {e.referrer}, + where: fragment("? ilike ?", e.referrer, ^filter_query) + ) + + "browser" -> + from(e in q, select: {e.browser}, where: fragment("? ilike ?", e.browser, ^filter_query)) + + "browser_version" -> + from(e in q, + select: {e.browser_version}, + where: fragment("? ilike ?", e.browser_version, ^filter_query) + ) + + "operating_system" -> + from(e in q, select: {e.operating_system}, where: fragment("? ilike ?", e.operating_system, ^filter_query)) + + "operating_system_version" -> + from(e in q, + select: {e.operating_system_version}, + where: fragment("? ilike ?", e.operating_system_version, ^filter_query) + ) + end + + + ClickhouseRepo.all(q) |> Enum.map(fn {suggestion} -> suggestion end) |> Enum.filter(fn suggestion -> suggestion != "" end) + end end diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 5985e4689..3936d4288 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -340,4 +340,11 @@ defmodule PlausibleWeb.Api.StatsController do def handle_errors(conn, %{kind: kind, reason: reason}) do json(conn, %{error: Exception.format_banner(kind, reason)}) end + + def filter_suggestions(conn, params) do + site = conn.assigns[:site] + query = Query.from(site.timezone, params) + + json(conn, Stats.make_suggestions(site, query, params["filter_name"], params["q"])) + end end diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index c7cbf68f4..4440031dd 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -69,6 +69,7 @@ defmodule PlausibleWeb.Router do get "/:domain/screen-sizes", StatsController, :screen_sizes get "/:domain/conversions", StatsController, :conversions get "/:domain/property/:prop_name", StatsController, :prop_breakdown + get "/:domain/suggestions/:filter_name", StatsController, :filter_suggestions end scope "/api/v1/stats", PlausibleWeb.Api do