-
Notifications
You must be signed in to change notification settings - Fork 17.9k
feat(explore): Add anomaly detection for timeseries charts #40954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
DeepFriedYeti
wants to merge
12
commits into
apache:master
Choose a base branch
from
mercedes-benz:feat/anomaly-detection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6479834
FEAT: Adding Anomaly detection to superset
DeepFriedYeti fbc050c
Merge branch 'apache:master' into feat/anomaly-detection
DeepFriedYeti 657722d
fix(anomaly-detection): move the imports to the top
DeepFriedYeti afa2d47
Merge branch 'master' into feat/anomaly-detection
DeepFriedYeti 2a1122d
Merge branch 'master' into feat/anomaly-detection
DeepFriedYeti d956df7
fix(anomaly-detection): resolved the issues mentioned in the code rev…
DeepFriedYeti f328cf5
chore(docs): update yarn.lock to fix pre-commit dirty working directory
DeepFriedYeti 75a684e
fix(anomaly-detection): fixed failing tests
DeepFriedYeti 5b3f1e0
Merge branch 'master' into feat/anomaly-detection
DeepFriedYeti 682bd96
fix(anomaly-detection): fixed formatting using prettier
DeepFriedYeti f597a21
Merge branch 'feat/anomaly-detection' of https://github.com/mercedes-…
DeepFriedYeti a36da3d
Merge branch 'master' into feat/anomaly-detection
DeepFriedYeti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...et-frontend/packages/superset-ui-chart-controls/src/operators/anomalyDetectionOperator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { | ||
| PostProcessingAnomalyDetection, | ||
| getXAxisLabel, | ||
| } from '@superset-ui/core'; | ||
| import { PostProcessingFactory } from './types'; | ||
|
|
||
| export const anomalyDetectionOperator: PostProcessingFactory< | ||
| PostProcessingAnomalyDetection | ||
| > = (formData, queryObject) => { | ||
| const xAxisLabel = getXAxisLabel(formData); | ||
| if (formData.anomalyDetectionEnabled && xAxisLabel) { | ||
| const method: string = formData.anomalyDetectionMethod || 'zscore'; | ||
| // Prophet requires a temporal x-axis; skip if no temporal indicator present | ||
| if ( | ||
| method === 'prophet' && | ||
| !formData.granularity_sqla && | ||
| !formData.time_grain_sqla | ||
| ) { | ||
| return undefined; | ||
| } | ||
| const options: Record<string, unknown> = { method, index: xAxisLabel }; | ||
| if (method === 'prophet') { | ||
| options.confidence_interval = | ||
| parseFloat(formData.anomalyDetectionConfidenceInterval) || 0.8; | ||
| options.yearly_seasonality = formData.anomalyDetectionSeasonalityYearly; | ||
| options.weekly_seasonality = formData.anomalyDetectionSeasonalityWeekly; | ||
| options.daily_seasonality = formData.anomalyDetectionSeasonalityDaily; | ||
| } else { | ||
| options.rolling_window = | ||
| parseInt(formData.anomalyDetectionRollingWindow, 10) || 14; | ||
| options.sensitivity = | ||
| parseFloat(formData.anomalyDetectionSensitivity) || 3.0; | ||
| } | ||
| return { | ||
| operation: 'anomaly_detection', | ||
| options, | ||
| } as PostProcessingAnomalyDetection; | ||
| } | ||
| return undefined; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
superset-frontend/packages/superset-ui-chart-controls/src/sections/anomalyDetection.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { t } from '@apache-superset/core/translation'; | ||
| import { legacyValidateInteger, legacyValidateNumber } from '@superset-ui/core'; | ||
| import { ControlPanelSectionConfig } from '../types'; | ||
| import { displayTimeRelatedControls } from '../utils'; | ||
|
|
||
| const validateRange = | ||
| ( | ||
| check: (n: number) => boolean, | ||
| message: string, | ||
| ): ((v: unknown) => string | false) => | ||
| (v: unknown) => { | ||
| const n = Number(v); | ||
| return Number.isFinite(n) && check(n) ? t(message) : false; | ||
| }; | ||
|
|
||
| const validateMinRollingWindow = validateRange( | ||
| n => n < 3, | ||
| 'Rolling window must be >= 3', | ||
| ); | ||
| const validatePositiveNumber = validateRange( | ||
| n => n <= 0, | ||
| 'Value must be a positive number', | ||
| ); | ||
| const validateConfidenceInterval = validateRange( | ||
| n => n <= 0 || n >= 1, | ||
| 'Confidence interval must be between 0 and 1 (exclusive)', | ||
| ); | ||
| export const ANOMALY_DEFAULT_DATA = { | ||
| anomalyDetectionEnabled: false, | ||
| anomalyDetectionMethod: 'zscore', | ||
| anomalyDetectionRollingWindow: 14, | ||
| anomalyDetectionSensitivity: 3.0, | ||
| anomalyDetectionConfidenceInterval: 0.8, | ||
| anomalyDetectionSeasonalityYearly: null, | ||
| anomalyDetectionSeasonalityWeekly: null, | ||
| anomalyDetectionSeasonalityDaily: null, | ||
| }; | ||
|
|
||
| export const anomalyDetectionControls: ControlPanelSectionConfig = { | ||
| label: t('Anomaly Detection'), | ||
| expanded: false, | ||
| visibility: displayTimeRelatedControls, | ||
| controlSetRows: [ | ||
| [ | ||
| { | ||
| name: 'anomalyDetectionEnabled', | ||
| config: { | ||
| type: 'CheckboxControl', | ||
| label: t('Enable anomaly detection'), | ||
| renderTrigger: false, | ||
| default: ANOMALY_DEFAULT_DATA.anomalyDetectionEnabled, | ||
| description: t('Enable anomaly detection on the time series'), | ||
| }, | ||
| }, | ||
| ], | ||
| [ | ||
| { | ||
| name: 'anomalyDetectionMethod', | ||
| config: { | ||
| type: 'SelectControl', | ||
| label: t('Detection method'), | ||
| choices: [ | ||
| ['zscore', t('Z-Score')], | ||
| ['mad', t('MAD (Median Absolute Deviation)')], | ||
| ['prophet', t('Prophet (Seasonality-aware)')], | ||
| ], | ||
| default: ANOMALY_DEFAULT_DATA.anomalyDetectionMethod, | ||
| description: t( | ||
| 'Algorithm to use for anomaly detection. Z-Score uses rolling mean and standard deviation. MAD uses rolling median absolute deviation which is more robust to outliers. Prophet uses Facebook Prophet to model seasonality and flags points outside the confidence interval.', | ||
| ), | ||
| }, | ||
| }, | ||
| ], | ||
| [ | ||
| { | ||
| name: 'anomalyDetectionRollingWindow', | ||
| config: { | ||
| type: 'TextControl', | ||
| label: t('Rolling window'), | ||
| validators: [legacyValidateInteger, validateMinRollingWindow], | ||
| default: ANOMALY_DEFAULT_DATA.anomalyDetectionRollingWindow, | ||
| description: t( | ||
| 'Size of the rolling window for computing statistics. Must be >= 3.', | ||
| ), | ||
| visibility: ({ controls }) => | ||
| controls?.anomalyDetectionMethod?.value !== 'prophet', | ||
| }, | ||
| }, | ||
| ], | ||
| [ | ||
| { | ||
| name: 'anomalyDetectionSensitivity', | ||
| config: { | ||
| type: 'TextControl', | ||
| label: t('Sensitivity'), | ||
| validators: [legacyValidateNumber, validatePositiveNumber], | ||
| default: ANOMALY_DEFAULT_DATA.anomalyDetectionSensitivity, | ||
| description: t( | ||
| 'Threshold for anomaly detection. Higher values mean fewer anomalies are detected. Typical values: 2.0 (more sensitive) to 4.0 (less sensitive).', | ||
| ), | ||
| visibility: ({ controls }) => | ||
| controls?.anomalyDetectionMethod?.value !== 'prophet', | ||
| }, | ||
| }, | ||
| ], | ||
| [ | ||
| { | ||
| name: 'anomalyDetectionConfidenceInterval', | ||
| config: { | ||
| type: 'TextControl', | ||
| label: t('Confidence interval'), | ||
| validators: [legacyValidateNumber, validateConfidenceInterval], | ||
| default: ANOMALY_DEFAULT_DATA.anomalyDetectionConfidenceInterval, | ||
| description: t( | ||
| 'Width of the confidence interval. Should be between 0 and 1', | ||
| ), | ||
| visibility: ({ controls }) => | ||
| controls?.anomalyDetectionMethod?.value === 'prophet', | ||
| }, | ||
| }, | ||
| ], | ||
| [ | ||
| { | ||
| name: 'anomalyDetectionSeasonalityYearly', | ||
| config: { | ||
| type: 'SelectControl', | ||
| freeForm: true, | ||
| label: t('Yearly seasonality'), | ||
| choices: [ | ||
| [null, t('default')], | ||
| [true, t('Yes')], | ||
| [false, t('No')], | ||
| ], | ||
| default: ANOMALY_DEFAULT_DATA.anomalyDetectionSeasonalityYearly, | ||
| description: t( | ||
| 'Should yearly seasonality be applied. An integer value will specify Fourier order of seasonality.', | ||
| ), | ||
| visibility: ({ controls }) => | ||
| controls?.anomalyDetectionMethod?.value === 'prophet', | ||
| }, | ||
| }, | ||
| ], | ||
| [ | ||
| { | ||
| name: 'anomalyDetectionSeasonalityWeekly', | ||
| config: { | ||
| type: 'SelectControl', | ||
| freeForm: true, | ||
| label: t('Weekly seasonality'), | ||
| choices: [ | ||
| [null, t('default')], | ||
| [true, t('Yes')], | ||
| [false, t('No')], | ||
| ], | ||
| default: ANOMALY_DEFAULT_DATA.anomalyDetectionSeasonalityWeekly, | ||
| description: t( | ||
| 'Should weekly seasonality be applied. An integer value will specify Fourier order of seasonality.', | ||
| ), | ||
| visibility: ({ controls }) => | ||
| controls?.anomalyDetectionMethod?.value === 'prophet', | ||
| }, | ||
| }, | ||
| ], | ||
| [ | ||
| { | ||
| name: 'anomalyDetectionSeasonalityDaily', | ||
| config: { | ||
| type: 'SelectControl', | ||
| freeForm: true, | ||
| label: t('Daily seasonality'), | ||
| choices: [ | ||
| [null, t('default')], | ||
| [true, t('Yes')], | ||
| [false, t('No')], | ||
| ], | ||
| default: ANOMALY_DEFAULT_DATA.anomalyDetectionSeasonalityDaily, | ||
| description: t( | ||
| 'Should daily seasonality be applied. An integer value will specify Fourier order of seasonality.', | ||
| ), | ||
| visibility: ({ controls }) => | ||
| controls?.anomalyDetectionMethod?.value === 'prophet', | ||
| }, | ||
| }, | ||
| ], | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.