Advertisement
BaltimoreHacker

Infection History / Prediction

Mar 30th, 2020
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.64 KB | None | 0 0
  1. // https://www.mdwestserve.com/coronavirus/graphs.php
  2. global $today;
  3. global $normal;
  4. $today = array();
  5. $normal = array();
  6. $q = "SELECT distinct name_of_location FROM coronavirus_populations ";
  7. $r = $core->query($q);
  8. while($d = mysqli_fetch_array($r)){
  9.     $today[$d[name_of_location]] = 0; // count
  10.     $normal[$d[name_of_location]] = ''; // string
  11. }
  12.  
  13. global $days_to_predict;
  14. $days_to_predict = '7';
  15. if(isset($_POST['days'])){
  16.     $days_to_predict = $_POST['days']; 
  17. }
  18.  
  19. global $buffer;
  20. $buffer = '.85';
  21. if(isset($_POST['buffer'])){
  22.     $buffer = $_POST['buffer'];
  23. }
  24. global $debug_in;
  25. global $debug_out;
  26.  
  27. function total_count($county){
  28.     global $core;
  29.     $q = "SELECT number_of_people FROM coronavirus_populations where name_of_location = '$county' ";
  30.     $r = $core->query($q);
  31.     $d = mysqli_fetch_array($r);
  32.     return $d['number_of_people']; 
  33. }
  34. function rate_of_infection($county){
  35.     global $core;
  36.     $q = "SELECT rate_of_infection FROM coronavirus_populations where name_of_location = '$county' ";
  37.     $r = $core->query($q);
  38.     $d = mysqli_fetch_array($r);
  39.     return $d['rate_of_infection'];
  40. }
  41. function show_on_graph($county){
  42.     // if has cases = true else false
  43.     global $core;
  44.     $q = "SELECT * FROM `coronavirus` order by id desc limit 1";
  45.     $r = $core->query($q);
  46.     $d = mysqli_fetch_array($r);
  47.     $key = $county.'COVID19Cases';
  48.     $count = $d[$key];
  49.     if ($count == 0){
  50.         return 'false';
  51.     }else{
  52.         return 'true'; 
  53.     }
  54.    
  55. }
  56. function make_county($county){
  57.         global $core;
  58.         $return = '';
  59.         $t = '0'; // days
  60.         $dt= '1'; // change in days
  61.         // history
  62.         $q = "SELECT distinct just_date FROM `coronavirus` order by just_date asc";
  63.     $r = $core->query($q);
  64.     $count=0;
  65.     global $today;
  66.     while($d = mysqli_fetch_array($r)){
  67.         $date = $d['just_date'];
  68.         $q2 = "SELECT * FROM `coronavirus` where just_date = '$date' order by id desc";
  69.         $r2 = $core->query($q2);
  70.         $d2 = mysqli_fetch_array($r2);
  71.         $key = $county.'COVID19Cases';
  72.         $last_count = $count;
  73.         $count = $d2[$key];
  74.         $return .= '{ label: "'.$date.'", y: '.$count.' }, ';
  75.         $today[$county] = $count;
  76.     }
  77.         // predictive
  78.         $next = date('Y-m-d',strtotime($date)+86400);
  79.         $return .= make_county_prediction($county,$next,$count,$dt);
  80.         $return = rtrim(trim($return), ",");
  81.     return $return;
  82. }
  83. function make_county_prediction($county,$start,$count,$dt){
  84.     global $debug_in;
  85.     global $debug_out;
  86.     global $core;
  87.     $return = '';
  88.     $start = new DateTime ($start, new DateTimeZone ('UTC'));
  89.     global $days_to_predict;
  90.     $end = new DateTime (" +$days_to_predict days", new DateTimeZone ('UTC'));
  91.     $interval = new DateInterval ('P1D');
  92.     $range = new DatePeriod ($start, $interval, $end);
  93.     $N = $count; // total cases
  94.     $Nmax = total_count($county); // population
  95.     $a = rate_of_infection($county); // percentage infection rate  
  96.     $day_buffer = 0;
  97.     foreach ($range as $date) {
  98.     $out = $date->format ('Y-m-d');
  99.         $debug_in .= "<li>$out N:$N</li>";
  100.         $Nold=$N;
  101.         $N=$N+$a*(1-$N/$Nmax)*$N*$dt;  
  102.         $r=($N-$Nold)/$dt;
  103.     if(empty($base)){
  104.         // only set base once
  105.         global $buffer;
  106.         $base = ($N - $r)*$buffer;
  107.         $debug_out .= "<li><b>BASE: $base</b></li>";
  108.     }
  109.     $r = $r + $base;
  110.     $debug_out .= "<li>$out N:$N r:$r</li>";
  111.         $return .= '{ label: "'.$out.'", y: '.$r.' }, ';
  112.     global $today;
  113.     global $normal;
  114.     if (intval($today[$county]) > intval($r) && intval($r) != 0 && $normal[$county] == '' && $day_buffer > 14){
  115.         $normal[$county] = "<p style='background-color:lightgreen; '>On $out $county went under ".$today[$county]." to $r</p>";    
  116.     }
  117.     $day_buffer++;
  118.     }
  119.     return $return;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement