aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/server/idb/stock/v1/stock.go
blob: b87a921e9cd1b756964d5b3090a07da1f2a43af3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package stock

import (
	"context"
	"fmt"
	"log/slog"

	pb "github.com/ansg191/ibd-trader/backend/api/idb/stock/v1"
	"github.com/ansg191/ibd-trader/backend/internal/database"
	"github.com/ansg191/ibd-trader/backend/internal/leader/manager/ibd/scrape"
	"github.com/ansg191/ibd-trader/backend/internal/redis/taskqueue"

	"cloud.google.com/go/longrunning/autogen/longrunningpb"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/anypb"
	"google.golang.org/protobuf/types/known/timestamppb"
)

const ScrapeOperationPrefix = "scrape"

type Server struct {
	pb.UnimplementedStockServiceServer

	db    database.Executor
	queue taskqueue.TaskQueue[scrape.TaskInfo]
}

func New(db database.Executor, queue taskqueue.TaskQueue[scrape.TaskInfo]) *Server {
	return &Server{db: db, queue: queue}
}

func (s *Server) CreateStock(ctx context.Context, request *pb.CreateStockRequest) (*pb.CreateStockResponse, error) {
	task, err := s.queue.Enqueue(ctx, scrape.TaskInfo{Symbol: request.Symbol})
	if err != nil {
		slog.ErrorContext(ctx, "failed to enqueue task", "err", err)
		return nil, status.New(codes.Internal, "failed to enqueue task").Err()
	}
	op := &longrunningpb.Operation{
		Name:     fmt.Sprintf("%s/%s", ScrapeOperationPrefix, task.ID.String()),
		Metadata: new(anypb.Any),
		Done:     false,
		Result:   nil,
	}
	err = op.Metadata.MarshalFrom(&pb.StockScrapeOperationMetadata{
		Symbol:    request.Symbol,
		StartTime: timestamppb.New(task.ID.Timestamp()),
	})
	if err != nil {
		slog.ErrorContext(ctx, "failed to marshal metadata", "err", err)
		return nil, status.New(codes.Internal, "failed to marshal metadata").Err()
	}
	return &pb.CreateStockResponse{Operation: op}, nil
}

func (s *Server) GetStock(ctx context.Context, request *pb.GetStockRequest) (*pb.GetStockResponse, error) {
	//TODO implement me
	panic("implement me")
}

func (s *Server) ListStocks(ctx context.Context, request *pb.ListStocksRequest) (*pb.ListStocksResponse, error) {
	//TODO implement me
	panic("implement me")
}