Skip to content

Instantly share code, notes, and snippets.

@agarciamontoro
Last active August 3, 2023 17:22
Show Gist options
  • Save agarciamontoro/75a64765f74c5eae86805e7ea3a33c15 to your computer and use it in GitHub Desktop.
Save agarciamontoro/75a64765f74c5eae86805e7ea3a33c15 to your computer and use it in GitHub Desktop.
Hacky script to merge two CSVs with handler rates
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strconv"
)
type Line struct {
Handler string
Rate float64
}
var (
communityFlag *string
loadtestFlag *string
outputFlag *string
)
func init() {
communityFlag = flag.String("community", "", "Path to the community CSV file.")
loadtestFlag = flag.String("loadtest", "", "Path to the loadtest CSV file.")
outputFlag = flag.String("output", "out.csv", "Name of the output CSV file with the merged contents.")
}
func main() {
flag.Parse()
if *communityFlag == "" {
log.Fatal(fmt.Errorf("--community flag must not be empty"))
}
if *loadtestFlag == "" {
log.Fatal(fmt.Errorf("--community flag must not be empty"))
}
outFile, err := os.Create(*outputFlag)
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
commMap, err := readCsv(*communityFlag)
if err != nil {
log.Fatal(err)
}
ltMap, err := readCsv(*loadtestFlag)
if err != nil {
log.Fatal(err)
}
allMap := make(map[string]bool)
for handler := range ltMap {
allMap[handler] = true
}
for handler := range commMap {
allMap[handler] = true
}
out := csv.NewWriter(outFile)
out.Write([]string{"Handler", "Community rate", "Loadtest rate"})
for handler := range allMap {
row := []string{handler}
commRate, commOk := commMap[handler]
if commOk {
row = append(row, strconv.FormatFloat(commRate, 'f', 6, 64))
} else {
row = append(row, "-")
}
ltRate, ltOk := ltMap[handler]
if ltOk {
row = append(row, strconv.FormatFloat(ltRate, 'f', 6, 64))
} else {
row = append(row, "-")
}
out.Write(row)
}
out.Flush()
}
func readCsv(path string) (map[string]float64, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
reader.LazyQuotes = true // Avoid error when parsing quotes in the header (which we will ignore in any case)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
recordsMap := make(map[string]float64)
for idx, row := range records[1:] {
if len(row) != 3 {
return nil, fmt.Errorf("row %d has %d columns, but 3 are expected", idx, len(row))
}
// Ignore the first column, which is the time
_, handler, rate := row[0], row[1], row[2]
recordsMap[handler], err = strconv.ParseFloat(rate, 64)
if err != nil {
return nil, err
}
}
return recordsMap, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment