-
Notifications
You must be signed in to change notification settings - Fork 18
Move backfill-metric to a command #455
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
jgraham
wants to merge
2
commits into
main
Choose a base branch
from
webcompat_command_class
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
2 commits
Select commit
Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion
2
...mpat-kb/data/sql/webcompat_knowledge_base/tables/webcompat_topline_metric_daily/meta.toml
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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| name = "webcompat_topline_metric_daily" | ||
| etl = ["metric", "metric-backfill"] | ||
| etl = ["metric"] |
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
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
104 changes: 104 additions & 0 deletions
104
jobs/webcompat-kb/webcompat_kb/commands/backfill_metric.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,104 @@ | ||
| import argparse | ||
| import logging | ||
| import os | ||
| from typing import Optional | ||
|
|
||
| from .. import projectdata | ||
| from ..base import Command | ||
| from ..bqhelpers import BigQuery, DatasetId, get_client | ||
| from ..config import Config | ||
| from ..projectdata import Project | ||
|
|
||
|
|
||
| def backfill_metric_daily( | ||
| project: Project, | ||
| client: BigQuery, | ||
| write: bool, | ||
| metric_name: str, | ||
| ) -> None: | ||
| metric_dfns, metric_types = project.data.metric_dfns, project.data.metric_types | ||
| daily_metric_types = [ | ||
| metric_type for metric_type in metric_types if "daily" in metric_type.contexts | ||
| ] | ||
|
|
||
| metric = None | ||
| for metric in metric_dfns: | ||
| if metric.name == metric_name: | ||
| break | ||
| else: | ||
| raise ValueError(f"Metric named {metric_name} not found") | ||
|
|
||
| select_fields = [] | ||
| field_names = [] | ||
| conditions = [] | ||
| for metric_type in daily_metric_types: | ||
| field_name = f"{metric_type.name}_{metric.name}" | ||
| field_names.append(field_name) | ||
| select_fields.append( | ||
| f"{metric_type.agg_function('bugs', metric)} AS {field_name}" | ||
| ) | ||
| conditions.append(f"metric_daily.{field_name} IS NULL") | ||
| select_query = f""" | ||
| SELECT | ||
| date, | ||
| {",\n ".join(select_fields)} | ||
| FROM | ||
| `{project["webcompat_knowledge_base"]["scored_site_reports"]}` AS bugs | ||
| JOIN `{project["webcompat_knowledge_base"]["webcompat_topline_metric_daily"]}` as metric_daily | ||
| ON | ||
| DATE(bugs.creation_time) <= metric_daily.date | ||
| AND IF (bugs.resolved_time IS NOT NULL, DATE(bugs.resolved_time) >= date, TRUE) | ||
| WHERE | ||
| {metric.condition("bugs")} AND {" AND ".join(conditions)} | ||
| GROUP BY | ||
| date | ||
| ORDER BY date""" | ||
|
|
||
| update_query = f""" | ||
| UPDATE `{project["webcompat_knowledge_base"]["webcompat_topline_metric_daily"]}` AS metric_daily | ||
| SET | ||
| {",\n ".join(f"metric_daily.{field_name}=new_data.{field_name}" for field_name in field_names)} | ||
| FROM ({select_query}) AS new_data | ||
| WHERE new_data.date = metric_daily.date | ||
| """ | ||
|
|
||
| if write: | ||
| result = client.query(update_query) | ||
| else: | ||
| logging.info(f"Would run query:\n{update_query}") | ||
| result = client.query(select_query) | ||
| logging.info(f"Would set {list(result)}") | ||
|
|
||
|
|
||
| class BackfillMetric(Command): | ||
| def argument_parser(self) -> argparse.ArgumentParser: | ||
| parser = super().argument_parser() | ||
| parser.add_argument("metric", action="store", help="Metric name to update") | ||
| return parser | ||
|
|
||
| def main(self, args: argparse.Namespace) -> Optional[int]: | ||
| client = get_client(args.bq_project_id) | ||
| config = Config(write=args.write, stage=args.stage) | ||
| project = projectdata.load( | ||
| client, args.bq_project_id, os.path.normpath(args.data_path), set(), config | ||
| ) | ||
| if args.metric not in project.data.metric_dfns: | ||
| raise ValueError(f"Unknown metric {args.metric}") | ||
|
|
||
| bq_client = BigQuery( | ||
| client, | ||
| DatasetId(args.bq_project_id, "webcompat_knowledge_base"), | ||
| args.write, | ||
| None, | ||
| ) | ||
|
|
||
| backfill_metric_daily( | ||
| project, | ||
| bq_client, | ||
| config.write, | ||
| args.metric, | ||
| ) | ||
| return None | ||
|
|
||
|
|
||
| main = BackfillMetric() | ||
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
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.
should it check metric.name here? something like:
if args.metric not in [m.name for m in project.data.metric_dfns]