From c673e8dedf47ccbec40a6af12cb8cfc43e8915ed Mon Sep 17 00:00:00 2001 From: Gerdriaan Mulder Date: Sat, 25 Jan 2020 13:33:18 +0100 Subject: [PATCH] Prepare for pricing texts --- cmd/cdrtool/main.go | 57 ++++++++++++++++++++++++++----------- combine.go | 68 +++++++++++++++++++++++++++++++++++++++++++++ import_cdr.go | 1 + 3 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 combine.go diff --git a/cmd/cdrtool/main.go b/cmd/cdrtool/main.go index ce91256..d78bb82 100644 --- a/cmd/cdrtool/main.go +++ b/cmd/cdrtool/main.go @@ -11,14 +11,13 @@ import ( var ( csvFile = flag.String("csv", "", "use this call detail record CSV file") - validate = flag.Bool("validate", false, "only validate the call detail record CSV, perform no other actions") pricesFile = flag.String("prices", "", "use this pricing CSV file") priceExec = flag.Bool("execute", false, "price each CDR according to the prices file") debug = flag.Bool("debug", false, "show extra debugging") ) func usage() { - fmt.Println("Usage: cdrtool -csv [-prices ] [-validate]") + fmt.Println("Usage: cdrtool -csv [-prices ] [-debug] [-execute]") flag.PrintDefaults() } @@ -26,6 +25,7 @@ func main() { flag.Parse() if *csvFile == "" { + usage() log.Fatalf("mandatory -csv not set") } @@ -37,27 +37,23 @@ func main() { } imported, err := cdr.ImportCSV(*csvFile) - if *validate { - if err != nil { - log.Fatalf("[validate] importing %q failed: %v", *csvFile, err) - } - log.Printf("[validate] importing %q succeeded", *csvFile) - return - } if err != nil { log.Fatalf("importing CSV failed: %v", err) } + if *debug { + log.Printf("importing CDR CSV %q succeeded", *csvFile) + } importedPrices, err := cdr.ImportPricesFile(*pricesFile) - if *validate { + if err != nil && *priceExec { + log.Fatalf("importing prices CSV %q failed: %v", *pricesFile, err) + } + if *debug { if err != nil { - log.Fatalf("[validate] importing %q failed: %v", *pricesFile, err) + log.Printf("[warning] importing prices CSV %q failed: %v", *pricesFile, err) + } else { + log.Printf("importing prices CSV %q succeeded", *pricesFile) } - log.Printf("[validate] importing %q succeeded", *pricesFile) - return - } - if err != nil { - log.Fatalf("importing CSV failed: %v", err) } if *debug { @@ -71,6 +67,35 @@ func main() { } if *priceExec { + texts, err := cdr.CombineTextCDRs(imported, importedPrices) + if err != nil { + log.Fatalf("combining text CDRs with prices failed: %v", err) + } + /* + data, err := cdr.CombineDataCDRs(imported, importedPrices) + if err != nil { + log.Fatalf("combining data CDRs with prices failed: %v", err) + } + + calls, err := cdr.CombineCallCDRs(imported, importedPrices) + if err != nil { + log.Fatalf("combining call CDRs with prices failed: %v", err) + } + */ + + if *debug { + for _, t := range texts { + fmt.Printf("t: %+v\n", t) + } + /* + for _, d := range data { + fmt.Printf("d: %+v\n", d) + } + for _, c := range calls { + fmt.Printf("c: %+v\n", c) + } + */ + } } } diff --git a/combine.go b/combine.go new file mode 100644 index 0000000..b8d7395 --- /dev/null +++ b/combine.go @@ -0,0 +1,68 @@ +package cdr + +import ( + "fmt" +) + +func CombineTextCDRs(cdrLines []Line, prices []Price) ([]Text, error) { + // Preprocess the price list into a map of "destination" -> Price, filter out the Text destinations, and count how many Text CDRs we have + destinationToPriceLine := make(map[string]Price) + numTextCDRs := 0 + for _, l := range cdrLines { + if l.Kind != TextLine { + continue + } + if _, ok := destinationToPriceLine[l.Destination]; !ok { + for _, p := range prices { + if p.Type != PriceText { + continue + } + if p.Destination == l.Destination { + destinationToPriceLine[l.Destination] = p + break + } + } + } + numTextCDRs++ + } + + // Now convert each text CDR to a priced Text + ret := make([]Text, numTextCDRs) + i := 0 + for _, l := range cdrLines { + if l.Kind != TextLine { + continue + } + ret[i] = Text{ + PricedLine{ + Line: l, + Cost: destinationToPriceLine[l.Destination].BuyEach, + Price: destinationToPriceLine[l.Destination].SellEach, + }, + } + i++ + } + + return ret, nil +} + +func CombineDataCDRs(cdrLines []Line, prices []Price) ([]Data, error) { + numDataCDRs := 0 + for _, l := range cdrLines { + if l.Kind != DataLine { + continue + } + numDataCDRs++ + } + ret := make([]Data, numDataCDRs) + return ret, fmt.Errorf("not yet implemented") +} + +func CombineCallCDRs(cdrLines []Line, prices []Price) ([]Call, error) { + for _, l := range cdrLines { + if l.Kind != VoiceLine { + continue + } + } + return nil, fmt.Errorf("not yet implemented") +} diff --git a/import_cdr.go b/import_cdr.go index 88a88cf..974fa7f 100644 --- a/import_cdr.go +++ b/import_cdr.go @@ -61,6 +61,7 @@ func ImportCSV(fn string) ([]Line, error) { if err != nil { return nil, fmt.Errorf("opening file %q failed: %v", fn, err) } + defer f.Close() csvReader := csv.NewReader(f) csvReader.FieldsPerRecord = FieldsPerCDRLine