|
| 1 | +import React, { useState, useEffect, useMemo } from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import { Spin, Steps } from 'antd'; |
| 4 | +import { insightPatternsExtractor } from '@antv/ava'; |
| 5 | +import { JsonView, defaultStyles, collapseAllNested } from 'react-json-view-lite'; |
| 6 | + |
| 7 | +const App = () => { |
| 8 | + const data = useMemo( |
| 9 | + () => [ |
| 10 | + { |
| 11 | + date: '2019-08-01', |
| 12 | + discount_price: 727.12, |
| 13 | + }, |
| 14 | + { |
| 15 | + date: '2019-08-02', |
| 16 | + discount_price: 729.59, |
| 17 | + }, |
| 18 | + { |
| 19 | + date: '2019-08-03', |
| 20 | + discount_price: 730.21, |
| 21 | + }, |
| 22 | + { |
| 23 | + date: '2019-08-04', |
| 24 | + discount_price: 732.11, |
| 25 | + }, |
| 26 | + { |
| 27 | + date: '2019-08-05', |
| 28 | + discount_price: 733.22, |
| 29 | + }, |
| 30 | + { |
| 31 | + date: '2019-08-06', |
| 32 | + discount_price: 741.19, |
| 33 | + }, |
| 34 | + { |
| 35 | + date: '2019-08-07', |
| 36 | + discount_price: 742.37, |
| 37 | + }, |
| 38 | + { |
| 39 | + date: '2019-08-08', |
| 40 | + discount_price: 752.34, |
| 41 | + }, |
| 42 | + { |
| 43 | + date: '2019-08-09', |
| 44 | + discount_price: 761.12, |
| 45 | + }, |
| 46 | + { |
| 47 | + date: '2019-08-10', |
| 48 | + discount_price: 783.99, |
| 49 | + }, |
| 50 | + { |
| 51 | + date: '2019-08-11', |
| 52 | + discount_price: 791.23, |
| 53 | + }, |
| 54 | + { |
| 55 | + date: '2019-08-12', |
| 56 | + discount_price: 781.99, |
| 57 | + }, |
| 58 | + { |
| 59 | + date: '2019-08-13', |
| 60 | + discount_price: 835.71, |
| 61 | + }, |
| 62 | + { |
| 63 | + date: '2019-08-14', |
| 64 | + discount_price: 839.24, |
| 65 | + }, |
| 66 | + { |
| 67 | + date: '2019-08-15', |
| 68 | + discount_price: 883.51, |
| 69 | + }, |
| 70 | + { |
| 71 | + date: '2019-08-16', |
| 72 | + discount_price: 873.98, |
| 73 | + }, |
| 74 | + { |
| 75 | + date: '2019-08-17', |
| 76 | + discount_price: 802.78, |
| 77 | + }, |
| 78 | + { |
| 79 | + date: '2019-08-18', |
| 80 | + discount_price: 807.05, |
| 81 | + }, |
| 82 | + { |
| 83 | + date: '2019-08-19', |
| 84 | + discount_price: 885.12, |
| 85 | + }, |
| 86 | + { |
| 87 | + date: '2019-08-20', |
| 88 | + discount_price: 1018.85, |
| 89 | + }, |
| 90 | + { |
| 91 | + date: '2019-08-21', |
| 92 | + discount_price: 934.49, |
| 93 | + }, |
| 94 | + { |
| 95 | + date: '2019-08-22', |
| 96 | + discount_price: 908.74, |
| 97 | + }, |
| 98 | + { |
| 99 | + date: '2019-08-23', |
| 100 | + discount_price: 930.55, |
| 101 | + }, |
| 102 | + { |
| 103 | + date: '2019-08-24', |
| 104 | + discount_price: 978.53, |
| 105 | + }, |
| 106 | + { |
| 107 | + date: '2019-08-25', |
| 108 | + discount_price: 931.47, |
| 109 | + }, |
| 110 | + { |
| 111 | + date: '2019-08-26', |
| 112 | + discount_price: 891, |
| 113 | + }, |
| 114 | + { |
| 115 | + date: '2019-08-27', |
| 116 | + discount_price: 836.41, |
| 117 | + }, |
| 118 | + { |
| 119 | + date: '2019-08-28', |
| 120 | + discount_price: 826.11, |
| 121 | + }, |
| 122 | + { |
| 123 | + date: '2019-08-29', |
| 124 | + discount_price: 820.11, |
| 125 | + }, |
| 126 | + { |
| 127 | + date: '2019-08-30', |
| 128 | + discount_price: 811.11, |
| 129 | + }, |
| 130 | + ], |
| 131 | + [] |
| 132 | + ); |
| 133 | + const [result, setResult] = useState([]); |
| 134 | + const [insightLoading, setInsightLoading] = useState(true); |
| 135 | + const [currentStep, setCurrentStep] = useState(0); |
| 136 | + |
| 137 | + const getMyInsights = async () => { |
| 138 | + if (data) { |
| 139 | + const insightResult = insightPatternsExtractor({ |
| 140 | + data, |
| 141 | + dimensions: [{ fieldName: 'date' }], |
| 142 | + measures: [{ fieldName: 'discount_price', method: 'SUM' }], |
| 143 | + insightType: 'time_series_outlier', |
| 144 | + options: { |
| 145 | + dataValidation: true, |
| 146 | + algorithmParameter: { |
| 147 | + outlier: { |
| 148 | + method: 'IQR', |
| 149 | + iqrK: 1.5, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }); |
| 154 | + setResult(insightResult); |
| 155 | + setInsightLoading(false); |
| 156 | + } |
| 157 | + }; |
| 158 | + |
| 159 | + useEffect(() => { |
| 160 | + getMyInsights(); |
| 161 | + }, []); |
| 162 | + |
| 163 | + const dataContent = <JsonView data={data} shouldExpandNode={collapseAllNested} style={defaultStyles} />; |
| 164 | + |
| 165 | + const insightsContent = <JsonView data={result} shouldExpandNode={collapseAllNested} style={defaultStyles} />; |
| 166 | + |
| 167 | + const steps = [ |
| 168 | + { |
| 169 | + title: 'Data', |
| 170 | + desc: 'Source data:', |
| 171 | + content: dataContent, |
| 172 | + }, |
| 173 | + { |
| 174 | + title: 'Insights', |
| 175 | + desc: 'Insights extracted from data:', |
| 176 | + content: insightsContent, |
| 177 | + }, |
| 178 | + ]; |
| 179 | + |
| 180 | + return ( |
| 181 | + <> |
| 182 | + <div style={{ width: '50%', margin: '0 auto' }}> |
| 183 | + <Steps current={currentStep} onChange={setCurrentStep} items={steps} size="default" /> |
| 184 | + </div> |
| 185 | + <p>{steps[currentStep].desc}</p> |
| 186 | + |
| 187 | + <div className="steps-content" style={{ height: 'calc(100% - 80px)' }}> |
| 188 | + <Spin spinning={insightLoading}>{steps[currentStep].content}</Spin> |
| 189 | + </div> |
| 190 | + </> |
| 191 | + ); |
| 192 | +}; |
| 193 | + |
| 194 | +ReactDOM.render(<App />, document.getElementById('container')); |
0 commit comments