1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00

fix report button click listener (#1509)

fix search result exact match to top

linting

memory limit for es search in dev instance

Co-authored-by: 20C <code@20c.com>
This commit is contained in:
Stefan Pratter
2024-01-18 19:47:07 +02:00
committed by GitHub
parent 04a6d8f071
commit af4120ad87
4 changed files with 94 additions and 71 deletions

View File

@@ -24,6 +24,10 @@ services:
- discovery.type=single-node
networks:
- peeringdb_network
deploy:
resources:
limits:
memory: 2048M
redis:
image: "docker.io/redis:7"

View File

@@ -252,19 +252,23 @@ def new_elasticsearch():
return es
def order_results_alphabetically(result, search_term):
def order_results_alphabetically(result, search_terms):
"""
Order the search results alphabetically and put the exact case-insensitive matches in front.
Args:
- result: A dictionary containing categories and their search results.
- search_term: A string representing the search term.
- search_term: A list of search terms.
Returns:
- result: A dictionary containing the search results in alphabetical order.
"""
search_term_lower = search_term.lower()
# Make sure the search terms are lower case
search_terms_lower = [term.lower() for term in search_terms]
# Add the search terms as a single string to the list of search terms
search_terms_lower.append(" ".join(search_terms_lower))
for category in result:
result[category] = sorted(result[category], key=lambda x: x["name"].lower())
@@ -272,7 +276,7 @@ def order_results_alphabetically(result, search_term):
exact_match_index = -1
for index, item in enumerate(result[category]):
if item["name"].lower() == search_term_lower:
if item["name"].lower() in search_terms_lower:
exact_match_index = index
break
@@ -294,11 +298,16 @@ def search_v2(term, geo={}):
qs = " ".join([str(elem) for elem in term])
keywords = qs.split()
# will track the exact matches to put them on top of the results
look_for_exact_matches = []
result = ""
for keyword in keywords:
if keyword == "OR" or keyword == "AND":
result += f" {keyword}"
else:
# track the exact matches
look_for_exact_matches.append(keyword)
result += f" *{keyword}*"
term = result
if PARTIAL_IPV6_ADDRESS.match(" ".join(qs.split())):
@@ -358,11 +367,12 @@ def search_v2(term, geo={}):
append_result(
sq["_index"],
sq["_id"],
f"{sq['_source']['name']} ({sq['_source']['asn']})",
f"{sq['_source']['name']}",
sq["_source"]["org"]["id"],
None,
result,
pk_map,
{"asn": sq["_source"]["asn"]},
)
elif sq["_index"] == "org":
append_result(
@@ -385,7 +395,7 @@ def search_v2(term, geo={}):
pk_map,
)
result = order_results_alphabetically(result, term)
result = order_results_alphabetically(result, look_for_exact_matches)
return result
@@ -415,12 +425,18 @@ def add_secondary_entries(sq, result, pk_map):
append_result(tag, pk, name, org_id, sub_name, result, pk_map)
def append_result(tag, pk, name, org_id, sub_name, result, pk_map):
def append_result(tag, pk, name, org_id, sub_name, result, pk_map, extra={}):
if pk in pk_map[tag]:
return
pk_map[tag][pk] = True
result[tag].append(
{"id": pk, "name": name, "org_id": int(org_id), "sub_name": sub_name}
{
"id": pk,
"name": name,
"org_id": int(org_id),
"sub_name": sub_name,
"extra": extra,
}
)

View File

@@ -76,7 +76,7 @@
<div class="view_title">{% trans "Networks" %} ({{ count_net }})</div>
{% for row in search_net%}
<div class="result_row">
<a href="/{{'Network'|ref_tag}}/{{row.id}}">{{row.name}}</a>
<a href="/{{'Network'|ref_tag}}/{{row.id}}">{{row.name}}{% if search_version == 2 %} ({{ row.extra.asn }}){% endif %}</a>
{% if row.sponsorship %}
<a href="/sponsors" class="sponsor {{ row.sponsorship.css }}">{{ row.sponsorship.label }} {% trans "sponsor" %}</a>
{% endif %}

View File

@@ -537,16 +537,53 @@
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
{% if permissions.can_write %}
<script language="javascript" type="text/javascript">
{% if request.user.is_authenticated|flag_bad_data_needs_auth %}
var showReportDataElmnt = document.getElementById("showReportData")
if(showReportDataElmnt != null){
showReportDataElmnt.addEventListener("click", function() {
// Show the modal using Bootstrap's modal method
$('#report-bad-data').modal('show');
});
}
function submitForm() {
let inputValue = document.getElementById('searchInput').value;
let form = document.getElementById('quick-search-view');
form.action = "/search/v2?q=" + encodeURIComponent(inputValue);
return true;
}
reportDataFormElmnt = document.getElementById("reportDataForm")
if(reportDataFormElmnt != null){
reportDataFormElmnt.addEventListener("submit", function(event) {
event.preventDefault(); // Prevent the default form submission
// Retrieve form data
const formData = new FormData(event.target);
const formObject = {};
formData.forEach(function(value, key) {
formObject[key] = value;
});
var subject = "I am seeing bad data with " + formObject["obj_type"] + " at " + formObject["view_url"];
var fields = "The field(s) in question are:\n"+formObject["fields"]+"\n";
var reason = "I think this is bad data because:\n"+formObject["reason"]+"\n";
var expected = "The correct data is probably:\n"+formObject["expected"]+"\n";
var body = fields+reason+expected;
// Construct the mailto link
var mailtoLink = `mailto:${formObject["server_email"]}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
// Open a pop-up window with the mailto link
var popup = window.open(mailtoLink, "EmailPopup", "width=600, height=400");
// Clear the form or perform other actions
event.target.reset();
});
}
{% endif %}
{% if permissions.can_write %}
function submitForm() {
let inputValue = document.getElementById('searchInput').value;
let form = document.getElementById('quick-search-view');
form.action = "/search/v2?q=" + encodeURIComponent(inputValue);
return true;
}
const deleteElement = (e) => {
@@ -563,69 +600,35 @@ function submitForm() {
new_service_identifier.editable("toggle");
}
document.getElementById("showReportData").addEventListener("click", function() {
// Show the modal using Bootstrap's modal method
$('#report-bad-data').modal('show');
});
$(document).ready(function() {
PeeringDB.incomplete_data_notify();
PeeringDB.ViewActions.init();
document.getElementById("reportDataForm").addEventListener("submit", function(event) {
event.preventDefault(); // Prevent the default form submission
// Retrieve form data
const formData = new FormData(event.target);
const formObject = {};
formData.forEach(function(value, key) {
formObject[key] = value;
});
// You can now use the form data as needed, for example, send it to a server via AJAX or process it in JavaScript
console.log(formObject);
var subject = "I am seeing bad data with " + formObject["obj_type"] + " at " + formObject["view_url"];
var fields = "The field(s) in question are:\n"+formObject["fields"]+"\n";
var reason = "I think this is bad data because:\n"+formObject["reason"]+"\n";
var expected = "The correct data is probably:\n"+formObject["expected"]+"\n";
var body = fields+reason+expected;
// Construct the mailto link
var mailtoLink = `mailto:${formObject["server_email"]}?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
// Open a pop-up window with the mailto link
var popup = window.open(mailtoLink, "EmailPopup", "width=600, height=400");
console.log(mailtoLink);
// Clear the form or perform other actions
event.target.reset();
});
$(document).ready(function() {
PeeringDB.incomplete_data_notify();
PeeringDB.ViewActions.init();
var editForm = $('[data-edit-target="api:{{ref_tag}}:update"]')
editForm.on('action-success:submit', function(e,data) {
if(PeeringDB.ixf_proposals && PeeringDB.ixf_proposals.require_refresh) {
document.location.href = document.location.href;
} else {
PeeringDB.ViewTools.after_submit(editForm, data);
setTimeout(PeeringDB.incomplete_data_notify, 500);
}
});
editForm.on('action-success:toggle', function(e, payload) {
if(payload.mode == "view") {
var editForm = $('[data-edit-target="api:{{ref_tag}}:update"]')
editForm.on('action-success:submit', function(e,data) {
if(PeeringDB.ixf_proposals && PeeringDB.ixf_proposals.require_refresh) {
document.location.href = document.location.href;
} else {
PeeringDB.ViewTools.after_submit(editForm, data);
setTimeout(PeeringDB.incomplete_data_notify, 500);
}
}
})
});
$('[data-bs-toggle="tooltip"]').tooltip({container:"body", trigger:"hover"});
editForm.on('action-success:toggle', function(e, payload) {
if(payload.mode == "view") {
if(PeeringDB.ixf_proposals && PeeringDB.ixf_proposals.require_refresh) {
document.location.href = document.location.href;
}
}
})
});
$('[data-bs-toggle="tooltip"]').tooltip({container:"body", trigger:"hover"});
});
{% endif %}
</script>
{% endif %}
{% if ref_tag == "campus" %}
<script>