draft ingest csv handler
This commit is contained in:
parent
9ab669868d
commit
e46b71be51
10
README.md
10
README.md
@ -6,7 +6,15 @@ Prueba técnica para el puesto de desarrollador Go/C++
|
||||
|
||||
Compilar todos los servicios e iniciar los contenedores Docker.
|
||||
|
||||
`docker compose --env-file <path/to/file> up --build`
|
||||
```bash
|
||||
docker compose --env-file <path/to/file> up --build`
|
||||
```
|
||||
|
||||
Hacer petición POST con fichero a `/ingest/csv`
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/ingest/csv -F "file=@meteo.csv"
|
||||
```
|
||||
|
||||
## Decisiones técnica
|
||||
|
||||
|
||||
@ -44,4 +44,7 @@ var (
|
||||
ErrMinTempOutOfRange = errors.New("min temp out of range (must be >= -20°C)")
|
||||
ErrRainfallOutOfRange = errors.New("rainfall out of range (must be 0-500 mm)")
|
||||
ErrCloudinessOutOfRange = errors.New("cloudiness out of range (must be 0-100%)")
|
||||
ErrParsingForm = errors.New("error parsing form")
|
||||
ErrRetrievingFile = errors.New("error retrieving file")
|
||||
ErrReadingFile = errors.New("error reading file")
|
||||
)
|
||||
|
||||
@ -1,8 +1,15 @@
|
||||
package meteo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Handler struct{}
|
||||
@ -12,7 +19,60 @@ func NewHandler() *Handler {
|
||||
}
|
||||
|
||||
func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "hello from csv")
|
||||
err := r.ParseMultipartForm(10 << 20)
|
||||
if err != nil {
|
||||
http.Error(w, ErrParsingForm.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(w, ErrRetrievingFile.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
content, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
http.Error(w, ErrReadingFile.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
hash := sha256.Sum256(content)
|
||||
checksum := hex.EncodeToString(hash[:])
|
||||
|
||||
fileStats := &FileStats{
|
||||
FileChecksum: checksum,
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
csvParser := &CSV{}
|
||||
inserted, rejected, err := csvParser.Parse(bytes.NewReader(content), fileStats)
|
||||
if err != nil {
|
||||
slog.Error(ErrCannotParseFile.Error(),
|
||||
"filename", header.Filename,
|
||||
"error", err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
fileStats.ElapsedMS = int(time.Since(start).Milliseconds())
|
||||
|
||||
slog.Info("CSV file processed",
|
||||
"filename", header.Filename,
|
||||
"rows_inserted", fileStats.RowsInserted,
|
||||
"rows_rejected", fileStats.RowsRejected,
|
||||
"elapsed_ms", fileStats.ElapsedMS,
|
||||
"file_checksum", fileStats.FileChecksum,
|
||||
"inserted_data", inserted,
|
||||
"rejected_data", rejected,
|
||||
)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(H{
|
||||
"stats": fileStats,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user