index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <div :class="card_style">
  3. <div v-if="!readOnly" class="d-flex align-items-center justify-content-between">
  4. <monitor-header
  5. :time.sync="time"
  6. :timeGroup.sync="timeGroup"
  7. :showTimegroup="true"
  8. :showGroupFunc="true"
  9. :groupFunc.sync="groupFunc"
  10. :customTime.sync="customTime"
  11. :showCustomTimeText="time==='custom'"
  12. :showAutoRefresh="!useLocalPanels"
  13. customTimeUseTimeStamp
  14. @refresh="handleRefresh" />
  15. <template v-if="!useLocalPanels">
  16. <a-dropdown-button
  17. v-if="!readOnly && panels.length > 1"
  18. :title="$t('monitor.dashboard.dialog.project.create')"
  19. class="text-truncate"
  20. @click="createChart"
  21. placement="topLeft">
  22. <a-icon type="plus-circle" />
  23. {{ $t('monitor.dashboard.dialog.project.create') }}
  24. <a-menu slot="overlay" @click="handleMenuClick">
  25. <a-menu-item key="adjust_order">
  26. {{ $t('monitor.adjust_chart_order') }}
  27. </a-menu-item>
  28. </a-menu>
  29. <a-icon slot="icon" type="down" />
  30. </a-dropdown-button>
  31. <a-button v-else style="margin-left: 8px;" icon="plus-circle" @click="createChart">
  32. {{ $t('monitor.dashboard.dialog.project.create')}}
  33. </a-button>
  34. </template>
  35. </div>
  36. <div :class="card_style" :style="readOnly && !selectable ? '' :'padding-top: 20px;'">
  37. <dashboard-card ref="dashboardCard" v-if="readOnly && !selectable" :card_style="card_style" :chartHeigth="chartHeigth" @chose_panel="chose_panel" :panel="panels.length > 0 ? panels[0] : {}" :focusPanelId="focusPanelId" :selectable="selectable" :readOnly="readOnly" :dashboard_id="id" :edit-chart="handleEditChart" :updated_at="updatedAt" :extraParams="extraParams" @delete="handleDelete" />
  38. <template v-else>
  39. <div v-for="(item, index) in panels" :key="index">
  40. <dashboard-card
  41. :card_style="card_style"
  42. :chartHeigth="chartHeigth"
  43. :panel="item"
  44. :focusPanelId="focusPanelId"
  45. :selectable="selectable"
  46. :readOnly="readOnly"
  47. :dashboard_id="id"
  48. :edit-chart="handleEditChart"
  49. :updated_at="updatedAt"
  50. :extraParams="extraParams"
  51. :time="time"
  52. :timeGroup="timeGroup"
  53. :customTime="customTime"
  54. :groupFunc="groupFunc"
  55. :tablePageSize="tablePageSize"
  56. :useLocalPanels="useLocalPanels"
  57. :cursorMove="point => cursorMove(index, point)"
  58. :otherCursorMovePoint="cursorMoveIndex === index ? [-10, -10] : cursorMovePoint"
  59. @pageChange="(pager) => pageChange(item, pager)"
  60. @chose_panel="chose_panel"
  61. @delete="handleDelete" />
  62. </div>
  63. </template>
  64. <!-- <a-list v-else :grid="{ column: 1 }" :data-source="panels">
  65. <a-list-item slot="renderItem" slot-scope="item" className="owner-item">
  66. <dashboard-card
  67. :card_style="card_style"
  68. :chartHeigth="chartHeigth"
  69. :panel="item"
  70. :focusPanelId="focusPanelId"
  71. :selectable="selectable"
  72. :readOnly="readOnly"
  73. :dashboard_id="id"
  74. :edit-chart="editChart"
  75. :updated_at="updatedAt"
  76. :extraParams="extraParams"
  77. :time="time"
  78. :timeGroup="timeGroup"
  79. :customTime="customTime"
  80. :groupFunc="groupFunc"
  81. :tablePageSize="tablePageSize"
  82. @pageChange="(pager) => pageChange(item, pager)"
  83. @chose_panel="chose_panel"
  84. @delete="handleDelete" />
  85. </a-list-item>
  86. </a-list> -->
  87. </div>
  88. </div>
  89. </template>
  90. <script>
  91. import { uuid } from '@/utils/utils'
  92. import MonitorHeader from '@/sections/Monitor/Header'
  93. import MonitorTimeMixin from '@/mixins/monitorTime'
  94. import DashboardCard from '../DashboardCard'
  95. export default {
  96. name: 'DashboardCards',
  97. components: {
  98. DashboardCard,
  99. MonitorHeader,
  100. },
  101. mixins: [MonitorTimeMixin],
  102. props: {
  103. id: {
  104. type: String,
  105. default: '',
  106. },
  107. createChart: {
  108. type: Function,
  109. required: true,
  110. },
  111. adjustChartOrder: {
  112. type: Function,
  113. required: true,
  114. },
  115. editChart: {
  116. type: Function,
  117. required: true,
  118. },
  119. extraParams: {
  120. type: Object,
  121. default: () => ({}),
  122. },
  123. readOnly: {
  124. type: Boolean,
  125. default: () => {
  126. return false
  127. },
  128. },
  129. selectable: {
  130. type: Boolean,
  131. default: () => {
  132. return false
  133. },
  134. },
  135. focusPanelId: {
  136. type: String,
  137. default: () => {
  138. return ''
  139. },
  140. },
  141. card_style: {
  142. type: String,
  143. },
  144. chartHeigth: {
  145. type: String,
  146. default: '320px',
  147. },
  148. panelId: {
  149. type: String,
  150. },
  151. useLocalPanels: {
  152. type: Boolean,
  153. default: false,
  154. },
  155. localPanels: {
  156. type: Array,
  157. default: () => [],
  158. },
  159. },
  160. data () {
  161. return {
  162. loading: false,
  163. scope: this.$store.getters.scope,
  164. dashboard: {},
  165. panelMan: new this.$Manager('alertpanels', 'v1'),
  166. dashboardMan: new this.$Manager('alertdashboards', 'v1'),
  167. charts: [],
  168. updatedAt: '',
  169. time: '1h',
  170. timeGroup: '1m',
  171. customTime: null,
  172. groupFunc: 'mean',
  173. tablePageSize: 10,
  174. cursorMoveIndex: -1,
  175. cursorMovePoint: [-10, -10],
  176. }
  177. },
  178. computed: {
  179. panels () {
  180. if (this.useLocalPanels) return this.localPanels
  181. if (this.panelId) {
  182. const obj = this.dashboard.alert_panel_details ? this.dashboard.alert_panel_details.find(x => { return x.panel_id === this.panelId }) : {}
  183. return [obj]
  184. } else {
  185. return this.dashboard.alert_panel_details ? this.dashboard.alert_panel_details : []
  186. }
  187. },
  188. },
  189. watch: {
  190. id: {
  191. deep: true,
  192. handler () {
  193. this.fetchCharts()
  194. },
  195. },
  196. time (val) {
  197. this.saveMonitorConfig()
  198. },
  199. timeGroup () {
  200. this.saveMonitorConfig()
  201. },
  202. groupFunc () {
  203. this.saveMonitorConfig()
  204. },
  205. customTime () {
  206. this.saveMonitorConfig()
  207. },
  208. },
  209. mounted () {
  210. this.fetchCharts()
  211. },
  212. methods: {
  213. handleEditChart ({ id }) {
  214. this.$emit('editChart', { id, time: this.time, timeGroup: this.timeGroup, customTime: this.customTime, groupFunc: this.groupFunc })
  215. this.editChart({ id, time: this.time, timeGroup: this.timeGroup, customTime: this.customTime, groupFunc: this.groupFunc })
  216. },
  217. pageChange (panel, pager) {
  218. this.saveMonitorConfig({ tablePageSize: pager.limit })
  219. },
  220. initTablePageSize (size) {
  221. this.tablePageSize = size
  222. },
  223. handleMenuClick () {
  224. this.$emit('adjustChartOrder', this.dashboard)
  225. },
  226. changePanelsOrder (panels) {
  227. this.dashboard.alert_panel_details = [...panels]
  228. },
  229. fetchDataSuccess () {
  230. this.saveMonitorConfig()
  231. },
  232. resize () {
  233. this.$refs.dashboardCard && this.$refs.dashboardCard.resize()
  234. },
  235. chose_panel (obj) {
  236. this.$emit('chose_panels', { id: obj.id, name: obj.name })
  237. },
  238. handleRefresh () {
  239. this.fetchCharts(true)
  240. },
  241. handleDelete () {
  242. this.fetchCharts()
  243. },
  244. getFirstDashborad (data) {
  245. for (let i = 0; i < data.length; i++) {
  246. const element = data[i]
  247. if (element.alert_panel_details && element.alert_panel_details.length > 0) {
  248. return element
  249. }
  250. }
  251. },
  252. async fetchCharts (refresh = false) {
  253. this.saveMonitorConfig()
  254. if (this.useLocalPanels && !refresh) return
  255. this.loading = true
  256. try {
  257. const params = {
  258. scope: this.scope,
  259. details: true,
  260. $t: uuid(),
  261. }
  262. if (this.id) {
  263. const { data } = await this.dashboardMan.get({ id: this.id, params })
  264. this.dashboard = data
  265. const first = this.dashboard.alert_panel_details ? this.dashboard.alert_panel_details : []
  266. this.$emit('chose_first_panel', first && first.length > 0 ? first[0].panel_id : '')
  267. } else {
  268. const { data: { data } } = await this.dashboardMan.list({ params })
  269. this.dashboard = this.getFirstDashborad(data)
  270. const first = this.dashboard.alert_panel_details
  271. this.$emit('chose_first_panel', { panel: first[0], dashboardId: this.dashboard })
  272. this.$emit('chose_panels', { id: first[0].panel_id, name: first[0].panel_name, dashboardId: this.dashboard.id })
  273. }
  274. this.updatedAt = new Date().toISOString()
  275. this.loading = false
  276. } catch (error) {
  277. throw error
  278. } finally {
  279. this.loading = false
  280. }
  281. },
  282. cursorMove (index, point) {
  283. this.cursorMoveIndex = index
  284. this.cursorMovePoint = point
  285. },
  286. },
  287. }
  288. </script>
  289. <style scoped>
  290. .owner-item {
  291. margin: 0;
  292. }
  293. </style>