Skip to content
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

[Test] Gvm/search layer test #11630

Draft
wants to merge 14 commits into
base: dev/8.0.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 190 additions & 2 deletions arches/app/media/js/viewmodels/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,192 @@ define([
'templates/views/components/map-popup.htm'
], function($, _, arches, ko, koMapping, mapPopupProvider, mapConfigurator, ariaUtils) {
const viewModel = function(params) {
var self = this;
const ZOOM_THRESHOLD = 14;
const self = this;
const searchLayerIds = ['clusters', 'cluster-count', 'unclustered-point', 'individual-points', 'individual-geometries'];

const searchLayerDefinitions = [
{
id: 'individual-points',
type: 'circle',
source: 'search-layer-source',
'source-layer': 'points',
minzoom: ZOOM_THRESHOLD,
paint: {
'circle-color': '#fa6003',
'circle-radius': 5,
'circle-opacity': 1
}
},
{
id: 'individual-geometries',
type: 'fill',
source: 'search-layer-source',
'source-layer': 'geometries',
minzoom: ZOOM_THRESHOLD-1,
paint: {
'fill-color': '#fa6003',
'fill-opacity': 0.3,
'fill-outline-color': '#fa6003'
}
},
{
"id": "clusters",
"type": "circle",
"source": "search-layer-source",
"source-layer": "clusters",
"filter": [
"all",
["has", "count"],
[">", ["get", "count"], 1]
],
"paint": {
"circle-color": "#fa6003",
"circle-radius": [
"step",
["get", "count"],
15,
10, 20,
50, 25,
100, 30,
500, 35,
1000, 40
],
"circle-opacity": 0.8
},
"maxzoom": ZOOM_THRESHOLD,
"minzoom": 1
},
{
"id": "cluster-count",
"type": "symbol",
"source": "search-layer-source",
"source-layer": "clusters",
"filter": [
"all",
["has", "count"],
[">", ["get", "count"], 0]
],
"layout": {
"text-field": "{count}",
"text-font": ["DIN Offc Pro Medium", "Arial Unicode MS Bold"],
"text-size": 12
},
"paint": {
"text-color": "#ffffff"
},
"maxzoom": ZOOM_THRESHOLD,
"minzoom": 1
},
{
"id": "unclustered-point",
"type": "circle",
"source": "search-layer-source",
"source-layer": "clusters",
"filter": [
"all",
["has", "count"],
["==", ["get", "count"], 1]
],
"paint": {
"circle-color": "#fa6003",
"circle-radius": 5,
"circle-opacity": 1
},
minzoom: ZOOM_THRESHOLD
}
];
this.searchQueryId = params.searchQueryId || ko.observable();
this.searchQueryId.subscribe(function (searchId) {
if (searchId) {
self.addSearchLayer(searchId);
} else if (ko.unwrap(self.map)) {
self.removeSearchLayer();
}
});

this.addClusterClickHandlers = function() {
const map = self.map();

// Handle clicks on clusters
map.on('click', 'clusters', function(e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['clusters'] });
var feature = features[0];
var count = feature.properties.count;

if (count > 1) {
// Zoom in on the cluster
var coordinates = feature.geometry.coordinates.slice();
map.easeTo({
center: coordinates,
zoom: map.getZoom() + 2
});
} else {
// For count == 1, show a popup
self.onFeatureClick(features, e.lngLat, mapboxgl);
}
});

// Change the cursor to a pointer when over clusters and unclustered points
map.on('mouseenter', 'clusters', function() {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'clusters', function() {
map.getCanvas().style.cursor = '';
});
map.on('mouseenter', 'unclustered-point', function() {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'unclustered-point', function() {
map.getCanvas().style.cursor = '';
});
};


this.addSearchLayer = function (searchId) {
console.log(searchId);
if (!self.map())
return;
const tileUrlTemplate = `${window.location.origin}/search-layer/{z}/{x}/{y}.pbf?searchid=${encodeURIComponent(searchId)}`;

// Remove existing source and layer if they exist
searchLayerIds.forEach(layerId => {
if (self.map().getLayer(layerId)) {
self.map().removeLayer(layerId);
}
});
if (self.map().getSource('search-layer-source')) {
self.map().removeSource('search-layer-source');
}

// Add the vector tile source
self.map().addSource('search-layer-source', {
type: 'vector',
tiles: [tileUrlTemplate],
minzoom: 0,
maxzoom: 22,
});

// Add the layer to display the data
searchLayerDefinitions.forEach(mapLayer => {
self.map().addLayer(mapLayer);
});

self.addClusterClickHandlers();
// Optionally, fit the map to the data bounds
// self.fitMapToDataBounds(searchId);
};

this.removeSearchLayer = function () {
searchLayerDefinitions.forEach(mapLayer => {
if (self.map().getLayer(mapLayer.id)) {
self.map().removeLayer(mapLayer.id);
}
});
if (self.map().getSource('search-layer-source')) {
self.map().removeSource('search-layer-source');
}
};


var geojsonSourceFactory = function() {
Expand Down Expand Up @@ -61,7 +246,10 @@ define([
if (ko.unwrap(params.bounds)) {
map.fitBounds(ko.unwrap(params.bounds), boundingOptions);
}

// If searchQueryId is already available, add the search layer
if (self.searchQueryId()) {
self.addSearchLayer(self.searchQueryId());
}
});

this.bounds = ko.observable(ko.unwrap(params.bounds) || arches.hexBinBounds);
Expand Down
9 changes: 1 addition & 8 deletions arches/app/media/js/views/components/search/map-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ define([
options.name = "Map Filter";
BaseFilter.prototype.initialize.call(this, options);

options.searchQueryId = this.searchQueryId;
options.sources = {
"geojson-search-buffer-data": {
"type": "geojson",
Expand Down Expand Up @@ -369,14 +370,6 @@ define([
this.updateFilter();
}, this);

this.searchAggregations.subscribe(this.updateSearchResultsLayers, this);
if (ko.isObservable(bins)) {
bins.subscribe(this.updateSearchResultsLayers, this);
}
if (this.searchAggregations()) {
this.updateSearchResultsLayers();
}
this.mouseoverInstanceId.subscribe(updateSearchResultPointLayer);
}, this);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ define([

this.selectedPopup = ko.observable('');
this.sharedStateObject.selectedPopup = this.selectedPopup;
this.searchQueryId = ko.observable(null);
this.sharedStateObject.searchQueryId = this.searchQueryId;
var firstEnabledFilter = _.find(this.sharedStateObject.searchFilterConfigs, function(filter) {
return filter.config.layoutType === 'tabbed';
}, this);
Expand Down Expand Up @@ -51,6 +53,47 @@ define([
this.searchFilterVms[componentName](this);
},

doQuery: function() {
const queryObj = JSON.parse(this.queryString());
if (self.updateRequest) { self.updateRequest.abort(); }
self.updateRequest = $.ajax({
type: "GET",
url: arches.urls.search_results,
data: queryObj,
context: this,
success: function(response) {
_.each(this.sharedStateObject.searchResults, function(value, key, results) {
if (key !== 'timestamp') {
delete this.sharedStateObject.searchResults[key];
}
}, this);
_.each(response, function(value, key, response) {
if (key !== 'timestamp') {
this.sharedStateObject.searchResults[key] = value;
}
}, this);
this.sharedStateObject.searchResults.timestamp(response.timestamp);
this.searchQueryId(this.sharedStateObject.searchResults.searchqueryid);
this.sharedStateObject.userIsReviewer(response.reviewer);
this.sharedStateObject.userid(response.userid);
this.sharedStateObject.total(response.total_results);
this.sharedStateObject.hits(response.results.hits.hits.length);
this.sharedStateObject.alert(false);
},
error: function(response, status, error) {
const alert = new AlertViewModel('ep-alert-red', arches.translations.requestFailed.title, response.responseJSON?.message);
if(self.updateRequest.statusText !== 'abort'){
this.alert(alert);
}
this.sharedStateObject.loading(false);
},
complete: function(request, status) {
self.updateRequest = undefined;
window.history.pushState({}, '', '?' + $.param(queryObj).split('+').join('%20'));
this.sharedStateObject.loading(false);
}
});
},
});

return ko.components.register(componentName, {
Expand Down
74 changes: 73 additions & 1 deletion arches/app/search/components/standard_search_view.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Dict, Tuple

import hashlib
from arches.app.models.system_settings import settings
from arches.app.search.components.base_search_view import BaseSearchView
from arches.app.search.components.base import SearchFilterFactory
Expand All @@ -16,9 +16,11 @@
user_is_resource_exporter,
)
from arches.app.utils.string_utils import get_str_kwarg_as_bool
from django.core.cache import cache
from django.utils.translation import gettext as _
from datetime import datetime
import logging
import json


details = {
Expand Down Expand Up @@ -136,6 +138,38 @@ def append_dsl(self, search_query_object, **kwargs):
if load_tiles:
search_query_object["query"].include("tiles")

def execute_resourceids_only_query(self, search_query_object, cache, se, **kwargs):
search_request_object = kwargs.get("search_request_object", self.request.GET)
resourceids_only_query_hash_key = create_searchresults_cache_key(
self.request, search_request_object, resourceids_only=True
)

hpla_idx = f"{settings.ELASTICSEARCH_PREFIX}_{RESOURCES_INDEX}"
pit_response = se.es.open_point_in_time(
index=hpla_idx, keep_alive="5m" # Adjust as needed
)
pit_id = pit_response.get("id")
# Perform the search
search_query_object["query"].prepare()
query_dsl = search_query_object["query"].dsl
search_response = se.es.search(
pit={"id": pit_id, "keep_alive": "5m"}, _source=False, **query_dsl
)

# Cache the pit_id and search parameters
cache.set(
resourceids_only_query_hash_key + "_pit",
pit_id,
timeout=300,
)
cache.set(
resourceids_only_query_hash_key + "_dsl",
search_query_object["query"].__str__(),
timeout=300,
)

return resourceids_only_query_hash_key

def execute_query(self, search_query_object, response_object, **kwargs):
for_export = get_str_kwarg_as_bool("export", self.request.GET)
pages = self.request.GET.get("pages", None)
Expand Down Expand Up @@ -232,12 +266,22 @@ def handle_search_results_query(
if returnDsl:
return response_object, search_query_object

for_export = get_str_kwarg_as_bool("export", sorted_query_obj)
if not for_export:
resourceids_only_query_hash_key = self.execute_resourceids_only_query(
search_query_object,
cache,
se,
search_request_object=sorted_query_obj,
)

for filter_type, querystring in list(sorted_query_obj.items()):
search_filter = search_filter_factory.get_filter(filter_type)
if search_filter:
search_filter.execute_query(search_query_object, response_object)

if response_object["results"] is not None:
response_object["searchqueryid"] = resourceids_only_query_hash_key
# allow filters to modify the results
for filter_type, querystring in list(sorted_query_obj.items()):
search_filter = search_filter_factory.get_filter(filter_type)
Expand All @@ -256,3 +300,31 @@ def handle_search_results_query(
response_object[key] = value

return response_object, search_query_object


def create_searchresults_cache_key(request, search_query, **kwargs):
"""
method to create a hash cache key
blends a snapshot of the current database with whatever the searchquery was
also cleans/sorts the searchquery before converting to string in order to normalize
kwargs:
- dict: search_query - contains search query filters/parameters
"""
resourceids_only = kwargs.get("resourceids_only", False)
user_proxy = (
request.user.username
if request.user.username == "anonymous"
else str(request.user.id)
)
search_query_string = "".join([k + str(v) for k, v in sorted(search_query.items())])

search_query_string = search_query_string.strip()

hashable_string = search_query_string + user_proxy
hashable_string += "rids" if resourceids_only else ""
b = bytearray()
b.extend((search_query_string + user_proxy).encode())
search_query_cache_key_hash = hashlib.sha1(b)
search_query_cache_key_hash = search_query_cache_key_hash.hexdigest()

return search_query_cache_key_hash
Loading
Loading