index.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import { Card } from 'antd';
  3. import { useEffect, useRef, useState } from 'react';
  4. import './index.less';
  5. import Service from './service';
  6. import encodeQuery from '@/utils/encodeQuery';
  7. import { useRequest } from 'umi';
  8. import DashBoard, { DashBoardTopCard } from '@/components/DashBoard';
  9. import type { EChartsOption } from 'echarts';
  10. import Echarts from '@/components/DashBoard/echarts';
  11. // import moment from 'moment';
  12. import { AMap } from '@/components';
  13. import { Marker } from 'react-amap';
  14. import { EnvironmentOutlined } from '@ant-design/icons';
  15. import SystemConst from '@/utils/const';
  16. type RefType = {
  17. getValues: Function;
  18. };
  19. const service = new Service('device/instance');
  20. const DeviceBoard = () => {
  21. const [deviceOnline, setDeviceOnline] = useState(0);
  22. const [deviceOffline, setDeviceOffline] = useState(0);
  23. const [productPublish, setProductPublish] = useState(0);
  24. const [productUnPublish, setProductUnPublish] = useState(0);
  25. const [options, setOptions] = useState<EChartsOption>({});
  26. const [onlineOptions, setOnlineOptions] = useState<EChartsOption>({});
  27. const [yesterdayCount, setYesterdayCount] = useState(0);
  28. const [deviceOptions, setDeviceOptions] = useState<EChartsOption>({});
  29. const [month, setMonth] = useState(0);
  30. const [day, setDay] = useState(0);
  31. const [point, setPoint] = useState([]);
  32. const [amapKey, setAmapKey] = useState<any>();
  33. const ref = useRef<RefType>();
  34. const { data: deviceTotal } = useRequest(service.deviceCount, {
  35. formatResult: (res) => res.result,
  36. });
  37. const { data: productTotal } = useRequest(service.productCount, {
  38. defaultParams: [{}],
  39. formatResult: (res) => res.result,
  40. });
  41. //设备数量
  42. const deviceStatus = async () => {
  43. const onlineRes = await service.deviceCount(encodeQuery({ terms: { state: 'online' } }));
  44. if (onlineRes.status === 200) {
  45. setDeviceOnline(onlineRes.result);
  46. }
  47. const offlineRes = await service.deviceCount(encodeQuery({ terms: { state: 'offline' } }));
  48. if (offlineRes.status === 200) {
  49. setDeviceOffline(offlineRes.result);
  50. }
  51. };
  52. //产品数量
  53. const productStatus = async () => {
  54. const pusblish = await service.productCount({
  55. terms: [
  56. {
  57. column: 'state',
  58. value: '1',
  59. },
  60. ],
  61. });
  62. if (pusblish.status === 200) {
  63. setProductPublish(pusblish.result);
  64. }
  65. const unpublish = await service.productCount({
  66. terms: [
  67. {
  68. column: 'state',
  69. value: '0',
  70. },
  71. ],
  72. });
  73. if (unpublish.status === 200) {
  74. setProductUnPublish(unpublish.result);
  75. }
  76. };
  77. //当前在线
  78. const getOnline = async () => {
  79. const res = await service.dashboard([
  80. {
  81. dashboard: 'device',
  82. object: 'session',
  83. measurement: 'online',
  84. dimension: 'agg',
  85. group: 'aggOnline',
  86. params: {
  87. state: 'online',
  88. limit: 15,
  89. from: 'now-15d',
  90. time: '1d',
  91. format: 'yyyy-MM-dd',
  92. },
  93. },
  94. ]);
  95. if (res.status === 200) {
  96. const x = res.result.map((item: any) => item.data.timeString).reverse();
  97. const y = res.result.map((item: any) => item.data.value);
  98. setYesterdayCount(y?.[1]);
  99. setOnlineOptions({
  100. xAxis: {
  101. type: 'category',
  102. data: x,
  103. show: false,
  104. },
  105. yAxis: {
  106. type: 'value',
  107. show: false,
  108. },
  109. grid: {
  110. top: '5%',
  111. bottom: 0,
  112. },
  113. tooltip: {
  114. trigger: 'axis',
  115. axisPointer: {
  116. type: 'shadow',
  117. },
  118. },
  119. series: [
  120. {
  121. name: '在线数',
  122. data: y.reverse(),
  123. type: 'bar',
  124. itemStyle: {
  125. color: '#2F54EB',
  126. },
  127. },
  128. ],
  129. });
  130. }
  131. };
  132. //今日设备消息量
  133. const getDevice = async () => {
  134. const res = await service.dashboard([
  135. {
  136. dashboard: 'device',
  137. object: 'message',
  138. measurement: 'quantity',
  139. dimension: 'agg',
  140. group: 'today',
  141. params: {
  142. time: '1h',
  143. format: 'yyyy-MM-dd HH:mm:ss',
  144. limit: 24,
  145. from: 'now-1d',
  146. },
  147. },
  148. {
  149. dashboard: 'device',
  150. object: 'message',
  151. measurement: 'quantity',
  152. dimension: 'agg',
  153. group: 'oneday',
  154. params: {
  155. time: '1d',
  156. format: 'yyyy-MM-dd',
  157. from: 'now-1d',
  158. },
  159. },
  160. {
  161. dashboard: 'device',
  162. object: 'message',
  163. measurement: 'quantity',
  164. dimension: 'agg',
  165. group: 'thisMonth',
  166. params: {
  167. time: '1M',
  168. format: 'yyyy-MM',
  169. limit: 1,
  170. from: 'now-1M',
  171. },
  172. },
  173. ]);
  174. if (res.status === 200) {
  175. const thisMonth = res.result.find((item: any) => item.group === 'thisMonth').data.value;
  176. const oneDay = res.result.find((item: any) => item.group === 'oneday').data.value;
  177. setDay(oneDay);
  178. setMonth(thisMonth);
  179. const today = res.result.filter((item: any) => item.group === 'today');
  180. const x = today.map((item: any) => item.data.timeString).reverse();
  181. const y = today.map((item: any) => item.data.value).reverse();
  182. setDeviceOptions({
  183. tooltip: {
  184. trigger: 'axis',
  185. axisPointer: {
  186. type: 'shadow',
  187. },
  188. },
  189. xAxis: {
  190. type: 'category',
  191. boundaryGap: false,
  192. show: false,
  193. data: x,
  194. },
  195. yAxis: {
  196. type: 'value',
  197. show: false,
  198. },
  199. grid: {
  200. top: '2%',
  201. bottom: 0,
  202. },
  203. series: [
  204. {
  205. name: '消息量',
  206. data: y,
  207. type: 'line',
  208. smooth: true,
  209. color: '#685DEB',
  210. areaStyle: {
  211. color: {
  212. type: 'linear',
  213. x: 0,
  214. y: 0,
  215. x2: 0,
  216. y2: 1,
  217. colorStops: [
  218. {
  219. offset: 0,
  220. color: '#685DEB', // 100% 处的颜色
  221. },
  222. {
  223. offset: 1,
  224. color: '#FFFFFF', // 0% 处的颜色
  225. },
  226. ],
  227. global: false, // 缺省为 false
  228. },
  229. },
  230. },
  231. ],
  232. });
  233. }
  234. };
  235. const getEcharts = async () => {
  236. const data = ref.current!.getValues();
  237. if (data && data.time.type !== 'today') {
  238. const res = await service.dashboard([
  239. {
  240. dashboard: 'device',
  241. object: 'message',
  242. measurement: 'quantity',
  243. dimension: 'agg',
  244. group: 'device_msg',
  245. params: {
  246. time: '1d',
  247. format: 'yyyy.MM.dd',
  248. limit: Math.ceil((data.time.end - data.time.start) / (1 * 24 * 3600 * 1000) + 1),
  249. // from: moment(data.time.start).format('yyyy-MM-DD HH:MM:SS'),
  250. // to: moment(data.time.end).format('yyyy-MM-DD HH:MM:SS'),
  251. from: data.time.start,
  252. to: data.time.end,
  253. },
  254. },
  255. ]);
  256. if (res.status === 200) {
  257. const x = res.result.map((item: any) => item.data.timeString).reverse();
  258. const y = res.result.map((item: any) => item.data.value).reverse();
  259. setOptions({
  260. xAxis: {
  261. type: 'category',
  262. data: x,
  263. },
  264. yAxis: {
  265. type: 'value',
  266. },
  267. tooltip: {
  268. trigger: 'axis',
  269. // axisPointer: {
  270. // type: 'shadow',
  271. // },
  272. },
  273. grid: {
  274. top: '2%',
  275. bottom: '5%',
  276. left: '2%',
  277. right: '2%',
  278. },
  279. series: [
  280. {
  281. name: '消息量',
  282. data: y,
  283. type: 'line',
  284. smooth: true,
  285. color: '#685DEB',
  286. areaStyle: {
  287. color: {
  288. type: 'linear',
  289. x: 0,
  290. y: 0,
  291. x2: 0,
  292. y2: 1,
  293. colorStops: [
  294. {
  295. offset: 0,
  296. color: '#685DEB', // 100% 处的颜色
  297. },
  298. {
  299. offset: 1,
  300. color: '#FFFFFF', // 0% 处的颜色
  301. },
  302. ],
  303. global: false, // 缺省为 false
  304. },
  305. },
  306. },
  307. ],
  308. });
  309. }
  310. } else {
  311. const res = await service.dashboard([
  312. {
  313. dashboard: 'device',
  314. object: 'message',
  315. measurement: 'quantity',
  316. dimension: 'agg',
  317. group: 'device_msg',
  318. params: {
  319. time: '1h',
  320. format: 'yyyy.MM.dd HH:MM:SS',
  321. limit: 24,
  322. from: data.time.start,
  323. to: data.time.end,
  324. },
  325. },
  326. ]);
  327. if (res.status === 200) {
  328. const x = res.result.map((item: any) => item.data.timeString).reverse();
  329. const y = res.result.map((item: any) => item.data.value).reverse();
  330. setOptions({
  331. xAxis: {
  332. type: 'category',
  333. data: x,
  334. },
  335. yAxis: {
  336. type: 'value',
  337. },
  338. tooltip: {
  339. trigger: 'axis',
  340. },
  341. grid: {
  342. top: '2%',
  343. bottom: '5%',
  344. left: '2%',
  345. right: '2%',
  346. },
  347. series: [
  348. {
  349. name: '消息量',
  350. data: y,
  351. type: 'line',
  352. smooth: true,
  353. color: '#685DEB',
  354. areaStyle: {
  355. color: {
  356. type: 'linear',
  357. x: 0,
  358. y: 0,
  359. x2: 0,
  360. y2: 1,
  361. colorStops: [
  362. {
  363. offset: 0,
  364. color: '#685DEB', // 100% 处的颜色
  365. },
  366. {
  367. offset: 1,
  368. color: '#FFFFFF', // 0% 处的颜色
  369. },
  370. ],
  371. global: false, // 缺省为 false
  372. },
  373. },
  374. },
  375. ],
  376. });
  377. }
  378. }
  379. };
  380. //地图数据
  381. const geo = async (data?: any) => {
  382. const res = await service.getGeo(data);
  383. if (res.status === 200) {
  384. setPoint(res.result.features);
  385. }
  386. };
  387. useEffect(() => {
  388. deviceStatus();
  389. productStatus();
  390. getOnline();
  391. getDevice();
  392. geo({});
  393. }, []);
  394. useEffect(() => {
  395. const api = localStorage.getItem(SystemConst.AMAP_KEY);
  396. setAmapKey(api);
  397. }, [localStorage.getItem(SystemConst.AMAP_KEY)]);
  398. return (
  399. <PageContainer>
  400. <div className={'device-dash-board'}>
  401. <DashBoardTopCard>
  402. <DashBoardTopCard.Item
  403. title={'产品数量'}
  404. value={productTotal}
  405. footer={[
  406. {
  407. title: '已发布',
  408. value: productPublish,
  409. status: 'success',
  410. },
  411. {
  412. title: '未发布',
  413. value: productUnPublish,
  414. status: 'error',
  415. },
  416. ]}
  417. span={6}
  418. >
  419. <img src={require('/public/images/device/device-product.png')} />
  420. </DashBoardTopCard.Item>
  421. <DashBoardTopCard.Item
  422. title={'设备数量'}
  423. value={deviceTotal}
  424. footer={[
  425. {
  426. title: '在线',
  427. value: deviceOnline,
  428. status: 'success',
  429. },
  430. {
  431. title: '离线',
  432. value: deviceOffline,
  433. status: 'error',
  434. },
  435. ]}
  436. span={6}
  437. >
  438. <img src={require('/public/images/device/device-number.png')} />
  439. </DashBoardTopCard.Item>
  440. <DashBoardTopCard.Item
  441. title={'当前在线'}
  442. value={deviceOnline}
  443. footer={[
  444. {
  445. title: '昨日在线',
  446. value: yesterdayCount,
  447. },
  448. ]}
  449. span={6}
  450. >
  451. <Echarts options={onlineOptions} />
  452. </DashBoardTopCard.Item>
  453. <DashBoardTopCard.Item
  454. title={'今日设备消息量'}
  455. value={day}
  456. footer={[
  457. {
  458. title: '当月设备消息量',
  459. value: month,
  460. },
  461. ]}
  462. span={6}
  463. >
  464. <Echarts options={deviceOptions} />
  465. </DashBoardTopCard.Item>
  466. </DashBoardTopCard>
  467. <DashBoard
  468. title={'设备消息'}
  469. options={options}
  470. ref={ref}
  471. height={500}
  472. defaultTime={'week'}
  473. showTime={true}
  474. showTimeTool={true}
  475. onParamsChange={getEcharts}
  476. />
  477. {amapKey && (
  478. <Card style={{ marginTop: 10 }}>
  479. <div
  480. style={{
  481. fontSize: '16px',
  482. fontWeight: 'bold',
  483. marginBottom: 10,
  484. }}
  485. >
  486. 设备分布
  487. </div>
  488. <div>
  489. <AMap
  490. AMapUI
  491. style={{
  492. height: 500,
  493. width: '100%',
  494. }}
  495. >
  496. {point.map((item: any) => (
  497. //@ts-ignore
  498. <Marker
  499. position={{
  500. longitude: item.geometry.coordinates?.[0],
  501. latitude: item.geometry.coordinates?.[1],
  502. }}
  503. >
  504. <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
  505. <div
  506. style={{
  507. backgroundColor: '#666666',
  508. color: 'white',
  509. textAlign: 'center',
  510. marginBottom: 5,
  511. }}
  512. >
  513. {item.properties.deviceName}
  514. </div>
  515. <div>
  516. <EnvironmentOutlined style={{ color: 'blue', fontSize: 22 }} />
  517. </div>
  518. </div>
  519. </Marker>
  520. ))}
  521. </AMap>
  522. </div>
  523. </Card>
  524. )}
  525. </div>
  526. </PageContainer>
  527. );
  528. };
  529. export default DeviceBoard;