Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PlotHorizon
  2. {
  3.   contructor(Horizon)
  4.   {
  5.     this.SetHorizon(Horizon);
  6.  
  7.     this.LabelFormat['1Day'] = this.TickFormat_1Day;
  8.     this.LabelFormat['1Week'] = this.TickFormat_1Day;
  9.     this.LabelFormat['1Month'] = this.TickFormat_1Day;
  10.     this.LabelFormat['1Year'] = this.TickFormat_1Day;
  11.  
  12.  
  13.  
  14.     SetHorizon(Horizon)
  15.     {
  16.       switch (Horizon)
  17.       {
  18.         case "1Day":
  19.         case "1Week":
  20.         case "1Day":
  21.         case "1Week":
  22.           this.Horizon = Horizon;
  23.           break;
  24.         default:
  25.           this.Horizon = "1Day";
  26.       }
  27.     }
  28.  
  29.     LabelFormatter()
  30.     {
  31.       this.SetHorizon(this.Horizon);
  32.       return this.LabelFormat[this.Horizon];
  33.     }
  34.  
  35.     TickFormat_1Day(value, index, values)
  36.     {
  37.       if (values[index].major)
  38.       {
  39.         return moment(value).format(LT);
  40.       }
  41.       else
  42.       {
  43.         return null;
  44.       }
  45.     }
  46.  
  47.     TickFormat_1Week(value, index, values)
  48.     {
  49.       if (values[index].major)
  50.       {
  51.         return moment(value).format("D-M LT");
  52.       }
  53.       else
  54.       {
  55.         return null;
  56.       }
  57.     }
  58.  
  59.     TickFormat_1Month(value, index, values)
  60.     {
  61.       if (values[index].major)
  62.       {
  63.         return moment(value).format("D-M");
  64.       }
  65.       else
  66.       {
  67.         return null;
  68.       }
  69.     }
  70.  
  71.     TickFormat_1Year(value, index, values)
  72.     {
  73.       if (values[index].major)
  74.       {
  75.         return moment(value).format("M");
  76.       }
  77.       else
  78.       {
  79.         return null;
  80.       }
  81.     }
  82.  
  83.   }
  84. }
  85.  
  86.  
  87. class ServerStatsMgrClass
  88. {
  89.   constructor()
  90.   {
  91.     this.TemplateDom = $("#StatIndicatorTemplate");
  92.     $(".StatIndicatorTile").on('click', this.HandleTileClick.bind(this));
  93.     this.TileInfo = {
  94.       "Stat_MailQStat":
  95.       {
  96.         Threshold: [0, 10, 25],
  97.         Colors: ["lime", "orange", "red"],
  98.         Unit: "msg",
  99.         Image: "./images/Stats_Msg.png"
  100.       },
  101.       "Stat_VolumeSpace":
  102.       {
  103.         Threshold: [0, 90, 95],
  104.         Colors: ["lime", "orange", "red"],
  105.         Unit: "%",
  106.         Image: "./images/Stats_Disk.png"
  107.       },
  108.       "Stat_MySQLStats":
  109.       {
  110.         Threshold: [0, 500, 700],
  111.         Colors: ["lime", "orange", "red"],
  112.         Unit: null,
  113.         Image: "./images/Stats_Connections.png"
  114.       },
  115.       "Stat_EngineStats":
  116.       {
  117.         Threshold: [0, 100, 200],
  118.         Colors: ["red", "orange", "lime"],
  119.         Unit: "Boats/s",
  120.         Image: "./images/Stats_Speed.png"
  121.       },
  122.       "BoatCount":
  123.       {
  124.         Threshold: [0],
  125.         Colors: ["lime"],
  126.         Unit: null
  127.       },
  128.       "RaceCount":
  129.       {
  130.         Threshold: [0],
  131.         Colors: ["lime"],
  132.         Unit: null
  133.       },
  134.       "max_connections":
  135.       {
  136.         Threshold: [0],
  137.         Colors: ["blue"],
  138.         Image: "./images/Stats_Connections.png"
  139.       },
  140.     };
  141.     this.PlotHorizon = new PlotHorizon("1Day");
  142.   }
  143.  
  144.   LoadStats()
  145.   {
  146.     $("#StatsPreloader").removeClass("hidden");
  147.     $.get("/ws/serverinfo/ServerStatus.php?v=" + Math.round(new Date().getTime() / 1000 / 60 / 3), this.HandleStatLoaded.bind(this));
  148.   }
  149.  
  150.   HandleStatLoaded(e)
  151.   {
  152.     this.Stats = e;
  153.     this.DataSetTiles = [];
  154.     this.DisplayCurrentValues();
  155.     $("#StatsPreloader").addClass("hidden");
  156.   }
  157.  
  158.   DisplayCurrentValues()
  159.   {
  160.     this.Stats.Data.sort();
  161.     for (let index in this.Stats.Data)
  162.     {
  163.       if (this.Stats.Data[index])
  164.       {
  165.         let TypedDataRow = this.Stats.Data[index];
  166.         let color = null;
  167.  
  168.         if (this.TileInfo[TypedDataRow.TypeName])
  169.         {
  170.           color = this.TileInfo[TypedDataRow.TypeName];
  171.         }
  172.  
  173.         TypedDataRow.Data = TypedDataRow.Data.sort(this.TypedRowSorter);
  174.         for (let ValueIndex in TypedDataRow.Data)
  175.         {
  176.           let DataRow = TypedDataRow.Data[ValueIndex];
  177.           DataRow.Values = DataRow.Values.sort(this.DataRowSorter);
  178.  
  179.           if (DataRow.Name)
  180.           {
  181.             let Name = DataRow.Name;
  182.             let Values = DataRow.Values;
  183.             let Value = Values[Values.length - 1].value;
  184.             let LocalColor = color;
  185.             if (this.TileInfo[Name])
  186.             {
  187.               LocalColor = this.TileInfo[Name];
  188.             }
  189.             let TileId = this.UpdateStatTile(Name, Value, LocalColor);
  190.             this.DataSetTiles[TileId] = DataRow;
  191.           }
  192.         }
  193.       }
  194.     }
  195.   }
  196.  
  197.   TypedRowSorter(r1, r2)
  198.   {
  199.     if (r1.Name && r2.Name)
  200.     {
  201.       if (r1.Name > r2.Name)
  202.       {
  203.         return 1;
  204.       }
  205.       else if (r1.Name < r2.Name)
  206.       {
  207.         return -1;
  208.       }
  209.       else
  210.       {
  211.         return 0;
  212.       }
  213.     }
  214.     else
  215.     {
  216.       if (r1 >= r2)
  217.       {
  218.         return 1;
  219.       }
  220.       else
  221.       {
  222.         return -1;
  223.       }
  224.     }
  225.   }
  226.  
  227.   DataRowSorter(r1, r2)
  228.   {
  229.     if (r1.date && r2.date)
  230.     {
  231.       if (r1.date === r2.date)
  232.       {
  233.         if (r1.value > r2.value)
  234.         {
  235.           return 1;
  236.         }
  237.         else
  238.         {
  239.           return -1;
  240.         }
  241.       }
  242.       else if (r1.date > r2.date)
  243.       {
  244.         return 1;
  245.       }
  246.       else
  247.       {
  248.         return -1;
  249.       }
  250.     }
  251.     else
  252.     {
  253.       if (r1 >= r2)
  254.       {
  255.         return 1;
  256.       }
  257.       else
  258.       {
  259.         return -1;
  260.       }
  261.     }
  262.   }
  263.  
  264.   UpdateStatTile(Name, Value, TileInfo)
  265.   {
  266.     let TileId = "Stt_" + Name.replace(/\//g, "_");
  267.     let Tile = $("#" + TileId)[0];
  268.  
  269.     if (!Tile)
  270.     {
  271.       // Create a new tile and add to Dom
  272.       let NewTile = this.TemplateDom.clone().removeClass("hidden")[0];
  273.       $(NewTile).attr("Id", TileId);
  274.       $(NewTile).find("[Fld_Id='title']").text(Name);
  275.       $(NewTile).on('click', this.HandleTileClick.bind(this));
  276.       if (TileInfo.Unit)
  277.       {
  278.         $(NewTile).find("[Fld_Id='unit']").text(TileInfo.Unit);
  279.       }
  280.       if (TileInfo.Image)
  281.       {
  282.         $(NewTile).find("[src]").attr("src", TileInfo.Image);
  283.       }
  284.       else
  285.       {
  286.         $(NewTile).find("[src]").addClass("hidden");
  287.       }
  288.       $("#CountersList").append(NewTile);
  289.       Tile = NewTile;
  290.     }
  291.     $(Tile).find("[Fld_Id='value']").text(RoundPow(Value, 2));
  292.     let color = "lightgrey";
  293.     if (TileInfo)
  294.     {
  295.       let index = 0;
  296.       while (Value >= TileInfo.Threshold[index])
  297.       {
  298.         color = TileInfo.Colors[index];
  299.         index++;
  300.       }
  301.     }
  302.     $(Tile).find(".StatusColor").css("background-color", color);
  303.     return TileId;
  304.  
  305.   }
  306.  
  307.   HandleTileClick(e)
  308.   {
  309.     let DataSetId = e.currentTarget.attributes.Id.value;
  310.  
  311.     if (this.DataSetTiles[DataSetId])
  312.     {
  313.       this.PlotTileData(DataSetId);
  314.     }
  315.  
  316.   }
  317.  
  318.  
  319.  
  320.   PlotTileData(DataSetId)
  321.   {
  322.     let Labels = [];
  323.     let Values = [];
  324.     let DataName = this.DataSetTiles[DataSetId].Name;
  325.     for (let index in this.DataSetTiles[DataSetId].Values)
  326.     {
  327.       let Data = this.DataSetTiles[DataSetId].Values[index];
  328.       Labels.push(new Date(Data.date * 1000));
  329.       Values.push(Data.value);
  330.     }
  331.     let Height = $("#StatsContainer").css("Height");
  332.     $("#StatsPlotCanvas").css("Height", Height);
  333.     let ctx = $("#StatsPlotCanvas")[0].getContext("2d");
  334.     var chart = new Chart(ctx,
  335.     {
  336.       // The type of chart we want to create
  337.       type: 'line',
  338.       // The data for our dataset
  339.       data:
  340.       {
  341.         labels: Labels,
  342.         datasets: [
  343.         {
  344.           label: DataName,
  345.           backgroundColor: '#88fccd49',
  346.           borderColor: '#fccd49',
  347.           data: Values,
  348.           pointRadius: 1,
  349.           borderWidth: 1,
  350.           //showLine: false
  351.         }]
  352.       },
  353.       // Configuration options go here
  354.       options:
  355.       {
  356.         scales:
  357.         {
  358.           yAxes: [
  359.           {
  360.             ticks:
  361.             {
  362.               // Include a dollar sign in the ticks
  363.               callback: function(value, index, values)
  364.               {
  365.                 return ServerStatsMgr.PlotHorizon.LabelFormatter(value, index, values);
  366.               }
  367.             }
  368.           }]
  369.         }
  370.       }
  371.     });
  372.   }
  373. }
  374.  
  375.  
  376. var ServerStatsMgr = new ServerStatsMgrClass();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement