diff --git a/backend.py b/backend.py index e8575bc..2c7245a 100644 --- a/backend.py +++ b/backend.py @@ -218,4 +218,31 @@ class AnimeBackend: self.db.commit() except Exception as e: logging.error(f"Error deleting year {year}: {e}") - self.db.rollback() \ No newline at end of file + self.db.rollback() + + def get_total_entries(self): + try: + cursor = self.db.cursor() + cursor.execute("SELECT COUNT(*) FROM anime") + return cursor.fetchone()[0] + except Exception as e: + logging.error(f"Error getting total entries: {e}") + return 0 + + def get_completed_entries(self): + try: + cursor = self.db.cursor() + cursor.execute("SELECT COUNT(*) FROM anime WHERE status = 'completed'") + return cursor.fetchone()[0] + except Exception as e: + logging.error(f"Error getting completed entries: {e}") + return 0 + + def get_entries_by_type(self): + try: + cursor = self.db.cursor() + cursor.execute("SELECT type, COUNT(*) FROM anime GROUP BY type ORDER BY 2 DESC") + return cursor.fetchall() # Returns list of tuples: (type, count) + except Exception as e: + logging.error(f"Error getting entries by type: {e}") + return [] \ No newline at end of file diff --git a/frontend.py b/frontend.py index 87c20b2..ce00314 100644 --- a/frontend.py +++ b/frontend.py @@ -221,6 +221,9 @@ class AnimeTracker(QMainWindow): random_act = QAction('Random Pick', self) random_act.triggered.connect(self.random_pick) tools_menu.addAction(random_act) + statistics_act = QAction('Statistics', self) + statistics_act.triggered.connect(self.show_statistics) + tools_menu.addAction(statistics_act) help_menu = menubar.addMenu('Help') shortcuts_act = QAction('Shortcuts', self) shortcuts_act.triggered.connect(self.show_shortcuts) @@ -713,6 +716,41 @@ class AnimeTracker(QMainWindow): else: QMessageBox.information(self, "Random Pick", "No unwatched anime in this tab.") + def show_statistics(self): + dialog = QDialog(self) + dialog.setWindowTitle("Anime Statistics") + layout = QVBoxLayout(dialog) + + total = self.backend.get_total_entries() + completed = self.backend.get_completed_entries() + percentage = (completed / total * 100) if total > 0 else 0 + + layout.addWidget(QLabel(f"Total anime entries: {total}")) + layout.addWidget(QLabel(f"Total completed entries: {completed}")) + layout.addWidget(QLabel(f"Percentage of completed entries: {percentage:.2f}%")) + + layout.addWidget(QLabel("Number of anime entries by type:")) + types_data = self.backend.get_entries_by_type() + if types_data: + for typ, count in types_data: + display_type = typ if typ else "None" # Handle empty type as 'None' + layout.addWidget(QLabel(f" {display_type}: {count}")) + else: + layout.addWidget(QLabel(" No data available")) + + db_path = 'anime_backlog.db' + if os.path.exists(db_path): + db_size_kb = os.path.getsize(db_path) / 1024 + layout.addWidget(QLabel(f"Size of the database: {db_size_kb:.2f} Kb")) + else: + layout.addWidget(QLabel("Size of the database: N/A (database file not found)")) + + buttons = QDialogButtonBox(QDialogButtonBox.Ok) + buttons.accepted.connect(dialog.accept) + layout.addWidget(buttons) + + dialog.exec_() + def import_csv(self): file_name, _ = QFileDialog.getOpenFileName(self, "Import CSV", "", "CSV Files (*.csv)") if file_name: