script.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. $(document).ready(function() {
  2. var max_trials=1000
  3. var headerTitleElement = $("#header h1");
  4. var entriesElement = $("#k8petstore-entries");
  5. var hostAddressElement = $("#k8petstore-host-address");
  6. var currentEntries = []
  7. var updateEntryCount = function(data, trial) {
  8. if(currentEntries.length > 1000)
  9. currentEntries.splice(0,100);
  10. //console.info("entry count " + data) ;
  11. currentEntries[trial]=data ;
  12. }
  13. var updateEntries = function(data) {
  14. entriesElement.empty();
  15. //console.info("data - > " + Math.random())
  16. //uncommend for debugging...
  17. //entriesElement.append("<br><br> CURRENT TIME : "+ $.now() +"<br><br>TOTAL entries : "+ JSON.stringify(currentEntries)+"<br><br>")
  18. var c1 = currentEntries[currentEntries.length-1]
  19. var c2 = currentEntries[currentEntries.length-2]
  20. entriesElement.append("<br><br> CURRENT TIME : "+ $.now() +"<br><br>TOTAL entries : "+ c1 +"<BR>transaction delta " + (c1-c2) +"<br><br>")
  21. f(currentEntries);
  22. $.each(data, function(key, val) {
  23. //console.info(key + " -> " +val);
  24. entriesElement.append("<p>" + key + " " + val.substr(0,50) + val.substr(100,150) + "</p>");
  25. });
  26. }
  27. // colors = purple, blue, red, green, yellow
  28. var colors = ["#549", "#18d", "#d31", "#2a4", "#db1"];
  29. var randomColor = colors[Math.floor(5 * Math.random())];
  30. (
  31. function setElementsColor(color) {
  32. headerTitleElement.css("color", color);
  33. })
  34. (randomColor);
  35. hostAddressElement.append(document.URL);
  36. // Poll every second.
  37. (function fetchGuestbook() {
  38. // Get JSON by running the query, and append
  39. $.getJSON("lrange/k8petstore").done(updateEntries).always(
  40. function() {
  41. setTimeout(fetchGuestbook, 2000);
  42. });
  43. })();
  44. (function fetchLength(trial) {
  45. $.getJSON("llen").done(
  46. function a(llen1){
  47. updateEntryCount(llen1, trial)
  48. }).always(
  49. function() {
  50. // This function is run every 2 seconds.
  51. setTimeout(
  52. function(){
  53. trial+=1 ;
  54. fetchLength(trial);
  55. f();
  56. }, 5000);
  57. }
  58. )
  59. })(0);
  60. });