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

completed assessment #4

Open
wants to merge 1 commit into
base: main
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
cache.sqlite
.env
55 changes: 55 additions & 0 deletions RUNTIME_INSTRUCTIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Instructions for installing and running the project:

1- Install requiraments in the requirements.txt file:

```
pip install -r requirements.txt
```

2- Set the enviroment variable ALERTUS_CREDENTIALS in a file called .env:

```
ALERTUS_CREDENTIALS=<base64 encoding of <your_username>:<your_password>>
```

3- Run Flask server

```
flask run --port 8000
```

4- Run Tests

```
pytest
```

### Github Link:

https://github.com/iamrdr97

### Explaning Challenges and Highlights:

One of the biggest challenges I faced doing this project was implementing the user interface, since I had never used
technologies like jinja2 or bootstrap, and it had been a long time since I used JavaScript and jQuery.

Another important challenge was accessing weather.gov and alertus services through their APIs, but using their
documentation allowed me to achieve my goal.

What I am most proud of is having completed the project in the established time, despite the fact that to do, so I used
tools and frameworks that I do not usually use, such as flask and the ones mentioned above for the user interface.

### List of relevant libraries used:

#### requests:

**requests** allows to send HTTP requests in an easy way

#### requests-cache:

**requests-cache** is a persistent cache that provides an easy way to get better performance with the python requests
library.

#### pytest:

**pytest** is a testing framework that allows to test the code in a simple way.
67 changes: 67 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import json

import requests_cache
from flask import Flask, render_template, request

import database_connection_manager
from utils import load_settings_from_settings_txt_file, update_settings_file, get_new_forecast_and_store_it
from weather_alert_logging import get_logger

app = Flask(__name__)

requests_cache.install_cache('cache', backend='sqlite', expire_after=600)

app.logger = get_logger()


@app.route('/')
def index():
"""
This is the main page of the app.
Here the user can see the last ten forecasts saved in the database.
:return: The rendered data template.
"""
forecasts = database_connection_manager.get_last_ten_forecasts()
if forecasts is None:
app.logger.error('Could not fetch forecasts from database')
return {'error': 'Could not fetch the forecasts from database.'}, 500
settings = load_settings_from_settings_txt_file()
return render_template('data.html', forecasts=forecasts, settings=settings)


@app.route('/settings/', methods=['GET', 'POST'])
def settings_view():
"""
This is the settings page of the app.
Here the user can change the settings of the app.
:return: The rendered settings template.
"""
if request.method == 'POST':
settings = request.form
update_settings_file(settings)
app.logger.info(f'Settings updated successfully, new settings: {settings}')
else:
settings = load_settings_from_settings_txt_file()
return render_template('settings.html', settings=settings)


@app.route('/forecast/', methods=['GET'])
def forecast_view():
"""
Endpoint to fetch the forecast for the next three hours and store it in the database.
:return: The forecast as a json object with the fields used to store them in the database.
"""
new_forecast = get_new_forecast_and_store_it()
if new_forecast is None:
app.logger.error('Could not fetch new forecast')
return {'error': 'Could not fetch the forecast.'}, 500
forecasts = {"timestamp": new_forecast['timestamp'], "longitude": new_forecast['longitude'],
"latitude": new_forecast['latitude'], "first_forecast": new_forecast['first_forecast'],
"second_forecast": new_forecast['second_forecast'], "third_forecast": new_forecast['third_forecast'],
'alert_generated': new_forecast['alert_generated'], 'alert_id': new_forecast['alert_id']}
app.logger.info(f'New forecast fetched successfully, new forecast: {forecasts}')
return json.dumps(forecasts)


if __name__ == '__main__':
app.run(debug=True, host='localhost', port=8000)
Binary file added database.db
Binary file not shown.
55 changes: 55 additions & 0 deletions database_connection_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import sqlite3

from weather_alert_logging import get_logger

logger = get_logger()


def check_table_exists(connection: sqlite3.Connection):
try:
connection.execute("SELECT * FROM forecasts;")
except sqlite3.OperationalError as e:
if 'no such table' in e.args[0]:
connection.execute(
"CREATE TABLE forecasts (timestamp REAL, long REAL, lat REAL, first_forecast INT, second_forecast "
"INTEGER, third_forecast INTEGER, alert_generated INTEGER, alert_id INTEGER );")
connection.commit()


def get_connection():
try:
connection = sqlite3.connect('database.db')
check_table_exists(connection)
return connection
except sqlite3.Error as e:
logger.error(e)
return None


def get_last_ten_forecasts():
try:
connection = get_connection()
cursor = connection.execute("SELECT * FROM forecasts ORDER BY timestamp DESC LIMIT 10;")
forecasts = cursor.fetchall()
except sqlite3.Error as e:
logger.error(e)
return None

