index.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <div v-if="!loading">
  3. <div v-if="isEmpty">
  4. <data-empty style="padding-top: 200px;">
  5. <a-button type="primary" @click="handleCreateDashboard">
  6. {{ $t('monitor.dashboard.dialog.create') }}
  7. </a-button>
  8. </data-empty>
  9. </div>
  10. <div v-else>
  11. <a-row :gutter="8">
  12. <a-col :span="10">
  13. <a-row type="flex" :gutter="8" justify="start">
  14. <a-col style="padding-top: 6px;padding-bottom: 6px;">{{ $t('monitor.dashboard.label') + ':' }}</a-col>
  15. <a-col flex="auto">
  16. <base-select
  17. filterable
  18. style="min-width: 150px;"
  19. resource="alertdashboards"
  20. v-model="dashboardId"
  21. :options="dashboards" />
  22. </a-col>
  23. </a-row>
  24. </a-col>
  25. <a-col :span="4">
  26. <a-button type="primary" @click="handleCreateDashboard">
  27. {{ $t('monitor.dashboard.dialog.create') }}
  28. </a-button>
  29. </a-col>
  30. <a-col :span="9" />
  31. <a-col :span="1">
  32. <a-dropdown style="float: right" :trigger="['click']" placement="bottomRight">
  33. <a class="ant-dropdown-link font-weight-bold pl-2 pr-2 h-100 d-block action-btn" @click="e => e.preventDefault()">
  34. <icon type="more" style="font-size: 18px;" />
  35. </a>
  36. <a-menu slot="overlay" @click="handleActionClick">
  37. <a-menu-item key="handleEditName"><a-icon type="edit" />{{$t('monitor.edit_name')}}</a-menu-item>
  38. <a-menu-item key="handleClone"><a-icon type="copy" />{{$t('dashboard.text_107')}}</a-menu-item>
  39. <a-menu-item key="handleDelete"><a-icon type="delete" />{{$t('scope.text_18')}}</a-menu-item>
  40. </a-menu>
  41. </a-dropdown>
  42. </a-col>
  43. </a-row>
  44. <a-row>
  45. <a-divider />
  46. </a-row>
  47. <a-row v-if="dashboardId">
  48. <dashboard-cards ref="dashboardCards" :id="dashboardId" :extraParams="extraParams" :create-chart="createChart" @adjustChartOrder="adjustChartOrder" :edit-chart="editChart" />
  49. </a-row>
  50. </div>
  51. </div>
  52. </template>
  53. <script>
  54. import i18n from '@/locales'
  55. import storage from '@/utils/storage'
  56. import DialogMixin from '@/mixins/dialog'
  57. import WindowsMixin from '@/mixins/windows'
  58. import DashboardCards from '@Monitor/components/MonitorCard/DashboardCards'
  59. import { getNameDescriptionTableColumn } from '@/utils/common/tableColumn'
  60. const alertDashboardScopeColumn = {
  61. field: 'scope',
  62. title: i18n.t('IAM.text_1'),
  63. formatter: ({ row }) => {
  64. const data = row
  65. let desc = '-'
  66. if (data.scope === 'system') {
  67. desc = i18n.t('monitor.dashboard.select.option', [i18n.t('shareScope.system')])
  68. }
  69. if (data.scope === 'domain') {
  70. desc = i18n.t('monitor.dashboard.select.option', [data.project_domain, i18n.t('cloudenv.text_393')])
  71. }
  72. if (data.scope === 'project') {
  73. desc = i18n.t('monitor.dashboard.select.option', [data.project, i18n.t('cloudenv.text_254')])
  74. }
  75. return desc
  76. },
  77. }
  78. export default {
  79. name: 'DashboardIndex',
  80. components: {
  81. DashboardCards,
  82. },
  83. mixins: [DialogMixin, WindowsMixin],
  84. data () {
  85. const dashboardId = this.$route.query.dashboard_id || ''
  86. return {
  87. scope: this.$store.getters.scope,
  88. loading: true,
  89. manager: new this.$Manager('alertdashboards', 'v1'),
  90. dashboards: [],
  91. dashboardId: dashboardId,
  92. }
  93. },
  94. computed: {
  95. isEmpty () {
  96. return !this.dashboards || this.dashboards.length === 0
  97. },
  98. dashboard () {
  99. if (this.dashboardId) {
  100. const ds = this.dashboards.filter((item) => { return item.id === this.dashboardId })
  101. if (ds.length > 0) {
  102. return ds[0]
  103. }
  104. }
  105. return {}
  106. },
  107. extraParams () {
  108. const scope = this.scope
  109. const params = { scope: scope }
  110. if (this.dashboard.domain_id) {
  111. params.domain_id = this.dashboard.domain_id
  112. }
  113. if (this.dashboard.tenant_id) {
  114. params.project_id = this.dashboard.tenant_id
  115. }
  116. return params
  117. },
  118. },
  119. watch: {
  120. dashboardId: {
  121. immediate: true,
  122. handler: function (val) {
  123. if (val) {
  124. // 记录到本地
  125. this.setMonitorLocal({ monitorDashboardId: val })
  126. }
  127. },
  128. },
  129. },
  130. created () {
  131. this.dashboardId ? this.fetchDashboards() : this.switchDashboard(false)
  132. },
  133. methods: {
  134. getMonitorConfig () {
  135. return storage.get('__oc_monitor_query_config__', {})
  136. },
  137. setMonitorLocal (config) {
  138. const monitorConfig = this.getMonitorConfig()
  139. storage.set('__oc_monitor_query_config__', {
  140. ...monitorConfig,
  141. ...config,
  142. })
  143. },
  144. handleCreateDashboard () {
  145. this.createDialog('CreateMonitorDashboard', {
  146. refresh: this.switchDashboard,
  147. })
  148. },
  149. adjustChartOrder (dashboard) {
  150. this.createDialog('MonitorDashboardAdjustOrderDialog', {
  151. dashboard: dashboard,
  152. data: this.dashboards.filter((item) => { return item.id === dashboard.id }),
  153. columns: [getNameDescriptionTableColumn(), alertDashboardScopeColumn],
  154. ok: (panels) => {
  155. const index = this.dashboards.findIndex((item) => { return item.id === dashboard.id })
  156. if (index > -1) {
  157. this.dashboards[index].alert_panel_details = panels
  158. this.$refs.dashboardCards.changePanelsOrder(panels)
  159. } else {
  160. this.fetchDashboards()
  161. }
  162. },
  163. })
  164. },
  165. handleActionClick ({ key }) {
  166. if (this[key]) this[key]()
  167. },
  168. handleEditName () {
  169. const index = this.dashboards.findIndex((item) => { return item.id === this.dashboardId })
  170. this.createDialog('MonitorDashboardChangeName', {
  171. data: [this.dashboards[index]],
  172. columns: [getNameDescriptionTableColumn(), alertDashboardScopeColumn],
  173. ok: (name) => {
  174. this.dashboards = this.dashboards.map((item, idx) => {
  175. if (idx === index) item.name = name
  176. return item
  177. })
  178. },
  179. })
  180. },
  181. handleClone () {
  182. this.createDialog('CloneMonitorDashboard', {
  183. data: this.dashboards.filter((item) => { return item.id === this.dashboardId }),
  184. refresh: this.switchDashboard,
  185. columns: [getNameDescriptionTableColumn(), alertDashboardScopeColumn],
  186. })
  187. },
  188. handleDelete () {
  189. this.createDialog('DeleteMonitorDashboard', {
  190. data: this.dashboards.filter((item) => { return item.id === this.dashboardId }),
  191. refresh: this.switchDashboard,
  192. columns: [getNameDescriptionTableColumn(), alertDashboardScopeColumn],
  193. })
  194. },
  195. createChart () {
  196. this.$router.push({
  197. name: 'MonitorDashboardChartCreate',
  198. params: {
  199. dashboard: this.dashboardId,
  200. ...this.extraParams,
  201. },
  202. })
  203. },
  204. editChart ({ id, time, timeGroup, customTime, groupFunc }) {
  205. if (!id) return
  206. this.$router.push({
  207. name: 'MonitorDashboardChartUpdate',
  208. params: {
  209. id,
  210. time,
  211. timeGroup,
  212. customTime,
  213. groupFunc,
  214. dashboard: this.dashboardId,
  215. ...this.extraParams,
  216. },
  217. })
  218. },
  219. switchDashboard (ignoreLocal = true) {
  220. this.fetchDashboards((data) => {
  221. // 使用本地保存的
  222. const monitorConfig = this.getMonitorConfig()
  223. if (monitorConfig.monitorDashboardId && !ignoreLocal && data.some(item => item.id === monitorConfig.monitorDashboardId)) {
  224. this.dashboardId = monitorConfig.monitorDashboardId
  225. } else {
  226. this.dashboardId = data.length ? data[0].id : ''
  227. }
  228. })
  229. },
  230. async fetchDashboards (callback) {
  231. this.loading = true
  232. this.dashboards = []
  233. try {
  234. const params = {
  235. scope: this.scope,
  236. }
  237. const { data: { data } } = await this.manager.list({ params })
  238. this.dashboards = data
  239. if (callback && typeof callback === 'function') {
  240. callback(data)
  241. }
  242. this.loading = false
  243. } catch (error) {
  244. throw error
  245. } finally {
  246. this.loading = false
  247. }
  248. },
  249. },
  250. }
  251. </script>