Skip to content

Commit

Permalink
Merge pull request #812 from jan-stanek/illness-type
Browse files Browse the repository at this point in the history
Nabídky očkování pro jiné onemocnění
  • Loading branch information
msusicky authored Apr 21, 2024
2 parents d1f8620 + a09df55 commit e75c0b3
Show file tree
Hide file tree
Showing 23 changed files with 343 additions and 151 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/update-web-offers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: update-web

on:
workflow_dispatch

concurrency: production_env

jobs:
deploy:
name: Update static pages - offers
if: ${{ github.event.workflow_run.conclusion == 'success' }}
environment: production
runs-on: ubuntu-22.04
env:
APP_DIR: '${HOME}/prd/app'
SSH_HOST: ${{ secrets.SSH_HOST }}
SSH_KEY: ${{ secrets.SSH_KEY }}
SSH_PORT: ${{ secrets.SSH_PORT }}
SSH_USER_WEB: ${{ secrets.SSH_USER_WEB }}
steps:
- name: Setup SSH key
run: |
mkdir -p ~/.ssh/
ssh-keyscan -H ${SSH_HOST} >> ~/.ssh/known_hosts
eval `ssh-agent -s`
echo "${SSH_KEY}" | tr -d '\r' | ssh-add -
echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK" >> $GITHUB_ENV
- name: Check if webserver is running
run: ssh ${SSH_USER_WEB}@${SSH_HOST} -p ${SSH_PORT} "systemctl status ockovani-prd.service"
- name: Create static pages
run: ssh ${SSH_USER_WEB}@${SSH_HOST} -p ${SSH_PORT} "bash ${APP_DIR}/scripts/create_static_pages_offers.sh"
2 changes: 1 addition & 1 deletion .github/workflows/update-web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
- name: Check if webserver is running
run: ssh ${SSH_USER_WEB}@${SSH_HOST} -p ${SSH_PORT} "systemctl status ockovani-prd.service"
- name: Create static pages
run: ssh ${SSH_USER_WEB}@${SSH_HOST} -p ${SSH_PORT} "cd ${APP_DIR} && bash scripts/create_static_pages.sh"
run: ssh ${SSH_USER_WEB}@${SSH_HOST} -p ${SSH_PORT} "bash ${APP_DIR}/scripts/create_static_pages.sh"
14 changes: 6 additions & 8 deletions app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import date, timedelta

import click
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from sqlalchemy import text

from app import app, db
from app.etl import MetricsEtl
Expand Down Expand Up @@ -92,15 +92,13 @@ def clean_db_command():
"""Deletes older partition than 14 days. Executed weekly via cron"""
eng = db.engine

eng.execute(
"DELETE FROM ockovani_registrace WHERE import_id<(SELECT min(id) FROM importy WHERE start>now()-'14 days'::interval)")
with eng.connect() as conn:
conn.execute(text("DELETE FROM ockovani_registrace WHERE import_id<(SELECT min(id) FROM importy WHERE start>now()-'14 days'::interval)"))
app.logger.info("Old data deleted.")

connection = eng.raw_connection()
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = connection.cursor()
cursor.execute("VACUUM(FULL, ANALYZE) ockovani_registrace")
with eng.connect() as conn:
with conn.execution_options(isolation_level='AUTOCOMMIT'):
conn.execute(text("VACUUM(FULL, ANALYZE) ockovani_registrace"))
app.logger.info("Vacuum finished.")
connection.autocommit = False

exit(0)
63 changes: 30 additions & 33 deletions app/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from datetime import datetime

from flask import session, redirect, request, url_for
from flask.json import dump

from app import db, bp
from app.models import ZdravotnickeStredisko, PrakticiLogin, PrakticiKapacity, Vakcina
from app.models import ZdravotnickeStredisko, PrakticiLogin, PrakticiKapacity


@bp.route("/praktici_admin/register", methods=['POST'])
Expand All @@ -33,32 +32,13 @@ def praktici_admin_register():
elif user is not None:
session['error'] = 'Zdravotnické zařízení je už registrováno.'
else:
random.seed(datetime.now())
random.seed(datetime.now().timestamp())
characters = string.ascii_letters + string.digits
passwd = ''.join(random.choice(characters) for i in range(8))

user = PrakticiLogin(id, passwd)
db.session.add(user)

vaccines = db.session.query(Vakcina).all()

