index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <wrapper
  3. fill
  4. margin
  5. padding
  6. background
  7. >
  8. <platform-page
  9. class="l-flex__fill"
  10. @change="onTenantChanged"
  11. >
  12. <schema-table
  13. ref="table"
  14. :schema="schema"
  15. />
  16. </platform-page>
  17. <confirm-dialog
  18. ref="editDialog"
  19. title="日志抓取配置"
  20. @confirm="onConfirm"
  21. >
  22. <div class="c-sibling-item--v c-grid-form u-align-self--center">
  23. <div class="c-grid-form__row u-font-size--xs u-bold">
  24. <button
  25. class="o-button"
  26. @click="showDocument"
  27. >
  28. 抓取说明
  29. </button>
  30. </div>
  31. <div class="c-grid-form__row u-font-size--xs u-bold">当前状态:{{ logSetting.activate ? '已下发配置' : '未下发配置' }}</div>
  32. <span class="c-grid-form__label u-required">抓取时长</span>
  33. <div class="l-flex--row">
  34. <el-input-number
  35. v-model="logSetting.duration"
  36. controls-position="right"
  37. :min="60"
  38. :max="86400"
  39. step-strictly
  40. />&nbsp;秒
  41. </div>
  42. <span class="c-grid-form__label u-required">抓取指令</span>
  43. <div
  44. class="has-info"
  45. data-info="多条指令以分号分隔"
  46. >
  47. <el-input
  48. v-model="logSetting.commands"
  49. type="textarea"
  50. :rows="5"
  51. />
  52. </div>
  53. <span class="c-grid-form__label u-required">是否重启</span>
  54. <el-switch
  55. v-model="logSetting.reboot"
  56. class="c-grid-form__option"
  57. active-color="#13ce66"
  58. inactive-color="#ff4949"
  59. />
  60. </div>
  61. <template #footer="{ confirm, cancel }">
  62. <button
  63. class="c-sibling-item o-button"
  64. @click="confirm"
  65. >
  66. 开启抓取
  67. </button>
  68. <button
  69. class="c-sibling-item o-button o-button--cancel"
  70. @click="cancel"
  71. >
  72. 取消
  73. </button>
  74. </template>
  75. </confirm-dialog>
  76. <table-dialog
  77. ref="resultDialog"
  78. size="lg"
  79. title="抓取结果"
  80. :schema="resultSchema"
  81. />
  82. <table-dialog
  83. ref="heartbeatDialog"
  84. size="lg fixed"
  85. :title="title"
  86. :schema="heartbeatSchema"
  87. />
  88. </wrapper>
  89. </template>
  90. <script>
  91. import { parseTime } from '@/utils'
  92. import { getAssetUrl } from '@/api/asset'
  93. import { getDevicesByTenant } from '@/api/device'
  94. import {
  95. getRemoteLogConfig,
  96. startRemoteLog,
  97. stopRemoteLog,
  98. getRemoteLogs,
  99. getHeartbeats
  100. } from './api'
  101. const defaultLogSettingForm = {
  102. activate: false,
  103. duration: 60,
  104. commands: 'logcat -vtime | grep -i -E "MqttClient|PushCallback|MqttService"',
  105. reboot: false
  106. }
  107. export default {
  108. name: 'DeviceLog',
  109. data () {
  110. return {
  111. logSetting: {},
  112. schema: {
  113. autoRefreshEachPage: true,
  114. props: {
  115. size: 'small'
  116. },
  117. list: this.getDevicesByTenant,
  118. buttons: [
  119. { label: '未入库设备心跳记录', on: this.onHeartbeats }
  120. ],
  121. filters: [
  122. { key: 'serialNumber', type: 'search', placeholder: '序列号' },
  123. { key: 'mac', type: 'search', placeholder: 'MAC' },
  124. { key: 'name', type: 'search', placeholder: '名称' },
  125. { type: 'refresh' }
  126. ],
  127. cols: [
  128. { prop: 'name', label: '名称', 'min-width': 120 },
  129. { prop: 'serialNumber', label: '序列号', 'min-width': 80 },
  130. { prop: 'mac', label: 'MAC', 'min-width': 80 },
  131. { prop: 'remark', label: '型号', width: 100 },
  132. { type: 'tag', render: ({ activate, onlineStatus }) => activate
  133. ? onlineStatus === 0
  134. ? { type: 'primary', label: '待接入' }
  135. : onlineStatus === 1
  136. ? { type: 'success', label: '在线' }
  137. : { type: 'danger', label: '离线' }
  138. : { type: 'warning', label: '未激活' }, 'size': 'sm', width: 80 },
  139. { type: 'invoke', render: [
  140. { label: '开始抓取', on: this.onStart },
  141. { label: '停止抓取', on: this.onStop },
  142. { label: '抓取结果', on: this.onResult },
  143. { label: '心跳记录', on: this.onHeartbeat }
  144. ], width: 300 }
  145. ]
  146. },
  147. curDeviceId: null,
  148. title: '',
  149. sn: ''
  150. }
  151. },
  152. computed: {
  153. heartbeatSchema () {
  154. return {
  155. nonPagination: true,
  156. autoRefresh: true,
  157. list: this.getheartbeatData,
  158. condition: { sn: this.sn },
  159. filters: this.sn
  160. ? null
  161. : [
  162. { key: 'sn', type: 'search', placeholder: '序列号' },
  163. { type: 'refresh' }
  164. ],
  165. cols: this.sn
  166. ? [
  167. { prop: 'sn', label: '序列号' },
  168. { prop: 'mac', label: 'MAC', width: 160 },
  169. { prop: 'ip', label: 'ip', width: 140 },
  170. { prop: 'settingId', label: '当前事件' },
  171. { label: '上报时间', render: ({ timestamp }) => parseTime(timestamp), width: 180 }
  172. ]
  173. : [
  174. { prop: 'sn', label: '序列号' },
  175. { prop: 'mac', label: 'MAC', width: 200 },
  176. { prop: 'ip', label: 'ip', width: 180 },
  177. { label: '上报时间', render: ({ timestamp }) => parseTime(timestamp), width: 180 }
  178. ]
  179. }
  180. },
  181. resultSchema () {
  182. return {
  183. list: getRemoteLogs,
  184. autoRefresh: true,
  185. condition: { deviceId: this.curDeviceId },
  186. cols: [
  187. { prop: 'settingId', label: '事件' },
  188. { label: '执行状态', type: 'tag', render: ({ status }) => {
  189. return {
  190. type: ['warning', 'success', 'danger', 'primary'][status],
  191. label: ['抓取中', '成功', '失败', '取消'][status]
  192. }
  193. } },
  194. { prop: 'message', label: '备注' },
  195. { prop: 'createTime', label: '抓取开始时间', width: 180, align: 'center' },
  196. { prop: 'commandArrayString', label: '抓取命令数组' },
  197. { prop: 'duration', label: '抓取时长(s)', width: 100, align: 'center' },
  198. { type: 'invoke', render: [
  199. { label: '下载日志', allow: ({ status }) => status === 1, on: this.onDownload }
  200. ] }
  201. ]
  202. }
  203. }
  204. },
  205. methods: {
  206. onTenantChanged (tenant) {
  207. this.$tenant = tenant
  208. this.$refs.table?.pageTo(1)
  209. },
  210. getDevicesByTenant (params) {
  211. if (!this.$tenant) {
  212. return Promise.resolve({ data: [] })
  213. }
  214. return getDevicesByTenant(this.$tenant.path, params)
  215. },
  216. getheartbeatData (params) {
  217. if (this.sn) {
  218. return getHeartbeats(params, this.sn).then(({ data }) => {
  219. if (data.length && data[0].timestamp !== this.$heartbeats[0]?.timestamp) {
  220. this.$heartbeats.unshift(data[0])
  221. }
  222. return { data: this.$heartbeats }
  223. })
  224. }
  225. return getHeartbeats(params)
  226. },
  227. onHeartbeats () {
  228. this.title = '未入库设备心跳记录'
  229. this.sn = ''
  230. this.$refs.heartbeatDialog.show()
  231. },
  232. onHeartbeat ({ name, serialNumber }) {
  233. this.title = `${name}心跳记录`
  234. this.sn = serialNumber
  235. this.$heartbeats = []
  236. this.$refs.heartbeatDialog.show()
  237. },
  238. onStop ({ id }) {
  239. stopRemoteLog({
  240. duration: 60,
  241. deviceId: id,
  242. activate: false
  243. })
  244. },
  245. onStart (device) {
  246. getRemoteLogConfig(device.id).then(({ data }) => {
  247. if (data) {
  248. const { activate, duration, commands, reboot } = data
  249. this.logSetting = {
  250. activate,
  251. duration,
  252. commands: commands ? commands.join(';') : [],
  253. reboot
  254. }
  255. } else {
  256. this.logSetting = { ...defaultLogSettingForm }
  257. }
  258. this.curDeviceId = device.id
  259. this.$refs.editDialog.show()
  260. })
  261. },
  262. async onConfirm (done) {
  263. const { duration, commands, reboot } = this.logSetting
  264. if (~commands.indexOf(';')) {
  265. this.$message({
  266. type: 'warning',
  267. message: '请使用英文输入下的【;】符号'
  268. })
  269. return
  270. }
  271. await startRemoteLog({
  272. deviceId: this.curDeviceId,
  273. duration,
  274. activate: true,
  275. commands: commands.split(';'),
  276. reboot
  277. })
  278. done()
  279. },
  280. onResult (device) {
  281. this.$deviceName = device.name
  282. this.curDeviceId = device.id
  283. this.$refs.resultDialog.show()
  284. },
  285. showDocument () {
  286. window.open('https://inspur-rd.feishu.cn/docx/M4oIdhmU1oFyvAxErn3cYv6Cnuf', '_blank')
  287. },
  288. onDownload ({ fileName, createTime, duration }) {
  289. const a = document.createElement('a')
  290. a.style.display = 'none'
  291. a.setAttribute('target', '_blank')
  292. a.setAttribute('download', `${this.$deviceName}-${createTime}-${duration}s日志.zip`)
  293. a.href = getAssetUrl(fileName)
  294. document.body.appendChild(a)
  295. a.click()
  296. document.body.removeChild(a)
  297. }
  298. }
  299. }
  300. </script>