95% completed auto-complete setup

Still needs:
- tests
- goals added as filter
pull/1089/head^2
Vignesh Joglekar 6 years ago
parent 22d38c4cd8
commit 4ca39cc406

@ -1,253 +0,0 @@
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'
]

@ -3,9 +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'
import Datamap from 'datamaps'
class FilterModal extends React.Component {
constructor(props) {
@ -18,8 +18,8 @@ class FilterModal extends React.Component {
filterSaved: false,
suggestions: [],
fetchSuggestionsTimeout: undefined,
initialSuggest: true,
focusedSuggestionIndex: -1
focusedSuggestionIndex: -1,
preventSuggestions: true
}
this.editableGoals = Object.keys(this.state.query.filters).filter(filter => !['goal', 'props'].includes(filter))
@ -35,15 +35,24 @@ class FilterModal extends React.Component {
}
componentDidUpdate(prevProps, prevState) {
const { query, selectedFilter, updatedValue, preventSuggestions } = this.state
const { query, selectedFilter, updatedValue, preventSuggestions, fetchSuggestionsTimeout } = 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] || "")
let updatedValue = negated ? query.filters[selectedFilter].slice(1) : (query.filters[selectedFilter] || "")
clearTimeout(fetchSuggestionsTimeout)
clearTimeout(this.state.fetchSuggestionsTimeout)
if (selectedFilter == 'country') {
const allCountries = Datamap.prototype.worldTopo.objects.world.geometries;
const selectedCountry = allCountries.find((c) => c.id === updatedValue) || { properties: { name: updatedValue } };
updatedValue = selectedCountry.properties.name
}
this.setState({ updatedValue, negated, suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 })
this.setState({ updatedValue, negated, suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: false }, () => {
if (selectedFilter) {
this.fetchSuggestions()
}
})
}
if (prevState.updatedValue !== updatedValue) {
@ -51,13 +60,11 @@ class FilterModal extends React.Component {
let fetchSuggestionsTimeout;
clearTimeout(this.state.fetchSuggestionsTimeout)
if (!['country', 'screen'].includes(selectedFilter)) {
fetchSuggestionsTimeout = setTimeout(this.fetchSuggestions, 1000)
}
this.setState({ suggestions: [], fetchSuggestionsTimeout, preventSuggestions: false, focusedSuggestionIndex: -1 })
this.setState({ suggestions: [], preventSuggestions: false, focusedSuggestionIndex: -1, noSuggestions: false }, () => {
this.setState({ fetchSuggestionsTimeout: setTimeout(this.fetchSuggestions, 200) })
})
} else {
this.setState({ suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1 })
this.setState({ suggestions: [], fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: false })
}
}
}
@ -68,7 +75,7 @@ class FilterModal extends React.Component {
handleKeydown(e) {
const { suggestions } = this.state
let { focusedSuggestionIndex, updatedValue, preventSuggestions } = this.state
let { focusedSuggestionIndex, updatedValue, preventSuggestions, selectedFilter } = this.state
const navigationKeys = ['ArrowUp', undefined, 'ArrowDown']
if (e.target.id !== 'suggestions_input') return true
@ -82,7 +89,7 @@ class FilterModal extends React.Component {
}
if (navigationKeys.includes(e.key)) {
focusedSuggestionIndex += navigationKeys.indexOf(e.key)-1
focusedSuggestionIndex += navigationKeys.indexOf(e.key) - 1
if (focusedSuggestionIndex < 0) {
focusedSuggestionIndex = suggestions.length - 1
} else if (focusedSuggestionIndex >= suggestions.length) {
@ -100,47 +107,30 @@ class FilterModal extends React.Component {
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 }))
const updatedQuery = { ...query, filters: { ...query.filters, [selectedFilter]: null } }
if (selectedFilter == 'country') {
const matchedCountries = Datamap.prototype.worldTopo.objects.world.geometries.filter(c => c.properties.name.includes(updatedValue.trim()))
const matches = matchedCountries.map(c => c.id)
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/country`, updatedQuery, { q: matches })
.then((res) => {
const suggestions = res.map(code => matchedCountries.filter(c => c.id == code)[0].properties.name)
this.setState({ suggestions, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: res.length == 0 })
})
} else {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/suggestions/${selectedFilter}`, updatedQuery, { q: updatedValue.trim() })
.then((res) => {
this.setState({ suggestions: res, fetchSuggestionsTimeout: undefined, focusedSuggestionIndex: -1, noSuggestions: res.length == 0 })
})
}
}
renderSelector(filter) {
const { updatedValue, suggestions, initialSuggest, inputFocused, focusedSuggestionIndex } = this.state;
const { updatedValue, suggestions, inputFocused, focusedSuggestionIndex, selectedFilter } = 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>
</>
)
}
if (!filter) return null
return <div className="relative">
<input
@ -155,10 +145,6 @@ class FilterModal extends React.Component {
})
}}
onFocus={() => {
if (initialSuggest) {
this.setState({ initial: false })
this.fetchSuggestions()
}
this.setState({ inputFocused: true })
}}
onBlur={() => this.setState({ inputFocused: false })}
@ -176,13 +162,13 @@ class FilterModal extends React.Component {
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">
<div className="rounded bg-white dark:bg-gray-850 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`}
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-950 hover:text-gray-900 dark:hover:text-gray-100 flex items-center justify-between cursor-pointer`}
onClick={() => {
this.setState({ updatedValue: suggestion, preventSuggestions: true })
this.setState({ updatedValue: suggestion, preventSuggestions: true, suggestions: [] })
}}
>
{suggestion}
@ -194,12 +180,9 @@ class FilterModal extends React.Component {
}
renderBody() {
const { selectedFilter, negated, updatedValue, query } = this.state;
const { selectedFilter, negated, updatedValue, query, noSuggestions } = this.state;
const finalFilterValue = (this.negationSupported(selectedFilter) && negated ? '!' : '') + updatedValue
const finalizedQuery = new URLSearchParams(window.location.search)
const validFilter = this.editableGoals.includes(selectedFilter) && updatedValue
finalizedQuery.set(selectedFilter, finalFilterValue)
const validFilter = this.editableGoals.includes(selectedFilter) && updatedValue.trim()
return (
<React.Fragment>
@ -208,6 +191,15 @@ class FilterModal extends React.Component {
<div className="my-4 border-b border-gray-300"></div>
<main className="modal__content">
<form className="flex flex-col" onSubmit={() => {
let finalFilterValue = (this.negationSupported(selectedFilter) && negated ? '!' : '') + updatedValue.trim()
if (selectedFilter == 'country') {
const allCountries = Datamap.prototype.worldTopo.objects.world.geometries;
const selectedCountry = allCountries.find((c) => c.properties.name === finalFilterValue) || { id: finalFilterValue };
finalFilterValue = selectedCountry.id
}
const finalizedQuery = new URLSearchParams(window.location.search)
finalizedQuery.set(selectedFilter, finalFilterValue)
if (validFilter) {
this.setState({ finalizedQuery })
}
@ -239,6 +231,13 @@ class FilterModal extends React.Component {
{this.renderSelector(selectedFilter)}
{noSuggestions &&
<span className="text-red-500 text-md flex mt-2 px-3">
<svg className="w-8 h-8 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="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
<span className="">No results found in current dashboard, try removing some filters{updatedValue.trim().length ? ', checking your input' : ''} and ensuring this data exists</span>
</span>
}
<button
type="submit"
disabled={!validFilter}

@ -1598,16 +1598,38 @@ defmodule Plausible.Stats.Clickhouse do
end
end
def make_suggestions(site, query, "country", filter_search) do
filter_search =
String.split(filter_search, ",")
|> Enum.map(fn c -> Plausible.Stats.CountryName.to_alpha2(c) end)
q =
from(
e in base_session_query(site, query),
group_by: e.country_code,
order_by: [desc: fragment("count(*)")],
select: {e.country_code}
)
ClickhouseRepo.all(q)
|> Enum.map(fn {x} -> x end)
|> Enum.filter(fn c -> Enum.find(filter_search, false, fn x -> x == c end) end)
|> Enum.map(fn c -> Plausible.Stats.CountryName.to_alpha3(c) end)
|> Enum.slice(0..9)
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
filter_name =
case filter_name do
"page" -> "pathname"
"source" -> "referrer_source"
"os" -> "operating_system"
"os_version" -> "operating_system_version"
"screen" -> "screen_size"
_ -> filter_name
end
q =
if(filter_name == "pathname",
@ -1680,16 +1702,26 @@ defmodule Plausible.Stats.Clickhouse do
)
"operating_system" ->
from(e in q, select: {e.operating_system}, where: fragment("? ilike ?", e.operating_system, ^filter_query))
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
"screen_size" ->
from(e in q,
select: {e.screen_size},
where: fragment("? ilike ?", e.screen_size, ^filter_query)
)
end
ClickhouseRepo.all(q) |> Enum.map(fn {suggestion} -> suggestion end) |> Enum.filter(fn suggestion -> suggestion != "" end)
ClickhouseRepo.all(q)
|> Enum.map(fn {suggestion} -> suggestion end)
|> Enum.filter(fn suggestion -> suggestion != "" end)
end
end

Loading…
Cancel
Save