From a5ee6111fc46148bf8ce1104184b4ec4576f8420 Mon Sep 17 00:00:00 2001 From: Florian Brinker Date: Fri, 8 Nov 2019 13:33:46 +0100 Subject: [PATCH] alpha state --- .gitignore | 2 ++ README.md | 33 ++++++++++++++++++++++++ main.go | 60 ++++++++++++++++++++++++++++++------------- template.html | 70 +++++++++++++++++++++++++++++++++++++++++++++------ test.csv | 3 --- 5 files changed, 140 insertions(+), 28 deletions(-) create mode 100644 README.md delete mode 100644 test.csv diff --git a/.gitignore b/.gitignore index 8e53e99..eb63f7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ job-visualizer +/*.csv +/*.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..13dd521 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# Job Visualizer +This project is meant to visualize scheduled jobs for a given timespan, +so you're able to see bottlenecks or timeframes where to many jobs are run simultaneously. + +**This tool analyzes a CSV file and creates an HTML file.** + +## Usage +For usage information call +``` +./job-visualizer -h +``` + +That will result in +``` +Usage of ./job-visualizer: + -file string + File to import + -output string + File to generate +``` + +* `--file` Is a csv file you want to analyze +* `--output` Is an html file the tool will generate + +### CSV File Format +Currently the format is fixed and planned to be configurable in the future. + +You need the following columns: +* Prefix/Group +* job_name: The name of the job +* job_type: The type of the job +* start_time: A string with start times for the group, e.g. `"2019-11-02 21:10:21,2019-11-03 21:19:33"` +* end_time: As start_time, but the end times, e.g. `"2019-11-02 21:10:22,2019-11-03 21:19:33"` \ No newline at end of file diff --git a/main.go b/main.go index 3ef6700..1577fda 100644 --- a/main.go +++ b/main.go @@ -7,33 +7,39 @@ import ( "fmt" "html/template" "io" + "log" "os" + "strings" ) func main() { var file = flag.String("file", "", "File to import") - var columnStart = flag.Int("columnStart", -1, "Column number with a start Datetime") - var columnEnd = flag.Int("columnEnd", -1, "Column number with a end Datetime") - var columnDescription = flag.Int("columnDescription", -1, "Column number with a descriptive name or similar") + var output = flag.String("output", "", "File to generate") + //var columnStart = flag.Int("columnStart", -1, "Column number with a start Datetime") + //var columnEnd = flag.Int("columnEnd", -1, "Column number with a end Datetime") + //var columnDescription = flag.Int("columnDescription", -1, "Column number with a descriptive name or similar") flag.Parse() - if _, err := os.Stat(*file); os.IsNotExist(err) || *columnStart == -1 || *columnEnd == -1 || *columnDescription == -1 { + if _, err := os.Stat(*file); os.IsNotExist(err) || *output == "" { flag.Usage() return } - fmt.Println("Scanning file " + *file + "...") - readCsvFile(*file, *columnStart, *columnEnd, *columnDescription) + fmt.Println("Analyzing " + *file + "...") + readCsvFile(*file, *output) } -func readCsvFile(csvFile string, columnStart int, columnEnd int, columnDescription int) { - f, _ := os.Open(csvFile) - r := csv.NewReader(bufio.NewReader(f)) +func readCsvFile(input string, output string) { + csvFile, _ := os.Open(input) + reader := csv.NewReader(bufio.NewReader(csvFile)) type JobDetails struct { - Start string - End string - Description string + Id int + Name string + Type string + Provider string + Start []string + End []string } type ViewData struct { @@ -42,25 +48,43 @@ func readCsvFile(csvFile string, columnStart int, columnEnd int, columnDescripti var data ViewData + id := 0 for { - row, err := r.Read() + row, err := reader.Read() if err == io.EOF { break } + id++ job := JobDetails{ - Start: row[columnStart], - End: row[columnEnd], - Description: row[columnDescription], + Id: id, + Name: row[1], + Type: row[2], + Provider: row[0], + Start: strings.Split(row[3], ","), + End: strings.Split(row[4], ","), } data.Jobs = append(data.Jobs, job) } + fmt.Println("Scanning done.") + fmt.Println("Creating template...") tpl, err := template.ParseFiles("template.html") if err != nil { fmt.Println(err) } - fmt.Printf("%+v\n", data) - tpl.Execute(os.Stdout, data) + + outputFile, err := os.Create(output) + if err != nil { + log.Println("create file: ", err) + return + } + err = tpl.Execute(outputFile, data) + if err != nil { + log.Print("execute: ", err) + return + } + outputFile.Close() + fmt.Println("Generated " + output) } diff --git a/template.html b/template.html index 631049c..b28bc7c 100644 --- a/template.html +++ b/template.html @@ -1,9 +1,65 @@ - - {{ range .Jobs }} - - - - {{ end }} -
{{ .Description }}
+ + + + + + + + +
+ + \ No newline at end of file diff --git a/test.csv b/test.csv deleted file mode 100644 index 09ce20a..0000000 --- a/test.csv +++ /dev/null @@ -1,3 +0,0 @@ -2019-01-01 00:00:00,2019-01-01 00:08:00,echo Hello -2019-01-03 01:05:00,2019-01-03 03:14:00,echo Ola -2019-01-03 01:07:00,2019-01-03 12:45:00,echo Aloah \ No newline at end of file