index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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) {
  238. // console.log(Math.ceil((data.time.end - data.time.start) / (1 * 24 * 3600 * 1000) + 1));
  239. const res = await service.dashboard([
  240. {
  241. dashboard: 'device',
  242. object: 'message',
  243. measurement: 'quantity',
  244. dimension: 'agg',
  245. group: 'device_msg',
  246. params: {
  247. time: '1d',
  248. format: 'yyyy.MM.dd',
  249. limit: Math.ceil((data.time.end - data.time.start) / (1 * 24 * 3600 * 1000) + 1),
  250. // from: moment(data.time.start).format('yyyy-MM-DD HH:MM:SS'),
  251. // to: moment(data.time.end).format('yyyy-MM-DD HH:MM:SS'),
  252. from: data.time.start,
  253. to: data.time.end,
  254. },
  255. },
  256. ]);
  257. if (res.status === 200) {
  258. const x = res.result.map((item: any) => item.data.timeString).reverse();
  259. const y = res.result.map((item: any) => item.data.value).reverse();
  260. setOptions({
  261. xAxis: {
  262. type: 'category',
  263. data: x,
  264. },
  265. yAxis: {
  266. type: 'value',
  267. },
  268. tooltip: {
  269. trigger: 'axis',
  270. // axisPointer: {
  271. // type: 'shadow',
  272. // },
  273. },
  274. grid: {
  275. top: '2%',
  276. bottom: '5%',
  277. left: '2%',
  278. right: '2%',
  279. },
  280. series: [
  281. {
  282. name: '消息量',
  283. data: y,
  284. type: 'line',
  285. smooth: true,
  286. color: '#685DEB',
  287. areaStyle: {
  288. color: {
  289. type: 'linear',
  290. x: 0,
  291. y: 0,
  292. x2: 0,
  293. y2: 1,
  294. colorStops: [
  295. {
  296. offset: 0,
  297. color: '#685DEB', // 100% 处的颜色
  298. },
  299. {
  300. offset: 1,
  301. color: '#FFFFFF', // 0% 处的颜色
  302. },
  303. ],
  304. global: false, // 缺省为 false
  305. },
  306. },
  307. },
  308. ],
  309. });
  310. }
  311. }
  312. };
  313. //地图数据
  314. const geo = async (data?: any) => {
  315. const res = await service.getGeo(data);
  316. if (res.status === 200) {
  317. setPoint(res.result.features);
  318. }
  319. };
  320. useEffect(() => {
  321. deviceStatus();
  322. productStatus();
  323. getOnline();
  324. getDevice();
  325. geo({});
  326. }, []);
  327. useEffect(() => {
  328. const api = localStorage.getItem(SystemConst.AMAP_KEY);
  329. setAmapKey(api);
  330. }, [localStorage.getItem(SystemConst.AMAP_KEY)]);
  331. return (
  332. <PageContainer>
  333. <div className={'device-dash-board'}>
  334. <DashBoardTopCard>
  335. <DashBoardTopCard.Item
  336. title={'产品数量'}
  337. value={productTotal}
  338. footer={[
  339. {
  340. title: '已发布',
  341. value: productPublish,
  342. status: 'success',
  343. },
  344. {
  345. title: '未发布',
  346. value: productUnPublish,
  347. status: 'error',
  348. },
  349. ]}
  350. span={6}
  351. >
  352. <img src={require('/public/images/device/device-product.png')} />
  353. </DashBoardTopCard.Item>
  354. <DashBoardTopCard.Item
  355. title={'设备数量'}
  356. value={deviceTotal}
  357. footer={[
  358. {
  359. title: '在线',
  360. value: deviceOnline,
  361. status: 'success',
  362. },
  363. {
  364. title: '离线',
  365. value: deviceOffline,
  366. status: 'error',
  367. },
  368. ]}
  369. span={6}
  370. >
  371. <img src={require('/public/images/device/device-number.png')} />
  372. </DashBoardTopCard.Item>
  373. <DashBoardTopCard.Item
  374. title={'当前在线'}
  375. value={deviceOnline}
  376. footer={[
  377. {
  378. title: '昨日在线',
  379. value: yesterdayCount,
  380. },
  381. ]}
  382. span={6}
  383. >
  384. <Echarts options={onlineOptions} />
  385. </DashBoardTopCard.Item>
  386. <DashBoardTopCard.Item
  387. title={'今日设备消息量'}
  388. value={day}
  389. footer={[
  390. {
  391. title: '当月设备消息量',
  392. value: month,
  393. },
  394. ]}
  395. span={6}
  396. >
  397. <Echarts options={deviceOptions} />
  398. </DashBoardTopCard.Item>
  399. </DashBoardTopCard>
  400. <DashBoard
  401. title={'设备消息'}
  402. options={options}
  403. ref={ref}
  404. height={500}
  405. defaultTime={'week'}
  406. showTime={true}
  407. showTimeTool={true}
  408. onParamsChange={getEcharts}
  409. />
  410. {amapKey && (
  411. <Card style={{ marginTop: 10 }}>
  412. <div
  413. style={{
  414. fontSize: '16px',
  415. fontWeight: 'bold',
  416. marginBottom: 10,
  417. }}
  418. >
  419. 设备分布
  420. </div>
  421. <div>
  422. <AMap
  423. AMapUI
  424. style={{
  425. height: 500,
  426. width: '100%',
  427. }}
  428. >
  429. {point.map((item: any) => (
  430. //@ts-ignore
  431. <Marker
  432. position={{
  433. longitude: item.geometry.coordinates?.[0],
  434. latitude: item.geometry.coordinates?.[1],
  435. }}
  436. >
  437. <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
  438. <div
  439. style={{
  440. backgroundColor: '#666666',
  441. color: 'white',
  442. textAlign: 'center',
  443. marginBottom: 5,
  444. }}
  445. >
  446. {item.properties.deviceName}
  447. </div>
  448. <div>
  449. <EnvironmentOutlined style={{ color: 'blue', fontSize: 22 }} />
  450. </div>
  451. </div>
  452. </Marker>
  453. ))}
  454. </AMap>
  455. </div>
  456. </Card>
  457. )}
  458. </div>
  459. </PageContainer>
  460. );
  461. };
  462. export default DeviceBoard;