Files
prog-team-proj/ev2.py
2026-01-05 15:29:49 -01:00

173 lines
4.3 KiB
Python

import logging
import os
import time
from utilsv2 import mongo, parser, stats, utils
from utilsv2.log import logger
OS = os.name
MAIN_MENU = {
"1": "Adicionar novos dados",
"2": "Aplicar filtros",
"3": "Estatísticas",
"4": "Limpar filtros",
"q": "Sair",
}
FILTER_MENU = {
"1": "Data início",
"2": "Magnitudes",
"3": "Profundidade",
"4": "GAP",
"6": "Limpar filtros",
"7": "Mostrar filtros",
"q": "Voltar ao menu principal",
}
def clear_screen():
os.system("cls" if OS == "nt" else "clear")
def print_menu(menu: dict[str, str]):
clear_screen()
for k, v in menu.items():
print(f"[{k}]: {v}")
def filter_menu(old_fiters):
filters = old_fiters
while True:
print_menu(FILTER_MENU)
usrIn = input()
match usrIn:
# datas
case "1":
clear_screen()
print(
"Formato da data: YYYY-MM-DD\nInserir datas de corte, separadas por uma vírgula(,)"
)
aux = input()
d1, d2 = aux.split(",", maxsplit=1)
d1 = utils.toDateTime(d1)
d2 = utils.toDateTime(d2)
filters["DateTime"] = {}
if d1 != -1:
filters["DateTime"]["$gte"] = d1
if d2 != -1:
filters["DateTime"]["$lte"] = d2
# magnitudes
case "2":
clear_screen()
print("Inserir magnitudes de corte, separadas por uma vírgula(,)")
aux = input()
d1, d2 = aux.split(",", maxsplit=1)
d1 = utils.toFloat(d1)
d2 = utils.toFloat(d2)
filters["Magnitudes.L.Magnitude"] = {}
if d1 != -1:
filters["Magnitudes.L.Magnitude"]["$gte"] = d1
if d2 != -1:
filters["Magnitudes.L.Magnitude"]["$lte"] = d2
# Profundidades
case "3":
clear_screen()
print("Inserir profundidades de corte, separadas por uma vírgula(,)")
aux = input()
d1, d2 = aux.split(",", maxsplit=1)
d1 = utils.toFloat(d1)
d2 = utils.toFloat(d2)
filters["Depth"] = {}
if d1 != -1:
filters["Depth"]["$gte"] = d1
if d2 != -1:
filters["Depth"]["$lte"] = d2
# GAP
case "4":
clear_screen()
print("Inserir GAP")
aux = input()
gap = utils.toInt(aux)
filters["GAP"] = {}
if aux:
filters["GAP"]["$lte"] = gap
case "6":
fliters = {}
case "7":
print(filters)
time.sleep(2.0)
case "q":
return filters
def graph_menu():
pass
def main():
cli = mongo.connect("mongodb://localhost:27017")
filters = {}
while True:
print_menu(MAIN_MENU)
usrIn = input()
match usrIn:
case "1":
aux = input("Ficheiro a ler:")
if utils.fileExists(aux):
logger.info(f"Parsing the file {aux}")
ev, st = parser.parse(aux)
mongo.add_events(cli, "quakes", ev, "main")
mongo.add_stations(cli, "stations", st, "main")
else:
print(f"Could not open the file {aux}")
logger.error(f"Could not open the file {aux}")
time.sleep(2.0)
case "2":
filters = filter_menu(filters)
case "3":
print(filters)
v = mongo.filter_query(cli, "quakes", filters, "test")
stats.stats(v)
time.sleep(2.0)
case "q":
break
mongo.close(cli)
if __name__ == "__main__":
logger = logging.getLogger(__name__)
# initialization
logger.info("Started")
main()
logger.info("Ended")