Show restricted benefit criteria in popover

This commit is contained in:
RunasSudo 2023-01-23 20:31:20 +11:00
parent 200922ec13
commit 562ed39600
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 107 additions and 3 deletions

View File

@ -24,6 +24,7 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.2/font/bootstrap-icons.css" integrity="sha256-4RctOgogjPAdwGbwq+rxfwAmSpZhWaafcZR9btzUk18=" crossorigin="anonymous">
<style type="text/css">
ul { margin-top: 0.5em; margin-bottom: 0 }
.popover { max-width: 400px }
</style>
</head>
<body>
@ -111,14 +112,28 @@
let ulRestrictions = document.createElement('ul');
for (let restriction of restrictions) {
let li = document.createElement('li');
ulRestrictions.appendChild(li);
li.innerHTML = restriction['indication'];
if (item['benefit_type'] === 'streamlined') {
li.innerHTML += ' (' + restriction['treatment_of'] + ')';
}
if (restriction['num_criteria'] > 0) {
li.innerHTML += ' <span class="text-muted">(' + restriction['num_criteria'] + ' criteria)</span>';
li.innerHTML += ' ';
let span = document.createElement('span');
span.classList.add('text-muted');
span.innerText = '(' + restriction['num_criteria'] + ' criteria)';
li.appendChild(span);
// Create popover for criteria
new bootstrap.Popover(span, {
trigger: 'hover focus',
title: restriction['indication'],
content: restriction['criteria_rendered'],
html: true
});
}
ulRestrictions.appendChild(li);
}
td.appendChild(ulRestrictions);
}
@ -212,6 +227,14 @@
return results;
}
function groupBy(array, grouper) {
// Array.prototype.group not yet supported in most (any) browsers
return array.reduce(function(result, item) {
(result[grouper(item)] = result[grouper(item)] || []).push(item);
return result;
}, {});
};
main();
</script>
</body>

View File

@ -29,7 +29,7 @@ cur.execute('DROP TABLE IF EXISTS pbs_item_restriction')
cur.execute('CREATE TABLE pbs_item_restriction (item_code TEXT, restriction_code INTEGER)')
cur.execute('DROP TABLE IF EXISTS pbs_restriction')
cur.execute('CREATE TABLE pbs_restriction (code INTEGER PRIMARY KEY, treatment_of INTEGER, indication TEXT, criteria_operator TEXT)')
cur.execute('CREATE TABLE pbs_restriction (code INTEGER PRIMARY KEY, treatment_of INTEGER, indication TEXT, criteria_operator TEXT, criteria_rendered TEXT)')
cur.execute('DROP TABLE IF EXISTS pbs_restriction_criteria')
cur.execute('CREATE TABLE pbs_restriction_criteria (restriction_code INTEGER, criteria_code INTEGER)')

81
render_pbs_criteria.py Normal file
View File

@ -0,0 +1,81 @@
# Copyright © 2023 Lee Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import itertools
import sqlite3
# Open database
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
# Populate pbs_restriction.criteria_rendered
# sql.js is too slow to do the database JOINs at runtime
class Operator:
def __init__(self, operator, nodes):
self.operator = operator
self.nodes = nodes
def flatten(self):
# If contains just 1 element, return it as itself, not as an Operator
if len(self.nodes) == 1:
return self.nodes[0]
# Otherwise flatten recursively
flattened = [n.flatten() if isinstance(n, Operator) else n for n in self.nodes]
return Operator(self.operator, flattened)
@staticmethod
def render(op):
if isinstance(op, str):
return '<p>' + op + '</p>'
elif isinstance(op, Operator):
if len(op.nodes) == 1:
# Single simple string
return '<p>' + op.nodes[0] + '</p>'
if not any(isinstance(n, Operator) for n in op.nodes):
# All simple strings
return '<ul><li><p>' + ', <i>{}</i></p></li><li><p>'.format({'all': 'AND', 'any': 'OR', 'one-of': 'OR'}[op.operator]).join(n.rstrip('.') for n in op.nodes) + '</p></li></ul>'
else:
# Mix of Operator +/- simple strings
return '<p><i>{}</i></p>'.format({'all': 'AND', 'any': 'OR', 'one-of': 'OR'}[op.operator]).join(Operator.render(n) for n in op.nodes)
else:
# Must be a list of a single item
return Operator.render(op[0])
cur.execute('SELECT * FROM pbs_restriction INNER JOIN pbs_restriction_criteria ON pbs_restriction.code = pbs_restriction_criteria.restriction_code INNER JOIN pbs_criteria ON pbs_restriction_criteria.criteria_code = pbs_criteria.code INNER JOIN pbs_criteria_parameter ON pbs_criteria.code = pbs_criteria_parameter.criteria_code')
flat_parameters = cur.fetchall()
parameters_by_restriction = itertools.groupby(flat_parameters, lambda p: p['restriction_code'])
for restriction_code, restriction_parameters in parameters_by_restriction:
restriction_parameters = list(restriction_parameters)
# Build tree of nodes representing parameters
restriction_operator = Operator(restriction_parameters[0]['criteria_operator'], [])
parameters_by_criteria = itertools.groupby(restriction_parameters, lambda p: p['criteria_code'])
for criteria_code, criteria_parameters in parameters_by_criteria:
criteria_parameters = list(criteria_parameters)
criteria_operator = Operator(criteria_parameters[0]['parameters_operator'], [p['text'] for p in criteria_parameters])
restriction_operator.nodes.append(criteria_operator)
# Flatten and render the tree
restriction_operator = restriction_operator.flatten()
rendered = Operator.render(restriction_operator)
cur.execute('UPDATE pbs_restriction SET criteria_rendered = ? WHERE code = ?', (rendered, restriction_code))
con.commit()