]> Softwares of Agnibho - simpleipd.git/blob - www/res/calc.js
58eaa6eec9bf55534c7863f1374f898d58e7fe0b
[simpleipd.git] / www / res / calc.js
1 function entrySort(i, j){
2 if(i[0]==j[0]){
3 return 0;
4 }
5 else if(i[0]>j[0]){
6 return 1;
7 }
8 else{
9 return -1;
10 }
11 }
12 var io=[];
13 var clinical={pr:[], rr:[], temperature:[], spo2:[], sbp:[], dbp:[]};
14 var reports={};
15 var treatment={};
16 var clinDict={pr: "Pulse Rate", rr: "Respiratory Rate", temperature: "Temperature", spo2: "SpO2", sbp: "Systolic BP", dbp: "Diastolic BP"}
17 var reportsDict={};
18 $(document).ready(function(){
19 var ctx1=$("#clinChart")[0].getContext("2d");
20 var ctx2=$("#reportsChart")[0].getContext("2d");
21 var ctx3=$("#drugsChart")[0].getContext("2d");
22 $.getJSON("chart.php?pid="+pid+"&get=nursing", function(data){
23 flag="";
24 $.each(data, function(num, entry){
25 // INTAKE-OUTPUT
26 stamp=new Date(entry.date+" "+entry.time);
27 if(entry.io_from!="" && entry.io_to!=""){
28 start=new Date(entry.date+" "+entry.io_from);
29 end=new Date(entry.date+" "+entry.io_to);
30 if(!isNaN(start.getTime()) && !isNaN(end.getTime())){
31 if(end.getTime()-start.getTime()>24*60*60*1000){
32 flag=entry.date;
33 }
34 if(start==end){
35 flag=entry.date;
36 }
37 if(start>end){
38 start.setDate(start.getDate()-1);
39 }
40 io.push({start: start, end: end, in: entry.intake, out: entry.output});
41 }
42 }
43 // CLINICAL
44 if(entry.pr){
45 clinical.pr.push([stamp, entry.pr]);
46 }
47 if(entry.rr){
48 clinical.rr.push([stamp, entry.rr]);
49 }
50 if(entry.temperature){
51 clinical.temperature.push([stamp, entry.temperature]);
52 }
53 if(entry.spo2){
54 clinical.spo2.push([stamp, entry.spo2]);
55 }
56 if(entry.bp){
57 clinical.sbp.push([stamp, entry.bp.split("/")[0]]);
58 }
59 if(entry.bp){
60 clinical.dbp.push([stamp, entry.bp.split("/")[1]]);
61 }
62 });
63 // INTAKE-OUTPUT
64 approxIn=0;
65 approxOut=0;
66 ioGap=0;
67 io.forEach(function(i){
68 if(ioGap<(24*60*60*1000)){
69 ioBuff=ioGap+(i.end.getTime()-i.start.getTime());
70 if(ioBuff>(24*60*60*1000)){
71 frac=((24*60*60*1000)-ioGap)/(ioBuff-ioGap);
72 ioGap=(24*60*60*1000);
73 }
74 else{
75 frac=1;
76 ioGap=ioBuff;
77 }
78 approxIn=approxIn+(Number(i.in)*frac);
79 approxOut=approxOut+(Number(i.out)*frac);
80 if(flag==""){
81 $(".ioGap").each(function(){
82 $(this).text(Math.round(ioGap/3600000));
83 });
84 $("#approxIn").text(Math.round(approxIn));
85 $("#approxOut").text(Math.round(approxOut));
86 $("#ioData").removeClass("d-none");
87 }
88 else{
89 $("#ioInconsistent").text(flag);
90 $("#ioAlert").removeClass("d-none");
91 }
92 }
93 });
94 // CLINICAL
95 $.getJSON("chart.php?pid="+pid+"&get=physician", function(data){
96 $.each(data, function(num, entry){
97 stamp=new Date(entry.date+" "+entry.time);
98 if(entry.pr){
99 clinical.pr.push([stamp, entry.pr]);
100 }
101 if(entry.rr){
102 clinical.rr.push([stamp, entry.rr]);
103 }
104 if(entry.temperature){
105 clinical.temperature.push([stamp, entry.temperature]);
106 }
107 if(entry.spo2){
108 clinical.spo2.push([stamp, entry.spo2]);
109 }
110 if(entry.bp){
111 clinical.sbp.push([stamp, entry.bp.split("/")[0]]);
112 }
113 if(entry.bp){
114 clinical.dbp.push([stamp, entry.bp.split("/")[1]]);
115 }
116 });
117 Object.keys(clinical).forEach(function(i){
118 clinical[i].sort(entrySort);
119 $("#clinVar").html($("#clinVar").html()+"<option value="+i+">"+clinDict[i]+"</option>");
120 });
121 });
122 // REPORTS
123 $.getJSON("chart.php?pid="+pid+"&get=reports", function(data){
124 ajaxArray=[];
125 $.each(data, function(num, entry){
126 if(entry.form){
127 ajax=$.getJSON("forms/"+entry.form+".schema.json", function(data){
128 stamp=new Date(entry.date+" "+entry.time);
129 Object.keys(entry).forEach(function(i){
130 name=entry.form+"-"+i;
131 if(entry[i] && !isNaN(entry[i])){
132 if(!Array.isArray(reports[name])){
133 reportsDict[name]=data.properties[i].description;
134 reports[name]=[];
135 }
136 reports[name].push([stamp, entry[i]]);
137 }
138 });
139 });
140 ajaxArray.push(ajax);
141 }
142 });
143 $.when.apply($,ajaxArray).then(function(){
144 reports=Object.keys(reports).sort().reduce(function(obj, key){
145 obj[key]=reports[key];
146 return obj;
147 },{});
148 Object.keys(reports).forEach(function(i){
149 reports[i].sort(entrySort);
150 $("#reportsVar").html($("#reportsVar").html()+"<option value="+i+">"+reportsDict[i]+"</option>");
151 });
152 });
153 });
154 // TREATMENT
155 $.getJSON("chart.php?pid="+pid+"&get=treatment", function(data){
156 $.each(data, function(num, entry){
157 treat=[];
158 treat[0]=entry.drug+" "+entry.dose+" "+entry.route+" "+entry.frequency;
159 treat[1]=$.parseJSON(entry.administer);
160 treatment[entry.drug]=treat;
161 });
162 Object.keys(treatment).forEach(function(i){
163 $("#drugVar").html($("#drugVar").html()+"<option>"+i+"</option>");
164 });
165 });
166 });
167 // EVENTS
168 $("#clinVar").change(function(){
169 $("#clinData").html("");
170 param=$("#clinVar").val();
171 data={labels: [], datasets: [{ label:clinDict[param], data: [], fill: false, borderColor: "rgb(75, 192, 192)", tension: 0.1 }]};
172 clinical[param].forEach(function(i){
173 data.labels.push(i[0].toLocaleString());
174 data.datasets[0].data.push(i[1]);
175 $("#clinData").html($("#clinData").html()+"<tr><td>"+i[0].toLocaleString()+"</td><td>"+i[1]+"</td></tr>");
176 });
177 try{
178 clinChart.destroy();
179 }catch(e){};
180 if(data.datasets[0].data.length>1){
181 clinChart=new Chart(ctx1, {
182 type: "line",
183 data: data
184 });
185 }
186 });
187 $("#reportsVar").change(function(){
188 $("#reportsData").html("");
189 param=$("#reportsVar").val();
190 data={labels: [], datasets: [{ label: reportsDict[param], data: [], fill: false, borderColor: "rgb(75, 192, 192)", tension: 0.1 }]};
191 reports[param].forEach(function(i){
192 data.labels.push(i[0].toLocaleString());
193 data.datasets[0].data.push(i[1]);
194 $("#reportsData").html($("#reportsData").html()+"<tr><td>"+i[0].toLocaleString()+"</td><td>"+i[1]+"</td></tr>");
195 try{
196 reportsChart.destroy();
197 }catch(e){};
198 if(data.datasets[0].data.length>1){
199 reportsChart=new Chart(ctx2, {
200 type: "line",
201 data: data
202 });
203 }
204 });
205 });
206 $("#drugVar").change(function(){
207 param=$("#drugVar").val();
208 data={labels: [], datasets: [{ label: param, data: [], backgroundColor: "rgb(75, 192, 192)"}]};
209 $("#drugData1").html(treatment[param][0]);
210 $("#drugData2").html("");
211 treatment[param][1].forEach(function(i){
212 // HARDCODED FIX FOR CORRUPTED DATA DUE TO PREVIOUS BUG
213 if(i<1622230200){
214 return;
215 }
216 // PLAN TO REMOVE IN FUTURE
217 d=new Date(i*1000);
218 data.datasets[0].data.push({x:d.getFullYear()+("0"+d.getMonth()).slice(-2)+("0"+d.getDate()).slice(-2), y:(24-d.getHours())});
219 $("#drugData2").html($("#drugData2").html()+" <span class='badge badge-success'>"+d.toLocaleString()+"</span>");
220 });
221 try{
222 drugsChart.destroy();
223 }catch(e){};
224 if(data.datasets[0].data.length>0){
225 drugsChart=new Chart(ctx3, {
226 type: "scatter",
227 data: data,
228 options: {
229 pointRadius: 10,
230 pointHoverRadius: 10,
231 scales: {
232 x: {
233 type: "linear",
234 position: "top",
235 ticks: {
236 callback: function(val, index){
237 if(Number.isInteger(val)){
238 val=val.toString();
239 return new Date(val.slice(0,4)+"-"+val.slice(4,6)+"-"+val.slice(6,8)).toLocaleDateString();
240 }
241 }
242 }
243 },
244 y: {
245 suggestedMin: 0,
246 suggestedMax: 24,
247 ticks: {
248 maxTicksLimit: 24,
249 callback: function(val, index){
250 return (24-val)+":00";
251 }
252 }
253 }
254 }
255 }
256 });
257 }
258 });
259 });