R/mod_servers_map.R

Defines functions get_servers mod_servers_map_server mod_servers_map_ui

maxmind_location_db_path <-
  "/opt/maxmind/GeoLite2-City_20210727/GeoLite2-City.mmdb"
maxmind_asn_db_path <-
  "/opt/maxmind/GeoLite2-ASN_20210727/GeoLite2-ASN.mmdb"
servers_json_url <-
  "https://serverstats.nordgedanken.dev/servers?include_members=true"

mapbox_tile_url <- "https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/{z}/{x}/{y}@2x?access_token={access_token}"

#' servers_map UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
#' @import leaflet
mod_servers_map_ui <- function(id) {
  ns <- NS(id)
  tagList(fillPage(div(
    class = "outer",
    leafletOutput(ns("mymap"), width = "100%", height = "100%")
  )))
}

#' servers_map Server Functions
#'
#' @noRd
#' @import leaflet
#' @import dplyr
mod_servers_map_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    
    points <- reactive(get_servers())
    config_map <- get_golem_config("map")
    output$mymap <- renderLeaflet({
      leaflet() %>%
        addTiles(urlTemplate = mapbox_tile_url, attribution = "© <a href=\"https://www.mapbox.com/map-feedback/\">Mapbox</a>", options = tileOptions(access_token = config_map$mapbox_access_token)) %>%
        addMarkers(
          data = points(),
          lat =  ~ lat,
          lng = ~ long,
          popup = ~ as.character(
            sprintf(
              "Serverdomain: %s<br/>IP ASO: %s<br/>IP ASN: %s",
              server,
              aso,
              asn
            )
          ),
          label = ~ as.character(server),
          clusterOptions = leaflet::markerClusterOptions()
        )
    })
  })
}

#'
#'
#' @import rgeolocate
#' @import purrr
#' @import dplyr
get_servers <- function() {
  read.csv(file = '/opt/serverstats/ips.csv', header = FALSE) %>%
    mutate(
      server = V1,
      lat = unlist(purrr::map(
        V2, maxmind, maxmind_location_db_path, c("latitude")
      )),
      long  = unlist(purrr::map(
        V2, maxmind, maxmind_location_db_path, c("longitude")
      )),
      aso  = unlist(purrr::map(
        V2, maxmind, maxmind_asn_db_path, c("aso")
      )),
      asn  = unlist(purrr::map(
        V2, maxmind, maxmind_asn_db_path, c("asn")
      ))
    ) -> df
  return(df)
}
## To be copied in the UI
# mod_servers_map_ui("servers_map_ui_1")

## To be copied in the server
# mod_servers_map_server("servers_map_ui_1")
mx-serverstats/server_stats_web_statistics documentation built on Dec. 21, 2021, 11:06 p.m.