Skip to content

Commit

Permalink
Add Sentry.io support
Browse files Browse the repository at this point in the history
  • Loading branch information
kdknigga committed Dec 2, 2023
1 parent b73a5a5 commit dc6f009
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 22 deletions.
50 changes: 49 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ requests-cache = "^1.0.1"
schedule = "^1.2.0"
skyfield = "^1.46"
timezonefinder = {extras = ["numba"], version = "^6.2.0"}
sentry-sdk = {extras = ["flask"], version = "^1.37.1"}

[tool.poetry.group.dev.dependencies]
black = "^23.3.0"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ python-dateutil==2.8.2 ; python_version >= "3.9" and python_version < "4.0"
requests-cache==1.1.0 ; python_version >= "3.9" and python_version < "4.0"
requests==2.31.0 ; python_version >= "3.9" and python_version < "4.0"
schedule==1.2.1 ; python_version >= "3.9" and python_version < "4.0"
sentry-sdk[flask]==1.37.1 ; python_version >= "3.9" and python_version < "4.0"
setuptools==68.2.2 ; python_version >= "3.9" and python_version < "4"
sgp4==2.22 ; python_version >= "3.9" and python_version < "4.0"
six==1.16.0 ; python_version >= "3.9" and python_version < "4.0"
Expand Down
6 changes: 3 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
scrollTop: ($('#output').offset().top)
},500);

if ("{{ dev_mode }}" == "true")
if ("{{ dev_mode|lower }}" == "true")
{
$('#debug').text(data.airport_debug);
$('#debug').append("\n");
Expand Down Expand Up @@ -112,7 +112,7 @@

<header class="masthead text-center text-white" style="padding-top: 50px;">
<div class="masthead-content">
<p class="text-warning border-warning text-uppercase fw-bolder" id="error_output" style="font-size: 20px;margin-right: 16px;margin-left: 16px;background-color: rgba(255,255,255,1); {% if not result['error'] %}display: none;{% endif %}">{% if result['error'] %}There is an error: {{ result['error'] }}{% endif %}<br></p>{% if dev_mode == "true" %}
<p class="text-warning border-warning text-uppercase fw-bolder" id="error_output" style="font-size: 20px;margin-right: 16px;margin-left: 16px;background-color: rgba(255,255,255,1); {% if not result['error'] %}display: none;{% endif %}">{% if result['error'] %}There is an error: {{ result['error'] }}{% endif %}<br></p>{% if dev_mode %}
<p class="text-warning d-md-block fw-bold" id="dev_site_warning" style="font-size: 20px;margin-right: 16px;margin-left: 16px;background-color: rgba(255,255,255,1);">Warning: This is the dev website. Production can be found at&nbsp;<a href="https://loggingnight.org/">https://loggingnight.org/</a><br></p>{% endif %}
<p class="d-none d-md-block" style="font-size: 20px;margin-right: 16px;margin-left: 16px;">Trouble keeping your sunsets straight from your civil twilights? Can't remember when your TOMATOFLAMES require FLAPS? Find USNO astronomical tables confusing? We've got your back.<br></p>
<p style="font-size: 20px;margin-right: 16px;margin-left: 16px;">Plug in your airport ID, and optionally a date, below and we'll tell you what the Federal Aviation Regulations say about what you need to fly and what you can log.</p>
Expand Down Expand Up @@ -199,7 +199,7 @@ <h3 class="text-center card-title">Until one hour before Sunrise and after one h
</div>
</div>
</div>
</section>{% if dev_mode == "true" %}
</section>{% if dev_mode %}
<pre id="debug" style="text-align: left;">{% if result %}{{ result|pprint }}{% endif %}</pre>
{% endif %}
<footer class="py-5 bg-black">
Expand Down
70 changes: 52 additions & 18 deletions webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,67 @@
import os
import threading
import time
from typing import Literal

import flask
import markupsafe
import schedule
import sentry_sdk
from dateutil import parser as dateparser
from flask import Flask, Response, render_template, request

from loggingnight import LoggingNight


def enable_housekeeping(run_interval=3600):
sentry_debug: bool = False
sentry_traces_sample_rate: float = 1.0
sentry_profiles_sample_rate: float = 0.6
gc_hours: int = 6
dev_mode: bool = False

app_env: str = os.environ.get("ENVIRONMENT", "local")
match app_env:
case "production":
pass
case "development":
sentry_profiles_sample_rate = 1.0
dev_mode = True
case "debug":
sentry_profiles_sample_rate = 1.0
sentry_debug = True
dev_mode = True
gc_hours = 1
case _:
# Default to "local"
app_env = "local"
sentry_profiles_sample_rate = 1.0
dev_mode = True
gc_hours = 1

sentry_dsn: str | None = os.environ.get("SENTRY_DSN", None)
if sentry_dsn is not None:
sentry_sdk.init(
dsn=sentry_dsn,
traces_sample_rate=sentry_traces_sample_rate,
profiles_sample_rate=sentry_profiles_sample_rate,
enable_tracing=True,
debug=sentry_debug,
environment=app_env,
)


def enable_housekeeping(run_interval: int = 3600):
cease_continuous_run = threading.Event()

class ScheduleThread(threading.Thread):
@staticmethod
def run(): # pylint: disable=arguments-differ
def run(self) -> None:
while not cease_continuous_run.is_set():
schedule.run_pending()
time.sleep(run_interval)

continuous_thread = ScheduleThread()
continuous_thread.start()

schedule.every(6).hours.do(LoggingNight.garbage_collect_cache)
schedule.every(gc_hours).hours.do(LoggingNight.garbage_collect_cache)


enable_housekeeping()
Expand All @@ -37,18 +74,15 @@ def run(): # pylint: disable=arguments-differ
"__name__", static_url_path="/assets", static_folder="templates/assets"
)

