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

[DO NOT MERGE YET] Error handling and logging for global and policy searches #6581

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions fec/fec/static/scss/components/_messages.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
margin: u(2rem 0);
padding: u(5rem 2rem 2rem 2rem);

.message--white {
background-color: #fff
}

p:last-child {
margin-bottom: 0;
}
Expand Down
8 changes: 6 additions & 2 deletions fec/search/templates/search/policy_guidance_search_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{% block feed %}
<div class="row" id="results-container">
<div class="main">
{% if results.results %}
{% if results.meta.count > 0 %}
<section id="site">
<div class="heading--section" class="row">
<h2 class="u-float-left u-no-margin">Results</h2>
Expand Down Expand Up @@ -65,12 +65,16 @@ <h3>
{% endif %}
</div>
</section>
{% elif results.meta.count == 0 %}
{% elif results.meta.count == 0 and not policy_search_error %}
<div class="message message--info">
<h2>No results</h2>
<p>We didn’t find any results matching <strong>&ldquo;{{search_query}}&rdquo;</strong>.</p>
<p>Try changing your search term.</p>
</div>
{% elif policy_search_error %}
<div class="message message--alert">
<p>{{policy_search_error | safe }}</p>
</div>
{% endif %}
</div>
</div>
Expand Down
49 changes: 47 additions & 2 deletions fec/search/templates/search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,55 @@ <h3>
</div>
</section>
{% endif %}
{% else %}
{% if site_search_error %}
<div class="message message--small message--alert">
<p>{{ site_search_error | safe }} </p>
</div>
{% endif %}
{% if cand_search_error %}
<div class="message message--small message--alert">
<p>{{ cand_search_error | safe }} </p>
</div>
{% endif %}
{% if comm_search_error %}
<div class="message message--small message--alert">
<p>{{ comm_search_error | safe }} </p>
</div>
{% endif %}
{% elif results.count == 0 %}
<div class="message message--info">
<h2>No results</h2>
<p>We didn’t find any pages matching <strong>&ldquo;{{search_query}}&rdquo;</strong>.</p>
{% if site_search_error or cand_search_error or comm_search_error %}
<p>We didn’t find any pages matching <strong>&ldquo;{{search_query}}&rdquo;</strong> in:</p>
<ul class="list--bulleted">
{% if 'candidates' in type and not cand_search_error %}
<li><strong>Candidates</strong></li>
{% endif %}
{% if 'committees' in type and not comm_search_error %}
<li><strong>Committees</strong></li>
{% endif %}
{% if 'site' in type and not site_search_error %}
<li><strong>Other pages</strong></li>
{% endif %}
</ul>
{% if site_search_error %}
<div class="message message--white message--small message--alert">
<p>{{ site_search_error | safe }} </p>
</div>
{% endif %}
{% if cand_search_error %}
<div class="message message--white message--small message--alert">
<p>{{ cand_search_error | safe }}</p>
</div>
{% endif %}
{% if comm_search_error %}
<div class="message message--white message--small message--alert">
<p>{{ comm_search_error | safe }}</p>
</div>
{% endif %}
{% else %}
<p>We didn’t find any pages matching <strong>&ldquo;{{search_query}}&rdquo;</strong></p>
{% endif %}
<div class="message--alert__bottom">
<p>Think this was a mistake?<br>Try one of the other search tools or submit feedback.</p>
<p>
Expand Down
78 changes: 57 additions & 21 deletions fec/search/views.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import os
import requests
import math
import logging

from urllib import parse

from django.shortcuts import render
from django.conf import settings

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def search_candidates(query):
"""Searches the data API for candidates matching the query"""
path = os.path.join(settings.FEC_API_VERSION, 'candidates', 'search')
url = parse.urljoin(settings.FEC_API_URL, path)
r = requests.get(url, params={
'q': query, 'sort': '-receipts', 'per_page': 3, 'api_key': settings.FEC_API_KEY_PRIVATE
})
return r.json()

try:
r = requests.get(url, params={'q': query, 'sort': '-receipts', 'per_page': 3, 'api_key': settings.FEC_API_KEY_PRIVATE})
return r.json()
except (Exception) as ex:
logger.error('search_canndidates:' + ex.__class__.__name__)
return None

Check warning on line 25 in fec/search/views.py

View check run for this annotation

Codecov / codecov/patch

fec/search/views.py#L23-L25

Added lines #L23 - L25 were not covered by tests


def search_committees(query):
"""Searches the data API for committees matching the query"""
path = os.path.join(settings.FEC_API_VERSION, 'committees')
url = parse.urljoin(settings.FEC_API_URL, path)
r = requests.get(url, params={
'q': query, 'per_page': 3, 'sort': '-receipts', 'api_key': settings.FEC_API_KEY_PRIVATE
})
return r.json()

