-
Notifications
You must be signed in to change notification settings - Fork 5
Time inconsistencies heuristic for faulty measurements detection #147
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
Open
LDiazN
wants to merge
22
commits into
main
Choose a base branch
from
time-inconsistencies
base: main
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.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
60d31ed
Add fixture to test volume analysis
LDiazN e433f92
Add volume analysis tests
LDiazN ccf99d4
Fixing testing of volume analysis query
LDiazN 617edd8
Fix broken tests
LDiazN 5a5d590
Add assert to check expected time in event
LDiazN 0fa1509
Add task to analyse measurement volume for measurement anomalies
LDiazN 894e810
simplify tests code
LDiazN c37eefb
black reformat
LDiazN cf90993
Add volume analysis files
LDiazN c1c2665
Remove hourly analysis from dependencies
LDiazN 985fdef
Add software_name key details and probe_id
LDiazN 8ed602b
Add time inconsistencies analysis
LDiazN 711bcdf
Add time inconsistencies fixtures
LDiazN 609e9a2
Add tests for the time inconsistencies analysis
LDiazN 01817ff
Refactor tests
LDiazN 83df810
Create task for time inconsistencies
LDiazN dfb58cf
Add integration test for time inconsistency analysis
LDiazN afd0d70
Add time inconsistency analysis dag
LDiazN d78bba0
Change time definition to be similar to other tables
LDiazN 44bd91b
Change time to ts in tests
LDiazN 334c065
Change time column to ts
LDiazN b8b2e18
Merge branch 'volume-analysis' into time-inconsistencies
LDiazN 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
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
90 changes: 90 additions & 0 deletions
90
oonipipeline/src/oonipipeline/analysis/time_inconsistencies.py
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,90 @@ | ||
| """ | ||
| This file will implement time inconsistencies analysis for anomaly detection. | ||
|
|
||
| We consider that there's a time inconsistency anomaly when the measurement_start_time | ||
| differs significantly from the timestamp embedded in the measurement_uid. | ||
| """ | ||
|
|
||
| from clickhouse_driver import Client as Clickhouse | ||
| from datetime import datetime | ||
| import logging | ||
| import orjson | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def run_time_inconsistencies_analysis( | ||
| clickhouse_url: str, start_time: datetime, end_time: datetime, threshold: int | ||
| ): | ||
| """ | ||
| This function will measure the drift between the reported measurement_start_time | ||
| and the time it was reported to the fastpath. | ||
|
|
||
| threshold: time in seconds to trigger an anomaly | ||
| """ | ||
|
|
||
| db = Clickhouse.from_url(clickhouse_url) | ||
| query = """ | ||
| SELECT | ||
| probe_cc, | ||
| probe_asn, | ||
| measurement_uid, | ||
| measurement_start_time, | ||
| parseDateTimeBestEffort(substring(measurement_uid, 1, 15)) AS uid_timestamp, | ||
| dateDiff('second', parseDateTimeBestEffort(substring(measurement_uid, 1, 15)), measurement_start_time) AS diff_seconds | ||
| FROM fastpath | ||
| WHERE | ||
| measurement_start_time >= %(start_time)s AND | ||
| measurement_start_time < %(end_time)s AND | ||
| abs(dateDiff('second', parseDateTimeBestEffort(substring(measurement_uid, 1, 15)), measurement_start_time)) >= %(treshold)s | ||
| ORDER BY diff_seconds DESC | ||
| """ | ||
|
|
||
| rows = ( | ||
| db.execute( | ||
| query, | ||
| params={ | ||
| "start_time": start_time, | ||
| "end_time": end_time, | ||
| "treshold": threshold, | ||
| }, | ||
| ) | ||
| or [] | ||
| ) | ||
|
|
||
| if len(rows) == 0: | ||
| log.info("No time inconsistency anomalies where found") | ||
| return | ||
| else: | ||
| log.info(f"Found {len(rows)} time inconsistencies from {start_time} to {end_time}") | ||
|
|
||
| values = [] | ||
| for row in rows: | ||
|
|
||
| ( | ||
| probe_cc, | ||
| probe_asn, | ||
| measurement_uid, | ||
| measurement_start_time, | ||
| uid_timestamp, | ||
| diff_seconds, | ||
| ) = row | ||
|
|
||
| # Note that: | ||
| # diff_seconds < 0 => Measurement from the future | ||
| # diff_seconds >= 0 => Measurement too far away in the past | ||
| details = { | ||
| "measurement_uid": measurement_uid, | ||
| "measurement_start_time": measurement_start_time.isoformat(), | ||
| "uid_timestamp": uid_timestamp.isoformat(), | ||
| "diff_seconds": diff_seconds, | ||
| "threshold": threshold, | ||
| } | ||
|
|
||
| values.append(("time_inconsistency", probe_cc, probe_asn, orjson.dumps(details).decode())) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To facilitate querying and analysis we should maybe have two keys here for future and past measurements so we can look at them separately |
||
|
|
||
| db.execute( | ||
| "INSERT INTO faulty_measurements (type, probe_cc, probe_asn, details) VALUES", | ||
| values, | ||
| types_check=True, | ||
| ) | ||
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,88 @@ | ||
| """ | ||
| This file will implement volume analysis for anomaly detection. | ||
|
|
||
| We consider that there's a volume anomaly when a single probe seems to be sending too many measurements | ||
| in small time windows | ||
| """ | ||
|
|
||
| from clickhouse_driver import Client as Clickhouse | ||
| from datetime import datetime, timedelta | ||
| import logging | ||
| import orjson | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def run_volume_analysis( | ||
| clickhouse_url: str, start_time: datetime, end_time: datetime, threshold: int | ||
| ): | ||
|
|
||
| db = Clickhouse.from_url(clickhouse_url) | ||
| query = """ | ||
| SELECT | ||
| probe_cc, probe_asn, engine_version, | ||
| software_name, software_version, platform, architecture, | ||
| toStartOfMinute(measurement_start_time) as minute_start, | ||
| count() as total | ||
| FROM fastpath | ||
| WHERE | ||
| measurement_start_time >= %(start_time)s AND | ||
| measurement_start_time < %(end_time)s | ||
| GROUP BY probe_cc, probe_asn, engine_version, software_name, software_version, platform, architecture, minute_start | ||
| HAVING total >= %(treshold)s | ||
| """ | ||
|
|
||
| rows = ( | ||
| db.execute( | ||
| query, | ||
| params={ | ||
| "start_time": start_time, | ||
| "end_time": end_time, | ||
| "treshold": threshold, | ||
| }, | ||
| ) | ||
| or [] | ||
| ) | ||
|
|
||
| if len(rows) == 0: | ||
| log.info("No volume anomalies where found") | ||
| return | ||
|
|
||
| # Prepare results to insert | ||
| values = [] | ||
| for row in rows: | ||
|
|
||
| ( | ||
| probe_cc, | ||
| probe_asn, | ||
| engine_version, | ||
| software_name, | ||
| software_version, | ||
| platform, | ||
| architecture, | ||
| minute_start, | ||
| total, | ||
| ) = row | ||
|
|
||
| # Calculate end_time as start of next minute | ||
| minute_end = minute_start + timedelta(minutes=1) | ||
|
|
||
| details = { | ||
| "start_time": minute_start.isoformat(), | ||
| "end_time": minute_end.isoformat(), | ||
| "engine_version": engine_version, | ||
| "software_name": software_name, | ||
| "software_version": software_version, | ||
| "platform": platform, | ||
| "architecture": architecture, | ||
| "total": total, | ||
| "threshold": threshold, | ||
| } | ||
|
|
||
| values.append(("volume", probe_cc, probe_asn, orjson.dumps(details).decode())) | ||
|
|
||
| db.execute( | ||
| "INSERT INTO faulty_measurements (type, probe_cc, probe_asn, details) VALUES", | ||
| values, | ||
| types_check=True, | ||
| ) |
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
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
35 changes: 35 additions & 0 deletions
35
oonipipeline/src/oonipipeline/tasks/time_inconsistencies.py
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,35 @@ | ||
| """ | ||
| This file defines the task for the time inconsistencies analysis for faulty | ||
| measurements detection | ||
| """ | ||
|
|
||
| from dataclasses import dataclass | ||
| from datetime import datetime, timedelta, timezone | ||
| from ..analysis.time_inconsistencies import run_time_inconsistencies_analysis | ||
|
|
||
|
|
||
| @dataclass | ||
| class MakeTimeInconsistenciesParams: | ||
| clickhouse_url: str | ||
| timestamp: str | ||
| threshold: int | ||
|
|
||
|
|
||
| def make_time_inconsistencies_analysis(params: MakeTimeInconsistenciesParams): | ||
| if "T" in params.timestamp: | ||
| start_time = (datetime.strptime(params.timestamp, "%Y-%m-%dT%H")).replace( | ||
| tzinfo=timezone.utc | ||
| ) | ||
| end_time = start_time + timedelta(hours=1) | ||
| else: | ||
| start_time = (datetime.strptime(params.timestamp, "%Y-%m-%d")).replace( | ||
| tzinfo=timezone.utc | ||
| ) | ||
| end_time = start_time + timedelta(days=1) | ||
|
|
||
| run_time_inconsistencies_analysis( | ||
| clickhouse_url=params.clickhouse_url, | ||
| start_time=start_time, | ||
| end_time=end_time, | ||
| threshold=params.threshold, | ||
| ) |
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,35 @@ | ||
| """ | ||
| This file defines the task for the volume analysis for faulty | ||
| measurements detection | ||
| """ | ||
|
|
||
| from dataclasses import dataclass | ||
| from datetime import datetime, timedelta, timezone | ||
| from ..analysis.volume import run_volume_analysis | ||
|
|
||
|
|
||
| @dataclass | ||
| class MakeVolumeParams: | ||
| clickhouse_url: str | ||
| timestamp: str | ||
| threshold: int | ||
|
|
||
|
|
||
| def make_volume_analysis(params: MakeVolumeParams): | ||
| if "T" in params.timestamp: | ||
| start_time = (datetime.strptime(params.timestamp, "%Y-%m-%dT%H")).replace( | ||
| tzinfo=timezone.utc | ||
| ) | ||
| end_time = start_time + timedelta(hours=1) | ||
| else: | ||
| start_time = (datetime.strptime(params.timestamp, "%Y-%m-%d")).replace( | ||
| tzinfo=timezone.utc | ||
| ) | ||
| end_time = start_time + timedelta(days=1) | ||
|
|
||
| run_volume_analysis( | ||
| clickhouse_url=params.clickhouse_url, | ||
| start_time=start_time, | ||
| end_time=end_time, | ||
| threshold=params.threshold, | ||
| ) |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should separate the checks for measurements from the future, from the ones from the past. Measurements from the future should never happen and are a sign of a probe with a faulty clock. Those from the past may be normal, since probes might be re-uploading measurements later.