monitoredAction.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import {ActionTypes} from 'containers/View/constants'
  2. import {
  3. LOGIN,
  4. LOGGED,
  5. DOWNLOAD_FILE
  6. } from 'containers/App/constants'
  7. import { ActionTypes as DashboardActionTypes } from 'containers/Dashboard/constants'
  8. import { IDataDownloadStatistic } from 'app/containers/Dashboard/types'
  9. import {uuid} from 'utils/util'
  10. interface IDownloadFields {
  11. action: 'download'
  12. email: string
  13. org_id: number
  14. project_id: string
  15. project_name: string
  16. sub_viz_id: number
  17. sub_viz_name: string
  18. user_id: number
  19. viz_id: number
  20. viz_name: string
  21. viz_type: 'dashboard' | 'display'
  22. task_id: number // 当下载一个dashboard时候, 其下每个widget的单条数据 task_id是一致的
  23. task_type: 'dashboard' | 'widget'
  24. widget_id: number
  25. widget_name: string
  26. dashboard_rel_widget: number
  27. groups: string[]
  28. filters: object[] // 按重构之后的对象数组记录
  29. variables: string[]
  30. create_time: string
  31. }
  32. const {
  33. LOAD_VIEW_DATA_FROM_VIZ_ITEM,
  34. LOAD_VIEW_DATA_FROM_VIZ_ITEM_SUCCESS
  35. } = ActionTypes
  36. import { statistic, IOperation } from './statistic.dv'
  37. const dataAction = {
  38. [DashboardActionTypes.DRILL_DASHBOARDITEM]: 'drill',
  39. [DashboardActionTypes.DELETE_DRILL_HISTORY]: 'drill',
  40. [DashboardActionTypes.MONITORED_SYNC_DATA_ACTION]: 'sync',
  41. [DashboardActionTypes.MONITORED_SEARCH_DATA_ACTION]: 'search',
  42. [DashboardActionTypes.MONITORED_LINKAGE_DATA_ACTION]: 'linkage'
  43. }
  44. const otherAction = {
  45. [LOGGED]: 'login',
  46. [DashboardActionTypes.INITIATE_DOWNLOAD_TASK_SUCCESS]: 'download'
  47. }
  48. export const monitoreAction = (action: {type: string, payload: object}) => {
  49. const actionType = mapMonitoreToAction(action, statistic.getRecord('operation')['action'])
  50. }
  51. const reportAction = ['initial', 'drill', 'sync', 'search', 'linkage']
  52. function getWidgetDetailFieldsbyDownload (action) {
  53. const taskType = action.payload.type
  54. const taskId = uuid(8, 16)
  55. const downloadDetails: IDownloadFields[] = action.statistic && action.statistic.length ? action.statistic.map((statistic) => {
  56. const { widget: {id, name}, itemId, param } = statistic as IDataDownloadStatistic
  57. const { filters, params, groups } = param
  58. return {
  59. widget_id: id,
  60. widget_name: name,
  61. variables: params,
  62. groups,
  63. filters,
  64. dashboard_rel_widget: itemId,
  65. task_type: taskType,
  66. task_id: taskId
  67. }
  68. }) : []
  69. return downloadDetails
  70. }
  71. function getWidgetDetailFieldsByOthers (action) {
  72. if (action && action.statistic) {
  73. const { groups, filters, variables, tempFilters, linkageFilters, tempVariables,
  74. globalVariables, linkageVariables, globalFilters, widget: {id, name}} = action.statistic
  75. let bootstrapFilters = [...filters]
  76. let bootstrapVariables = [...variables]
  77. if (tempFilters && tempFilters.length) {
  78. bootstrapFilters = filters.concat(tempFilters)
  79. }
  80. if (linkageFilters && linkageFilters.length) {
  81. bootstrapFilters = bootstrapFilters.concat(linkageFilters)
  82. }
  83. if (globalFilters && globalFilters.length) {
  84. bootstrapFilters = bootstrapFilters.concat(globalFilters)
  85. }
  86. // 全局 组件 联动 变量
  87. if (linkageVariables && linkageVariables.length) {
  88. bootstrapVariables = bootstrapVariables.concat(linkageVariables)
  89. }
  90. if (globalVariables && globalVariables.length) {
  91. bootstrapVariables = bootstrapVariables.concat(globalVariables)
  92. }
  93. if (tempVariables && tempVariables.length) {
  94. bootstrapVariables = bootstrapVariables.concat(tempVariables)
  95. }
  96. return {
  97. widget_id: id,
  98. widget_name: name,
  99. variables: bootstrapVariables,
  100. groups,
  101. filters: bootstrapFilters
  102. }
  103. }
  104. return false
  105. }
  106. function mapMonitoreToAction (action: {type: string, payload: object}, initialType: string) {
  107. let actionType = initialType
  108. const isOtherAction = Object.entries(otherAction).map(([k, v]) => k).some((other) => other === action.type)
  109. const isDataAction = Object.entries(dataAction).map(([k, v]) => k).some((other) => other === action.type)
  110. if (isOtherAction) {
  111. actionType = otherAction[action.type]
  112. // 避免与initial混淆,此三种状态不update operationRecord 的action值
  113. if (actionType === 'download') {
  114. const widgetDetailFields = getWidgetDetailFieldsbyDownload(action)
  115. const newData = widgetDetailFields.map((widget, index) => ({
  116. ...widget,
  117. ...statistic.operationRecord,
  118. action: actionType,
  119. create_time: statistic.getCurrentDateTime()
  120. }))
  121. statistic.sendOperation(newData)
  122. }
  123. }
  124. if (isDataAction) {
  125. actionType = dataAction[action.type]
  126. // change action type
  127. statistic.updateSingleFleld<IOperation>('operation', 'action', actionType)
  128. }
  129. if (action.type === LOAD_VIEW_DATA_FROM_VIZ_ITEM_SUCCESS ||
  130. action.type === DashboardActionTypes.LOAD_DASHBOARD_ITEM_DATA_SUCCESS) {
  131. // todo 重启定时器
  132. statistic.isResetTime()
  133. const widgetDetailFields = getWidgetDetailFieldsByOthers(action)
  134. if (widgetDetailFields) {
  135. const newData = {
  136. ...widgetDetailFields,
  137. ...statistic.operationRecord,
  138. ...statistic.userData,
  139. create_time: statistic.getCurrentDateTime()
  140. }
  141. if (reportAction.some((report) => report === actionType)) {
  142. statistic.sendOperation(newData)
  143. }
  144. }
  145. }
  146. }