#!/usr/bin/env Rscript
# ==========================================================================
#     _   _ _ ____            ____          _____
#    | | | (_)  _ \ ___ _ __ / ___|___  _ _|_   _| __ __ _  ___ ___ _ __
#    | |_| | | |_) / _ \ '__| |   / _ \| '_ \| || '__/ _` |/ __/ _ \ '__|
#    |  _  | |  __/  __/ |  | |__| (_) | | | | || | | (_| | (_|  __/ |
#    |_| |_|_|_|   \___|_|   \____\___/|_| |_|_||_|  \__,_|\___\___|_|
#
#       ---  High-Performance Connectivity Tracer (HiPerConTracer)  ---
#                 https://www.nntb.no/~dreibh/hipercontracer/
# ==========================================================================
#
# High-Performance Connectivity Tracer (HiPerConTracer)
# Copyright (C) 2015-2025 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@simula.no

source("HiPerConTracer.R")


# ###### Main program #######################################################

# ====== Handle arguments ===================================================
args <- commandArgs(trailingOnly = TRUE)
if(sys.nframe() == 0L) {
   # Launched directly from shell:
   args <- commandArgs(trailingOnly = TRUE)
   # ------ Command-line start with Rscript ---------------------------------
   if(length(args) < 1) {
     stop("Usage: r-traceroute-example traceroute-hpct-file|traceroute-csv-file|results-directory")
   }
   name <- args[1]
} else {
   # Launched via source(...) in R:
   name <- "../traceroute.csv.gz"
   cat(paste(sep="", "Using default name ", name, "\n"))
}


# ====== Read input data ====================================================

# ------ Name is a directory ------------------------------------------------
if(dir.exists(name)) {
   cat(sep="", "Reading all Traceroute results in directory ", name, " ...\n")
   data <- readHiPerConTracerTracerouteResultsFromDirectory(name, "Traceroute-[^/]*(\\.hpct|\\.results)[^/]*")
# ------ Name is a HiPerConTracer results file (.hpct*) ---------------------
} else if(grepl("(\\.hpct[^/]*|\\.results[^/]*)$", name)) {
   cat(sep="", "Reading from HiPerConTracer results file ", name, " ...\n")
   data <- readHiPerConTracerTracerouteResults(name)
# ------ Name is a CSV file -------------------------------------------------
} else {
   cat(sep="", "Reading from CSV file ", name, " ...\n")
   data <- readHiPerConTracerTracerouteResultsFromCSV(name)
}

cat("\x1b[33mData columns:\x1b[0m\n")
print(colnames(data))


# ====== Processing example =================================================

# Obtain all distinct parameter combinations:
groups <- data %>% distinct(MeasurementID,
                            SourceIP,
                            DestinationIP,
                            Protocol)
# Note:
# groups[[n]] -> n-th column (1=MeasurementID, 2=SourceIP, ...)
# groups[n]   -> n-th row

for(i in seq(1, length(groups[[1]]))) {
   dataSubset <- data %>% filter(MeasurementID == groups[i]$MeasurementID,
                                 SourceIP      == groups[i]$SourceIP,
                                 DestinationIP == groups[i]$DestinationIP,
                                 Protocol      == groups[i]$Protocol
                                )

   if(length(dataSubset$RTT.App) > 0) {
      cat(sep="", "\x1b[36mMeasurement #", groups[i]$MeasurementID, ":   \x1b[35m",
         groups[i]$SourceIP, " <-> ", groups[i]$DestinationIP, " via ", groups[i]$Protocol, "   \x1b[32m",
         # Print time range:
         as.character(min(dataSubset$Timestamp)), " - ",
         as.character(max(dataSubset$Timestamp)), "\x1b[0m\n",
         # Print number of Traceroutes:
         length( subset(dataSubset, dataSubset$HopNumber == 1)$RTT.App ), " Traceroutes",
         "\x1b[0m\n")
   }
}
