Skip to content

Commit

Permalink
improve_batch
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti committed Aug 22, 2024
1 parent 591b8e2 commit f739621
Showing 1 changed file with 55 additions and 40 deletions.
95 changes: 55 additions & 40 deletions api/methods/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,17 @@ func UpdateMwanSeries(c *gin.Context) {
}
batch.Queue("INSERT INTO mwan_events (time, unit_id, wan, event, interface) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING", time.Unix(event.Timestamp, 0), unit_id, event.Wan, event.Event, event.Interface)
}
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
if batch.Len() != 0 {
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
logs.Logs.Println("[ERR][MWANEVENTS] error inserting data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
}
}

// return ok
Expand Down Expand Up @@ -129,14 +132,17 @@ func UpdateTsAttacks(c *gin.Context) {
}
batch.Queue("INSERT INTO ts_attacks (time, unit_id, ip) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING", time.Unix(attack.Timestamp, 0), unit_id, attack.Ip)
}
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
if batch.Len() != 0 {
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
logs.Logs.Println("[ERR][TSATTACKS] error inserting data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
}
}

// return ok
Expand Down Expand Up @@ -181,14 +187,17 @@ func UpdateTsMalware(c *gin.Context) {
}
batch.Queue("INSERT INTO ts_malware (time, unit_id, src, dst, category, chain) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT DO NOTHING", time.Unix(malware.Timestamp, 0), unit_id, malware.Src, malware.Dst, malware.Category, malware.Chain)
}
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
if batch.Len() != 0 {
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
logs.Logs.Println("[ERR][TSMALWARE] error inserting data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
}
}

// return ok
Expand Down Expand Up @@ -233,14 +242,17 @@ func UpdateOvpnConnections(c *gin.Context) {
}
batch.Queue("INSERT INTO ovpnrw_connections (time, unit_id, instance, common_name, virtual_ip_addr, remote_ip_addr, start_time, duration, bytes_received, bytes_sent) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) ON CONFLICT (time, unit_id, instance, common_name) DO UPDATE SET duration=EXCLUDED.duration, bytes_received=EXCLUDED.bytes_received, bytes_sent=EXCLUDED.bytes_sent", time.Unix(connection.Timestamp, 0), unit_id, connection.Instance, connection.CommonName, connection.VirtualIpAddr, connection.RemoteIpAddr, connection.StartTime, connection.Duration, connection.BytesReceived, connection.BytesSent)
}
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
if batch.Len() != 0 {
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
logs.Logs.Println("[ERR][OVPNCONNECTIONS] error inserting data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
}
}

// return ok
Expand Down Expand Up @@ -286,14 +298,17 @@ func UpdateDpiStats(c *gin.Context) {
}
batch.Queue("INSERT INTO dpi_stats (time, unit_id, client_address, client_name, protocol, host, application, bytes) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT (time, unit_id, client_address, protocol, host, application) DO UPDATE SET bytes = EXCLUDED.bytes", time.Unix(dpi.Timestamp, 0), unit_id, dpi.ClientAddress, dpi.ClientName, dpi.Protocol, dpi.Host, dpi.Application, dpi.Bytes)
}
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
if batch.Len() != 0 {
_, err := dbpool.SendBatch(dbctx, batch).Exec()
if err != nil {
logs.Logs.Println("[ERR][DPISTATS] error inserting data: " + err.Error())
c.JSON(http.StatusInternalServerError, structs.Map(response.StatusInternalServerError{
Message: "Error inserting data",
Data: err.Error(),
Code: 500,
}))
return
}
}

// return ok
Expand Down

0 comments on commit f739621

Please sign in to comment.