mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 10:41:32 +00:00
cleanups
This commit is contained in:
@@ -1,207 +0,0 @@
|
|||||||
<?php
|
|
||||||
$dsn = "mysql:host=" . getenv('DB_HOST') . ";dbname=" . getenv('DB_DATABASE');
|
|
||||||
$username = getenv('DB_USERNAME');
|
|
||||||
$password = getenv('DB_PASSWORD');
|
|
||||||
|
|
||||||
$pdo = new PDO($dsn, $username, $password);
|
|
||||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
||||||
|
|
||||||
checkFailedData();
|
|
||||||
|
|
||||||
$previous = $pdo->query("SELECT * FROM signalytic_data")->fetch(PDO::FETCH_ASSOC)['last_fetch_date'];
|
|
||||||
|
|
||||||
$hospitalInformation = $pdo->query("SELECT * FROM hospital_information")->fetch(PDO::FETCH_ASSOC);
|
|
||||||
$episodes = $pdo->query("SELECT triage_id, consultation_id, gender, clinic_id, name, slug FROM patient_episodes
|
|
||||||
JOIN patients ON patient_episodes.patient_id = patients.id
|
|
||||||
LEFT JOIN clinics ON patient_episodes.clinic_id = clinics.id
|
|
||||||
WHERE patient_episodes.created_at > '{$previous}'");
|
|
||||||
|
|
||||||
$episodes_created_male = 0;
|
|
||||||
$episodes_created_female = 0;
|
|
||||||
$episodes_completed_with_outcome = [];
|
|
||||||
$episodes_per_clinic = [];
|
|
||||||
$episodes_triaged = 0;
|
|
||||||
|
|
||||||
while ($row = $episodes->fetch(PDO::FETCH_ASSOC)) {
|
|
||||||
if ($row['gender'] == 1) {
|
|
||||||
$episodes_created_male++;
|
|
||||||
} else if ($row['gender'] == 2) {
|
|
||||||
$episodes_created_female++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_null($row['triage_id'])) {
|
|
||||||
$episodes_triaged++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_null($row['clinic_id'])) {
|
|
||||||
$episodes_per_clinic[$row['clinic_id']] = [
|
|
||||||
'clinic_name' => $row['name'],
|
|
||||||
'clinic_type' => $row['slug'],
|
|
||||||
'clinic_count' => ($episodes_per_clinic[$row['clinic_id']]['clinic_count'] ?? 0) + 1,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!is_null($row['consultation_id'])) {
|
|
||||||
$cons = $pdo->query("SELECT consultations.outcome_id, outcomes.name FROM consultations
|
|
||||||
JOIN outcomes ON consultations.outcome_id = outcomes.id
|
|
||||||
WHERE consultations.id = '{$row['consultation_id']}'")->fetch(PDO::FETCH_ASSOC);
|
|
||||||
$episodes_completed_with_outcome[$cons['outcome_id']] = [
|
|
||||||
'outcome_name' => $cons['name'],
|
|
||||||
'outcome_count' => ($episodes_completed_with_outcome[$cons['outcome_id']]['outcome_count'] ?? 0) + 1,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$users_seen = $pdo->query("SELECT * FROM users WHERE last_seen > '{$previous}'")->rowCount();
|
|
||||||
$male_patients = $pdo->query("SELECT * FROM patients WHERE created_at > '{$previous}' AND gender = '1'")->rowCount();
|
|
||||||
$female_patients = $pdo->query("SELECT * FROM patients WHERE created_at > '{$previous}' AND gender = '2'")->rowCount();
|
|
||||||
$receipts = $pdo->query("SELECT * FROM track_receipts WHERE created_at > '{$previous}'")->rowCount();
|
|
||||||
$pharmacyRec = $pdo->query("SELECT * FROM pharmacy_stock_reconciliations WHERE created_at > '{$previous}' AND completion_status = 1")->rowCount();
|
|
||||||
$storeRec = $pdo->query("SELECT * FROM store_stock_reconciliations WHERE created_at > '{$previous}' AND completion_status = 1")->rowCount();
|
|
||||||
$quotations = $pdo->query("SELECT * FROM quotations WHERE created_at > '{$previous}' AND receive_date IS NOT NULL")->rowCount();
|
|
||||||
|
|
||||||
$inpatients = $pdo->query("SELECT inpatient_info.discharged, inpatient_info.died_on, patients.gender FROM inpatient_info JOIN patients ON inpatient_info.patient_id = patients.id WHERE inpatient_info.created_at > '{$previous}'");
|
|
||||||
|
|
||||||
$patients_admitted_male = 0;
|
|
||||||
$patients_admitted_female = 0;
|
|
||||||
$patients_discharged_male = 0;
|
|
||||||
$patients_discharged_female = 0;
|
|
||||||
|
|
||||||
while ($row = $inpatients->fetch(PDO::FETCH_ASSOC)) {
|
|
||||||
if ($row['gender'] == 1) {
|
|
||||||
$patients_admitted_male++;
|
|
||||||
} else if ($row['gender'] == 2) {
|
|
||||||
$patients_admitted_female++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($row['discharged'] === 1 || !is_null($row['died_on'])) {
|
|
||||||
if ($row['gender'] == 1) {
|
|
||||||
$patients_discharged_male++;
|
|
||||||
} else if ($row['gender'] == 2) {
|
|
||||||
$patients_discharged_female++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$investigations = $pdo->query("SELECT * FROM ordered_investigations WHERE created_at > '{$previous}'");
|
|
||||||
|
|
||||||
$investigation_requests = 0;
|
|
||||||
$investigation_results = 0;
|
|
||||||
|
|
||||||
while ($row = $investigations->fetch(PDO::FETCH_ASSOC)) {
|
|
||||||
$investigation_requests++;
|
|
||||||
|
|
||||||
// top_investigations
|
|
||||||
|
|
||||||
if ($row['investigation_status'] === 1) {
|
|
||||||
$investigation_results++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$treatments = $pdo->query("SELECT * FROM treatments WHERE created_at > '{$previous}'");
|
|
||||||
|
|
||||||
$prescriptions = 0;
|
|
||||||
$prescriptions_dispensed = 0;
|
|
||||||
|
|
||||||
while ($row = $treatments->fetch(PDO::FETCH_ASSOC)) {
|
|
||||||
$prescriptions++;
|
|
||||||
|
|
||||||
if ($row['dispense_status'] === 1) {
|
|
||||||
$prescriptions_dispensed++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$top_diagnosis = $pdo->query("SELECT primary_diagnosis, COUNT(*) AS freq, name FROM consultations JOIN diagnoses ON consultations.primary_diagnosis = diagnoses.id WHERE consultations.created_at > '{$previous}' GROUP BY primary_diagnosis ORDER BY freq DESC LIMIT 5")->fetchAll();
|
|
||||||
|
|
||||||
$data = json_encode([
|
|
||||||
'facility_name' => $hospitalInformation['name'],
|
|
||||||
'facility_email' => $hospitalInformation['email'],
|
|
||||||
'facility_address' => $hospitalInformation['address'],
|
|
||||||
'facility_unique_identifier' => $hospitalInformation['unique_hospital_identifier'] ?? '',
|
|
||||||
'time_period' => "{$previous} to " . date("Y-m-d H:i:s"),
|
|
||||||
'episodes_created_male' => $episodes_created_male,
|
|
||||||
'episodes_created_female' => $episodes_created_female,
|
|
||||||
'episodes_completed_with_outcome' => $episodes_completed_with_outcome,
|
|
||||||
'episodes_triaged' => $episodes_triaged,
|
|
||||||
'patients_admitted_male' => $patients_admitted_male,
|
|
||||||
'patients_admitted_female' => $patients_admitted_female,
|
|
||||||
'patients_discharged_male' => $patients_discharged_male,
|
|
||||||
'patients_discharged_female' => $patients_discharged_female,
|
|
||||||
'number_of_prescriptions' => $prescriptions,
|
|
||||||
'number_of_prescriptions_dispensed' => $prescriptions_dispensed,
|
|
||||||
'payment_receipts_generated' => $receipts,
|
|
||||||
'active_users' => $users_seen,
|
|
||||||
'male_patients_registered' => $male_patients,
|
|
||||||
'female_patients_registered' => $female_patients,
|
|
||||||
'investigation_requests' => $investigation_requests,
|
|
||||||
'investigation_requests_with_results' => $investigation_results,
|
|
||||||
'times_inventory_stock_received' => $quotations,
|
|
||||||
'times_stock_reconciled_pharmacy' => $pharmacyRec,
|
|
||||||
'times_stock_reconciled_store' => $storeRec,
|
|
||||||
'top_diagnosis' => $top_diagnosis,
|
|
||||||
'top_investigations' => [],
|
|
||||||
'top_prescribed_drugs_opd' => [],
|
|
||||||
'top_prescribed_drugs_ipd' => [],
|
|
||||||
'top_dispensed_drugs_opd' => [],
|
|
||||||
'top_dispensed_drugs_ipd' => [],
|
|
||||||
'episodes_per_clinic' => $episodes_per_clinic,
|
|
||||||
]);
|
|
||||||
|
|
||||||
sendData(false, $data);
|
|
||||||
|
|
||||||
function checkFailedData()
|
|
||||||
{
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->connect(getenv('REDIS_HOST'), getenv('REDIS_PORT'));
|
|
||||||
|
|
||||||
$failedRequests = $redis->lrange("failed_requests", 0, -1);
|
|
||||||
|
|
||||||
foreach ($failedRequests as $request) {
|
|
||||||
$data = json_decode($request, true);
|
|
||||||
|
|
||||||
$response = sendData(true, $data['data']);
|
|
||||||
|
|
||||||
if ($response) {
|
|
||||||
// success, remove from redis list
|
|
||||||
$redis->lrem("failed_requests", $request, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendData($is_retry, $data)
|
|
||||||
{
|
|
||||||
global $pdo;
|
|
||||||
$server_endpoint = getenv("SERVER_URL");
|
|
||||||
|
|
||||||
$ch = curl_init($server_endpoint);
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
|
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
||||||
$error = curl_error($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
|
|
||||||
if ($httpCode >= 200 && $httpCode < 300) {
|
|
||||||
// success
|
|
||||||
$stmt = $pdo->prepare("UPDATE signalytic_data SET last_fetch_date = ? WHERE id = ?");
|
|
||||||
$stmt->execute([date("Y-m-d H:i:s"), 1]);
|
|
||||||
return true;
|
|
||||||
} else if (!$is_retry) {
|
|
||||||
// request failed
|
|
||||||
$failedRequest = json_encode([
|
|
||||||
'timestamp' => date('Y-m-d H:i:s'),
|
|
||||||
'data' => $data,
|
|
||||||
'error' => $error ?: "HTTP Code $httpCode",
|
|
||||||
]);
|
|
||||||
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->connect(getenv('REDIS_HOST'), getenv('REDIS_PORT'));
|
|
||||||
$redis->lpush("failed_requests", $failedRequest);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user