mutations.js 3.2 KB
Newer Older
1
import Vue from 'vue';
2
import * as types from './mutation_types';
3
import { normalizeMetrics, sortMetrics, normalizeQueryResult } from './utils';
4 5 6 7 8 9 10

export default {
  [types.REQUEST_METRICS_DATA](state) {
    state.emptyState = 'loading';
    state.showEmptyState = true;
  },
  [types.RECEIVE_METRICS_DATA_SUCCESS](state, groupData) {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    state.groups = groupData.map(group => {
      let { metrics } = group;

      // for backwards compatibility, and to limit Vue template changes:
      // for each group alias panels to metrics
      // for each panel alias metrics to queries
      if (state.useDashboardEndpoint) {
        metrics = group.panels.map(panel => ({
          ...panel,
          queries: panel.metrics,
        }));
      }

      return {
        ...group,
        metrics: normalizeMetrics(sortMetrics(metrics)),
      };
    });
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    if (!state.groups.length) {
      state.emptyState = 'noData';
    } else {
      state.showEmptyState = false;
    }
  },
  [types.RECEIVE_METRICS_DATA_FAILURE](state, error) {
    state.emptyState = error ? 'unableToConnect' : 'noData';
    state.showEmptyState = true;
  },
  [types.RECEIVE_DEPLOYMENTS_DATA_SUCCESS](state, deployments) {
    state.deploymentData = deployments;
  },
  [types.RECEIVE_DEPLOYMENTS_DATA_FAILURE](state) {
    state.deploymentData = [];
  },
  [types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS](state, environments) {
    state.environments = environments;
  },
  [types.RECEIVE_ENVIRONMENTS_DATA_FAILURE](state) {
    state.environments = [];
  },
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  [types.SET_QUERY_RESULT](state, { metricId, result }) {
    if (!metricId || !result || result.length === 0) {
      return;
    }

    state.showEmptyState = false;

    state.groups.forEach(group => {
      group.metrics.forEach(metric => {
        metric.queries.forEach(query => {
          if (query.metric_id === metricId) {
            state.metricsWithData.push(metricId);
            // ensure dates/numbers are correctly formatted for charts
            const normalizedResults = result.map(normalizeQueryResult);
            Vue.set(query, 'result', Object.freeze(normalizedResults));
          }
        });
      });
    });
  },
72 73 74 75
  [types.SET_ENDPOINTS](state, endpoints) {
    state.metricsEndpoint = endpoints.metricsEndpoint;
    state.environmentsEndpoint = endpoints.environmentsEndpoint;
    state.deploymentsEndpoint = endpoints.deploymentsEndpoint;
76
    state.dashboardEndpoint = endpoints.dashboardEndpoint;
77
    state.currentDashboard = endpoints.currentDashboard;
78
    state.projectPath = endpoints.projectPath;
79 80 81
  },
  [types.SET_DASHBOARD_ENABLED](state, enabled) {
    state.useDashboardEndpoint = enabled;
82
  },
83 84 85
  [types.SET_MULTIPLE_DASHBOARDS_ENABLED](state, enabled) {
    state.multipleDashboardsEnabled = enabled;
  },
86 87 88
  [types.SET_GETTING_STARTED_EMPTY_STATE](state) {
    state.emptyState = 'gettingStarted';
  },
89 90 91 92
  [types.SET_NO_DATA_EMPTY_STATE](state) {
    state.showEmptyState = true;
    state.emptyState = 'noData';
  },
93 94 95
  [types.SET_ALL_DASHBOARDS](state, dashboards) {
    state.allDashboards = dashboards;
  },
96 97 98
  [types.SET_ADDITIONAL_PANEL_TYPES_ENABLED](state, enabled) {
    state.additionalPanelTypesEnabled = enabled;
  },
T
Tristan Read 已提交
99 100 101
  [types.SET_SHOW_ERROR_BANNER](state, enabled) {
    state.showErrorBanner = enabled;
  },
102
};