Log to DB, even if it's a small outage

This commit is contained in:
Liam Steckler 2024-01-29 22:42:12 -05:00
parent b5bb71e099
commit ea5d52494e

59
scl.py
View file

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