Skip to content

Location search filter #71

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

Merged
merged 4 commits into from
Jul 23, 2025
Merged
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
38 changes: 34 additions & 4 deletions src/assets/js/selectedIndicatorsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ async function checkGeoCoverage(geoValue) {
}
}

function getAvailableGeos(indicators) {
var availableGeos = null;

const csrftoken = Cookies.get("csrftoken");

const submitData = {
indicators: indicators
}

$.ajax({
url: "get_available_geos/",
type: "POST",
async: false, // Synchronous request to ensure availableGeos is populated before returning
dataType: "json",
contentType: "application/json",
headers: { "X-CSRFToken": csrftoken },
data: JSON.stringify(submitData),
}).done(function (data) {
availableGeos = data.geographic_granularities;
});
return availableGeos;
}

$("#geographic_value").on("select2:select", function (e) {
var geo = e.params.data;
checkGeoCoverage(geo.id).then((notCoveredIndicators) => {
Expand All @@ -187,6 +210,14 @@ $("#geographic_value").on("select2:select", function (e) {

$("#showSelectedIndicatorsButton").click(function () {
alertPlaceholder.innerHTML = "";
const availableGeos = getAvailableGeos(checkedIndicatorMembers);
const locationIds = $("#location_search").select2("data").map((item) => item.id);
$("#geographic_value").select2({
data: availableGeos,
minimumInputLength: 0,
maximumSelectionLength: 5,
});
$('#geographic_value').val(locationIds).trigger('change');
if (!indicatorHandler.checkForCovidcastIndicators()) {
$('#geographic_value').val(null).trigger('change');
$("#geographic_value").prop("disabled", true);
Expand All @@ -200,11 +231,10 @@ $("#showSelectedIndicatorsButton").click(function () {
}
})
});
var otherEndpointLocationsWarning = `<div class="alert alert-info" data-mdb-alert-init role="alert">` +
` <div>Please, note that some indicator sets may require to select location(s) that is/are different from location(s) above.<br> `
nonCovidcastIndicatorSets = [...new Set(checkedIndicatorMembers.filter(indicator => indicator["_endpoint"] != "covidcast").map((indicator) => indicator["indicator_set"]))];
otherEndpointLocationsWarning += `Different location is required for following Indicator Set(s): ${nonCovidcastIndicatorSets.join(", ")}`
otherEndpointLocationsWarning += `</div></div>`
var otherEndpointLocationsWarning = `<div class="alert alert-info" data-mdb-alert-init role="alert">`
otherEndpointLocationsWarning += `For Indicator Set(s): ${nonCovidcastIndicatorSets.join(", ")}, please use the Location menu below:`
otherEndpointLocationsWarning += `</div>`
if (indicatorHandler.getFluviewIndicators().length > 0) {
$("#differentLocationNote").html(otherEndpointLocationsWarning)
if (document.getElementsByName("fluviewRegions").length === 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/indicatorsets/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.urls import path
from django.urls.resolvers import URLPattern
from indicatorsets.views import (IndicatorSetListView, epivis,
generate_export_data_url, preview_data, create_query_code)
generate_export_data_url, preview_data, create_query_code, get_available_geos)

urlpatterns: list[URLPattern] = [
path("", IndicatorSetListView.as_view(), name="indicatorsets"),
path("epivis/", epivis, name="epivis"),
path("export/", generate_export_data_url, name="export"),
path("preview_data/", preview_data, name="preview_data"),
path("create_query_code/", create_query_code, name="create_query_code"),
path("get_available_geos/", get_available_geos, name="get_available_geos"),
]
39 changes: 39 additions & 0 deletions src/indicatorsets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,3 +540,42 @@ def create_query_code(request):
{"python_code_blocks": python_code_blocks, "r_code_blocks": r_code_blocks},
safe=False,
)


def get_available_geos(request):
if request.method == "POST":
geo_values = []
data = json.loads(request.body)
indicators = data.get("indicators", [])
grouped_indicators = group_by_property(indicators, "data_source")
for data_source, indicators in grouped_indicators.items():
indicators_str = ",".join(indicator["indicator"] for indicator in indicators)
response = requests.get(f"{settings.EPIDATA_URL}covidcast/geo_indicator_coverage", params={"data_source": data_source, "signals": indicators_str}, auth=("epidata", settings.EPIDATA_API_KEY))
if response.status_code == 200:
data = response.json()
if len(data["epidata"]):
geo_values.extend(data["epidata"])
unique_values = set(geo_values)
geo_levels = set([el.split(":")[0] for el in unique_values])
geo_unit_ids = set([geo_value.split(":")[1] for geo_value in unique_values])
geographic_granularities = [
{
"id": f"{geo_unit.geo_level.name}:{geo_unit.geo_id}",
"geoType": geo_unit.geo_level.name,
"text": geo_unit.display_name,
"geoTypeDisplayName": geo_unit.geo_level.display_name,
}
for geo_unit in GeographyUnit.objects.filter(geo_level__name__in=geo_levels).filter(geo_id__in=geo_unit_ids)
.prefetch_related("geo_level")
.order_by("level")
]
grouped_geographic_granularities = group_by_property(
geographic_granularities, "geoTypeDisplayName"
)
geographic_granularities = []
for key, value in grouped_geographic_granularities.items():
geographic_granularities.append({
"text": key,
"children": value,
})
return JsonResponse({"geographic_granularities": geographic_granularities}, safe=False)
18 changes: 4 additions & 14 deletions src/templates/indicatorsets/selectedIndicatorsModal.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,9 @@ <h5 class="modal-title" id="selectedIndicatorsModalLabel">Selected indicators</h
</div>
</div>
</div>
<script src="{% static 'js/indicatorHandler.js' %}"></script>
<script src="{% static 'js/selectedIndicatorsModal.js' %}"></script>
<script src="{% static 'js/select2_init.js' %}"></script>
<script>
$(document).ready(function () {
const geoValues = {{ geographic_granularities|safe }};
var urlParams = JSON.parse(JSON.stringify({{ url_params_dict|safe }}));
initSelect2('geographic_value', geoValues);
if (urlParams.location_search != "") {
var locationSearch = urlParams.location_search;
var locationIds = locationSearch.map((item) => item);
$('#geographic_value').val(locationIds).trigger('change');
}
});

const geographicGranularities = {{ geographic_granularities|safe }};
</script>
<script src="{% static 'js/indicatorHandler.js' %}"></script>
<script src="{% static 'js/selectedIndicatorsModal.js' %}"></script>
<script src="{% static 'js/select2_init.js' %}"></script>