Skip to content
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

Fix Ruff Errors Across the Codebase #3

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.12
3.12.7
8 changes: 2 additions & 6 deletions edge_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,17 @@
if "ENVIRONMENT" in os.environ:
app.config.from_envvar("ENVIRONMENT")

# Set optional bootswatch theme
app.config["FLASK_ADMIN_SWATCH"] = "cerulean"

app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql+psycopg2://{}@/{}".format(
app.config["DBUSER"],
app.config["DBNAME"],
)

# engine = create_engine("postgresql+psycopg2://%s@/%s"%(app.config['DBUSER'], app.config['DBNAME']), echo=True)
# SessionMaker = scoped_session(sessionmaker(bind=engine))
db.init_app(app)


with app.app_context():
# Base.metadata.create_all(engine)
db.metadata.create_all(db.engine)

from alembic import command, config
Expand Down Expand Up @@ -73,7 +69,6 @@

admin = Admin(app, name="Risk Assesment", template_mode="bootstrap3")

# work with session
admin.add_view(RiskVectorModelView(db.session))
admin.add_view(TestModelView(db.session))
admin.add_view(ModelView(GpsData, db.session))
Expand All @@ -88,7 +83,8 @@
@click.command()
@click.option("--port", default=50000)
def serve(port: int | str) -> None:
app.run(host="0.0.0.0", port=port)
# Intentionally binding to all interfaces - ensure proper firewall rules are in place
app.run(host="0.0.0.0", port=port) # noqa: S104


if __name__ == "__main__":
Expand Down
67 changes: 31 additions & 36 deletions misc/vector_data_as_cloudwatch_metrics_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

custommetrics = boto3.client("cloudwatch")

MAX_METRICS_PER_REQUEST = 1000 # AWS CloudWatch limit for metrics per request
EXCLUDED_VECTOR_ID = 3


