From f73962178cbe5602452c6a6f1bf046d5305650d1 Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Thu, 22 Aug 2024 12:59:13 +0200 Subject: [PATCH] improve_batch --- api/methods/report.go | 95 +++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/api/methods/report.go b/api/methods/report.go index 4c2e83b..bdadee5 100644 --- a/api/methods/report.go +++ b/api/methods/report.go @@ -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 @@ -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 @@ -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 @@ -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 @@ -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