I am writing a stats in a csv file for incoming diameter traffic in my golang server but the file contain a "" character at the start of rach row.
01.;34642222231118599998;21;6588283272|6588283272|300|0|46692|1582611861|,|2001|01.;34642222231118599998;21;6588283272|gytwocsdr.circles.asia|circles.asia|0|1|1582611861 ****01.;34642222231118599998;22;6588080153|6588080153|300|0|46692|1582611861|,|2001|01.;34642222231118599998;22;6588080153|gytwocsdr.circles.asia|circles.asia|0|1|1582611861 ****01.;34642222231118599998;23;6587508893|6587508893|300|0|46692|1582611861|,|2001|01.;34642222231118599998;23;6587508893|gytwocsdr.circles.asia|circles.asia|0|1|1582611861
Please guide me on how to fix this.
stats.go>>
package main
import (
"encoding/csv"
"os"
)
var (
filename = "stats.csv"
)
// write the request and response to csv file
func updateCSVFile(req string, resp string) error {
file, err := os.OpenFile("/home/gyuser/charging-control-engine/stats/stats.csv", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return err
}
defer file.Close()
// solve the encoding problem
file.WriteString("\xEF\xBB\xBF")
writer := csv.NewWriter(file)
defer writer.Flush()
return writer.Write([]string{req, resp})
}
ccr.go >>>
package main
import (
"log"
"time"
"github.com/fiorix/go-diameter/v4/diam/sm"
"github.com/fiorix/go-diameter/v4/diam"
"github.com/fiorix/go-diameter/v4/diam/avp"
)
const (
// CCRInitial - ccr request type
CCRInitial = 1
// CCRUpdate - ccr request type
CCRUpdate = 2
// CCRTermination - ccr request type
CCRTermination = 3
// VM - flags VM-
VM = avp.Mbit | avp.Vbit
// M - flags M-
M = avp.Mbit
// TGPP - vendor id for TGPP
TGPP = 10415
)
// Decoder for command requests
type Decoder interface {
Decode() (*diam.Message, error)
WriteCSV() error
}
// CCRGxDecoder try to parse the gx requests with ccr
type CCRGxDecoder struct {
msg *diam.Message
gx *GxStruct
r *CCRGxRequest
q *CCRGxResponse
auth bool
start time.Time
}
// handle the Credit Control Request(CCR)
func handleCCR(s *sm.Settings, conf *Config) diam.HandlerFunc {
return func(c diam.Conn, m *diam.Message) {
var decoder Decoder
// application id for Gx and Gy
//if m.Header.ApplicationID == conf.Gx.ID {
// decoder = NewCCRGxDecoder(m, &conf.Gx, conf.Server.Auth)
//} else
if m.Header.ApplicationID == conf.Gy.ID {
decoder = NewCCRGyDecoder(m, &conf.Gy, conf.Server.Auth)
} else {
log.Printf("invalid application id: %v\n", m.Header.ApplicationID)
return
}
// decode the requests and make the answer message
nm, err := decoder.Decode()
if err != nil {
log.Printf("decode failed: %v, %v\n", err, m)
return
}
// write the message back to the peer
if _, err := nm.WriteTo(c); err != nil {
log.Printf("failed to write message: %v\n", err)
return
}
// update the request and response to csv file
if err := decoder.WriteCSV(); err != nil {
log.Printf("write csv: %v\n", err)
}
}
}