-
Notifications
You must be signed in to change notification settings - Fork 4
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
Store outages in Outages.yaml #6
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- name: MGHPCC Shutdown 2024 | ||
information: https://nerc.mghpcc.org/event/mghpcc-annual-power-shutdown-2024/ | ||
timeframes: | ||
- start: "2024-05-22T08:00:00-0400" | ||
end: "2024-05-28T23:00:00-0400" |
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. I think I would either merge these models into |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||
from typing import Annotated | ||||||
from datetime import datetime, timezone | ||||||
|
||||||
import pydantic | ||||||
|
||||||
from .models import Base | ||||||
|
||||||
|
||||||
class OutageTimeFrames(Base): | ||||||
start: datetime | ||||||
end: datetime | ||||||
Comment on lines
+10
to
+11
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. In the Rate model, we use attributes 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. For rate I chose |
||||||
|
||||||
|
||||||
class OutageItemDict(Base): | ||||||
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. This is not actually a dicitonary (unlike
Suggested change
|
||||||
name: str | ||||||
information: str | ||||||
timeframes: list[OutageTimeFrames] | ||||||
|
||||||
|
||||||
class Outages(pydantic.RootModel): | ||||||
root: list[OutageItemDict] | ||||||
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.
Suggested change
|
||||||
|
||||||
def get_outages_during(self, start, end): | ||||||
start = datetime.strptime(start, "%Y-%m-%d").astimezone(timezone.utc) | ||||||
end = datetime.strptime(end, "%Y-%m-%d").astimezone(timezone.utc) | ||||||
|
||||||
outages = [] | ||||||
for outage in self.root: | ||||||
for o in outage.timeframes: | ||||||
if start < o.start < end or start < o.end < end: | ||||||
outages.append((max(start, o.start), min(end, o.end))) | ||||||
|
||||||
return outages |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import requests | ||
import yaml | ||
|
||
from .models_outages import Outages | ||
|
||
DEFAULT_OUTAGES_FILE = "outages.yaml" | ||
DEFAULT_OUTAGES_URL = ( | ||
"https://raw.githubusercontent.com/CCI-MOC/nerc-outages/main/outages.yaml" | ||
) | ||
|
||
|
||
def load_from_url(url: str | None = None) -> Outages: | ||
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. Do we want the outages file to be loaded separately from the rates file, or should a single call to |
||
if url is None: | ||
url = DEFAULT_OUTAGES_URL | ||
|
||
r = requests.get(url, allow_redirects=True) | ||
r.raise_for_status() | ||
config = yaml.safe_load(r.content.decode("utf-8")) | ||
return Outages.model_validate(config) | ||
|
||
|
||
def load_from_file(path: str | None = None) -> Outages: | ||
if path is None: | ||
path = DEFAULT_OUTAGES_FILE | ||
|
||
with open(path, "r") as f: | ||
config = yaml.safe_load(f) | ||
return Outages.model_validate(config) |
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.
If this is always a URL, I would just call it "url" (and we would validate as such).