for vaccine in vaccines:
free_vaccine = PrakticiKapacity()
free_vaccine.datum_aktualizace = datetime.now()
free_vaccine.zdravotnicke_zarizeni_kod = id
free_vaccine.typ_vakciny = vaccine.vyrobce
free_vaccine.kraj = zarizeni.kraj
free_vaccine.mesto = zarizeni.obec
free_vaccine.nazev_ordinace = zarizeni.nazev_cely
free_vaccine.adresa = f'{zarizeni.ulice if zarizeni.ulice else zarizeni.obec} {zarizeni.cislo_domu}'
free_vaccine.pocet_davek = 0
free_vaccine.kontakt_email = zarizeni.email
free_vaccine.kontakt_tel = zarizeni.telefon
free_vaccine.poznamka = ''
free_vaccine.dospeli = zarizeni.druh_zarizeni_kod != 321
free_vaccine.deti = zarizeni.druh_zarizeni_kod == 321
db.session.add(free_vaccine)

db.session.commit()

session['user_id'] = id
Expand Down Expand Up @@ -122,31 +102,48 @@ def praktici_admin_edit():
if user is None:
session['error'] = 'Neplatné zdravotnické zařízení nebo heslo.'
else:
email = request.form.get('email')
user.email = email if email else None

telefon = request.form.get('telefon')
user.telefon = telefon if telefon else None

db.session.merge(user)

for i in range(len(request.form.getlist('typ_vakciny[]'))):
nemoc = request.form.getlist('nemoc[]')[i]
typ_vakciny = request.form.getlist('typ_vakciny[]')[i]

vaccine = db.session.query(PrakticiKapacity) \
.filter(PrakticiKapacity.zdravotnicke_zarizeni_kod == id) \
.filter(PrakticiKapacity.typ_vakciny == request.form.getlist('typ_vakciny[]')[i]) \
.filter(PrakticiKapacity.nemoc == nemoc) \
.filter(PrakticiKapacity.typ_vakciny == typ_vakciny) \
.one_or_none()

if vaccine is None:
continue
vaccine = PrakticiKapacity()
vaccine.zdravotnicke_zarizeni_kod = id
vaccine.nemoc = nemoc
vaccine.typ_vakciny = typ_vakciny

pocet_davek = request.form.getlist('pocet_davek[]')[i]
expirace = request.form.getlist('expirace[]')[i]
dospeli = [int(v) - 1 for v in request.form.getlist('dospeli[]')]
deti = [int(v) - 1 for v in request.form.getlist('deti[]')]

vaccine.datum_aktualizace = datetime.now()
vaccine.pocet_davek = pocet_davek if pocet_davek.isnumeric() else 0

dospeli = [int(v) - 1 for v in request.form.getlist('dospeli[]')]
vaccine.dospeli = i in dospeli

deti = [int(v) - 1 for v in request.form.getlist('deti[]')]
vaccine.deti = i in deti
vaccine.adresa = request.form.getlist('adresa[]')[i]
vaccine.kontakt_tel = request.form.getlist('kontakt_tel[]')[i]
vaccine.kontakt_email = request.form.getlist('kontakt_email[]')[i]

expirace = request.form.getlist('expirace[]')[i]
vaccine.expirace = expirace if len(expirace) > 0 else None

vaccine.datum_aktualizace = datetime.now()

vaccine.poznamka = request.form.getlist('poznamka[]')[i]

db.session.merge(vaccine)
db.session.commit()

db.session.commit()

return redirect(url_for('view.praktici_admin'))
4 changes: 2 additions & 2 deletions app/fetcher/fetcher_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def _init_fetchers(self, dataset: str) -> None:
if dataset == 'all':
# self._fetchers.append(CentersFetcher()) # replaced by CentersApiFetcher
self._fetchers.append(CentersApiFetcher())
# self._fetchers.append(OpeningHoursFetcher())
# self._fetchers.append(HealthFacilitiesFetcher())
self._fetchers.append(OpeningHoursFetcher())
self._fetchers.append(HealthFacilitiesFetcher())
# self._fetchers.append(DistributedFetcher()) # not updated since 2022-11-29
# self._fetchers.append(UsedFetcher()) # not updated since 2022-02-16
self._fetchers.append(RegistrationsFetcher())
Expand Down
1 change: 1 addition & 0 deletions app/fetcher/health_facilities_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def fetch(self, import_id: int) -> None:

df = df.drop(['id', 'kraj_nuts_kod', 'kraj_nazev', 'okres_nazev'], axis=1)
df = df.rename(columns={'zarizeni_kod': 'id', 'okres_lau_kod': 'okres_id'})
df = df.groupby('id').first().reset_index()

