80% of auto-complete filters progress

Still needs
- countries and screen into new format
- re-style dropdown and background
- drop debounce time
- tests
pull/1089/head^2
Vignesh Joglekar 6 years ago
parent 81c5e05760
commit 22d38c4cd8

@ -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'
]

@ -216,7 +216,7 @@ class Filters extends React.Component {
</>
)}
<span title={`Remove filter: ${formattedFilters[key]}`} className="filter-list-remove flex h-full w-full px-2 cursor-pointer hover:text-indigo-700 dark:hover:text-indigo-500 items-center" onClick={() => this.removeFilter(key, history, query)}>
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M6 18L18 6M6 6l12 12"></path></svg>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M6 18L18 6M6 6l12 12"></path></svg>
</span>
</span>
)

@ -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 (<select
value={updatedValue}
className="my-2 block w-full py-2 pl-3 pr-10 text-base border-gray-300 dark:border-gray-700 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:bg-gray-900 dark:text-gray-300"
placeholder="Select a Country"
onChange={(e) => this.setState({ updatedValue: e.target.value })}
>
{!Object.keys(country_codes).includes(updatedValue) && <option disabled value={updatedValue} className="hidden">Select a Country</option>}
<option disabled value="" className="hidden">Select a Country</option>
{Object.keys(country_codes).map(key => <option key={key} value={key}>{country_codes[key]}</option>)}
</select>)
}
if (filter === 'screen') {
return (
<>
<select
value={updatedValue}
className="my-2 block w-full py-2 pl-3 pr-10 text-base border-gray-300 dark:border-gray-700 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:bg-gray-900 dark:text-gray-300"
placeholder="Select a Screen Size"
onChange={(e) => this.setState({ updatedValue: e.target.value })}
>
{!screen_sizes.includes(updatedValue) && <option disabled value={updatedValue} className="hidden">Select a Screen Size</option>}
<option disabled value="" className="hidden">Select a Screen Size</option>
{screen_sizes.map(key => <option key={key} value={key}>{key}</option>)}
</select>
</>
)
}
return <div className="relative">
<input
id="suggestions_input"
type="text"
className="mt-4 bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500"
value={updatedValue}
placeholder="Filter value"
onChange={(e) => {
this.setState({
updatedValue: e.target.value
})
}}
onFocus={() => {
if (initialSuggest) {
this.setState({ initial: false })
this.fetchSuggestions()
}
this.setState({ inputFocused: true })
}}
onBlur={() => this.setState({ inputFocused: false })}
/>
<Transition
show={!!suggestions.length && inputFocused}
enter="transition ease-out duration-100 transform"
enterFrom="opacity-0 scale-95"
enterTo="opacity-100 scale-100"
leave="transition ease-in duration-75 transform"
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95"
>
<div
className="absolute mt-2 rounded shadow-md z-10"
style={{ width: '360px' }}
>
<div className="rounded bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 font-medium text-gray-800 dark:text-gray-200 py-1">
{suggestions.map((suggestion, index) => (
<span
key={suggestion}
className={`${focusedSuggestionIndex == index ? 'ring-2 ring-opacity-50' : ''} rounded px-4 py-2 md:text-sm leading-tight hover:bg-gray-200 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100 flex items-center justify-between cursor-pointer`}
onClick={() => {
this.setState({ updatedValue: suggestion, preventSuggestions: true })
}}
>
{suggestion}
</span>))}
</div>
</div>
</Transition>
</div>
}
renderBody() {
const { selectedFilter, negated, updatedValue, query } = this.state;
@ -81,15 +237,7 @@ class FilterModal extends React.Component {
</div>
}
{selectedFilter &&
<input
type="text"
className="mt-4 bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded w-full p-2 text-gray-700 dark:text-gray-300 leading-normal focus:outline-none focus:bg-white dark:focus:bg-gray-800 focus:border-gray-300 dark:focus:border-gray-500"
value={updatedValue}
placeholder="Filter value"
onChange={(e) => { this.setState({ updatedValue: e.target.value }) }}
/>
}
{this.renderSelector(selectedFilter)}
<button
type="submit"
@ -108,7 +256,7 @@ class FilterModal extends React.Component {
this.setState({ finalizedQuery })
}}
>
<svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
<svg className="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
Remove Filter
</button>
}

@ -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

@ -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

@ -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

Loading…
Cancel
Save