advanceStatsOprs.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Notes: This should be open in its original path-->
  5. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  6. <script src="../script/jquery-3.6.0.min.js"></script>
  7. <script src="../script/semantic/semantic.min.js"></script>
  8. </head>
  9. <body>
  10. <br>
  11. <div class="ui container">
  12. <div class="ui header">
  13. <div class="content">
  14. Advance Statistics Operations
  15. <div class="sub header">Selected Range: <span id="daterange"></span></div>
  16. </div>
  17. </div>
  18. <div class="ui divider"></div>
  19. <h3>Export Data</h3>
  20. <p>You can export the statistics collected by Zoraxy in the selected range for further analysis</p>
  21. <button class="ui basic teal button" onclick="handleExportAsCSV();"><i class="download icon"></i> Export CSV</button>
  22. <button class="ui basic pink button" onclick="handleExportAsJSON();"><i class="download icon"></i> Export JSON</button>
  23. <div class="ui divider"></div>
  24. <h3>Reset Statistics</h3>
  25. <p>You can reset the statistics within the selected time range for debug purpose. Note that this operation is irreversible.</p>
  26. <button class="ui basic red button" onclick="handleResetStats();"><i class="trash icon"></i> RESET STATISTICS</button>
  27. <br><br>
  28. <button class="ui basic button iframeOnly" style="float: right;" onclick="parent.hideSideWrapper();"><i class="remove icon"></i> Cancel</button>
  29. </div>
  30. <script>
  31. let startDate = "";
  32. let endDate = "";
  33. /*
  34. Actions Handler
  35. */
  36. function handleExportAsJSON(){
  37. window.open(`/api/analytic/exportRange?start=${startDate}&end=${endDate}&format=json`, 'download');
  38. }
  39. function handleExportAsCSV(){
  40. window.open(`/api/analytic/exportRange?start=${startDate}&end=${endDate}&format=csv`, 'download');
  41. }
  42. function handleResetStats(){
  43. if (confirm("Confirm remove statistics from " + startDate + " to " + endDate +"?")){
  44. $.ajax({
  45. url: "/api/analytic/resetRange?start=" + startDate + "&end=" + endDate,
  46. method: "DELETE",
  47. success: function(data){
  48. if (data.error != undefined){
  49. parent.msgbox(data.error, false, 5000);
  50. }else{
  51. parent.msgbox("Statistic Cleared");
  52. parent.hideSideWrapper();
  53. }
  54. }
  55. })
  56. }
  57. }
  58. /*
  59. Data Loading
  60. */
  61. function loadDateRange(){
  62. if (window.location.hash.length > 1){
  63. try{
  64. var dateRange = JSON.parse(decodeURIComponent(window.location.hash.substr(1)));
  65. startDate = dateRange[0].trim();
  66. endDate = dateRange[1].trim();
  67. //Check if they are valid dates
  68. if (!isValidDateFormat(startDate)){
  69. alert("Start date is not a valid date: " + startDate);
  70. return
  71. }
  72. if (!isValidDateFormat(endDate)){
  73. alert("End date is not a valid date: " + endDate);
  74. return
  75. }
  76. //Sort the two dates if they are placed in invalid orders
  77. var [s, e] = sortDates(startDate, endDate);
  78. startDate = s;
  79. endDate = e;
  80. $("#daterange").html(startDate + ` <i class="arrow right icon" style="margin-right: 0;"></i> ` + endDate);
  81. }catch(ex){
  82. alert("Invalid usage: Invalid date range given");
  83. }
  84. }
  85. }
  86. loadDateRange();
  87. function isValidDateFormat(dateString) {
  88. // Create a regular expression pattern for the yyyy-mm-dd format
  89. const pattern = /^\d{4}-\d{2}-\d{2}$/;
  90. // Check if the input string matches the pattern
  91. if (!pattern.test(dateString)) {
  92. return false; // Invalid format
  93. }
  94. // Parse the date components
  95. const year = parseInt(dateString.substring(0, 4), 10);
  96. const month = parseInt(dateString.substring(5, 7), 10);
  97. const day = parseInt(dateString.substring(8, 10), 10);
  98. // Check if the parsed components represent a valid date
  99. const date = new Date(year, month - 1, day);
  100. if (
  101. date.getFullYear() !== year ||
  102. date.getMonth() + 1 !== month ||
  103. date.getDate() !== day
  104. ) {
  105. return false; // Invalid date
  106. }
  107. return true; // Valid date in yyyy-mm-dd format
  108. }
  109. function sortDates(date1, date2) {
  110. // Parse the date strings
  111. const parsedDate1 = new Date(date1);
  112. const parsedDate2 = new Date(date2);
  113. // Compare the parsed dates
  114. if (parsedDate1 > parsedDate2) {
  115. // Swap the dates
  116. const temp = date1;
  117. date1 = date2;
  118. date2 = temp;
  119. }
  120. // Return the swapped dates
  121. return [date1, date2];
  122. }
  123. </script>
  124. </body>
  125. </html>