self._truncate()

Expand Down
10 changes: 4 additions & 6 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class Vakcina(db.Model):
vakcina_sklad = Column(Unicode, unique=True, nullable=False)
davky = Column(Integer, nullable=False)
aktivni = Column(Boolean, nullable=False)
nemoc = Column(Unicode, nullable=False)

def __repr__(self):
return f"<Vakcina(vyrobce='{self.vyrobce}')>"
Expand Down Expand Up @@ -1026,6 +1027,8 @@ class PrakticiLogin(db.Model):

zdravotnicke_zarizeni_kod = Column(Unicode, primary_key=True)
heslo = Column(Unicode, nullable=False)
email = Column(Unicode)
telefon = Column(Unicode)

def __init__(self, id, passwd):
self.zdravotnicke_zarizeni_kod = id
Expand All @@ -1041,17 +1044,12 @@ class PrakticiKapacity(db.Model):
zdravotnicke_zarizeni_kod = Column(Unicode, primary_key=True)
datum_aktualizace = Column(DateTime, nullable=False, default="now()")
typ_vakciny = Column(Unicode, primary_key=True)
kraj = Column(Unicode, nullable=False)
mesto = Column(Unicode, nullable=False)
nazev_ordinace = Column(Unicode, nullable=False)
adresa = Column(Unicode)
pocet_davek = Column(Integer, nullable=False)
kontakt_email = Column(Unicode)
kontakt_tel = Column(Unicode)
poznamka = Column(Unicode)
deti = Column(Boolean, nullable=False)
dospeli = Column(Boolean, nullable=False)
expirace = Column(Date)
nemoc = Column(Unicode, nullable=False)

def __repr__(self):
return f"<PrakticiKapacity(zdravotnicke_zarizeni_kod='{self.zdravotnicke_zarizeni_kod}')>"
Expand Down
34 changes: 23 additions & 11 deletions app/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from app import db
from app.context import get_import_date, get_import_id
from app.models import OckovaciMisto, Okres, Kraj, OckovaciMistoMetriky, CrMetriky, OckovaniRegistrace, Populace, \
PrakticiKapacity, OckovaniRezervace, Vakcina, ZdravotnickeStredisko
PrakticiKapacity, OckovaniRezervace, Vakcina, ZdravotnickeStredisko, PrakticiLogin


def unique_nrpzs_subquery():
Expand Down Expand Up @@ -86,15 +86,16 @@ def find_doctor_offices(nrpzs_kod):
df = pd.read_sql_query(
f"""
select coalesce(z.zarizeni_nazev, min(s.nazev_cely)) zarizeni_nazev, o.nazev okres, k.nazev kraj,
k.nazev_kratky kraj_kratky, s.druh_zarizeni, s.obec, s.psc, s.ulice, s.cislo_domu, s.telefon, s.email,
s.web, s.latitude, s.longitude
k.nazev_kratky kraj_kratky, s.druh_zarizeni, s.obec, s.psc, s.ulice, s.cislo_domu,
coalesce(p.telefon, s.telefon) telefon_c, coalesce(p.email, s.email) email_c, s.web, s.latitude, s.longitude
from ockovaci_zarizeni z
full join zdravotnicke_stredisko s on s.nrpzs_kod = z.id
left join praktici_login p on substring(p.zdravotnicke_zarizeni_kod from 1 for 11) = s.nrpzs_kod
left join okresy o on o.id = coalesce(z.okres_id, s.okres_kod)
join kraje k on k.id = o.kraj_id
where z.id = '{nrpzs_kod}' or s.nrpzs_kod = '{nrpzs_kod}'
group by z.zarizeni_nazev, o.nazev, k.nazev, k.nazev_kratky, s.druh_zarizeni, s.obec, s.psc, s.ulice,
s.cislo_domu, s.telefon, s.email, s.web, s.latitude, s.longitude
s.cislo_domu, telefon_c, email_c, s.web, s.latitude, s.longitude
""",
db.engine)

Expand Down Expand Up @@ -186,20 +187,22 @@ def find_doctors_vaccine_options():


