R/gn-plot.R

Defines functions gn_port_summary

Documented in gn_port_summary

#' Generate a simple port summary plot
#'
#' @md
#' @param port,protocol,gn_key same as [research_time_series()]
#' @param save if not `NULL`, the full path to save the file (with `.png` extension)
#' @return data and plot (invisibly)
#' @export
gn_port_summary <- function(port, protocol = c("tcp", "udp"),
                            save = NULL,
                            gn_key = Sys.getenv("GREYNOISE_API_KEY")) {

  research_time_series(
    port = port, protocol = protocol, summarise = TRUE, gn_key = gn_key
  ) -> psum

  logo <- png::readPNG(system.file("img", "gn-small.png", package="greynoise"))
  ann <- rasterGrob(logo, width=unit(10, "lines"))

  ggplot(psum, aes(date, ips)) +
    geom_area(color = "white", fill = "#ffffff55") +
    scale_x_date(expand=c(0,0)) +
    scale_y_continuous(expand = c(0,0), labels=scales::comma) +
    labs(
      x = NULL, y ="# Unique IPvs",
      title = sprintf("Port %s Summary History", port),
      caption = "Source: GreyNoise Intelligence"
    ) +
    theme_gn() -> gg

  gg <- flush_ticks(gg, cat = FALSE)

  gb <- ggplot_build(gg)
  gt <- ggplot_gtable(gb)

  gt$grobs[[17]] <- ann

  plot(gt)

  if (!is.null(save)) {

    ggsave(
      filename = path.expand(save),
      plot = gt,  device = "png", dpi = "retina",
      width = 7.5, height = 4.5, bg = "#131111"
    )

    message(sprintf("Plot saved to [%s]", path.expand(save)))

  }

  list(
    data = psum,
    plot = gt
  ) -> out

  invisible(out)

}
hrbrmstr/greynoise documentation built on March 11, 2020, 5:31 a.m.