R/mod_charts.R

Defines functions mod_charts_server mod_charts_ui

#' charts UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
#' @import ggplot2
#' @import plotly
mod_charts_ui <- function(id) {
  ns <- NS(id)
  tagList(fluidRow(
    theme = bs_theme(version = 4, bootswatch = "minty"),
    column(
      width = 6,
      HTML("<h4>Roomsizes across communities</h4>"),
      shinycssloaders::withSpinner(plotlyOutput(ns("communitiesPlot")))
    ),
    column(
      width = 6,
      HTML("<h4>Users across communities</h4>"),
      shinycssloaders::withSpinner(plotlyOutput(ns("usersPlot")))
    )
  ),
  fluidRow(
    theme = bs_theme(version = 4, bootswatch = "minty"),
    column(
      width = 6,
      HTML("<h4>Histogram of servers per room</h4>"),
      shinycssloaders::withSpinner(plotlyOutput(ns("serversPerRoomPlot")))
    )
  ),
  HTML("<br/>"))
}
    
#' charts Server Functions
#'
#' @import ggplot2
#' @import plotly
#' @import dplyr
#' @import bit64
#' @import DBI
#' @import pool
#' @import stringr
#' @import readr
#' @import treemapify
#' @noRd 
mod_charts_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    #ns <- session$ns
    database <- mod_database_server("database")
    
    output$serversPerRoomPlot <- renderPlotly({
      df = database$servers_by_room()
      p <- df |>
        mutate(count_log = log10(count)) |>
        ggplot2::ggplot(aes(x = count_log)) +
        geom_histogram(fill = "orange", binwidth = 0.5) +
        scale_x_continuous(labels = ~ {
          10 ^ .x
        }) +
        labs(x = 'Number of servers (log10)',
             y = 'Count of Rooms',
             caption = "Data exported in realtime on the synapse of nordgedanken.dev") +
        theme_bw() +
        scale_fill_viridis_d()
      fig <- plotly::ggplotly(p)
      fig
    })
    
    
    output$communitiesPlot <- renderPlotly({
      df = database$users_by_room()
      p <- df |>
        mutate(
          Community = case_when(
            str_detect(room_id, 'kde.org') ~ 'KDE',
            str_detect(room_id, 'mozilla.org') ~ 'Mozilla',
            str_detect(room_id, 'nixos.org') ~ 'NixOS',
            str_detect(room_id, 'libera.chat') ~ 'libera.chat',
            TRUE ~ 'Other'
          )
        )|>
        filter(Community != 'Other') |>
        ggplot2::ggplot(aes(x = count, fill = Community)) +
        geom_histogram(position = 'dodge', bins = 20) +
        labs(x = 'Number of Users in Room (log10)',
             y = 'Count of Rooms',
             caption = "Data exported in realtime on the synapse of nordgedanken.dev") +
        scale_x_log10() +
        theme_bw() +
        scale_fill_viridis_d()
      
      fig <- plotly::ggplotly(p)
      fig
    })
    output$usersPlot <- renderPlotly({
      df = database$users()
      p <- df |>
        mutate(
          Community = case_when(
            str_detect(state_key, 'kde.org') ~ 'KDE',
            str_detect(state_key, 'mozilla.org') ~ 'Mozilla',
            str_detect(state_key, 'nixos.org') ~ 'NixOS',
            str_detect(state_key, 'matrix.org') ~ 'Matrix.org',
            str_detect(state_key, 'libera.chat') ~ 'libera.chat',
            TRUE ~ 'Other'
          )
        ) |>
        filter(Community != 'Other') |>
        count(Community) |>
        ggplot2::ggplot(aes(x = Community,
                            y = n,
                            fill = Community)) +
        geom_col() +
        labs(x = 'Community',
             y = 'Count of Members',
             caption = "Data exported in realtime on the synapse of nordgedanken.dev") +
        scale_y_log10() +
        theme_bw() +
        scale_fill_viridis_d()
      
      fig <- plotly::ggplotly(p)
      fig
    })
  })
}
    
## To be copied in the UI
# mod_charts_ui("charts_ui_1")
    
## To be copied in the server
# mod_charts_server("charts_ui_1")
mx-serverstats/server_stats_web_statistics documentation built on Dec. 21, 2021, 11:06 p.m.