dev_mode = os.environ.get("LOGGINGNIGHT_DEV", "false")
if dev_mode == "true":
if dev_mode:
import pprint

application.config["DEBUG"] = True
else:
application.config["DEBUG"] = False
application.config["DEBUG"] = dev_mode


@application.route("/")
def index():
icao_identifier = request.args.get("airport")
def index() -> tuple[str, int] | str:
icao_identifier: str | None = request.args.get("airport")

try:
date = dateparser.parse(
Expand All @@ -73,15 +107,15 @@ def index():
return render_template("index.html", dev_mode=dev_mode, result=result)


def do_lookup(icao_identifier, date):
def do_lookup(icao_identifier: str, date) -> dict[str, str]:
ln = LoggingNight(icao_identifier, date, try_cache=True)

if ln.in_zulu:
time_format = "%H%M Zulu"
else:
time_format = "%I:%M %p"

if dev_mode == "true":
if dev_mode:
# pylint: disable=use-dict-literal
result = dict(
airport=icao_identifier,
Expand Down Expand Up @@ -116,7 +150,7 @@ def do_lookup(icao_identifier, date):


@application.route("/lookup", methods=["POST"])
def lookup():
def lookup() -> tuple[str, int] | str:
icao_identifier = markupsafe.escape(request.form["airport"])
datestring = markupsafe.escape(request.form["date"])

Expand All @@ -129,7 +163,7 @@ def lookup():
date = datetime.date.today()

try:
result = do_lookup(icao_identifier, date)
result: dict[str, str] = do_lookup(icao_identifier, date)
except Exception as e:
return str(e), 400
except: # pylint: disable=bare-except # noqa
Expand All @@ -139,7 +173,7 @@ def lookup():


@application.route("/displayCache")
def displayCache():
def displayCache() -> Response | Literal[False]:
if LoggingNight.enable_cache():
return Response(
json.dumps(
Expand All @@ -160,7 +194,7 @@ def sitemap():
# pylint: disable=consider-using-f-string
base_url = "https://loggingnight.org/?airport="
# fmt: off
icao_airports = ["VNY", "DVT", "APA", "PRC", "HIO", "FFZ", "IWA", "GFK", "LGB", "SEE", "MYF", "SFB", "SNA", "CHD", "FPR", "FRG", "TMB", \
icao_airports: list[str] = ["VNY", "DVT", "APA", "PRC", "HIO", "FFZ", "IWA", "GFK", "LGB", "SEE", "MYF", "SFB", "SNA", "CHD", "FPR", "FRG", "TMB", \
"PAO", "RVS", "VRB", "DAB", "PMP", "PVU", "SDL", "RHV", "CNO", "DTO", "BJC", "PDK", "FIN", "SGJ", "ORF", "CRQ", "DCU", \
"SMO", "ISM", "LVK", "VGT", "EUL", "BFI", "BDN", "HPN", "FXE", "CRG", "CMA", "LAL", "AWO", "ORD", "ATL", "LAX", "DFW", \
"DEN", "CLT", "LAS", "IAH", "JFK", "SFO", "SEA", "PHX", "EWR", "MIA", "DTW", "MSP", "LGA", "BOS", "PHL", "FLL", "MCO", \
Expand Down

0 comments on commit dc6f009

Please sign in to comment.