#install.packages("tidyverse")
#install.packages("this.path")

#read the CSV of the Netsafe checks
#output some counts used in the paper

library(tidyverse)
library(this.path)

dir <- this.dir()
data_path <- "data"

sink(file.path(dir, "netsafe.log"), append=FALSE, split=TRUE)

version
sessionInfo()

netsafe_data <- read_csv(file.path(dir, data_path, "Netsafe.csv"), col_names = TRUE)

netsafe_data_without_ssl <- 
  netsafe_data %>% select(!("SSL Checker"))

netsafe_data_only <- 
  netsafe_data_without_ssl %>%
  select(IPQS:TrustProfile)

positive_count <- sum(netsafe_data_only == "positive")
neutral_count <- sum(netsafe_data_only == "neutral")
negative_count <- sum(netsafe_data_only == "negative")
no_data_count <- sum(netsafe_data_only == "no-data")

total_count <-  
  positive_count + 
  neutral_count + 
  negative_count + 
  no_data_count

print(paste("Size of Netsafe data is:", 
            nrow(netsafe_data_only) * ncol(netsafe_data_only)))

print(paste("Total count of values = ", total_count))

total_count_exl_no_data <- 
  positive_count + 
  neutral_count + 
  negative_count

print(paste("Total count of values excluding 'no-data' = ", total_count_exl_no_data))

positive_percent <- round(100 * (positive_count / total_count_exl_no_data), digits = 0)
neutral_percent <- round(100 * (neutral_count / total_count_exl_no_data), digits = 0)
negative_percent <- round(100 * (negative_count / total_count_exl_no_data), digits = 0)

print(paste0("positive % (excluding 'no-data'): ",
      positive_percent, "%"))
print(paste0("neutral % (excluding 'no-data'): ",
             neutral_percent, "%"))
print(paste0("negative % (excluding 'no-data'): ",
             negative_percent, "%"))


netsafe_data_without_ssl %>%
  count(`Overall Recommendation`)

scamadviser_hit_rate_count <- netsafe_data_without_ssl %>%
  select(Scamadviser) %>%
  filter(Scamadviser == "negative") %>%
  count()

ipqs_hit_rate_count <- netsafe_data_without_ssl %>%
  select(IPQS) %>%
  filter(IPQS == "negative") %>%
  count()

print(paste("Scamadviser hit rate count: ", scamadviser_hit_rate_count))
print(paste("IPQS hit rate count: ", ipqs_hit_rate_count))


sink()


#check CSV of URL alerts

#alert_urls <- read_csv(file.path(dir, data_path, "scam-url-alerts.csv"), col_names = TRUE)





