github.com/outbrain/consul@v1.4.5/ui-v2/app/mixins/with-health-filtering.js (about)

     1  import Mixin from '@ember/object/mixin';
     2  import WithFiltering from 'consul-ui/mixins/with-filtering';
     3  import { computed, get } from '@ember/object';
     4  import ucfirst from 'consul-ui/utils/ucfirst';
     5  
     6  const countStatus = function(items, status) {
     7    if (status === '') {
     8      return get(items, 'length');
     9    }
    10    const key = `Checks${ucfirst(status)}`;
    11    return items.reduce(function(prev, item, i, arr) {
    12      const num = get(item, key);
    13      return (
    14        prev +
    15          (typeof num !== 'undefined'
    16            ? num
    17            : get(item, 'Checks').filter(function(item) {
    18                return item.Status === status;
    19              }).length) || 0
    20      );
    21    }, 0);
    22  };
    23  export default Mixin.create(WithFiltering, {
    24    queryParams: {
    25      status: {
    26        as: 'status',
    27      },
    28      s: {
    29        as: 'filter',
    30      },
    31    },
    32    healthFilters: computed('items', function() {
    33      const items = get(this, 'items');
    34      const objs = ['', 'passing', 'warning', 'critical'].map(function(item) {
    35        const count = countStatus(items, item);
    36        return {
    37          count: count,
    38          label: `${item === '' ? 'All' : ucfirst(item)} (${count.toLocaleString()})`,
    39          value: item,
    40        };
    41      });
    42      objs[0].label = `All (${objs
    43        .slice(1)
    44        .reduce(function(prev, item, i, arr) {
    45          return prev + item.count;
    46        }, 0)
    47        .toLocaleString()})`;
    48      return objs;
    49    }),
    50  });