advanceStatsOprs.html 6.2 KB

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