forecasts = [
{'timestamp': forecast[0], 'longitude': forecast[1], 'latitude': forecast[2], 'first_forecast': forecast[3],
'second_forecast': forecast[4], 'third_forecast': forecast[5], 'alert_generated': forecast[6],
'alert_id': forecast[7] if forecast[7] else "NULL"} for forecast in forecasts]

return forecasts


def insert_forecast(timestamp, long, lat, first_forecast,
second_forecast, third_forecast, alert_generated=0, alert_id=None):
try:
connection = get_connection()
connection.execute("INSERT INTO forecasts VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
(timestamp, long, lat, first_forecast, second_forecast, third_forecast, alert_generated,
alert_id))
connection.commit()
except sqlite3.Error as e:
logger.error(e)
30 changes: 30 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
appdirs==1.4.4
attrs==21.4.0
autopep8==1.6.0
cattrs==1.10.0
certifi==2021.10.8
charset-normalizer==2.0.12
click==8.1.2
Flask==2.1.1
idna==3.3
importlib-metadata==4.11.3
iniconfig==1.1.1
itsdangerous==2.1.2
Jinja2==3.1.1
MarkupSafe==2.1.1
packaging==21.3
pluggy==1.0.0
py==1.11.0
pycodestyle==2.8.0
pyparsing==3.0.8
pytest==7.1.1
python-dotenv==0.20.0
requests==2.27.1
requests-cache==0.9.3
six==1.16.0
toml==0.10.2
tomli==2.0.1
url-normalize==1.4.3
urllib3==1.26.9
Werkzeug==2.1.1
zipp==3.8.0
12 changes: 6 additions & 6 deletions settings.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Location Information
latitude = 39.7456
longitude = -97.0892
#Location Information
latitude = 25.877353
longitude = -80.130049

# Application Settings
threshold_value = 80
check_in_frequency = 30
#Application Settings
threshold_value = 70
check_in_frequency = 60
52 changes: 52 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link
crossorigin="anonymous"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
rel="stylesheet"
/>
<meta charset="UTF-8"/>
<title>{% block title %} {% endblock %} Weather Alert</title>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{{url_for('index')}}">Weather Alert</a>
<button
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
class="navbar-toggler"
data-target="#navbarNav"
data-toggle="collapse"
type="button"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{{url_for('settings_view')}}">Settings</a>
</li>
</ul>
</div>
</nav>
</head>
<body>
<script
crossorigin="anonymous"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
src="https://code.jquery.com/jquery-3.2.1.min.js"
></script>
<script
crossorigin="anonymous"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
></script>
<script
crossorigin="anonymous"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
></script>
<div class="content">{% block content %} {% endblock %}</div>
</body>
</html>
90 changes: 90 additions & 0 deletions templates/data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{% extends 'base.html' %} {% block title %} Data {% endblock %} {% block content
%}

<table class="table">
<thead>
<tr>
<th>Timestamp</th>
<th>Longitue</th>
<th>Latitude</th>
<th>First Forecast</th>
<th>Second Forecast</th>
<th>Third Forecast</th>
<th>Alert Generated</th>
<th>Alert ID</th>
</tr>
</thead>
<tbody>
{% for forecast in forecasts %}
<tr class="{{'red' if forecast.alert_generated else 'table_row'}}">
<td>{{ forecast.timestamp }}</td>
<td>{{ forecast.longitude }}</td>
<td>{{ forecast.latitude }}</td>
<td>{{ forecast.first_forecast }}</td>
<td>{{ forecast.second_forecast }}</td>
<td>{{ forecast.third_forecast }}</td>
<td>{{ forecast.alert_generated }}</td>
<td>{{ forecast.alert_id }}</td>
</tr>

<style>
.red {
color: red;
}
</style>

{% endfor %}
</tbody>
</table>

<script type="text/javascript">
$(document).ready(function () {
setInterval(function () {
$.ajax({
url: "/forecast/",
type: "GET",
success: function (data) {
let data_object = JSON.parse(data);
let row_class = "table_row";
if (data_object.alert_generated === true) {
data_object.alert_generated = 1;
row_class = "red";
} else {
data_object.alert_generated = 0;
}
if (data_object.alert_id == null) {
data_object.alert_id = "NULL";
}
const new_row =
"<tr class=" + row_class + "><td>" +
data_object.timestamp +
"</td><td>" +
data_object.longitude +
"</td><td>" +
data_object.latitude +
"</td><td>" +
data_object.first_forecast +
"</td><td>" +
data_object.second_forecast +
"</td><td>" +
data_object.third_forecast +
"</td><td>" +
data_object.alert_generated +
"</td><td>" +
data_object.alert_id +
"</td></tr>" +
"<style> .red { color: red; } </style>";


if ($("table tbody tr").length == 10) {
$("table tbody tr:last").remove()
}

$("tbody").prepend(new_row);
},
});
}, "{{settings.check_in_frequency}}" * 1000);
});
</script>

{% endblock %}
Loading