github.com/manicqin/nomad@v0.9.5/ui/app/components/freestyle/sg-stats-time-series.js (about)

     1  import Component from '@ember/component';
     2  import { computed } from '@ember/object';
     3  import d3TimeFormat from 'd3-time-format';
     4  import moment from 'moment';
     5  
     6  export default Component.extend({
     7    timerTicks: 0,
     8  
     9    startTimer: function() {
    10      this.set(
    11        'timer',
    12        setInterval(() => {
    13          const metricsHigh = this.metricsHigh;
    14          const prev = metricsHigh.length ? metricsHigh[metricsHigh.length - 1].value : 0.9;
    15          this.appendTSValue(
    16            metricsHigh,
    17            Math.min(Math.max(prev + Math.random() * 0.05 - 0.025, 0.5), 1)
    18          );
    19  
    20          const metricsLow = this.metricsLow;
    21          const prev2 = metricsLow.length ? metricsLow[metricsLow.length - 1].value : 0.1;
    22          this.appendTSValue(
    23            metricsLow,
    24            Math.min(Math.max(prev2 + Math.random() * 0.05 - 0.025, 0), 0.5)
    25          );
    26        }, 1000)
    27      );
    28    }.on('init'),
    29  
    30    appendTSValue(array, value, maxLength = 300) {
    31      array.addObject({
    32        timestamp: Date.now(),
    33        value,
    34      });
    35  
    36      if (array.length > maxLength) {
    37        array.splice(0, array.length - maxLength);
    38      }
    39    },
    40  
    41    willDestroy() {
    42      clearInterval(this.timer);
    43    },
    44  
    45    metricsHigh: computed(() => {
    46      return [];
    47    }),
    48  
    49    metricsLow: computed(() => {
    50      return [];
    51    }),
    52  
    53    staticMetrics: computed(() => {
    54      const ts = offset =>
    55        moment()
    56          .subtract(offset, 'm')
    57          .toDate();
    58      return [
    59        { timestamp: ts(20), value: 0.5 },
    60        { timestamp: ts(18), value: 0.5 },
    61        { timestamp: ts(16), value: 0.4 },
    62        { timestamp: ts(14), value: 0.3 },
    63        { timestamp: ts(12), value: 0.9 },
    64        { timestamp: ts(10), value: 0.3 },
    65        { timestamp: ts(8), value: 0.3 },
    66        { timestamp: ts(6), value: 0.4 },
    67        { timestamp: ts(4), value: 0.5 },
    68        { timestamp: ts(2), value: 0.6 },
    69        { timestamp: ts(0), value: 0.6 },
    70      ];
    71    }),
    72  
    73    secondsFormat() {
    74      return d3TimeFormat.timeFormat('%H:%M:%S');
    75    },
    76  });