diff --git a/ev2.py b/ev2.py index 6501387..d71969a 100644 --- a/ev2.py +++ b/ev2.py @@ -1,12 +1,152 @@ import logging +import os +from enum import Enum -from utilsv2 import mongo +from utilsv2 import mongo, stats from utilsv2.log import logger from utilsv2.parser import parse +from utilsv2.utils import toDateTime, toFloat, toInt + +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 = toDateTime(d1) + d2 = 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 = toFloat(d1) + d2 = toFloat(d2) + filters["Magnitudes.L.Magnitude"] = {} + + if d1 != -1: + filters["Magnitudes.L.Magnitude"]["$gte"] = d1 + + if d2 != -1: + filters["Magnitudes.L.Magnitude"]["$lte"] = d2 + case "2": + clear_screen() + print("Inserir profundidades de corte, separadas por uma vírgula(,)") + aux = input() + + d1, d2 = aux.split(",", maxsplit=1) + + d1 = toFloat(d1) + d2 = toFloat(d2) + filters["Depth"] = {} + + if d1 != -1: + filters["Depth"]["$gte"] = d1 + + if d2 != -1: + filters["Depth"]["$lte"] = d2 + + case "4": + clear_screen() + print("Inserir GAP") + aux = input() + + gap = toInt(aux) + if aux: + filters["GAP"]["$lte"] = gap + + case "6": + fliters = {} + + case "7": + print(filters) + + case "q": + return filters + + +def graph_menu(): + pass def main(): - pass + cli = mongo.connect("mongodb://localhost:27017") + + filters = {} + + while True: + print_menu(MAIN_MENU) + usrIn = input() + + match usrIn: + case "1": + raise NotImplementedError + case "2": + filters = filter_menu(filters) + case "3": + print(filters) + v = mongo.filter_query(cli, "quakes", filters, "test") + stats.calculate_stats(v, filters) + input() + + case "q": + break + + mongo.close(cli) if __name__ == "__main__": @@ -14,13 +154,5 @@ if __name__ == "__main__": # initialization logger.info("Started") - cli = mongo.connect("mongodb://localhost:27017") - - evs, stats = parse("dados.txt") - - mongo.add_events(cli, "quakes", evs) - mongo.add_stations(cli, "stations", stats) - - # cleanup - mongo.close(cli) + main() logger.info("Ended") diff --git a/requirements.txt b/requirements.txt index 0ac4274..9edfb57 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,3 @@ -dash==3.3.0 matplotlib==3.10.8 -numpy==2.3.5 -pandas==2.3.3 -plotly==6.5.0 +numpy==2.4.0 +pymongo==4.15.5 diff --git a/utilsv2/graphs.py b/utilsv2/graphs.py new file mode 100644 index 0000000..ff9a07f --- /dev/null +++ b/utilsv2/graphs.py @@ -0,0 +1,17 @@ +from matplotlib import pyplot as plt + + +class Plotter: + pass + + def plot_bars(self): + pass + + def plot_lin(self): + pass + + def plot_box(self): + pass + + def adjust_x(self): + pass diff --git a/utilsv2/mongo.py b/utilsv2/mongo.py index 4faecde..cb6b568 100644 --- a/utilsv2/mongo.py +++ b/utilsv2/mongo.py @@ -14,7 +14,7 @@ except ModuleNotFoundError: logger = logging.getLogger(__name__) -def connect(uri) -> MongoClient: +def connect(uri: str) -> MongoClient: try: client = MongoClient(uri) logger.info("Connected to the DB") @@ -26,10 +26,9 @@ def connect(uri) -> MongoClient: def add_events( - client: MongoClient, collection: str, data: list[dict[str, Any]] + client: MongoClient, collection: str, data: list[dict[str, Any]], db: str = "main" ) -> None: - db = client["main"] - coll = db[collection] + coll: Collection = client[db][collection] _res = coll.insert_many(data) @@ -40,10 +39,9 @@ def add_events( def add_stations( - client: MongoClient, collection: str, data: list[dict[str, Any]] + client: MongoClient, collection: str, data: list[dict[str, Any]], db: str = "main" ) -> None: - db = client["main"] - coll = db[collection] + coll: Collection = client[db][collection] _res = coll.insert_many(data) @@ -62,19 +60,28 @@ def close(client: MongoClient) -> None: logger.info("Closed the DB.") -def query_all(cli: MongoClient, collection: str) -> Any: - coll: Collection = cli.main[collection] +def query_all(client: MongoClient, collection: str, db: str = "main") -> Any: + coll: Collection = client[db][collection] result = coll.find({}) - for doc in result: - print(doc) + return list(result) -def filter_query(cli: MongoClient, collection: str, filter_by): - coll: Collection = cli.main[collection] +def filter_query( + client: MongoClient, collection: str, filter_by: dict[str, Any], db: str = "main" +): + coll: Collection = client[db][collection] - res = coll.find({""}) + res = coll.find( + filter_by, {"DateTime": 1, "Magnitudes": 1, "Depth": 1, "GAP": 1} + ).sort({"DateTime": 1}) + + if not res._empty: + res = list(res) + logger.info(f"Retrieved {len(res)} elements.") + + return res if __name__ == "__main__": diff --git a/utilsv2/stats.py b/utilsv2/stats.py new file mode 100644 index 0000000..5711381 --- /dev/null +++ b/utilsv2/stats.py @@ -0,0 +1,49 @@ +import time + +import numpy as np +from pymongo import MongoClient + + +def print_filters(filters): + _res = "" + for k, v in filters.items(): + _res += f"{k}: {v}\n" + + +def calculate_stats(data: list, filters): + _stats_txt = "Estatísticas\n" + _stats_txt += f"Número de eventos: {len(data)}\n" + _res = calc_mag(data) + _stats_txt += f"Magnitudes:\n\tMédia: {_res[0]} \u00b1 {_res[1]}\n\tMediana: {_res[2]}\n\tValor Mínimo: {_res[3]}\n\tValor Máximo: {_res[4]}\n" + _res = calc_depth(data) + _stats_txt += f"\nProfundidade:\n\tMédia: {_res[0]} \u00b1 {_res[1]}\n\tMediana: {_res[2]}\n\tValor Mínimo: {_res[3]}\n\tValor Máximo: {_res[4]}\n" + + fname = f"stats-{time.time_ns()}.txt" + with open(fname, "wb") as fp: + fp.write(_stats_txt.encode("utf-8")) + + print(_stats_txt) + + +def get_data(client: MongoClient): + pass + + +def calc_depth(data): + if len(data) == 0: + return 0 + depths = np.array([v["Depth"] for v in data], dtype=float) + return ( + np.average(depths), + np.std(depths), + np.median(depths), + np.min(depths), + np.max(depths), + ) + + +def calc_mag(data): + if len(data) == 0: + return 0 + mags = np.array([v["Magnitudes"]["L"]["Magnitude"] for v in data], dtype=float) + return (np.average(mags), np.std(mags), np.median(mags), np.min(mags), np.max(mags)) diff --git a/utilsv2/utils.py b/utilsv2/utils.py index 53d9d20..af4b2c6 100644 --- a/utilsv2/utils.py +++ b/utilsv2/utils.py @@ -1,2 +1,32 @@ +from datetime import datetime + + def is_empty(_str: str) -> bool: return len(_str.strip(" ")) == 0 + + +def toDateTime(dt: str) -> datetime | int: + if len(dt) == 0: + return -1 + try: + return datetime.strptime(dt, "%Y-%m-%d") + except ValueError: + return -1 + + +def toFloat(v: str) -> float: + if len(v) == 0: + return -1.0 + try: + return float(v) + except ValueError: + return -1.0 + + +def toInt(v: str) -> int | None: + if len(v) == 0: + return None + try: + return int(v) + except ValueError: + return None