Repository navigation
Bug 2077133 - Parse and validate flows.yaml and emit flow registration data #868
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
travis79
wants to merge
1
commit into
main
Choose a base branch
from
Bug2077133
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
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
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,13 @@ | ||
| ``flows.yaml`` file | ||
| ====================== | ||
|
|
||
| Documentation for the ``flows.yaml`` file is in the `flows YAML registry format in the Glean user documentation <https://mozilla.github.io/glean/book/reference/yaml/flows.html>`_. | ||
|
|
||
| JSON Schema | ||
| ----------- | ||
|
|
||
| There is a formal schema for validating ``flows.yaml`` files, included in its | ||
| entirety below: | ||
|
|
||
| .. literalinclude:: ../glean_parser/schemas/flows.1-0-0.schema.yaml | ||
| :language: yaml |
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
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,177 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| """ | ||
| Classes for each of the high-level flow types. | ||
| """ | ||
|
|
||
| from typing import Any, Dict, List, Mapping, Optional, Set | ||
|
|
||
| from . import util | ||
|
|
||
|
|
||
| # Milliseconds per `auto_close_timeout` unit. | ||
| TIMEOUT_UNIT_MS = { | ||
| "s": 1000, | ||
| "m": 60 * 1000, | ||
| "h": 60 * 60 * 1000, | ||
| "d": 24 * 60 * 60 * 1000, | ||
| } | ||
|
|
||
| # The roles an event can play in a flow, in the order they are listed in | ||
| # `flows.yaml`. | ||
| ROLES = ("starts_on", "ends_on", "includes") | ||
|
|
||
|
|
||
| class Flow: | ||
| def __init__( | ||
| self, | ||
| name: str, | ||
| description: str, | ||
| bugs: List[str], | ||
| data_reviews: List[str], | ||
| notification_emails: List[str], | ||
| starts_on: List[str], | ||
| ends_on: Optional[List[str]] = None, | ||
| includes: Optional[List[str]] = None, | ||
| auto_close_timeout: Optional[str] = None, | ||
| disabled: bool = False, | ||
| defined_in: Optional[Dict] = None, | ||
| no_lint: Optional[List[str]] = None, | ||
| _validated: bool = False, | ||
| ): | ||
| # Avoid cyclical import | ||
| from . import parser | ||
|
|
||
| self.name = name | ||
| self.description = description | ||
| self.bugs = bugs | ||
| self.data_reviews = data_reviews | ||
| self.notification_emails = notification_emails | ||
| self.starts_on = starts_on | ||
| self.ends_on = ends_on if ends_on is not None else [] | ||
| self.includes = includes if includes is not None else [] | ||
| self.auto_close_timeout = auto_close_timeout | ||
| self.disabled = disabled | ||
| self.defined_in = defined_in | ||
| if no_lint is None: | ||
| no_lint = [] | ||
| self.no_lint = no_lint | ||
|
|
||
| # The events this flow names that are not defined in any of the files | ||
| # given to the parser, and are assumed to be defined elsewhere. Filled | ||
| # in once every file has been parsed. | ||
| self.unresolved: Set[str] = set() | ||
|
|
||
| # _validated indicates whether this flow has already been jsonschema | ||
| # validated (but not any of the Python-level validation). | ||
| if not _validated: | ||
| data: Dict[str, util.JSONType] = { | ||
| "$schema": parser.FLOWS_ID, | ||
| self.name: self._serialize_input(), | ||
| } | ||
| for error in parser.validate(data): | ||
| raise ValueError(error) | ||
|
|
||
| if not self.starts_on: | ||
| raise ValueError("A flow must have at least one event in `starts_on`.") | ||
|
|
||
| # An event may play at most one role in a flow. | ||
| seen: Dict[str, str] = {} | ||
| for role in ROLES: | ||
| for event in getattr(self, role): | ||
| if event in seen: | ||
| raise ValueError( | ||
| f"Event '{event}' is in both `{seen[event]}` and `{role}`. " | ||
| "An event may appear in at most one of a flow's lists." | ||
| ) | ||
| seen[event] = role | ||
|
|
||
| @property | ||
| def type(self) -> str: | ||
| return "flow" | ||
|
|
||
| @property | ||
| def auto_close_timeout_ms(self) -> Optional[int]: | ||
| """ | ||
| The `auto_close_timeout`, in milliseconds, or `None` if unset. | ||
| """ | ||
| if self.auto_close_timeout is None: | ||
| return None | ||
| value, unit = self.auto_close_timeout[:-1], self.auto_close_timeout[-1] | ||
| return int(value) * TIMEOUT_UNIT_MS[unit] | ||
|
|
||
| def events(self) -> Dict[str, str]: | ||
| """ | ||
| Every event this flow names, mapped to its role: `starts_on`, | ||
| `ends_on`, or `includes`. | ||
| """ | ||
| return {event: role for role in ROLES for event in getattr(self, role)} | ||
|
|
||
| def _serialize_input(self) -> Dict[str, util.JSONType]: | ||
| d = self.serialize() | ||
| modified_dict = util.remove_output_params(d, "defined_in") | ||
| modified_dict = util.remove_output_params(modified_dict, "unresolved") | ||
| return modified_dict | ||
|
|
||
| def serialize(self) -> Dict[str, util.JSONType]: | ||
| """ | ||
| Serialize the flow back to JSON object model. | ||
| """ | ||
| d = self.__dict__.copy() | ||
| del d["name"] | ||
| d["unresolved"] = sorted(self.unresolved) | ||
| if d["auto_close_timeout"] is None: | ||
| del d["auto_close_timeout"] | ||
| return d | ||
|
|
||
|
|
||
| def flows_in(objs: Mapping[str, Any]) -> Dict[str, "Flow"]: | ||
| """ | ||
| Returns the flows in a parsed object tree, keyed by name. | ||
|
|
||
| Flows are stored under the `flows` key, which is also a valid metric | ||
| category name. The key holds flows only if it holds `Flow` objects; the | ||
| parser rejects a tree that mixes the two. | ||
| """ | ||
| candidate = objs.get("flows") | ||
| if not candidate: | ||
| return {} | ||
| if not all(isinstance(obj, Flow) for obj in candidate.values()): | ||
| return {} | ||
| return candidate | ||
|
|
||
|
|
||
| # The SDK's name for each role in a flow. | ||
| _REGISTRATION_ROLES = {"starts_on": "Start", "ends_on": "End", "includes": "Member"} | ||
|
|
||
|
|
||
| def registration_data(flows: Mapping[str, "Flow"]) -> List[Dict[str, Any]]: | ||
| """ | ||
| The flow registration data the Glean SDK loads at initialization, in a | ||
| form the code generators can render directly. | ||
|
|
||
| Flows are emitted as authored, flow to events, in name order. Each event | ||
| carries its role and whether it was resolved; the SDK builds the reverse | ||
| index itself. | ||
| """ | ||
| return [ | ||
| { | ||
| "name": flow.name, | ||
| "description": flow.description, | ||
| "events": [ | ||
| { | ||
| "identifier": event, | ||
| "role": _REGISTRATION_ROLES[role], | ||
| "unresolved": event in flow.unresolved, | ||
| } | ||
| for event, role in sorted(flow.events().items()) | ||
| ], | ||
| "auto_close_timeout_ms": flow.auto_close_timeout_ms, | ||
| "disabled": flow.disabled, | ||
| } | ||
| for _, flow in sorted(flows.items()) | ||
| ] |
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.
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.
What attempt? What instance?
The words here are not clear what they refer to.