94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
|
|
"github.com/c4t-but-s4d/neo/v2/internal/server/common"
|
|
"github.com/c4t-but-s4d/neo/v2/internal/server/utils"
|
|
"github.com/c4t-but-s4d/neo/v2/pkg/gstream"
|
|
logspb "github.com/c4t-but-s4d/neo/v2/proto/go/logs"
|
|
)
|
|
|
|
const (
|
|
logLinesBatchSize = 100
|
|
maxMsgSize = 4 * 1024 * 1024
|
|
)
|
|
|
|
func New(storage *LogStorage) *Server {
|
|
return &Server{
|
|
LoggingServer: common.NewLoggingServer("logs"),
|
|
|
|
storage: storage,
|
|
}
|
|
}
|
|
|
|
type Server struct {
|
|
logspb.UnimplementedServiceServer
|
|
common.LoggingServer
|
|
|
|
storage *LogStorage
|
|
}
|
|
|
|
func (s *Server) AddLogLines(ctx context.Context, lines *logspb.AddLogLinesRequest) (*emptypb.Empty, error) {
|
|
s.GetMethodLogger(ctx).Infof("New request with %d lines", len(lines.Lines))
|
|
|
|
decoded := make([]LogLine, 0, len(lines.Lines))
|
|
for _, line := range lines.Lines {
|
|
decoded = append(decoded, *NewLogLineFromProto(line))
|
|
}
|
|
if err := s.storage.Add(ctx, decoded); err != nil {
|
|
return nil, utils.WrapErrorf(codes.Internal, "добавление строк журнала: %v", err)
|
|
}
|
|
return &emptypb.Empty{}, nil
|
|
}
|
|
|
|
func (s *Server) SearchLogLines(req *logspb.SearchLogLinesRequest, stream logspb.Service_SearchLogLinesServer) error {
|
|
s.LogRequest(stream.Context(), req)
|
|
|
|
opts := GetOptions{
|
|
Exploit: req.Exploit,
|
|
Version: req.Version,
|
|
}
|
|
lines, err := s.storage.Get(stream.Context(), opts)
|
|
if err != nil {
|
|
return utils.WrapErrorf(codes.Internal, "поиск строк журнала: %v", err)
|
|
}
|
|
|
|
cache := gstream.NewDynamicSizeCache[*LogLine, logspb.SearchLogLinesResponse](
|
|
stream,
|
|
maxMsgSize,
|
|
func(lines []*LogLine) (*logspb.SearchLogLinesResponse, error) {
|
|
resp := &logspb.SearchLogLinesResponse{
|
|
Lines: make([]*logspb.LogLine, 0, len(lines)),
|
|
}
|
|
for _, line := range lines {
|
|
protoLine, err := line.ToProto()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("преобразование строки журнала: %w", err)
|
|
}
|
|
resp.Lines = append(resp.Lines, protoLine)
|
|
}
|
|
return resp, nil
|
|
},
|
|
)
|
|
|
|
for i, line := range lines {
|
|
if err := cache.Queue(line); err != nil {
|
|
return utils.WrapErrorf(codes.Internal, "добавление строки журнала в очередь: %v", err)
|
|
}
|
|
if (i-1+logLinesBatchSize)%logLinesBatchSize == 0 {
|
|
if err := cache.Flush(); err != nil {
|
|
return utils.WrapErrorf(codes.Internal, "отправка накопленных строк: %v", err)
|
|
}
|
|
}
|
|
}
|
|
if err := cache.Flush(); err != nil {
|
|
return utils.WrapErrorf(codes.Internal, "отправка последних строк: %v", err)
|
|
}
|
|
return nil
|
|
}
|