PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/service_age.php

https://bitbucket.org/southsidehealth/southsidehealth
PHP | 93 lines | 80 code | 12 blank | 1 comment | 10 complexity | b7368d0ace419592932779d84866ae04 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, LGPL-2.1
  1. #!/usr/local/bin/php
  2. <?php
  3. require_once('database.php');
  4. require_once('date.php');
  5. $query = "SELECT * FROM services WHERE 1";
  6. $result = mysql_query($query);
  7. if (!$result) {
  8. die("Invalid query: " . mysql_error());
  9. }
  10. while ($row = @mysql_fetch_assoc($result)) {
  11. $age = dateDiff('d',$row['modified'],date('Y-m-d'));
  12. $query = "UPDATE services SET age = " . $age . " WHERE id = " . $row['id'];
  13. echo "Updating age as " . $age . " for service id " . $row['id'] . "\n";
  14. $update_result = mysql_query($query);
  15. if (!$update_result) {
  16. die("Invalid query: " . mysql_error());
  17. }
  18. }
  19. $query = "SELECT SUM(age) as total_age, COUNT(*) as total_no_of_service from services WHERE 1";
  20. $result = mysql_query($query);
  21. if (!$result) {
  22. die("Invalid query: " . mysql_error());
  23. }
  24. $row = @mysql_fetch_assoc($result);
  25. $total_age = $row['total_age'];
  26. $total_no_of_service = $row['total_no_of_service'];
  27. $average_age_of_service = $total_age / $total_no_of_service;
  28. echo " No Of Services :" . $total_no_of_service . "\n";
  29. echo " Total Age of Services :" . $total_age . "\n";
  30. echo " Average Age of Services :" . $average_age_of_service . "\n";
  31. $query = "SELECT COUNT(*) as total_no_of_sites from sites WHERE 1";
  32. $result = mysql_query($query);
  33. if (!$result) {
  34. die("Invalid query: " . mysql_error());
  35. }
  36. $row = @mysql_fetch_assoc($result);
  37. $total_no_of_sites = $row['total_no_of_sites'];
  38. echo " Total No of Sites :" . $total_no_of_sites . "\n";
  39. $query = "SELECT COUNT(*) as total_no_of_locations from locations WHERE 1";
  40. $result = mysql_query($query);
  41. if (!$result) {
  42. die("Invalid query: " . mysql_error());
  43. }
  44. $row = @mysql_fetch_assoc($result);
  45. $total_no_of_locations = $row['total_no_of_locations'];
  46. echo " Total No of Locations :" . $total_no_of_locations . "\n";
  47. $query = "SELECT COUNT(*) as total_no_of_users from users WHERE 1";
  48. $result = mysql_query($query);
  49. if (!$result) {
  50. die("Invalid query: " . mysql_error());
  51. }
  52. $row = @mysql_fetch_assoc($result);
  53. $total_no_of_users = $row['total_no_of_users'];
  54. echo " Total No of Users:" . $total_no_of_users . "\n";
  55. $query = "UPDATE statistics
  56. SET
  57. no_of_services = $total_no_of_service,
  58. total_age_of_services = $total_age,
  59. average_age_of_services = $average_age_of_service,
  60. no_of_sites = $total_no_of_sites,
  61. no_of_locations = $total_no_of_locations,
  62. no_of_users = $total_no_of_users
  63. WHERE 1";
  64. echo $query . "\n";
  65. $update_result = mysql_query($query);
  66. if (!$update_result) {
  67. die("Invalid query: " . mysql_error());
  68. }
  69. $query = "INSERT INTO statistics_history (
  70. no_of_services,
  71. total_age_of_services,
  72. average_age_of_services,
  73. no_of_sites,
  74. no_of_locations,no_of_users,created)
  75. VALUES ($total_no_of_service, $total_age,$average_age_of_service,$total_no_of_sites,$total_no_of_locations,$total_no_of_users,now())";
  76. echo $query . "\n";
  77. $insert_result = mysql_query($query);
  78. if (!$insert_result) {
  79. die("Invalid query: " . mysql_error());
  80. }
  81. ?>