def has_query_succeeded(execution_id):
state = "RUNNING"
Expand All @@ -33,69 +36,61 @@ def gen_put_metric_requests(vector_id, value_timestamp_pairs):
request = None
for value, timestamp in value_timestamp_pairs:
if request is None:
request = {"Namespace": "tnc_edge_brancol_v1", "MetricData": []}
request["MetricData"].append(
request = {"Namespace": "tnc_edge_brancol_v1", "MetricData": []} # MetricData is already a list
request["MetricData"].append( # type: ignore # noqa: PGH003
{
"MetricName": f"vector_{vector_id}",
"Value": value,
"Timestamp": timestamp,
}
)
if len(request["MetricData"]) >= 1000:
if len(request["MetricData"]) >= MAX_METRICS_PER_REQUEST:
yield request
request = None
if request:
yield request


def main():
# 5. Query Athena table
query = "SELECT vector_id, score, datetime from tnc_edge.brancol_v1_tests"
response = athena.start_query_execution(
QueryString=query, ResultConfiguration={"OutputLocation": "s3://51-gema-dev-athena/"}
)

execution_id = response["QueryExecutionId"]
print(f"Get Num Rows execution id: {execution_id}")

query_status = has_query_succeeded(execution_id=execution_id)
print(f"Query state: {query_status}")

paginator = athena.get_paginator("get_query_results")
page_iterator = paginator.paginate(QueryExecutionId=execution_id)

def gen_results():
for page in page_iterator:
if len(page["ResultSet"]["Rows"]) > 1:
for row in page["ResultSet"]["Rows"][1:]:
yield row

def process_query_results(page_iterator):
grouped = {}
for row in gen_results():
for row in (row for page in page_iterator for row in page["ResultSet"]["Rows"][1:]):
vector_id = row["Data"][0]["VarCharValue"]
if vector_id not in grouped:
grouped[vector_id] = []
value = row["Data"][1].get("VarCharValue")
try:
value = float(value)
except:
except ValueError:
continue
timestamp = row["Data"][2].get("VarCharValue")
if timestamp is None:
if timestamp is None or timestamp <= dateparse("2023-10-20 23:00:00Z"):
continue

timestamp = dateparse(timestamp)
if timestamp <= dateparse("2023-10-20 23:00:00Z"):
continue
grouped[vector_id].append((value, timestamp))
return grouped


def main():
# 5. Query Athena table
query = "SELECT vector_id, score, datetime from tnc_edge.brancol_v1_tests"
response = athena.start_query_execution(
QueryString=query,
ResultConfiguration={"OutputLocation": "s3://51-gema-dev-athena/"}
)

execution_id = response["QueryExecutionId"]
print(f"Get Num Rows execution id: {execution_id}")

if not has_query_succeeded(execution_id=execution_id):
return

paginator = athena.get_paginator("get_query_results")
grouped = process_query_results(paginator.paginate(QueryExecutionId=execution_id))

for vector_id, value_timestamp_pairs in grouped.items():
if int(vector_id) == 3:
if int(vector_id) == EXCLUDED_VECTOR_ID:
continue
# metric_name = 'tnc_edge_brancol_v1_vector_{}'.format(vector_id)
for request in gen_put_metric_requests(
vector_id=vector_id, value_timestamp_pairs=value_timestamp_pairs
):
for request in gen_put_metric_requests(vector_id=vector_id, value_timestamp_pairs=value_timestamp_pairs):
print("putting {} values on ")
response = custommetrics.put_metric_data(**request)
print(response)
Expand Down
1 change: 1 addition & 0 deletions model/aifishdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AifishData(Base):
status = Column(String)

def __str__(self) -> str:
"""Return a string representation of the AifishData object."""
return (
"AifishData("
+ ", ".join(
Expand Down
1 change: 1 addition & 0 deletions model/boatschedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BoatSchedule(Base):
datetime = Column(DateTime(timezone=True), server_default=text("CURRENT_TIMESTAMP"))

def __str__(self) -> str:
"""Return string representation of BoatSchedule instance."""
return (
"BoatSchedule("
+ ", ".join(
Expand Down
1 change: 0 additions & 1 deletion model/deckhandeventview.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class DeckhandEventView(Base):
__tablename__ = "deckhandevents_mostrecentlonglineevent_jsonextracted"

id = Column(Integer, primary_key=True)
# jsonblob = Column(String)
datetime = Column(DateTime(timezone=True), server_default=text("CURRENT_TIMESTAMP"))

bycatchcount = Column(Integer)
Expand Down
1 change: 1 addition & 0 deletions model/gpsdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class GpsData(Base):
datetime = Column(DateTime(timezone=True), server_default=text("CURRENT_TIMESTAMP"))

def __str__(self) -> str:
"""Return string representation of GpsData object."""
return (
"GpsData("
+ ", ".join(
Expand Down
11 changes: 3 additions & 8 deletions model/internetdata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from flask_admin.contrib.sqla import ModelView
from sqlalchemy import Column, DateTime, Float, Integer, String, text

from .base import Base
Expand All @@ -12,9 +13,9 @@ class InternetData(Base):
packetloss = Column(Float())
returncode = Column(Integer())
datetime = Column(DateTime(timezone=True), server_default=text("CURRENT_TIMESTAMP"))
# fk = ForeignKeyConstraint(['id'], [RiskVector.id])

def __str__(self) -> str:
"""Return string representation of InternetData instance."""
return (
"InternetData("
+ ", ".join(
Expand All @@ -33,10 +34,6 @@ def __str__(self) -> str:
+ ")"
)


from flask_admin.contrib.sqla import ModelView


class InternetDataView(ModelView):
def __init__(
self,
Expand All @@ -49,7 +46,7 @@ def __init__(
menu_class_name=None,
menu_icon_type=None,
menu_icon_value=None,
):
) -> None:
super().__init__(
InternetData,
session,
Expand All @@ -67,5 +64,3 @@ def __init__(
column_display_pk = True
column_hide_backrefs = False
column_list = ["id", "traceroute", "ping", "packetloss", "returncode", "datetime"]
# column_searchable_list = ["name"]
# inline_models = (RiskVector,)
2 changes: 1 addition & 1 deletion model/ondeckdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class OndeckData(Base):
detection_confidence = Column(REAL)

def __str__(self) -> str:
"""Return a string representation of the OndeckData instance."""
return (
"OndeckData("
+ ", ".join(
Expand All @@ -30,7 +31,6 @@ def __str__(self) -> str:
for n in [
"id",
"video_uri",
# 'video_file',
"cocoannotations_uri",
"datetime",
"overallcount",
Expand Down
8 changes: 3 additions & 5 deletions model/riskvector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from flask_admin.contrib.sqla import ModelView
from flask_admin.model.template import EndpointLinkRowAction
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship
Expand All @@ -15,6 +16,7 @@ class RiskVector(Base):
tests = relationship("Test", back_populates="vector")

def __str__(self) -> str:
"""Return string representation of RiskVector instance."""
return (
"RiskVector("
+ ", ".join(
Expand All @@ -26,10 +28,6 @@ def __str__(self) -> str:
+ ")"
)


from flask_admin.contrib.sqla import ModelView


class RiskVectorModelView(ModelView):
def __init__(
self,
Expand All @@ -42,7 +40,7 @@ def __init__(
menu_class_name=None,
menu_icon_type=None,
menu_icon_value=None,
):
) -> None:
super().__init__(
RiskVector,
session,
Expand Down
11 changes: 3 additions & 8 deletions model/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum as PyEnum

from flask_admin.contrib.sqla import ModelView
from sqlalchemy import Column, DateTime, Enum, Float, ForeignKey, Integer, String, text
from sqlalchemy.orm import relationship

Expand All @@ -26,9 +27,9 @@ class Test(Base):
datetime_from = Column(DateTime(timezone=True))
datetime_to = Column(DateTime(timezone=True))
datetime = Column(DateTime(timezone=True), server_default=text("CURRENT_TIMESTAMP"))
# fk = ForeignKeyConstraint(['id'], [RiskVector.id])

def __str__(self) -> str:
"""Return string representation of Test object."""
return (
"Test("
+ ", ".join(
Expand All @@ -46,10 +47,6 @@ def __str__(self) -> str:
+ ")"
)


from flask_admin.contrib.sqla import ModelView


class TestModelView(ModelView):
def __init__(
self,
Expand All @@ -62,7 +59,7 @@ def __init__(
menu_class_name=None,
menu_icon_type=None,
menu_icon_value=None,
):
) -> None:
super().__init__(
Test,
session,
Expand Down Expand Up @@ -92,5 +89,3 @@ def __init__(
]
column_searchable_list = ["name"]
column_filters = ["vector_id", "datetime"]
# column_select_related_list=['vector']
# inline_models = (RiskVector,)
6 changes: 1 addition & 5 deletions model/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ class Track(Base):

id = Column(Integer, primary_key=True)
video_uri = Column(String)
# video_uri = Column(String, ForeignKey("video_files.decrypted_path"))
# video_file = relationship(VideoFile)
cocoannotations_uri = Column(String)
# cocoannotations_uri = Column(String, ForeignKey("ondeckdata.cocoannotations_uri"))
# ondeckdata = relationship(OndeckData)
track_id = Column(Integer)
first_framenum = Column(Integer)
last_framenum = Column(Integer)
confidences = Column(ARRAY(REAL))

datetime = Column(DateTime(timezone=True), server_default=text("CURRENT_TIMESTAMP"))
# detection_confidence = Column(REAL)

def __str__(self) -> str:
"""Return a string representation of the Track object."""
return (
"Track("
+ ", ".join(
Expand Down
3 changes: 1 addition & 2 deletions model/videofiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ class VideoFile(Base):
reencoded_stderr = Column(VARCHAR(), autoincrement=False, nullable=True)
cam_name = Column(VARCHAR(), nullable=True)

# ondeckdata = relationship("OndeckData", back_populates="video_file")

def __str__(self) -> str:
"""Return string representation of VideoFile instance."""
return (
"VideoFile("
+ ", ".join(
Expand Down
Loading
Loading