mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
add websockets suppor6
This commit is contained in:
@@ -67,7 +67,7 @@ async def create_request(
|
||||
|
||||
# Получаем актуальную статистику
|
||||
stats = requests.get_statistics(db)
|
||||
logger.info(f"Current statistics: {stats}")
|
||||
logger.info(f"Current statistics after new request: {stats}")
|
||||
|
||||
# Получаем полные данные о заявке для отправки через WebSocket
|
||||
request_data = {
|
||||
@@ -89,7 +89,7 @@ async def create_request(
|
||||
"statistics": stats
|
||||
}
|
||||
|
||||
logger.info(f"Broadcasting WebSocket message: {ws_message}")
|
||||
logger.info(f"Broadcasting WebSocket message for new request: {ws_message}")
|
||||
# Отправляем уведомление через WebSocket всем админам
|
||||
await notification_manager.broadcast_to_admins(ws_message)
|
||||
|
||||
@@ -154,10 +154,5 @@ def get_request_statistics(
|
||||
):
|
||||
"""Get request statistics (admin only)"""
|
||||
stats = requests.get_statistics(db)
|
||||
return {
|
||||
"total": stats["total"],
|
||||
"by_status": {
|
||||
status: count
|
||||
for status, count in stats["by_status"].items()
|
||||
}
|
||||
}
|
||||
logger.info(f"Returning statistics: {stats}")
|
||||
return stats
|
@@ -88,10 +88,12 @@ const periodOptions = [
|
||||
// Загрузка статистики
|
||||
const fetchStatistics = async () => {
|
||||
try {
|
||||
console.log('StatisticsPanel: Fetching statistics');
|
||||
const [statsResponse, chartsResponse] = await Promise.all([
|
||||
axios.get('/api/requests/statistics'),
|
||||
axios.get(`/api/statistics?period=${period.value}`)
|
||||
]);
|
||||
console.log('StatisticsPanel: Received statistics:', statsResponse.data);
|
||||
statistics.value = statsResponse.data;
|
||||
chartData.value = chartsResponse.data;
|
||||
} catch (error) {
|
||||
@@ -104,17 +106,26 @@ const handleWebSocketMessage = (data: any) => {
|
||||
console.log('StatisticsPanel: Received WebSocket message:', data);
|
||||
|
||||
if (data.statistics) {
|
||||
console.log('StatisticsPanel: Old statistics:', statistics.value);
|
||||
console.log('StatisticsPanel: Updating statistics:', data.statistics);
|
||||
statistics.value = data.statistics;
|
||||
statistics.value = {
|
||||
total: data.statistics.total,
|
||||
by_status: { ...data.statistics.by_status }
|
||||
};
|
||||
console.log('StatisticsPanel: New statistics:', statistics.value);
|
||||
}
|
||||
|
||||
// Обновляем статистику при изменении статуса заявки
|
||||
if (data.type === 'status_update' && data.data) {
|
||||
fetchStatistics(); // Обновляем графики
|
||||
// Обновляем статистику при изменении статуса заявки или новой заявке
|
||||
if (data.type === 'status_update' || data.type === 'new_request') {
|
||||
console.log('StatisticsPanel: Fetching new statistics due to update');
|
||||
fetchStatistics();
|
||||
}
|
||||
};
|
||||
|
||||
watch(period, fetchStatistics);
|
||||
watch(period, () => {
|
||||
console.log('StatisticsPanel: Period changed, fetching new data');
|
||||
fetchStatistics();
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
console.log('StatisticsPanel: Component mounted');
|
||||
|
@@ -52,10 +52,11 @@ class WebSocketClient {
|
||||
}
|
||||
|
||||
this.socket.onmessage = (event) => {
|
||||
console.log('WebSocket: Message received', event.data)
|
||||
console.log('WebSocket: Message received:', event.data)
|
||||
try {
|
||||
const data = JSON.parse(event.data)
|
||||
if (data.type !== 'pong') { // Игнорируем pong сообщения
|
||||
if (data.type !== 'pong') {
|
||||
console.log('WebSocket: Broadcasting message to handlers:', data)
|
||||
this.messageHandlers.forEach(handler => {
|
||||
try {
|
||||
handler(data)
|
||||
@@ -106,15 +107,17 @@ class WebSocketClient {
|
||||
}
|
||||
|
||||
addMessageHandler(handler: (data: any) => void) {
|
||||
console.log('WebSocket: Adding message handler')
|
||||
this.messageHandlers.push(handler)
|
||||
console.log('WebSocket: Message handler added')
|
||||
console.log('WebSocket: Total handlers:', this.messageHandlers.length)
|
||||
}
|
||||
|
||||
removeMessageHandler(handler: (data: any) => void) {
|
||||
console.log('WebSocket: Removing message handler')
|
||||
const index = this.messageHandlers.indexOf(handler)
|
||||
if (index > -1) {
|
||||
this.messageHandlers.splice(index, 1)
|
||||
console.log('WebSocket: Message handler removed')
|
||||
console.log('WebSocket: Handler removed, remaining handlers:', this.messageHandlers.length)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +134,7 @@ class WebSocketClient {
|
||||
this.currentType = null
|
||||
this.currentId = undefined
|
||||
this.isConnected.value = false
|
||||
console.log('WebSocket: Disconnected')
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -86,11 +86,13 @@ const statistics = ref({
|
||||
// Загрузка данных
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
console.log('AdminDashboard: Fetching data')
|
||||
const [requestsResponse, statsResponse] = await Promise.all([
|
||||
axios.get('/api/requests/admin'),
|
||||
axios.get('/api/requests/statistics')
|
||||
])
|
||||
requests.value = requestsResponse.data
|
||||
console.log('AdminDashboard: Received statistics:', statsResponse.data)
|
||||
statistics.value = statsResponse.data
|
||||
} catch (error) {
|
||||
console.error('Error fetching data:', error)
|
||||
@@ -103,42 +105,25 @@ const handleWebSocketMessage = (data: any) => {
|
||||
|
||||
// Обновляем статистику, если она пришла в сообщении
|
||||
if (data.statistics) {
|
||||
console.log('AdminDashboard: Old statistics:', statistics.value)
|
||||
console.log('AdminDashboard: Updating statistics:', data.statistics)
|
||||
statistics.value = {
|
||||
total: data.statistics.total,
|
||||
by_status: { ...data.statistics.by_status }
|
||||
}
|
||||
console.log('AdminDashboard: New statistics:', statistics.value)
|
||||
}
|
||||
|
||||
if (data.type === 'new_request' && data.data) {
|
||||
console.log('AdminDashboard: Adding new request:', data.data)
|
||||
// Добавляем новую заявку в начало списка
|
||||
requests.value = [data.data, ...requests.value]
|
||||
|
||||
// Обновляем счетчик для нового статуса
|
||||
const status = data.data.status
|
||||
if (!statistics.value.by_status[status]) {
|
||||
statistics.value.by_status[status] = 0
|
||||
}
|
||||
statistics.value.by_status[status]++
|
||||
statistics.value.total++
|
||||
} else if (data.type === 'status_update' && data.data) {
|
||||
console.log('AdminDashboard: Updating request status:', data.data)
|
||||
// Обновляем статус заявки в списке
|
||||
const request = requests.value.find(r => r.id === data.data.id)
|
||||
if (request) {
|
||||
const oldStatus = request.status
|
||||
const newStatus = data.data.status
|
||||
request.status = newStatus
|
||||
|
||||
// Обновляем счетчики статусов
|
||||
if (statistics.value.by_status[oldStatus]) {
|
||||
statistics.value.by_status[oldStatus]--
|
||||
}
|
||||
if (!statistics.value.by_status[newStatus]) {
|
||||
statistics.value.by_status[newStatus] = 0
|
||||
}
|
||||
statistics.value.by_status[newStatus]++
|
||||
request.status = data.data.status
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user