try:
r = requests.get(url, params={
'q': query, 'per_page': 3, 'sort': '-receipts', 'api_key': settings.FEC_API_KEY_PRIVATE})
return r.json()
except (Exception) as ex:
logger.error('search_committees:' + ex.__class__.__name__)
return None

Check warning on line 39 in fec/search/views.py

View check run for this annotation

Codecov / codecov/patch

fec/search/views.py#L37-L39

Added lines #L37 - L39 were not covered by tests


def prev_offset(limit, next_offset):
Expand Down Expand Up @@ -98,10 +109,12 @@
'limit': limit,
'offset': offset
}
r = requests.get('https://api.gsa.gov/technology/searchgov/v2/results/i14y', params=params)

if r.status_code == 200:
try:
r = requests.get('https://api.gsa.gov/technology/searchgov/v2/results/i14y', params=params)
return process_site_results(r.json(), limit=limit, offset=offset)
except (Exception) as ex:
logger.error('search_site:' + ex.__class__.__name__)
return None


def search(request):
Expand All @@ -117,26 +130,40 @@
results = {}
results['count'] = 0

site_search_error = ''
cand_search_error = ''
comm_search_error = ''

if 'candidates' in search_type and search_query:
results['candidates'] = search_candidates(search_query)
if results['candidates']:
results['count'] += len(results['candidates']['results'])
else:
cand_search_error = 'We were unable to search <strong>Candidates</strong> because that FEC API endpoint is currently not responding. Please try again later.'

if 'committees' in search_type and search_query:
results['committees'] = search_committees(search_query)
if results['committees']:
results['count'] += len(results['committees']['results'])
else:
comm_search_error = 'We were unable to search <strong>Committees</strong> because that FEC API endpoint is currently not responding. Please try again later.'

Check warning on line 149 in fec/search/views.py

View check run for this annotation

Codecov / codecov/patch

fec/search/views.py#L149

Added line #L149 was not covered by tests

if 'site' in search_type and search_query:
results['site'] = search_site(search_query, limit=limit, offset=offset)
if results['site']:
results['count'] += len(results['site']['results'])
else:
site_search_error = 'We were unable to search <strong>Other pages</strong> because search.gov is currently not responding. Please try again later.'

return render(request, 'search/search.html', {
'search_query': search_query,
'results': results,
'type': search_type,
'self': {'title': 'Search results'}
'self': {'title': 'Search results'},
'site_search_error': site_search_error,
'cand_search_error': cand_search_error,
'comm_search_error': comm_search_error,

})


Expand All @@ -153,28 +180,36 @@
'offset': offset
}

r = requests.get('https://api.gsa.gov/technology/searchgov/v2/results/i14y', params=params)
if r.status_code == 200:
try:
r = requests.get('https://api.gsa.gov/technology/searchgov/v2/results/i14y', params=params)

Check warning on line 184 in fec/search/views.py

View check run for this annotation

Codecov / codecov/patch

fec/search/views.py#L183-L184

Added lines #L183 - L184 were not covered by tests
return process_site_results(r.json(), limit=limit, offset=offset)
except (Exception) as ex:
logger.error('policy_guidance_search_site' + ex.__class__.__name__)
return None

Check warning on line 188 in fec/search/views.py

View check run for this annotation

Codecov / codecov/patch

fec/search/views.py#L186-L188

Added lines #L186 - L188 were not covered by tests


def policy_guidance_search(request):
"""
Takes a page request and calls the appropriate searches
depending on the type requested
"""

results = {}
limit = 10
search_query = request.GET.get('query', None)
offset = request.GET.get('offset', 0)

results = policy_guidance_search_site(search_query, limit=limit, offset=offset)
current_page = int(int(offset) / limit) + 1
num_pages = 1
total_count = 0
if results:
num_pages = math.ceil(int(results['meta']['count']) / limit)
total_count = results['meta']['count'] + results['best_bets']['count']
policy_search_error = ''

if search_query:
results = policy_guidance_search_site(search_query, limit=limit, offset=offset)

if results:
num_pages = math.ceil(int(results['meta']['count']) / limit)
total_count = results['meta']['count'] + results['best_bets']['count']

Check warning on line 210 in fec/search/views.py

View check run for this annotation

Codecov / codecov/patch

fec/search/views.py#L209-L210

Added lines #L209 - L210 were not covered by tests
else:
policy_search_error = 'We were unable to search <strong>Policy and Other Guidance</strong> because search.gov is currently not responding. Please try again later.'

resultset = {}
resultset['search_query'] = search_query
Expand All @@ -183,6 +218,7 @@
resultset['offset'] = offset
resultset['num_pages'] = num_pages
resultset['current_page'] = current_page
resultset['total_count'] = total_count
resultset['total_count'] = total_count,
resultset['policy_search_error'] = policy_search_error

return render(request, 'search/policy_guidance_search_page.html', resultset)