Log to DB, even if it's a small outage
This commit is contained in:
parent
b5bb71e099
commit
ea5d52494e
1 changed files with 153 additions and 136 deletions
59
scl.py
59
scl.py
|
@ -89,9 +89,10 @@ class SclOutage(Base):
|
|||
outage_size: Mapped[str] = mapped_column()
|
||||
status: Mapped[Optional[str]] = mapped_column()
|
||||
no_longer_in_response_time: Mapped[Optional[datetime]] = mapped_column()
|
||||
start_time: Mapped[datetime] = mapped_column()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"SclOutage(scl_outage_id={self.scl_outage_id!r}, most_recent_post_id={self.most_recent_post_id!r}, last_updated_time={self.last_updated_time!r}, no_longer_in_response_time={self.no_longer_in_response_time!r})"
|
||||
return f"SclOutage(scl_outage_id={self.scl_outage_id!r}, most_recent_post_id={self.most_recent_post_id!r}, last_updated_time={self.last_updated_time!r}, no_longer_in_response_time={self.no_longer_in_response_time!r}), start_time={self.start_time!r})"
|
||||
|
||||
|
||||
engine = create_engine("sqlite:///scl.db")
|
||||
|
@ -183,12 +184,12 @@ with Session(engine) as session:
|
|||
session.commit()
|
||||
|
||||
except NoResultFound:
|
||||
print("Existing record not found")
|
||||
if outage_size == "Small":
|
||||
# If the outage becomes medium/large, it'll then be posted as a new outage on the next run
|
||||
print("Outage is small, will not post")
|
||||
continue
|
||||
print("Existing record not found")
|
||||
|
||||
else:
|
||||
# Fallback location from the SCL API in case one couldn't be reverse geocoded
|
||||
area_text = event["city"]
|
||||
|
||||
|
@ -202,7 +203,10 @@ with Session(engine) as session:
|
|||
assert event["polygons"]["type"] == "polygon"
|
||||
for ring in event["polygons"]["rings"]:
|
||||
polygon = Polygon(
|
||||
ring, "{}7F".format(outage_color), outage_color, simplify=True
|
||||
ring,
|
||||
"{}7F".format(outage_color),
|
||||
outage_color,
|
||||
simplify=True,
|
||||
)
|
||||
map.add_polygon(polygon)
|
||||
map_image = map.render()
|
||||
|
@ -212,7 +216,9 @@ with Session(engine) as session:
|
|||
def num2deg(xtile, ytile, zoom):
|
||||
n = 1 << zoom
|
||||
lon_deg = xtile / n * 360.0 - 180.0
|
||||
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n)))
|
||||
lat_rad = math.atan(
|
||||
math.sinh(math.pi * (1 - 2 * ytile / n))
|
||||
)
|
||||
lat_deg = math.degrees(lat_rad)
|
||||
return lat_deg, lon_deg
|
||||
|
||||
|
@ -230,7 +236,9 @@ with Session(engine) as session:
|
|||
lat=center_lat_lon[0],
|
||||
lon=center_lat_lon[1],
|
||||
)
|
||||
geocode_headers = {"User-Agent": "seattlecitylight-mastodon-bot"}
|
||||
geocode_headers = {
|
||||
"User-Agent": "seattlecitylight-mastodon-bot"
|
||||
}
|
||||
geocode_response = requests.get(
|
||||
geocode_url, headers=geocode_headers
|
||||
)
|
||||
|
@ -245,20 +253,25 @@ with Session(engine) as session:
|
|||
!= "Seattle"
|
||||
):
|
||||
city_not_seattle_text = " of {}".format(
|
||||
geocode["features"][0]["properties"]["geocoding"]["city"]
|
||||
geocode["features"][0]["properties"]["geocoding"][
|
||||
"city"
|
||||
]
|
||||
)
|
||||
else:
|
||||
city_not_seattle_text = ""
|
||||
|
||||
street = geocode["features"][0]["properties"]["geocoding"]["name"]
|
||||
street = geocode["features"][0]["properties"]["geocoding"][
|
||||
"name"
|
||||
]
|
||||
|
||||
if (
|
||||
"locality" in geocode["features"][0]["properties"]["geocoding"]
|
||||
"locality"
|
||||
in geocode["features"][0]["properties"]["geocoding"]
|
||||
and outage_size != "Large"
|
||||
):
|
||||
locality = geocode["features"][0]["properties"]["geocoding"][
|
||||
"locality"
|
||||
]
|
||||
locality = geocode["features"][0]["properties"][
|
||||
"geocoding"
|
||||
]["locality"]
|
||||
if locality == "Uptown":
|
||||
locality = "Lower Queen Anne"
|
||||
|
||||
|
@ -271,7 +284,8 @@ with Session(engine) as session:
|
|||
locality, city_not_seattle_text
|
||||
)
|
||||
elif (
|
||||
"district" in geocode["features"][0]["properties"]["geocoding"]
|
||||
"district"
|
||||
in geocode["features"][0]["properties"]["geocoding"]
|
||||
):
|
||||
alt_text = "A map showing the location of the outage, centered around {} in the {} area{}.".format(
|
||||
street,
|
||||
|
@ -289,11 +303,13 @@ with Session(engine) as session:
|
|||
else:
|
||||
alt_text = "A map showing the location of the outage, centered around {} in {}.".format(
|
||||
street,
|
||||
geocode["features"][0]["properties"]["geocoding"]["city"],
|
||||
)
|
||||
area_text = geocode["features"][0]["properties"]["geocoding"][
|
||||
geocode["features"][0]["properties"]["geocoding"][
|
||||
"city"
|
||||
]
|
||||
],
|
||||
)
|
||||
area_text = geocode["features"][0]["properties"][
|
||||
"geocoding"
|
||||
]["city"]
|
||||
except Exception:
|
||||
alt_text = "A map showing the location of the outage."
|
||||
|
||||
|
@ -314,11 +330,11 @@ with Session(engine) as session:
|
|||
|
||||
post_text = """Seattle City Light is reporting a {} outage in {}.
|
||||
|
||||
Start Date: {}
|
||||
Est. Restoration: {}
|
||||
Cause: {}
|
||||
Start Date: {}
|
||||
Est. Restoration: {}
|
||||
Cause: {}
|
||||
|
||||
{}""".format(
|
||||
{}""".format(
|
||||
outage_size.lower(),
|
||||
area_text,
|
||||
start_time.strftime(post_datetime_format),
|
||||
|
@ -349,6 +365,7 @@ Cause: {}
|
|||
cause=event["cause"],
|
||||
status=status,
|
||||
outage_size=outage_size,
|
||||
start_time=start_time,
|
||||
)
|
||||
session.add(new_outage_record)
|
||||
session.commit()
|
||||
|
|
Loading…
Reference in a new issue