Statistic.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. 'use client'
  2. import cn from '@/utils/classnames'
  3. import ReactECharts from 'echarts-for-react'
  4. import * as echarts from 'echarts'
  5. import useSWR from 'swr'
  6. import { fetchStatistic } from '@/service/common'
  7. import { useAppContext } from '@/context/app-context'
  8. const cardClass = 'bg-[#FCFCFC] border rounded-[10px] border-[#E6E7EB] p-6 flex'
  9. const Statistic = () => {
  10. const { currentWorkspace, isCurrentWorkspaceOwner } = useAppContext()
  11. const { data: data1 }: any = useSWR(
  12. {
  13. url: '/datasets/count',
  14. params: {
  15. tenant_id: currentWorkspace.id,
  16. },
  17. },
  18. fetchStatistic,
  19. )
  20. const datasetsCount = data1?.data?.datasets_count || 0
  21. const deptsCount = data1?.data?.depts_count || 0
  22. const tagsCount = data1?.data?.tags_count || 0
  23. const { data: data2 }: any = useSWR(
  24. {
  25. url: '/datasets/type-stats',
  26. params: {
  27. tenant_id: currentWorkspace.id,
  28. },
  29. },
  30. fetchStatistic,
  31. )
  32. const typeData = data2?.data || []
  33. const typeOptions = {
  34. grid: {
  35. top: 0,
  36. bottom: 0,
  37. left: 0,
  38. right: 0,
  39. },
  40. tooltip: {
  41. trigger: 'item',
  42. },
  43. legend: {
  44. orient: 'horizontal',
  45. top: 0,
  46. itemWidth: 8,
  47. itemHeight: 8,
  48. },
  49. series: [
  50. {
  51. type: 'pie',
  52. radius: ['26%', '50%'],
  53. data: typeData.map((v: any) => {
  54. v.value = v.value || 100
  55. v.name = v.type
  56. return v
  57. }),
  58. label: {
  59. formatter: '{b} {d}%',
  60. },
  61. emphasis: {
  62. disabled: true,
  63. },
  64. },
  65. ],
  66. }
  67. const { data: data3 }: any = useSWR(
  68. {
  69. url: '/datasets/update-stats',
  70. params: {
  71. tenant_id: currentWorkspace.id,
  72. },
  73. },
  74. fetchStatistic,
  75. )
  76. const updateData = data3?.data || []
  77. const updateOptions = {
  78. grid: {
  79. top: 40,
  80. bottom: 20,
  81. left: 40,
  82. right: 0,
  83. },
  84. tooltip: {
  85. trigger: 'axis',
  86. },
  87. xAxis: {
  88. type: 'category',
  89. data: updateData.map((v: any) => v.period),
  90. axisLine: { show: false },
  91. axisTick: { show: false },
  92. splitLine: { show: false },
  93. axisLabel: {
  94. fontSize: 10,
  95. interval: 0,
  96. color: 'rgba(87, 98, 117, 1)',
  97. },
  98. },
  99. yAxis: {
  100. type: 'value',
  101. axisLine: { show: false },
  102. axisTick: { show: false },
  103. splitNumber: 3,
  104. splitLine: {
  105. lineStyle: {
  106. type: 'dashed',
  107. color: 'rgba(239, 241, 248, 1)',
  108. },
  109. },
  110. axisLabel: {
  111. fontSize: 10,
  112. },
  113. },
  114. series: [
  115. {
  116. type: 'bar',
  117. data: updateData.map((v: any) => v.count),
  118. itemStyle: {
  119. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  120. { offset: 0, color: '#2078FC' },
  121. { offset: 1, color: '#5DC9F9' },
  122. ]),
  123. },
  124. },
  125. ],
  126. }
  127. const { data: data4 }: any = useSWR(
  128. {
  129. url: '/tags',
  130. params: {
  131. type: 'knowledge',
  132. },
  133. },
  134. fetchStatistic,
  135. )
  136. const tagData = data4 || []
  137. const tagColors = ['#7185fb', '#4bc97d', '#2079fe', '#dba53c', '#4ec5f9']
  138. const tagOptions = {
  139. tooltip: {
  140. trigger: 'item',
  141. },
  142. series: [
  143. {
  144. type: 'graph',
  145. layout: 'force',
  146. data: tagData.map((v: any) => ({ name: v.name, value: Number(v.binding_count) })).sort((a: any, b: any) => b.value - a.value).map((v: any, i: number) => {
  147. if (i < Math.floor(tagData.length / 4)) {
  148. v.symbolSize = 100
  149. v.label = {
  150. fontSize: 30,
  151. }
  152. }
  153. else if (i < Math.floor(tagData.length / 2)) {
  154. v.symbolSize = 90
  155. v.label = {
  156. fontSize: 24,
  157. }
  158. }
  159. else if (i < Math.floor(tagData.length * 3 / 4)) {
  160. v.symbolSize = 80
  161. v.label = {
  162. fontSize: 20,
  163. }
  164. }
  165. else {
  166. v.symbolSize = 70
  167. v.label = {
  168. fontSize: 16,
  169. }
  170. }
  171. v.itemStyle = {
  172. color: tagColors[i % tagColors.length],
  173. }
  174. return v
  175. }),
  176. links: [],
  177. roam: true,
  178. force: {
  179. repulsion: 110,
  180. edgeLength: [10, 50],
  181. },
  182. symbolSize: 60,
  183. label: {
  184. show: true,
  185. color: '#ffffff',
  186. },
  187. emphasis: {
  188. disabled: true,
  189. },
  190. },
  191. ],
  192. }
  193. return (
  194. <div className="w-full px-12">
  195. <div className={cn('w-full gap-6', cardClass)}>
  196. <div className="flex h-[120px] grow items-center bg-[url('/imgs/statistic-bg.png')] bg-[length:100%_100%] bg-no-repeat pl-6">
  197. <img src="/imgs/statistic-icon-1.png" className="mr-6 h-16 w-16"/>
  198. <div>
  199. <div className="text-[36px] text-[#111111]">{datasetsCount}</div>
  200. <div className="text-[#111111]">知识库总量</div>
  201. </div>
  202. </div>
  203. <div className="flex h-[120px] grow items-center bg-[url('/imgs/statistic-bg.png')] bg-[length:100%_100%] bg-no-repeat pl-6">
  204. <img src="/imgs/statistic-icon-2.png" className="mr-6 h-16 w-16"/>
  205. <div>
  206. <div className="text-[36px] text-[#111111]">{deptsCount}</div>
  207. <div className="text-[#111111]">部门总量</div>
  208. </div>
  209. </div>
  210. <div className="flex h-[120px] grow items-center bg-[url('/imgs/statistic-bg.png')] bg-[length:100%_100%] bg-no-repeat pl-6">
  211. <img src="/imgs/statistic-icon-3.png" className="mr-6 h-16 w-16"/>
  212. <div>
  213. <div className="text-[36px] text-[#111111]">{tagsCount}</div>
  214. <div className="text-[#111111]">标签总量</div>
  215. </div>
  216. </div>
  217. </div>
  218. <div className='mt-6 flex w-full gap-8'>
  219. <div className={cn(cardClass, 'h-[360px] flex-1 flex-col overflow-hidden')}>
  220. <div className="text-2xl font-bold text-[#333333]">知识库类别统计</div>
  221. <div className="relative mt-3 flex flex-1 items-center justify-center">
  222. <img src="/imgs/statistic-icon-4.png" className="absolute"/>
  223. <ReactECharts option={typeOptions} className="flex-1" />
  224. </div>
  225. </div>
  226. <div className={cn(cardClass, 'h-[360px] flex-1 flex-col overflow-hidden')}>
  227. <div className="text-2xl font-bold text-[#333333]">知识库更新情况统计</div>
  228. <ReactECharts option={updateOptions} className="flex-1" />
  229. </div>
  230. <div className={cn(cardClass, 'h-[360px] flex-1 flex-col overflow-hidden bg-[url("/imgs/statistic-icon-5.png")] bg-[length:100%_100%] bg-no-repeat')}>
  231. <div className="text-2xl font-bold text-[#333333]">标签云图</div>
  232. <ReactECharts option={tagOptions} className="flex-1" />
  233. </div>
  234. </div>
  235. </div>
  236. )
  237. }
  238. export default Statistic