Files
Inf_Tec_Auto/statistics_view.py
2024-12-19 09:04:35 -08:00

131 lines
4.8 KiB
Python

from PySide6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QPushButton,
QLabel, QTableWidget, QTableWidgetItem,
QHeaderView)
from PySide6.QtCore import Qt
from PySide6.QtCharts import QChart, QChartView, QPieSeries, QBarSeries, QBarSet
from PySide6.QtGui import QPainter
from database import get_session
from models import Document, Folder, Statistics
import json
import os
from datetime import datetime
class StatisticsView(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setup_ui()
def setup_ui(self):
layout = QVBoxLayout(self)
# Refresh button
self.refresh_btn = QPushButton("Atualizar Estatísticas")
self.refresh_btn.clicked.connect(self.update_statistics)
layout.addWidget(self.refresh_btn)
# Summary layout
summary_layout = QHBoxLayout()
# Total documents
self.total_docs_label = QLabel("Total de Documentos: 0")
summary_layout.addWidget(self.total_docs_label)
# Total folders
self.total_folders_label = QLabel("Total de Pastas: 0")
summary_layout.addWidget(self.total_folders_label)
# Storage used
self.storage_label = QLabel("Espaço Utilizado: 0 MB")
summary_layout.addWidget(self.storage_label)
layout.addLayout(summary_layout)
# Charts layout
charts_layout = QHBoxLayout()
# Document types chart
self.types_chart = QChart()
self.types_chart.setTitle("Documentos por Tipo")
types_view = QChartView(self.types_chart)
types_view.setRenderHint(QPainter.RenderHint.Antialiasing)
charts_layout.addWidget(types_view)
# Brands chart
self.brands_chart = QChart()
self.brands_chart.setTitle("Documentos por Marca")
brands_view = QChartView(self.brands_chart)
brands_view.setRenderHint(QPainter.RenderHint.Antialiasing)
charts_layout.addWidget(brands_view)
layout.addLayout(charts_layout)
self.update_statistics()
def update_statistics(self):
session = get_session()
try:
# Count documents and folders
total_docs = session.query(Document).count()
total_folders = session.query(Folder).count()
# Calculate storage used
storage_used = 0
for doc in session.query(Document).all():
if os.path.exists(doc.file_path):
storage_used += os.path.getsize(doc.file_path)
for version in doc.versions:
if os.path.exists(version.file_path):
storage_used += os.path.getsize(version.file_path)
# Update labels
self.total_docs_label.setText(f"Total de Documentos: {total_docs}")
self.total_folders_label.setText(f"Total de Pastas: {total_folders}")
self.storage_label.setText(
f"Espaço Utilizado: {storage_used / (1024*1024):.2f} MB")
# Document types statistics
types_series = QPieSeries()
types_count = {}
for doc in session.query(Document).all():
types_count[doc.tipo_documento] = types_count.get(
doc.tipo_documento, 0) + 1
for doc_type, count in types_count.items():
types_series.append(f"{doc_type} ({count})", count)
self.types_chart.removeAllSeries()
self.types_chart.addSeries(types_series)
# Brands statistics
brands_series = QBarSeries()
brands_count = {}
for doc in session.query(Document).all():
brands_count[doc.marca] = brands_count.get(doc.marca, 0) + 1
brands_set = QBarSet("Marcas")
brands = list(brands_count.keys())
values = [brands_count[brand] for brand in brands]
brands_set.append(values)
brands_series.append(brands_set)
self.brands_chart.removeAllSeries()
self.brands_chart.addSeries(brands_series)
# Save statistics
stats = Statistics(
date=datetime.utcnow(),
total_documents=total_docs,
total_folders=total_folders,
documents_by_type=json.dumps(types_count),
documents_by_brand=json.dumps(brands_count),
storage_used=storage_used
)
session.add(stats)
session.commit()
except Exception as e:
print(f"Error updating statistics: {str(e)}")
finally:
session.close()