feat: accept batch (#4)

This commit is contained in:
Anthony Laibe 2021-11-09 13:53:49 +01:00 committed by GitHub
parent f0eca9dad5
commit c92bd95a8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"flag"
"log"
"time"
"github.com/status-im/dev-telemetry/telemetry"
@ -20,9 +21,14 @@ func main() {
aggregator := telemetry.NewAggregator(db)
c := cron.New()
c.AddFunc("0 * * * *", func() {
_, err := c.AddFunc("0 * * * *", func() {
aggregator.Run(time.Hour)
})
if err != nil {
log.Fatalf("Error adding cron job: %v", err)
}
c.Start()
defer c.Stop()

View File

@ -35,9 +35,9 @@ func handleHealthCheck(w http.ResponseWriter, r *http.Request) {
func (s *Server) createReceivedMessages(w http.ResponseWriter, r *http.Request) {
start := time.Now()
var receivedMessage ReceivedMessage
var receivedMessages []ReceivedMessage
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&receivedMessage); err != nil {
if err := decoder.Decode(&receivedMessages); err != nil {
log.Println(err)
err := respondWithError(w, http.StatusBadRequest, "Invalid request payload")
@ -48,16 +48,24 @@ func (s *Server) createReceivedMessages(w http.ResponseWriter, r *http.Request)
}
defer r.Body.Close()
if err := receivedMessage.put(s.DB); err != nil {
log.Println(err)
var ids []int
for _, receivedMessage := range receivedMessages {
if err := receivedMessage.put(s.DB); err != nil {
log.Println("could not save message", err, receivedMessage)
continue
}
ids = append(ids, receivedMessage.ID)
}
err := respondWithError(w, http.StatusInternalServerError, err.Error())
if len(ids) != len(receivedMessages) {
err := respondWithError(w, http.StatusInternalServerError, "Could not save all record")
if err != nil {
log.Println(err)
}
return
}
err := respondWithJSON(w, http.StatusCreated, receivedMessage)
err := respondWithJSON(w, http.StatusCreated, receivedMessages)
if err != nil {
log.Println(err)
}