monitor.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import { Card, Input, Radio, Select, Space, Spin, Tag, TimePicker, Tooltip } from "antd";
  2. import React, { useCallback, useEffect, useState } from "react";
  3. import { ColumnHeightOutlined, ColumnWidthOutlined, EyeInvisibleOutlined, EyeOutlined, RedoOutlined } from "@ant-design/icons";
  4. import useEcharts from '@/Hook/useEcharts'
  5. import { useModel } from 'umi'
  6. import { ListHourProps, ListType } from '@/services/adMonitor/adMonitor'
  7. import '../../adMonitor/adMonitorList/table.less'
  8. import moment from "moment";
  9. import TableData from "@/pages/launchSystemNew/components/TableData";
  10. import RuleAccountLog from "@/components/EarlyWarning/ruleAccountLog";
  11. import { LineField } from "@/pages/adMonitor/adMonitorList/config";
  12. import FilterQuery from "@/pages/adMonitor/adMonitorList/components/FilterQuery";
  13. import Details from "./Details";
  14. import { columnsMonitor } from "./tableMonitorConfig";
  15. import { qiliangpaihanghour } from "./config";
  16. interface newListType extends ListType {
  17. totalTimeUnit: 'total' | 'minute' | 'hour' | 'day',
  18. planTimeUnit: 'minute' | 'hour' | 'day'
  19. }
  20. /**
  21. * 今日起量监控
  22. * @param props
  23. * @returns
  24. */
  25. function Monitor(props: { onChange: () => void }) {
  26. const { onChange } = props
  27. const { getCostTrendV3List, getCostTopV3List, getListForHourV3, getAdqAccountList } = useModel('useAdMonitor.useMonitor')
  28. const { getPicherList } = useModel('useOperating.useWxGroupList')
  29. // 变量开始
  30. const [queryForm, setQueryForm] = useState<newListType>({ totalTimeUnit: 'day', planTimeUnit: 'hour', sysUserId: JSON.parse(sessionStorage.getItem('SYSUSERID') || '[]'), pageNum: 1, pageSize: 20 }) // 搜索变量//startTime: moment().format('YYYY-MM-DD'), endTime: moment().format('YYYY-MM-DD'),
  31. const { BarMonitor, LineMonitor } = useEcharts()
  32. const [barDis, setBarDis] = useState<any[]>([])
  33. const [lineDis, setLineDis] = useState<any[]>([])
  34. const [lineTitle, setLineTitle] = useState<string>('广告总消耗趋势')
  35. const [px, setPx] = useState<boolean>(false)//设置顶部图形的排列方式
  36. const [visible, setVisible] = useState<boolean>(false) // 详情弹窗控制
  37. const [aId, setAId] = useState<any>()
  38. const [showEacharts, setShowEacharts] = useState<boolean>(true)
  39. const [queryForHour, setQueryForHour] = useState<ListHourProps>({ pageNum: 1, pageSize: 20 })
  40. const [filterQuery, setFilterQuery] = useState<any>({})
  41. const [logVisible, setLogVisible] = useState<boolean>(false)
  42. const [adgroupId, setAdgroupId] = useState<string>('')
  43. const [adgroupName, setAdgroupName] = useState<string>('')
  44. const [accountIdRule, setAccountIdRule] = useState<string>('')
  45. const [trendColumns, setTrendColumns] = useState<string[]>(['cost'])
  46. const { totalTimeUnit, planTimeUnit, adgroup, accountId, sysUserId, groupAccountIds } = queryForm
  47. const configName = '起量广告排行明细3.0'
  48. const { getGroupList, groupListInit } = useModel('useLaunchAdq.useAdAuthorize')
  49. useEffect(() => {
  50. groupListInit()
  51. }, [])
  52. useEffect(() => {
  53. getList()
  54. }, [queryForHour, filterQuery, queryForm?.sysUserId, queryForm?.accountId, queryForm?.adgroup, queryForm?.groupAccountIds])
  55. const getList = () => {
  56. let message = localStorage.getItem(`myAdMonitorConfig1.0.1_${configName}`)
  57. if (message) {
  58. message = JSON.parse(message)
  59. }
  60. let isAccountId = true
  61. let isAdgroupId = true
  62. let isAdgroupName = true
  63. let columns: string[] = []
  64. if (message && Array.isArray(message)) {
  65. message.forEach((item: { serverIndex: any; dataIndex: string; }) => {
  66. if (!['event'].includes(item.dataIndex)) {
  67. if (item.dataIndex === 'account_id') {
  68. isAccountId = false
  69. } else if (item.dataIndex === 'adgroup_id') {
  70. isAdgroupId = false
  71. } else if (item.dataIndex === 'adgroup_name') {
  72. isAdgroupName = false
  73. }
  74. columns.push(item?.serverIndex || 'adgroup_data.' + item.dataIndex)
  75. }
  76. })
  77. } else {
  78. qiliangpaihanghour.forEach((item: any) => {
  79. item?.data?.forEach((d: { default: any, serverIndex: string, dataIndex: string }) => {
  80. if (d.default && !['event'].includes(d.dataIndex)) {
  81. if (d.dataIndex === 'account_id') {
  82. isAccountId = false
  83. } else if (d.dataIndex === 'adgroup_id') {
  84. isAdgroupId = false
  85. } else if (item.dataIndex === 'adgroup_name') {
  86. isAdgroupName = false
  87. }
  88. columns.push(d?.serverIndex || 'adgroup_data.' + d.dataIndex)
  89. }
  90. })
  91. })
  92. }
  93. if (isAccountId) {
  94. columns.push('adgroup_data.account_id')
  95. }
  96. if (isAdgroupId) {
  97. columns.push('adgroup_data.adgroup_id')
  98. }
  99. if (isAdgroupName) {
  100. columns.push('adgroup_data.adgroup_name')
  101. }
  102. let params = { ...queryForHour, ...filterQuery }
  103. if (queryForm?.sysUserId) {
  104. params.sysUserIds = queryForm?.sysUserId
  105. }
  106. if (queryForm?.accountId && queryForm?.accountId?.length > 0) {
  107. params.accountIdStr = queryForm?.accountId.toString()
  108. }
  109. if (queryForm?.adgroup) {
  110. params.adgroupIdStr = queryForm?.adgroup
  111. }
  112. if (queryForm?.groupAccountIds) {
  113. params.groupAccountIds = queryForm?.groupAccountIds
  114. }
  115. params.columns = columns
  116. getListForHourV3.run(params)
  117. }
  118. // 获取投手
  119. useEffect(() => {
  120. !getPicherList.data && getPicherList.run()
  121. }, [])
  122. // 获取广告账号
  123. useEffect(() => {
  124. !getAdqAccountList.data && getAdqAccountList.run()
  125. }, [])
  126. // // 获取排行数据,柱图
  127. useEffect(() => {
  128. getPlanCostList()
  129. }, [totalTimeUnit, accountId, sysUserId, groupAccountIds])
  130. // 获取今日计划总消耗图谱,折线
  131. useEffect(() => {
  132. getTootalCostList()
  133. }, [planTimeUnit, adgroup, accountId, sysUserId, groupAccountIds, trendColumns])
  134. /** 获取折线图 */
  135. const getTootalCostList = useCallback(async () => {
  136. let { totalTimeUnit, planTimeUnit, pageNum, pageSize, adgroup, sysUserId, accountId, ...newQueryForm } = queryForm
  137. let params = adgroup ? { ...newQueryForm, adgroupIdStr: adgroup } : newQueryForm
  138. let res = await getCostTrendV3List.run({ ...params, timeUnit: planTimeUnit, sysUserIds: sysUserId, accountIdStr: accountId?.join(), trendColumns })
  139. let data = trendColumns.map((field) => {
  140. let value: any = {}
  141. res?.data?.forEach((item: any, index: number) => {
  142. if (index === 0) value.legendName = LineField[field];
  143. value[item?.trend_unit] = item?.[field]
  144. });
  145. return value
  146. })
  147. setLineDis(() => data)
  148. setLineTitle(() => `广告总${LineField[trendColumns[0]]}趋势`)
  149. }, [queryForm, lineDis, trendColumns])
  150. /** 获取柱状图 */
  151. const getPlanCostList = useCallback(async () => {
  152. let { totalTimeUnit, planTimeUnit, pageNum, pageSize, sysUserId, accountId, ...newQueryForm } = queryForm
  153. let { adgroup, ...planQueryFrom } = newQueryForm
  154. let res = await getCostTopV3List.run({ ...planQueryFrom, timeUnit: totalTimeUnit, sysUserIds: sysUserId, accountId: accountId?.join(), topN: 10 })
  155. let data = res?.data?.map((item: { adgroupId: number, cost: number, adgroupName: string, accountId: number }) => {
  156. return { name: item.adgroupId.toString(), value: item.cost, adName: item.adgroupName, accountId: item.accountId }
  157. })
  158. data = data?.sort((a: any, b: any) => {
  159. var value1 = a['value'];
  160. var value2 = b['value'];
  161. return value2 - value1;
  162. })
  163. setBarDis(() => data)
  164. }, [queryForm, barDis])
  165. // 详情
  166. const details = (data: any) => {
  167. setAId(data)
  168. setVisible(true)
  169. }
  170. // 计划详情
  171. const planDetail = (data: any) => {
  172. sessionStorage.setItem('ADIDORNAME', JSON.stringify(data))
  173. onChange && onChange()
  174. }
  175. //全部接口刷新
  176. const refresh = () => {
  177. getCostTrendV3List.refresh()
  178. getCostTopV3List.refresh()
  179. getListForHourV3.refresh()
  180. }
  181. // 接口自动刷新10分钟一次
  182. useEffect(() => {
  183. let time = setInterval(() => {
  184. refresh()
  185. }, 1000 * 60 * 10)
  186. return () => {
  187. clearInterval(time)
  188. }
  189. }, [])
  190. //图形排列样式改变重新获取数据刷新图形
  191. const set = useCallback((b) => {
  192. setPx(b)
  193. getCostTrendV3List.refresh()
  194. getCostTopV3List.refresh()
  195. }, [getCostTopV3List, getCostTrendV3List, trendColumns])
  196. // 处理折线图数据
  197. const timePickerHandle = async (values: any, formatString: [string, string]) => {
  198. let res = await getCostTrendV3List.data
  199. let data = trendColumns.map((field) => {
  200. let value: any = {}
  201. res?.data?.forEach((item: any, index: number) => {
  202. if (index === 0) value.legendName = LineField[field];
  203. value[item?.trend_unit] = item?.[field]
  204. });
  205. return value
  206. })
  207. if (values) {
  208. let date = moment().format('YYYY-MM-DD')
  209. let strTime = formatString[0]
  210. let endTime = formatString[1]
  211. let dateTimeStr = `${date} ${strTime}:00`
  212. let dateTimeEnd = `${date} ${endTime}:00`
  213. let newData: any[] = data.map(item => {
  214. let { legendName, ...otherData } = item
  215. let newItem: any = { legendName }
  216. for (const key in otherData) {
  217. if (Object.prototype.hasOwnProperty.call(otherData, key)) {
  218. const value = otherData[key];
  219. if (moment(dateTimeStr) <= moment(key) && moment(key) <= moment(dateTimeEnd)) {
  220. newItem[key] = value
  221. }
  222. }
  223. }
  224. return newItem
  225. })
  226. setLineDis(() => newData)
  227. } else {
  228. setLineDis(() => data)
  229. }
  230. }
  231. const log = (value: any) => {
  232. setAccountIdRule(value.account_id)
  233. setAdgroupId(value.adgroup_id)
  234. setAdgroupName(value.adgroup_name)
  235. setLogVisible(true)
  236. }
  237. return <Space direction='vertical' style={{ width: '100%' }} className="monitor">
  238. <Card hoverable bodyStyle={{ padding: '12px 16px' }}>
  239. <div style={{ display: 'flex', justifyContent: 'space-between' }}>
  240. <Space>
  241. <Select
  242. showSearch
  243. value={queryForm.sysUserId}
  244. style={{ minWidth: 180, maxWidth: 250 }}
  245. mode='multiple'
  246. maxTagCount={1}
  247. allowClear
  248. placeholder="请选择投手"
  249. disabled={queryForm?.adgroup || queryForm?.accountId?.length > 0}
  250. onChange={(value: number[]) => {
  251. setQueryForm({ ...queryForm, sysUserId: value, pageNum: 1 })
  252. sessionStorage.setItem('SYSUSERID', value ? JSON.stringify(value) : '')
  253. }}
  254. filterOption={(input, option) =>
  255. (option?.children as any).toLowerCase().indexOf(input.toLowerCase()) >= 0
  256. }
  257. >
  258. {getPicherList?.data?.map((item: { nickname: string, userId: number }, index: number) =>
  259. <Select.Option
  260. value={item.userId}
  261. key={item.userId + '' + index}
  262. >
  263. {item.nickname}
  264. </Select.Option>
  265. )}
  266. </Select>
  267. <Select
  268. showSearch
  269. mode='multiple'
  270. maxTagCount={1}
  271. value={queryForm.accountId}
  272. style={{ minWidth: 220, maxWidth: 250 }}
  273. allowClear
  274. placeholder="请选择广告账号"
  275. onChange={(value: number[]) => {
  276. setQueryForm({ ...queryForm, accountId: value, pageNum: 1 })
  277. }}
  278. >
  279. {getAdqAccountList?.data?.data?.map((item: { id: number, accountId: number }) => <Select.Option
  280. value={item.accountId}
  281. key={item.id}
  282. >
  283. {item.accountId}
  284. </Select.Option>)}
  285. </Select>
  286. <Input
  287. value={queryForm?.accountId?.toString()}
  288. placeholder="输入广告账号"
  289. onChange={(e) => {
  290. setQueryForm({ ...queryForm, accountId: e.target.value ? e.target.value?.split(/[,,\s\n]+/) : undefined })
  291. }}
  292. allowClear
  293. />
  294. <Input
  295. value={queryForm.adgroup}
  296. placeholder="广告ID"
  297. onChange={(e) => {
  298. setQueryForm({ ...queryForm, adgroup: e.target.value })
  299. }}
  300. allowClear
  301. />
  302. <Select
  303. showSearch
  304. mode='multiple'
  305. maxTagCount={1}
  306. value={queryForm.groupAccountIds}
  307. style={{ minWidth: 150 }}
  308. allowClear
  309. placeholder="请选择账号分组"
  310. onChange={(value: number[]) => {
  311. setQueryForm({ ...queryForm, groupAccountIds: value, pageNum: 1 })
  312. }}
  313. >
  314. {getGroupList?.data?.map((item: { groupId: number, groupName: number }) => <Select.Option
  315. value={item.groupId}
  316. key={item.groupId}
  317. >
  318. {item.groupName}
  319. </Select.Option>)}
  320. </Select>
  321. </Space>
  322. <Space>
  323. <Tag onClick={() => setShowEacharts(!showEacharts)}>{showEacharts ? <><EyeInvisibleOutlined /> 隐藏</> : <><EyeOutlined /> 显示</>}</Tag>
  324. <Tag color="#2db7f5" onClick={refresh}><RedoOutlined /> 刷新</Tag>
  325. </Space>
  326. </div>
  327. </Card>
  328. {showEacharts && <Card hoverable bodyStyle={{ padding: '12px 16px' }}>
  329. <span style={{ position: 'absolute', top: 10, zIndex: 10 }}>
  330. {px ?
  331. <Tooltip title='左右排列'><Tag color='#f50' onClick={() => { set(false) }}><ColumnWidthOutlined /><span style={{ fontSize: 10 }}>左右排列</span></Tag></Tooltip>
  332. :
  333. <Tooltip title='上下排列'><Tag color='#f50' onClick={() => { set(true) }} ><ColumnHeightOutlined /><span style={{ fontSize: 10 }}>上下排列</span></Tag></Tooltip>
  334. }
  335. </span>
  336. <div className={!px ? 'charts' : 'charts charts100'}>
  337. <div>
  338. <div className="selectTime">
  339. <Space>
  340. <span style={{ fontSize: 10, color: '#999' }}>刷新时间:{getCostTopV3List?.data?.reqTime}</span>
  341. <Radio.Group value={queryForm.totalTimeUnit} buttonStyle="solid" size='small' onChange={(e) => { setQueryForm({ ...queryForm, totalTimeUnit: e.target.value }) }}>
  342. <Radio.Button value="total">总</Radio.Button>
  343. <Radio.Button value="day">天</Radio.Button>
  344. <Radio.Button value="hour">小时</Radio.Button>
  345. <Radio.Button value="minute">5min</Radio.Button>
  346. </Radio.Group>
  347. </Space>
  348. </div>
  349. {getCostTopV3List?.loading ? <Spin /> : <BarMonitor style={{ width: '100%', height: '100%' }} data={barDis} xName="今日消耗" yName="广告名称" onChange={(value: string, accountId) => { console.log(value, accountId); setQueryForm({ ...queryForm, adgroup: value }) }} planID={queryForm?.adgroup} />}
  350. </div>
  351. <div>
  352. <div className="selectTime">
  353. <Space>
  354. <span style={{ fontSize: 10, color: '#999' }}>刷新时间:{getCostTrendV3List?.data?.reqTime}</span>
  355. <Radio.Group value={queryForm.planTimeUnit} buttonStyle="solid" size='small' onChange={(e) => { setQueryForm({ ...queryForm, planTimeUnit: e.target.value }) }}>
  356. <Radio.Button value="hour">小时</Radio.Button>
  357. <Radio.Button value="minute">5min</Radio.Button>
  358. </Radio.Group>
  359. {queryForm.planTimeUnit === 'minute' && <TimePicker.RangePicker
  360. size="small"
  361. format='HH:mm'
  362. minuteStep={5}
  363. onChange={timePickerHandle}
  364. />}
  365. <Select
  366. showSearch
  367. mode='multiple'
  368. maxTagCount={1}
  369. value={trendColumns}
  370. style={{ minWidth: 130 }}
  371. allowClear
  372. placeholder="请选择图表字段"
  373. onChange={(value: string[]) => {
  374. setTrendColumns(value?.length ? value : ['cost'])
  375. }}
  376. size="small"
  377. >
  378. {Object.keys(LineField).map((key) => <Select.Option value={key} key={key}>
  379. {LineField[key]}
  380. </Select.Option>)}
  381. </Select>
  382. </Space>
  383. </div>
  384. {getCostTrendV3List?.loading ? <Spin /> : <LineMonitor style={{ width: '100%', height: '100%' }} series smooth data={lineDis} title={trendColumns?.length > 1 ? undefined : lineTitle} />}
  385. </div>
  386. </div>
  387. </Card>}
  388. <div className={'MYtable'}>
  389. <TableData
  390. refreshData={getList}
  391. bodyStyle={{ padding: '12px 16px' }}
  392. gutter={[0, 12]}
  393. columns={columnsMonitor(planDetail, details, (value) => { setQueryForm({ ...queryForm, adgroup: value }); }, log)}
  394. dataSource={getListForHourV3?.data?.data?.records}
  395. loading={getListForHourV3?.loading}
  396. ajax={getListForHourV3}
  397. leftChild={
  398. <Space>
  399. <FilterQuery onChange={(data) => {
  400. setFilterQuery(data)
  401. }} />
  402. </Space>
  403. }
  404. myKey={'adgroup_id'}
  405. fixed={{ left: 0, right: 2 }}
  406. total={getListForHourV3?.data?.data?.total}
  407. onChange={(props: any) => {
  408. let { sortData, pagination } = props
  409. let { current, pageSize } = pagination
  410. let newQueryForm = JSON.parse(JSON.stringify(queryForHour))
  411. newQueryForm.pageNum = current
  412. newQueryForm.pageSize = pageSize
  413. if (sortData && JSON.stringify('sortData') !== '{}') {
  414. let { field, order } = sortData // descend 降序 大到小 ascend 升序 小到大
  415. if (order) {
  416. newQueryForm.sortColumn = field
  417. newQueryForm.sortAsc = order === 'ascend'
  418. } else {
  419. Object.keys(newQueryForm).forEach(key => {
  420. if (key === 'sortColumn' || key === 'sortAsc') {
  421. delete newQueryForm[key]
  422. }
  423. })
  424. }
  425. } else {
  426. Object.keys(newQueryForm).forEach(key => {
  427. if (key === 'sortField' || key === 'sort') {
  428. delete newQueryForm[key]
  429. }
  430. })
  431. }
  432. setQueryForHour({ ...newQueryForm })
  433. }}
  434. page={queryForHour.pageNum}
  435. pageSize={queryForHour.pageSize}
  436. scroll={{ y: 750 }}
  437. config={qiliangpaihanghour}
  438. configName={configName}
  439. />
  440. </div>
  441. {visible && <Details visible={visible} onClose={() => { setVisible(false) }} data={aId} />}
  442. {logVisible && <RuleAccountLog accountId={accountIdRule} adgroupName={adgroupName} adgroupId={adgroupId} visible={logVisible} onClose={() => setLogVisible(false)} />}
  443. </Space>
  444. }
  445. export default React.memo(Monitor)