Advertisement
MCFoger

Untitled

Apr 12th, 2025 (edited)
469
0
29 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.22 KB | None | 0 0
  1. library(readxl)
  2. library(dplyr)
  3. library(ggplot2)
  4. library(lubridate)
  5. library(scales)
  6.  
  7. # Шлях до файлу
  8. file_path <- "test.xlsx"
  9.  
  10. # Отримуємо назви аркушів
  11. sheets <- excel_sheets(file_path)
  12. print(paste("Аркуші у файлі:", paste(sheets, collapse = ", ")))
  13.  
  14. # Завантаження першого аркуша
  15. df <- read_excel(file_path, sheet = sheets[1])
  16.  
  17. # Перевірка перших рядків
  18. head(df)
  19.  
  20. # Перетворення до datetime та розрахунок метрик
  21. df <- df %>%
  22.   mutate(
  23.     request_time = as.POSIXct(request_time, format = "%Y-%m-%d %H:%M:%S"),
  24.     start_time = as.POSIXct(start_time, format = "%Y-%m-%d %H:%M:%S"),
  25.     finish_time = as.POSIXct(finish_time, format = "%Y-%m-%d %H:%M:%S"),
  26.  
  27.     # Перераховуємо час у хвилини
  28.     response_time = as.numeric(difftime(start_time, request_time, units = "secs")) / 60,
  29.     handling_time = as.numeric(difftime(finish_time, start_time, units = "secs")) / 60,
  30.     total_time = as.numeric(difftime(finish_time, request_time, units = "secs")) / 60
  31.   )
  32.  
  33. # Видалення запитів з аномальними значеннями (наприклад, більше ніж 5000 хвилин)
  34. df <- df %>%
  35.   filter(response_time <= 5000)
  36.  
  37. # Базова статистика
  38. summary_stats <- df %>%
  39.   summarise(
  40.     avg_response_time = mean(response_time, na.rm = TRUE),
  41.     avg_handling_time = mean(handling_time, na.rm = TRUE),
  42.     avg_total_time = mean(total_time, na.rm = TRUE)
  43.   )
  44.  
  45. print("📌 Загальні середні метрики (у хвилинах):")
  46. print(summary_stats)
  47.  
  48. # Частка запитів > 15 та > 45 хв
  49. exceed_15 <- mean(df$response_time > 15, na.rm = TRUE)
  50. exceed_45 <- mean(df$response_time > 45, na.rm = TRUE)
  51.  
  52. cat("\n🔴 Частка запитів з response_time > 15 хв:", percent(exceed_15), "\n")
  53. cat("🔴 Частка запитів з response_time > 45 хв:", percent(exceed_45), "\n")
  54.  
  55. # Аналіз по командах
  56. team_stats <- df %>%
  57.   group_by(team) %>%
  58.   summarise(
  59.     count = n(),
  60.     avg_response = mean(response_time, na.rm = TRUE),
  61.     avg_handling = mean(handling_time, na.rm = TRUE),
  62.     avg_total = mean(total_time, na.rm = TRUE)
  63.   )
  64.  
  65. print("📊 Порівняння команд:")
  66. print(team_stats)
  67.  
  68. # Найгірші агенти
  69. moderator_stats <- df %>%
  70.   group_by(moderator) %>%
  71.   summarise(
  72.     count = n(),
  73.     avg_response = mean(response_time, na.rm = TRUE)
  74.   ) %>%
  75.   filter(count > 30) %>%
  76.   arrange(desc(avg_response))
  77.  
  78. print("🚨 Топ агентів за response_time:")
  79. print(head(moderator_stats, 10))
  80.  
  81. # Гістограма часу відповіді
  82. ggplot(df, aes(x = response_time)) +
  83.   geom_histogram(binwidth = 60, fill = "steelblue", color = "white") +
  84.   scale_x_continuous(limits = c(0, 5000)) +  # Збільшено ліміт для гістограми
  85.   labs(title = "Розподіл часу відповіді", x = "Хвилини", y = "Кількість запитів")
  86. # Збереження гістограми часу відповіді в PNG
  87. ggsave("~/Desktop/response_time_histogram.png", plot = last_plot(), width = 8, height = 6)
  88.  
  89. # Середній час по командах
  90. ggplot(team_stats, aes(x = team, y = avg_response, fill = team)) +
  91.   geom_bar(stat = "identity") +
  92.   labs(title = "Середній час відповіді по командах", x = "Команда", y = "Хвилини") +
  93.   theme_minimal()
  94. # Збереження графіка середнього часу по командах в PNG
  95. ggsave("~/Desktop/average_response_time_by_team.png", plot = last_plot(), width = 8, height = 6)
  96.  
  97. # Топ-агенти
  98. ggplot(head(moderator_stats, 10), aes(x = reorder(as.factor(moderator), -avg_response), y = avg_response)) +
  99.   geom_bar(stat = "identity", fill = "red") +
  100.   labs(title = "Топ-10 агентів з найбільшим часом відповіді", x = "ID Агента", y = "Середній час (хв)") +
  101.   coord_flip()
  102.  
  103.  
  104. # Збереження графіка топ-10 агентів в PNG
  105. ggsave("~/Desktop/top_10_agents.png", plot = last_plot(), width = 8, height = 6)
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement