Browse Source

Prepare for pricing texts

master
Gerdriaan Mulder 6 years ago
parent
commit
c673e8dedf
  1. 57
      cmd/cdrtool/main.go
  2. 68
      combine.go
  3. 1
      import_cdr.go

57
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 <file.csv> [-prices <file.csv>] [-validate]")
fmt.Println("Usage: cdrtool -csv <file.csv> [-prices <file.csv>] [-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)
}
*/
}
}
}

68
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")
}

1
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

Loading…
Cancel
Save