skywalking-booster-ui/src/views/dashboard/configuration/ConfigEdit.vue
2022-01-11 15:36:40 +08:00

349 lines
9.1 KiB
Vue

<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. -->
<template>
<div class="widget-config flex-v">
<div class="graph">
<div class="header">{{ title }}</div>
<div class="render-chart">
<component
:is="chartType"
:intervalTime="appStoreWithOut.intervalTime"
:data="source"
/>
</div>
<span v-show="!source">{{ t("noData") }}</span>
</div>
<div class="collapse" :style="{ height: configHeight + 'px' }">
<el-collapse
v-model="activeNames"
:style="{ '--el-collapse-header-font-size': '15px' }"
>
<el-collapse-item :title="t('metricName')" name="1">
<div>
<Selector
:value="metrics"
:options="metricOpts"
:multiple="true"
size="mini"
placeholder="Select a metric"
@change="changeMetrics"
class="selectors"
/>
<Selector
v-if="valueType"
:value="valueType"
:options="valueTypes"
size="mini"
placeholder="Select a metric"
@change="changeValueType"
class="selectors"
v-loading="loading"
/>
</div>
</el-collapse-item>
<el-collapse-item :title="t('selectVisualization')" name="2">
<div class="chart-types">
<span
v-for="(type, index) in ChartTypes"
:key="index"
@click="changeChartType(type)"
:class="{ active: type.value === chartType }"
>
{{ type.label }}
</span>
</div>
</el-collapse-item>
<el-collapse-item :title="t('graphStyles')" name="3">
<component
:is="`${chartType}Config`"
:config="dashboardStore.selectedGrid.graph"
/>
</el-collapse-item>
<el-collapse-item :title="t('widgetOptions')" name="4">
<WidgetOptions @update="updateWidgetOptions" />
</el-collapse-item>
<el-collapse-item :title="t('standardOptions')" name="5">
<StandardOptions />
</el-collapse-item>
</el-collapse>
</div>
<div class="footer">
<el-button size="mini">
{{ t("cancel") }}
</el-button>
<el-button size="mini" type="primary" @click="applyConfig">
{{ t("apply") }}
</el-button>
</div>
</div>
</template>
<script lang="ts">
import { reactive, defineComponent, toRefs, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useDashboardStore } from "@/store/modules/dashboard";
import { useAppStoreWithOut } from "@/store/modules/app";
import { ElMessage, ElButton, ElCollapse, ElCollapseItem } from "element-plus";
import { ValuesTypes, MetricQueryTypes, ChartTypes } from "../data";
import { Option } from "@/types/app";
import graphs from "../graphs";
import configs from "./graph-styles";
import WidgetOptions from "./WidgetOptions.vue";
import StandardOptions from "./StandardOptions.vue";
export default defineComponent({
name: "ConfigEdit",
components: {
...graphs,
...configs,
WidgetOptions,
StandardOptions,
ElButton,
ElCollapse,
ElCollapseItem,
},
setup() {
const loading = ref<boolean>(false);
const { t } = useI18n();
const dashboardStore = useDashboardStore();
const appStoreWithOut = useAppStoreWithOut();
const { selectedGrid } = dashboardStore;
const states = reactive<{
metrics: string[];
valueTypes: Option[];
valueType: string;
metricQueryType: string;
chartType: string;
activeNames: string;
source: any;
index: string;
}>({
metrics: selectedGrid.metrics || [],
valueTypes: [],
valueType: "",
metricQueryType: "",
chartType: selectedGrid.graph.type,
activeNames: "1",
source: {},
index: selectedGrid.i,
});
const widgetOpt = reactive<
| {
title: string;
tips: string;
}
| any
>({
tips: selectedGrid.widget.tips,
title: selectedGrid.widget.title,
});
queryMetricType(states.metrics[0]);
async function changeMetrics(arr: Option[]) {
if (!arr.length) {
states.valueTypes = [];
states.valueType = "";
return;
}
states.metrics = arr.map((d: Option) => String(d.value));
queryMetricType(String(arr[0].value));
}
async function queryMetricType(metric: string) {
loading.value = true;
const resp = await dashboardStore.fetchMetricType(metric);
loading.value = false;
if (resp.error) {
ElMessage.error(resp.data.error);
return;
}
const { typeOfMetrics } = resp.data;
states.valueTypes = ValuesTypes[typeOfMetrics];
states.valueType = ValuesTypes[typeOfMetrics][0].value;
}
function changeValueType(val: Option[]) {
states.valueType = String(val[0].value);
states.metricQueryType = (MetricQueryTypes as any)[states.valueType];
}
function changeChartType(item: Option) {
states.chartType = String(item.value);
}
const metricOpts = [
{ value: "service_apdex", label: "service_apdex" },
{ value: "service_sla", label: "service_sla" },
{ value: "service_cpm", label: "service_cpm" },
{ value: "service_resp_time", label: "service_resp_time" },
{ value: "service_percentile", label: "service_percentile" },
{
value: "service_mq_consume_latency",
label: "service_mq_consume_latency",
},
{ value: "service_mq_consume_count", label: "service_mq_consume_count" },
];
const configHeight = document.documentElement.clientHeight - 520;
function updateWidgetOptions(param: { label: string; value: string }) {
if (widgetOpt[param.label] === undefined) {
return;
}
widgetOpt[param.label] = param.value;
}
async function queryMetrics() {
const json = await dashboardStore.fetchMetricValue(
dashboardStore.selectedGrid
);
if (json.error) {
return;
}
const metricVal = json.data.readMetricsValues.values.values.map(
(d: { value: number }) => d.value
);
const m =
dashboardStore.selectedGrid.metrics &&
dashboardStore.selectedGrid.metrics[0];
if (!m) {
return;
}
states.source = {
[m]: metricVal,
};
}
queryMetrics();
function applyConfig() {
const opts = {
...dashboardStore.selectedGrid,
metrics: states.metrics,
queryMetricType: states.valueType,
chart: states.chartType,
widget: {
...dashboardStore.selectedGrid.widget,
title: widgetOpt.title,
tips: widgetOpt.tips,
},
graph: {
...dashboardStore.selectedGrid.graph,
},
standard: {
...dashboardStore.selectedGrid.standard,
},
};
dashboardStore.setConfigs(opts);
dashboardStore.setConfigPanel(false);
}
return {
...toRefs(widgetOpt),
...toRefs(states),
changeChartType,
changeValueType,
changeMetrics,
t,
appStoreWithOut,
ChartTypes,
metricOpts,
updateWidgetOptions,
configHeight,
dashboardStore,
applyConfig,
loading,
};
},
});
</script>
<style lang="scss" scoped>
.widget-config {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
}
.graph {
position: relative;
min-width: 1280px;
border: 1px solid #eee;
background-color: #fff;
}
.header {
height: 25px;
line-height: 25px;
text-align: center;
background-color: aliceblue;
font-size: 12px;
}
.render-chart {
padding: 5px;
height: 350px;
width: 100%;
}
.selectors {
width: 500px;
margin-right: 10px;
}
.el-collapse-item__header {
font-weight: bold;
}
.config {
min-width: 1280px;
}
.chart-types {
span {
display: inline-block;
padding: 5px 10px;
border: 1px solid #ccc;
background-color: #fff;
border-right: 0;
cursor: pointer;
}
span:nth-last-child(1) {
border-right: 1px solid #ccc;
}
}
span.active {
background-color: #409eff;
color: #fff;
}
.footer {
position: fixed;
bottom: 0;
right: 0;
border-top: 1px solid #eee;
padding: 10px;
text-align: right;
width: 100%;
background-color: #fff;
}
.collapse {
margin-top: 10px;
overflow: auto;
}
</style>