def find_free_vaccines_available(nrpzs_kod=None, okres_id=None, kraj_id=None):
return db.session.query(PrakticiKapacity.datum_aktualizace, PrakticiKapacity.pocet_davek,
PrakticiKapacity.typ_vakciny, PrakticiKapacity.mesto, PrakticiKapacity.nazev_ordinace,
PrakticiKapacity.deti, PrakticiKapacity.dospeli, PrakticiKapacity.kontakt_tel,
PrakticiKapacity.kontakt_email, PrakticiKapacity.expirace, PrakticiKapacity.poznamka,
PrakticiKapacity.kraj, ZdravotnickeStredisko.nrpzs_kod, ZdravotnickeStredisko.latitude,
ZdravotnickeStredisko.longitude) \
return db.session.query(PrakticiKapacity.datum_aktualizace, PrakticiKapacity.pocet_davek, PrakticiKapacity.nemoc,
PrakticiKapacity.typ_vakciny, PrakticiKapacity.deti, PrakticiKapacity.dospeli,
PrakticiKapacity.expirace, PrakticiKapacity.poznamka, PrakticiLogin.email,
PrakticiLogin.telefon, ZdravotnickeStredisko.nazev_cely, ZdravotnickeStredisko.obec,
ZdravotnickeStredisko.kraj, ZdravotnickeStredisko.nrpzs_kod, ZdravotnickeStredisko.latitude,
ZdravotnickeStredisko.longitude, ZdravotnickeStredisko.email.label('email_nrpzs'),
ZdravotnickeStredisko.telefon.label('telefon_nrpzs')) \
.join(PrakticiLogin, PrakticiLogin.zdravotnicke_zarizeni_kod == PrakticiKapacity.zdravotnicke_zarizeni_kod) \
.outerjoin(ZdravotnickeStredisko,
ZdravotnickeStredisko.zdravotnicke_zarizeni_kod == PrakticiKapacity.zdravotnicke_zarizeni_kod) \
.filter(or_(func.left(PrakticiKapacity.zdravotnicke_zarizeni_kod, 11) == nrpzs_kod, nrpzs_kod is None)) \
.filter(or_(ZdravotnickeStredisko.okres_kod == okres_id, okres_id is None)) \
.filter(or_(ZdravotnickeStredisko.kraj_kod == kraj_id, kraj_id is None)) \
.filter(PrakticiKapacity.pocet_davek > 0) \
.filter(or_(PrakticiKapacity.expirace == None, PrakticiKapacity.expirace >= get_import_date())) \
.order_by(PrakticiKapacity.kraj, PrakticiKapacity.mesto, PrakticiKapacity.nazev_ordinace,
.order_by(ZdravotnickeStredisko.kraj, ZdravotnickeStredisko.obec, ZdravotnickeStredisko.nazev_cely,
PrakticiKapacity.typ_vakciny) \
.all()

Expand All @@ -213,6 +216,15 @@ def find_free_vaccines_vaccine_options():
.all()


def find_free_vaccines_illness_options():
return db.session.query(PrakticiKapacity.nemoc) \
.filter(PrakticiKapacity.pocet_davek > 0) \
.filter(or_(PrakticiKapacity.expirace == None, PrakticiKapacity.expirace >= get_import_date())) \
.distinct(PrakticiKapacity.nemoc) \
.order_by(PrakticiKapacity.nemoc) \
.all()


def count_vaccines_center(center_id):
mista = pd.read_sql_query(
"""
Expand Down
10 changes: 7 additions & 3 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@
<div class="container">
<div class="row">
<div class="col">
<div class="alert alert-danger" role="alert">
Čas poslední aktualizace: <strong>{{ last_update }}</strong>.
POZOR! - Momentálně obnovený pilotní provoz, na webu mohou být chyby.
<div class="alert alert-info alert-dismissible fade show" role="alert">
<p class="mb-0">
Čas poslední aktualizace: <strong>{{ last_update }}</strong>.
<a class="alert-link" href="{{ url_for('view.napoveda') }}#aktualizace">
Jak často jsou data aktualizována?
</a>
</p>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
Expand Down
2 changes: 1 addition & 1 deletion app/templates/kraj.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h3>Kraj: {{ kraj.nazev_kratky }} </h3>
{{ prehled_praktici(doctors, doctors_vaccine_options, kraj_mode=True) }}
</div>
<div class="tab-pane" id="nabidky-ockovani">
{{ volne_vakciny(free_vaccines, free_vaccines_vaccine_options, filtered_mode=True) }}
{{ volne_vakciny(free_vaccines, free_vaccines_illness_options, filtered_mode=True) }}
</div>
<div class="tab-pane" id="statistiky">
<div class="row px-2 mb-n2">
Expand Down
Loading

0 comments on commit e75c0b3

Please sign in to comment.