Skip to content

Commit df372fa

Browse files
committed
added get history in app
1 parent 4e81a76 commit df372fa

4 files changed

Lines changed: 81 additions & 46 deletions

File tree

app/templates/welcome.html

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,48 @@ <h1 class="mb-4">🚀 Benvenuto nella Trading Dashboard</h1>
1010
<a href="{{ url_for('views.analytics') }}" class="btn btn-outline-primary">
1111
<i class="fa-solid fa-chart-pie"></i> Analytics
1212
</a>
13-
<!-- altre pagine future -->
14-
<a href="#" class="btn btn-outline-secondary">
13+
<a href="{{ url_for('views.portfolio') }}" class="btn btn-outline-success">
14+
<i class="fa-solid fa-briefcase"></i> Portfolio
15+
</a>
16+
<a href="{{ url_for('views.database') }}" class="btn btn-outline-secondary">
1517
<i class="fa-solid fa-database"></i> Gestione DB
1618
</a>
19+
<a href="{{ url_for('auth.logout') }}" class="btn btn-outline-danger">
20+
<i class="fa-solid fa-right-from-bracket"></i> Logout
21+
</a>
22+
23+
<!-- Pulsante che apre il modal -->
24+
<button class="btn btn-outline-warning" data-bs-toggle="modal" data-bs-target="#runScriptModal">
25+
<i class="fa-solid fa-play"></i> Esegui Data Fetcher
26+
</button>
27+
</div>
28+
</div>
29+
30+
<!-- Modal con il form -->
31+
<div class="modal fade" id="runScriptModal" tabindex="-1" aria-labelledby="runScriptModalLabel" aria-hidden="true">
32+
<div class="modal-dialog">
33+
<form method="POST" action="{{ url_for('views.run_script') }}">
34+
<div class="modal-content">
35+
<div class="modal-header">
36+
<h5 class="modal-title" id="runScriptModalLabel">Esegui Data Fetcher</h5>
37+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Chiudi"></button>
38+
</div>
39+
<div class="modal-body">
40+
<div class="mb-3">
41+
<label for="start_date" class="form-label">Data inizio</label>
42+
<input type="date" id="start_date" name="start_date" class="form-control" required>
43+
</div>
44+
<div class="mb-3">
45+
<label for="end_date" class="form-label">Data fine</label>
46+
<input type="date" id="end_date" name="end_date" class="form-control">
47+
<div class="form-text">Se vuoto, verrà usata la data odierna.</div>
48+
</div>
49+
</div>
50+
<div class="modal-footer">
51+
<button type="submit" class="btn btn-warning">Esegui</button>
52+
</div>
53+
</div>
54+
</form>
1755
</div>
1856
</div>
1957
{% endblock %}

app/views.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from flask import Blueprint, render_template, request
22
from flask_login import login_required
3-
from scripts.database import execute_query
3+
from datetime import datetime
4+
from scripts import database, google_services, data_fetcher
5+
46

57
views_bp = Blueprint("views", __name__)
68

@@ -9,6 +11,34 @@
911
def welcome():
1012
return render_template("welcome.html")
1113

14+
@views.route("/run-script", methods=["POST"])
15+
def run_script():
16+
try:
17+
start_date = request.form.get("start_date")
18+
end_date = request.form.get("end_date") or datetime.today().strftime("%Y-%m-%d")
19+
20+
if not start_date:
21+
flash("Devi specificare una data di inizio", "danger")
22+
return redirect(url_for("views.welcome"))
23+
24+
# 1. Prendo i tickers da Google Sheets
25+
tickers = google_services.get_universe_tickers_from_gsheet()
26+
27+
# 2. Trunco la tabella universe
28+
database.execute_query("TRUNCATE TABLE universe RESTART IDENTITY CASCADE", fetch=False)
29+
30+
# 3. Scarico i dati da yfinance
31+
rows = data_fetcher.get_data_for_db_between_dates(tickers, start_date, end_date)
32+
33+
# 4. Inserisco i dati nel DB
34+
inserted = database.insert_batch_universe(rows, conflict_resolution="DO NOTHING")
35+
36+
flash(f"Script eseguito correttamente ✅ ({inserted} righe inserite)", "success")
37+
except Exception as e:
38+
flash(f"Errore durante l'esecuzione: {e}", "danger")
39+
40+
return redirect(url_for("views.welcome"))
41+
1242
@views_bp.route("/analytics")
1343
@login_required
1444
def analytics():
@@ -38,7 +68,7 @@ def run_query():
3868
error = "⚠️ Nessuna query inviata"
3969
else:
4070
try:
41-
fetched, column_names = execute_query(query)
71+
fetched, column_names = database.execute_query(query)
4272
if fetched:
4373
columns = column_names # qui metti i nomi reali
4474
results = fetched

check_db.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

scripts/google_services.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,12 @@ def get_gsheet_client(service_account_info: dict | None = None,
8585

8686
creds = Credentials.from_service_account_info(service_account_info, scopes=scopes)
8787
return gspread.authorize(creds)
88+
89+
def get_universe_tickers_from_gsheet() -> list[str]:
90+
"""
91+
Legge la lista dei tickers dallo spreadsheet Universe.
92+
"""
93+
client = get_gsheet_client()
94+
sheet = client.open_by_key(config.UNIVERSE_SPREADSHEET_ID).sheet1
95+
tickers = sheet.col_values(4) # assumiamo colonna A
96+
return [t.strip().upper() for t in tickers if t.strip()]

0 commit comments

Comments
 (0)