discover.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import isArray from 'lodash/isArray';
  2. import XAxis from 'app/components/charts/components/xAxis';
  3. import AreaSeries from 'app/components/charts/series/areaSeries';
  4. import BarSeries from 'app/components/charts/series/barSeries';
  5. import {EventsStats} from 'app/types';
  6. import {lightTheme as theme} from 'app/utils/theme';
  7. import {slackChartDefaults, slackChartSize} from './slack';
  8. import {ChartType, RenderDescriptor} from './types';
  9. const discoverxAxis = XAxis({
  10. theme,
  11. boundaryGap: true,
  12. splitNumber: 3,
  13. isGroupedByDate: true,
  14. axisLabel: {fontSize: 11},
  15. });
  16. export const discoverCharts: RenderDescriptor<ChartType>[] = [];
  17. discoverCharts.push({
  18. key: ChartType.SLACK_DISCOVER_TOTAL_PERIOD,
  19. getOption: (
  20. data:
  21. | {seriesName: string; stats: EventsStats}
  22. | {seriesName?: string; stats: Record<string, EventsStats>}
  23. ) => {
  24. if (isArray(data.stats.data)) {
  25. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  26. const areaSeries = AreaSeries({
  27. name: data.seriesName,
  28. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  29. timestamp * 1000,
  30. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  31. ]),
  32. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  33. areaStyle: {color: color?.[0], opacity: 1},
  34. });
  35. return {
  36. ...slackChartDefaults,
  37. useUTC: true,
  38. color,
  39. series: [areaSeries],
  40. };
  41. }
  42. const stats = Object.keys(data.stats).map(key =>
  43. Object.assign({}, {key}, data.stats[key])
  44. );
  45. const color = theme.charts.getColorPalette(stats.length - 2);
  46. const series = stats
  47. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  48. .map((s, i) =>
  49. AreaSeries({
  50. name: s.key,
  51. stack: 'area',
  52. data: s.data.map(([timestamp, countsForTimestamp]) => [
  53. timestamp * 1000,
  54. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  55. ]),
  56. lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
  57. areaStyle: {color: color?.[i], opacity: 1},
  58. })
  59. );
  60. return {
  61. ...slackChartDefaults,
  62. xAxis: discoverxAxis,
  63. useUTC: true,
  64. color,
  65. series,
  66. };
  67. },
  68. ...slackChartSize,
  69. });
  70. discoverCharts.push({
  71. key: ChartType.SLACK_DISCOVER_TOTAL_DAILY,
  72. getOption: (
  73. data:
  74. | {seriesName: string; stats: EventsStats}
  75. | {seriesName?: string; stats: Record<string, EventsStats>}
  76. ) => {
  77. if (isArray(data.stats.data)) {
  78. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  79. const barSeries = BarSeries({
  80. name: data.seriesName,
  81. data: data.stats.data.map(([timestamp, countsForTimestamp]) => ({
  82. value: [
  83. timestamp * 1000,
  84. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  85. ],
  86. })),
  87. itemStyle: {color: color?.[0], opacity: 1},
  88. });
  89. return {
  90. ...slackChartDefaults,
  91. xAxis: discoverxAxis,
  92. useUTC: true,
  93. color,
  94. series: [barSeries],
  95. };
  96. }
  97. const stats = Object.keys(data.stats).map(key =>
  98. Object.assign({}, {key}, data.stats[key])
  99. );
  100. const color = theme.charts.getColorPalette(stats.length - 2);
  101. const series = stats
  102. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  103. .map((s, i) =>
  104. BarSeries({
  105. name: s.key,
  106. stack: 'area',
  107. data: s.data.map(([timestamp, countsForTimestamp]) => ({
  108. value: [
  109. timestamp * 1000,
  110. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  111. ],
  112. })),
  113. itemStyle: {color: color?.[i], opacity: 1},
  114. })
  115. );
  116. return {
  117. ...slackChartDefaults,
  118. xAxis: discoverxAxis,
  119. useUTC: true,
  120. color,
  121. series,
  122. };
  123. },
  124. ...slackChartSize,
  125. });
  126. discoverCharts.push({
  127. key: ChartType.SLACK_DISCOVER_TOP5_PERIOD,
  128. getOption: (
  129. data: {stats: Record<string, EventsStats>} | {seriesName?: string; stats: EventsStats}
  130. ) => {
  131. if (isArray(data.stats.data)) {
  132. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  133. const areaSeries = AreaSeries({
  134. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  135. timestamp * 1000,
  136. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  137. ]),
  138. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  139. areaStyle: {color: color?.[0], opacity: 1},
  140. });
  141. return {
  142. ...slackChartDefaults,
  143. useUTC: true,
  144. color,
  145. series: [areaSeries],
  146. };
  147. }
  148. const stats = Object.values(data.stats);
  149. const color = theme.charts.getColorPalette(stats.length - 2);
  150. const series = stats
  151. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  152. .map((topSeries, i) =>
  153. AreaSeries({
  154. stack: 'area',
  155. data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
  156. timestamp * 1000,
  157. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  158. ]),
  159. lineStyle: {color: color?.[i], opacity: 1, width: 0.4},
  160. areaStyle: {color: color?.[i], opacity: 1},
  161. })
  162. );
  163. return {
  164. ...slackChartDefaults,
  165. xAxis: discoverxAxis,
  166. useUTC: true,
  167. color,
  168. series,
  169. };
  170. },
  171. ...slackChartSize,
  172. });
  173. discoverCharts.push({
  174. key: ChartType.SLACK_DISCOVER_TOP5_DAILY,
  175. getOption: (
  176. data: {stats: Record<string, EventsStats>} | {seriesName?: string; stats: EventsStats}
  177. ) => {
  178. if (isArray(data.stats.data)) {
  179. const color = theme.charts.getColorPalette(data.stats.data.length - 2);
  180. const areaSeries = AreaSeries({
  181. data: data.stats.data.map(([timestamp, countsForTimestamp]) => [
  182. timestamp * 1000,
  183. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  184. ]),
  185. lineStyle: {color: color?.[0], opacity: 1, width: 0.4},
  186. areaStyle: {color: color?.[0], opacity: 1},
  187. });
  188. return {
  189. ...slackChartDefaults,
  190. useUTC: true,
  191. color,
  192. series: [areaSeries],
  193. };
  194. }
  195. const stats = Object.values(data.stats);
  196. const color = theme.charts.getColorPalette(stats.length - 2);
  197. const series = stats
  198. .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
  199. .map((topSeries, i) =>
  200. BarSeries({
  201. stack: 'area',
  202. data: topSeries.data.map(([timestamp, countsForTimestamp]) => [
  203. timestamp * 1000,
  204. countsForTimestamp.reduce((acc, {count}) => acc + count, 0),
  205. ]),
  206. itemStyle: {color: color?.[i], opacity: 1},
  207. })
  208. );
  209. return {
  210. ...slackChartDefaults,
  211. xAxis: discoverxAxis,
  212. useUTC: true,
  213. color,
  214. series,
  215. };
  216. },
  217. ...slackChartSize,
  218. });