Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
Much improved display of derived data on /location/x, refs #650
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 17, 2021
1 parent 344153e commit 0359ec1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
37 changes: 22 additions & 15 deletions vaccinate/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,21 +553,9 @@ def derive_availability_and_inventory(
source_location_to_use_for_vaccines_offered = None

if source_location_to_use_for_vaccines_offered:
inventory = source_location_to_use_for_vaccines_offered.import_json[
"inventory"
]
inventory_mapping = {
"moderna": "Moderna",
"pfizer_biontech": "Pfizer",
"johnson_johnson_janssen": "Johnson & Johnson",
"oxford_astrazeneca": "Astrazeneca",
}
in_stock = [
stock["vaccine"]
for stock in inventory
if stock.get("supply_level") != "out_of_stock"
]
vaccines_offered = list(sorted([inventory_mapping[v] for v in in_stock]))
vaccines_offered = (
source_location_to_use_for_vaccines_offered.vaccines_offered
)
vaccines_offered_provenance_source_location = (
source_location_to_use_for_vaccines_offered
)
Expand Down Expand Up @@ -1613,6 +1601,25 @@ def pydantic_convert(cls, id: str) -> SourceLocation:
raise ValueError("SourceLocation '{}' does not exist".format(id))
return obj

@property
def vaccines_offered(self):
try:
inventory = self.import_json["inventory"]
except KeyError:
return None
inventory_mapping = {
"moderna": "Moderna",
"pfizer_biontech": "Pfizer",
"johnson_johnson_janssen": "Johnson & Johnson",
"oxford_astrazeneca": "Astrazeneca",
}
in_stock = [
stock["vaccine"]
for stock in inventory
if stock.get("supply_level") != "out_of_stock"
]
return list(sorted([inventory_mapping[v] for v in in_stock]))

class Meta:
db_table = "source_location"
indexes = [models.Index(fields=["matched_location"])]
Expand Down
7 changes: 5 additions & 2 deletions vaccinate/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,19 @@ def location(request, public_id):
)
vaccines_offered_timeline.sort(key=lambda r: r["when"], reverse=True)

derived = location.derive_availability_and_inventory()

return render(
request,
"location.html",
{
"location": location,
"derived_availability_and_inventory": json.dumps(
location.derive_availability_and_inventory()._asdict(),
"derived_raw": json.dumps(
derived._asdict(),
indent=4,
default=lambda v: v.isoformat() if hasattr(v, "isoformat") else repr(v),
),
"derived": derived,
"availability_timeline": availability_timeline,
"vaccines_offered_timeline": vaccines_offered_timeline,
"source_locations": location.matched_source_locations.order_by(
Expand Down
44 changes: 40 additions & 4 deletions vaccinate/templates/location.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,50 @@ <h1>{{ location }}</h1>
<h2><samp style="font-size: 1.25em">{{ location.public_id }}</samp> - {{ location.full_address }}</h2>
<p><a href="/location">Find another location</a></p>

<section style="border: 2px solid #ccc; padding: 0.5em">
<h2>Derived availability and inventory</h2>

<p>We have logic that derives vaccine inventory and availability based on a combination of recently filed reports and recently scraped source locations. The <code>location.derived_availability_and_inventory()</code> method for this location currently returns:</p>

<pre>{{ derived_availability_and_inventory }}</pre>
<p><strong>Vaccines offered:</strong> {{ derived.vaccines_offered|default:"-" }}</p>
<p>According to
{% if derived.vaccines_offered_provenance_report %}report: {{ derived.vaccines_offered_provenance_report }}
{% else %}
source location: {{ derived.vaccines_offered_provenance_source_location }}
{% endif %}
on {{ derived.vaccines_offered_last_updated_at }}
{% if derived.vaccines_offered_provenance_report and derived.most_recent_source_location_on_vaccines_offered %}
<blockquote style="margin-top: 0">Most recent source location with an opinion was {{ derived.most_recent_source_location_on_vaccines_offered }} on {{ derived.most_recent_source_location_on_vaccines_offered.last_imported_at }} which said {{ derived.most_recent_source_location_on_vaccines_offered.vaccines_offered }}</blockquote>
{% endif %}
{% if derived.vaccines_offered_provenance_source_location and derived.most_recent_report_on_vaccines_offered %}
<blockquote style="margin-top: 0">Most recent report with an opinion was {{ derived.most_recent_report_on_vaccines_offered }} on {{ derived.most_recent_report_on_vaccines_offered.created_at }} which said {{ derived.most_recent_report_on_vaccines_offered.vaccines_offered }}</blockquote>
{% endif %}
</p>

<p>
<strong>Accepts appointments:</strong>{% if derived.accepts_appointments %}<span style="background-color: green; color: white; padding: 0.3em">True</span>{% else %} {{ derived.accepts_appointments }}{% endif %},
<strong>Accepts walk-ins:</strong>{% if derived.accepts_walkins %}<span style="background-color: green; color: white; padding: 0.3em">True</span>{% else %} {{ derived.accepts_walkins }}{% endif %}</p>
<p>According to
{% if derived.appointments_walkins_provenance_report %}report: {{ derived.appointments_walkins_provenance_report }} [{{ derived.appointments_walkins_provenance_report.availability }}]
{% else %}
source location: {{ derived.appointments_walkins_provenance_source_location }} which said {{ derived.appointments_walkins_provenance_source_location.import_json.availability }}
{% endif %}
on {{ derived.appointments_walkins_last_updated_at }}
{% if derived.appointments_walkins_provenance_report and derived.most_recent_source_location_on_availability %}
<blockquote style="margin-top: 0">Most recent source location with an opinion was {{ derived.most_recent_source_location_on_availability }} on {{ derived.most_recent_source_location_on_availability.last_imported_at }} which said {{ derived.most_recent_source_location_on_availability.import_json.availability }}</blockquote>
{% endif %}
{% if derived.vaccines_offered_provenance_source_location and derived.most_recent_report_on_availability %}
<blockquote style="margin-top: 0">Most recent report with an opinion was {{ derived.most_recent_report_on_availability }} on {{ derived.most_recent_report_on_availability.created_at }} which said {{ derived.most_recent_report_on_availability.availability }}</blockquote>
{% endif %}
</p>

<details><summary>Raw derived data</summary>
<pre>{{ derived_raw }}</pre>
</details>


{% if availability_timeline %}
<p>Availability:</p>
<p>Availability timeline:</p>
<ul>
{% for item in availability_timeline %}
<li><strong>{{ item.when }}</strong> ({{ item.type }})<br>
Expand All @@ -29,7 +65,7 @@ <h2>Derived availability and inventory</h2>
{% endif %}

{% if vaccines_offered_timeline %}
<p>Vaccines offered:</p>
<p>Vaccines offered timeline:</p>
<ul>
{% for item in vaccines_offered_timeline %}
<li><strong>{{ item.when }}</strong> ({{ item.type }})<br>
Expand All @@ -42,7 +78,7 @@ <h2>Derived availability and inventory</h2>
{% endfor %}
</ul>
{% endif %}

</section>
{% for title, content in api_previews %}
<h2>{{ title }}</h2>
<pre>{{ content }}</pre>
Expand Down

0 comments on commit 0359ec1

Please sign in to comment.