package main import ( "encoding/csv" "flag" "fmt" "log" "os" "sort" "src.wolkict.net/cdr" ) var ( csvFile = flag.String("csv", "", "use this call detail record CSV file") pricesFile = flag.String("prices", "", "use this pricing CSV file") priceExec = flag.Bool("execute", false, "price each CDR according to the prices file") debugImport = flag.Bool("debugImport", false, "show extra debugging info for import") debug = flag.Bool("debug", false, "show extra debugging info") export = flag.String("export", "", "export priced calls to this csv") ) func usage() { fmt.Println("Usage: cdrtool -csv [-prices ] [-debug] [-execute]") flag.PrintDefaults() } func main() { flag.Parse() cdr.SetDebug(*debug) if *csvFile == "" { usage() log.Fatalf("mandatory -csv not set") } if _, err := os.Open(*csvFile); err != nil { log.Fatalf("cannot access CSV %q: %v", *csvFile, err) } if _, err := os.Open(*pricesFile); err != nil { log.Printf("[warning] cannot access prices file %q", *pricesFile) } imported, err := cdr.ImportCSV(*csvFile) 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 err != nil && *priceExec { log.Fatalf("importing prices CSV %q failed: %v", *pricesFile, err) } if *debugImport { if err != nil { log.Printf("[warning] importing prices CSV %q failed: %v", *pricesFile, err) } else { log.Printf("importing prices CSV %q succeeded", *pricesFile) } } if *debugImport { for _, i := range imported { fmt.Printf("l: %+v\n", i) } for _, i := range importedPrices { fmt.Printf("p: %+v\n", i) } } 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) } } if *export != "" { toExport := make([][]string, 0) toExport = append(toExport, cdr.ExportLineHeader) sc := cdr.NewSortableCalls(calls) sort.Sort(sc) callCost := 0 callPrice := 0 for _, c := range sc.Calls { if *debug { fmt.Printf("%s\n", c.String()) } callCost += c.Cost callPrice += c.Price toExport = append(toExport, cdr.ExportCall(c)) } st := cdr.NewSortableTexts(texts) sort.Sort(st) textCost := 0 textPrice := 0 for _, t := range st.Texts { if *debug { fmt.Printf("%s\n", t.String()) } textCost += t.PricedLine.Cost textPrice += t.PricedLine.Price toExport = append(toExport, cdr.ExportText(t)) } sd := cdr.NewSortableData(data) sort.Sort(sd) dataCost := 0 dataPrice := 0 for _, d := range sd.Data { if *debug { fmt.Printf("%s\n", d.String()) } dataCost += d.PricedLine.Cost dataPrice += d.PricedLine.Price toExport = append(toExport, cdr.ExportData(d)) } fmt.Printf("callCost: %.4f, callPrice: %.4f\n", float64(callCost)/10000.0, float64(callPrice)/10000.0) fmt.Printf("textCost: %.4f, textPrice: %.4f\n", float64(textCost)/10000.0, float64(textPrice)/10000.0) fmt.Printf("dataCost: %.4f, dataPrice: %.4f\n", float64(dataCost)/10000.0, float64(dataPrice)/10000.0) _, err = os.Stat(*export) if !os.IsNotExist(err) { log.Fatalf("refusing to write to existing file %q for exporting", *export) } csvExportFile, err := os.Create(*export) if err != nil { log.Fatalf("cannot create export file %q: %v", *export, err) } csvWriter := csv.NewWriter(csvExportFile) err = csvWriter.WriteAll(toExport) if err != nil { log.Fatalf("cannot export csv to %q: %v", *export, err) } log.Printf("exported priced CDRs to %q", *export) } } }