feat: downloader

This commit is contained in:
Anatoly Antonov 2025-06-23 04:19:26 +03:00
parent b9c1a98895
commit 42e7924be9
37 changed files with 2422 additions and 94 deletions

View file

@ -0,0 +1,22 @@
package service
import (
"net/http"
"git.intra.yksa.space/gsn/predictor/internal/pkg/grib"
"git.intra.yksa.space/gsn/predictor/pkg/redis"
)
type Config struct {
// GRIB Configuration
Grib grib.Config `envPrefix:"GRIB_"`
// Redis Configuration
Redis redis.Config `envPrefix:"REDIS_"`
}
func (c *Config) CreateHTTPClient() *http.Client {
return &http.Client{
Timeout: c.Grib.Timeout,
}
}

View file

@ -5,12 +5,15 @@ import (
"time"
)
type Grib interface {
Update(ctx context.Context) error
Extract(ctx context.Context, lat, lon, alt float64, ts time.Time) ([2]float64, error)
Close() error
}
type Redis interface {
Lock(ctx context.Context, key string, ttl time.Duration) (func(context.Context), error)
Set(key string, value []byte, ttl time.Duration) error
Get(key string) ([]byte, error)
}
type Grib interface {
Update(ctx context.Context) error
Close() error
}

View file

@ -2,12 +2,26 @@ package service
import (
"context"
"net/http"
"git.intra.yksa.space/gsn/predictor/internal/pkg/ds"
"git.intra.yksa.space/gsn/predictor/internal/pkg/errcodes"
)
func (s *Service) PerformPrediction(ctx context.Context, params ds.PredictionParameters) ([]ds.PredicitonResult, error) {
return nil, errcodes.New(http.StatusNotImplemented, "not implemented", "please wait")
// Extract wind data at launch point
wind, err := s.ExtractWind(ctx, params.LaunchLatitude, params.LaunchLongitude, params.LaunchAltitude, params.LaunchDatetime)
if err != nil {
return nil, err
}
// TODO: Implement full prediction logic
result := ds.PredicitonResult{
Latitude: params.LaunchLatitude,
Longitude: params.LaunchLongitude,
Altitude: params.LaunchAltitude,
Timestamp: params.LaunchDatetime,
WindU: wind[0],
WindV: wind[1],
}
return []ds.PredicitonResult{result}, nil
}

View file

@ -2,21 +2,56 @@ package service
import (
"context"
"time"
"go.uber.org/zap"
)
type Service struct {
redis Redis
downloader Downloader
cfg *Config
redis Redis
grib Grib
logger *zap.Logger
}
func New(redis Redis, downloader Downloader) *Service {
return &Service{
redis: redis,
downloader: downloader,
func New(cfg *Config, gribService Grib, redisService Redis, logger *zap.Logger) (*Service, error) {
svc := &Service{
cfg: cfg,
redis: redisService,
grib: gribService,
logger: logger,
}
return svc, nil
}
// DownloadWeatherData downloads weather forecast data using the configured downloader
func (s *Service) DownloadWeatherData(ctx context.Context) error {
return s.downloader.Download(ctx)
// UpdateWeatherData updates weather forecast data using the configured grib service
func (s *Service) UpdateWeatherData(ctx context.Context) error {
return s.grib.Update(ctx)
}
// ExtractWind extracts wind data for given coordinates and time
func (s *Service) ExtractWind(ctx context.Context, lat, lon, alt float64, ts time.Time) ([2]float64, error) {
return s.grib.Extract(ctx, lat, lon, alt, ts)
}
// Update updates the GRIB data (implements updater.GribService)
func (s *Service) Update(ctx context.Context) error {
return s.UpdateWeatherData(ctx)
}
// Start starts the service
func (s *Service) Start() {
s.logger.Info("service started")
}
// Stop stops the service
func (s *Service) Stop() {
s.logger.Info("service stopped")
}
// Close closes the service and releases resources
func (s *Service) Close() error {
s.Stop()
return nil
}