graph.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!DOCTYPE html>
  2. <html lang="en" xml:lang="en">
  3. <head>
  4. <title>Data Graph</title>
  5. <meta charset="UTF-8">
  6. <script type="text/javascript" src='plotly-basic-2.18.2.min.js?v=$COMMIT_HASH'></script>
  7. <script type="text/javascript" src="common.js?v=$COMMIT_HASH"></script>
  8. <script type="text/javascript" src="readconfigparam.js?v=$COMMIT_HASH"></script>
  9. <style>
  10. h1 {font-size: 2em;}
  11. h2 {font-size: 1.5em; margin-block-start: 0.0em; margin-block-end: 0.2em;}
  12. h3 {font-size: 1.2em;}
  13. p {font-size: 1em;}
  14. body {
  15. font-family: Arial, Helvetica, sans-serif;
  16. }
  17. select {
  18. padding: 3px 5px;
  19. display: inline-block;
  20. border: 1px solid #ccc;
  21. font-size: 16px;
  22. margin-right: 10px;
  23. min-width: 100px;
  24. vertical-align: middle;
  25. }
  26. .button {
  27. padding: 5px 10px;
  28. width: 160px;
  29. font-size: 16px;
  30. }
  31. </style>
  32. <script>
  33. function run() {
  34. datefile = document.getElementById("datafiles").value;
  35. numbername = document.getElementById("numbers").value;
  36. showRrelativeValues = document.getElementById("showRrelativeValues").checked;
  37. //alert("Auslesen: " + datefile + " " + numbername);
  38. _domainname = getDomainname();
  39. fetch(_domainname + '/fileserver/log/data/' + datefile)
  40. .then(response => {
  41. // handle the response
  42. if (response.status == 404) {
  43. firework.launch("No data available for " + dateString, 'warning', 10000);
  44. }
  45. response.text()
  46. .then( result => {
  47. var lines = result.split("\n");
  48. var traceValue = { x: [], y: [], type: 'scatter', line: {width: 6}, name: 'Value'};
  49. var tracePreValue = { x: [], y: [], type: 'scatter', line: {width: 2}, name: 'Previous Value', visible: 'legendonly'};
  50. var traceChangeRate = { x: [], y: [], type: 'bar', yaxis: 'y2', opacity: 0.2, name: 'Change Rate'};
  51. var traceChangeAbsolute = { x: [], y: [], type: 'bar', yaxis: 'y2', opacity: 0.2, name: 'Change Absolute', visible: 'legendonly'};
  52. var timex = 1;
  53. for (let line of lines) {
  54. {
  55. //console.log(line);
  56. if (line.split(",")[1] == numbername)
  57. {
  58. var value = line.split(",")[3];
  59. var preValue = line.split(",")[4];
  60. var changeRate = line.split(",")[5];
  61. var changeAbsolute = line.split(",")[6];
  62. var time = line.split(",")[0];
  63. //console.log("> "+time+" "+value+"\n");
  64. traceValue.x.push(time);
  65. /* Catch empty fields */
  66. if (value == "" || isNaN(value)) {
  67. value = NaN;
  68. }
  69. if (preValue == "" || isNaN(preValue)) {
  70. preValue = NaN;
  71. }
  72. if (changeRate == "" || isNaN(changeRate)) {
  73. changeRate = NaN;
  74. }
  75. if (changeAbsolute == "" || isNaN(changeAbsolute)) {
  76. changeAbsolute = NaN;
  77. }
  78. traceValue.y.push(value);
  79. tracePreValue.y.push(preValue);
  80. traceChangeRate.y.push(changeRate);
  81. traceChangeAbsolute.y.push(changeAbsolute);
  82. }
  83. }
  84. }
  85. /* If the value trace starts with NaN, replace all those Nans with the first valid value */
  86. var firstNonNaNIndex = traceValue.y.findIndex(function (value) {
  87. return !isNaN(value);
  88. });
  89. if (firstNonNaNIndex === -1) {
  90. console.log("No valid value data available for number sequence '" + numbername + "'.");
  91. }
  92. else if (firstNonNaNIndex > 0) { // Replace all leading NaN with the first valid value
  93. console.log("The first leading values have all just NaN, replacing them with the value of",
  94. traceValue.y[firstNonNaNIndex], "at", traceValue.x[firstNonNaNIndex], "(Index:", firstNonNaNIndex, ")");
  95. for(var i = 0; i < firstNonNaNIndex; i++) {
  96. traceValue.y[i] = traceValue.y[firstNonNaNIndex];
  97. }
  98. }
  99. // Copy time to all traces
  100. tracePreValue.x = traceValue.x;
  101. traceChangeRate.x = traceValue.x;
  102. traceChangeAbsolute.x = traceValue.x;
  103. //console.log(traceValue.y);
  104. var offsetValue = traceValue.y[0];
  105. var offsetPreValue = tracePreValue.y[0];
  106. traceValue.connectgaps = true;
  107. if (showRrelativeValues) {
  108. traceValue.y.forEach(function(part, index, arr) {
  109. arr[index] = arr[index] - offsetValue;
  110. });
  111. tracePreValue.y.forEach(function(part, index, arr) {
  112. arr[index] = arr[index] - offsetPreValue;
  113. });
  114. }
  115. // console.log(traceValue.x)
  116. var data = [traceValue, tracePreValue, traceChangeRate, traceChangeAbsolute];
  117. var layout = {
  118. showlegend: true,
  119. colorway: ['green', 'black', 'blue', 'black'],
  120. yaxis: {title: 'Value'},
  121. yaxis2: {
  122. title: 'Change',
  123. overlaying: 'y',
  124. side: 'right'
  125. },
  126. margin: {
  127. l: 70,
  128. r: 70,
  129. b: 50,
  130. t: 40,
  131. pad: 4
  132. },
  133. legend: {
  134. x: 0.02,
  135. y: 0.97,
  136. xanchor: 'left'
  137. }
  138. };
  139. document.getElementById("chart").innerHTML = "";
  140. Plotly.newPlot('chart', data, layout, {displayModeBar: true});
  141. });
  142. }).catch((error) => {
  143. // handle the error
  144. console.log(error);
  145. });
  146. }
  147. </script>
  148. <link href="firework.css?v=$COMMIT_HASH" rel="stylesheet">
  149. <script type="text/javascript" src="jquery-3.6.0.min.js?v=$COMMIT_HASH"></script>
  150. <script type="text/javascript" src="firework.js?v=$COMMIT_HASH"></script>
  151. </head>
  152. <body>
  153. <h2>Data Graph</h2>
  154. <div id='chart'><p>Loading...<br></p></div>
  155. Number sequence:
  156. <select id="numbers" onchange="run();"></select>
  157. Day:
  158. <select id="datafiles" onchange="run();"></select>
  159. <input type="checkbox" id="showRrelativeValues" onclick = 'run();' unchecked ><label for="showRrelativeValues">Show relative values</label><br><br>
  160. <button class="button" onclick="run();">Refresh</button>
  161. <button class="button" onClick="window.location.href = 'data.html?v=$COMMIT_HASH'">Show Data Viewer</button>
  162. <button class="button" onClick="window.location.href = getDomainname() + '/fileserver/log/data/'">Show Data Files</button>
  163. <script>
  164. async function WriteModelFiles()
  165. {
  166. list_data = await getDATAList();
  167. var _indexDig = document.getElementById("datafiles");
  168. while (_indexDig.length)
  169. _indexDig.remove(0);
  170. for (var i = list_data.length - 1; i >= 0; --i)
  171. {
  172. var optionDig = document.createElement("option");
  173. var text = list_data[i];
  174. optionDig.text = text;
  175. optionDig.value = list_data[i];
  176. _indexDig.add(optionDig);
  177. }
  178. }
  179. async function WriteNumbers()
  180. {
  181. list_data = await getNUMBERSList();
  182. var _indexDig = document.getElementById("numbers");
  183. while (_indexDig.length)
  184. _indexDig.remove(0);
  185. for (var i = 0; i < list_data.length; ++i)
  186. {
  187. var optionDig = document.createElement("option");
  188. var text = list_data[i];
  189. optionDig.text = text;
  190. optionDig.value = list_data[i];
  191. _indexDig.add(optionDig);
  192. }
  193. }
  194. function Refresh() {
  195. setTimeout (function() {
  196. run();
  197. Refresh();
  198. }, 300000);
  199. }
  200. async function initializeGraph() {
  201. await Promise.all([WriteModelFiles(), WriteNumbers()]);
  202. run();
  203. Refresh();
  204. }
  205. initializeGraph().catch(function (error) {
  206. console.error("Could not initialize graph selectors:", error);
  207. });
  208. </script>
  209. </body>
  210. </html>