Execute PLAN_AGGREGATOR.md
This commit is contained in:
80
cmd/aggregator/server.go
Normal file
80
cmd/aggregator/server.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
pb "git.ipng.ch/ipng/nginx-logtail/proto/logtailpb"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// Server implements pb.LogtailServiceServer backed by the aggregator Cache.
|
||||
type Server struct {
|
||||
pb.UnimplementedLogtailServiceServer
|
||||
cache *Cache
|
||||
source string
|
||||
}
|
||||
|
||||
func NewServer(cache *Cache, source string) *Server {
|
||||
return &Server{cache: cache, source: source}
|
||||
}
|
||||
|
||||
func (srv *Server) TopN(_ context.Context, req *pb.TopNRequest) (*pb.TopNResponse, error) {
|
||||
if req == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "request is nil")
|
||||
}
|
||||
n := int(req.N)
|
||||
if n <= 0 {
|
||||
n = 10
|
||||
}
|
||||
entries := srv.cache.QueryTopN(req.Filter, req.GroupBy, n, req.Window)
|
||||
resp := &pb.TopNResponse{Source: srv.source}
|
||||
for _, e := range entries {
|
||||
resp.Entries = append(resp.Entries, &pb.TopNEntry{Label: e.Label, Count: e.Count})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (srv *Server) Trend(_ context.Context, req *pb.TrendRequest) (*pb.TrendResponse, error) {
|
||||
if req == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "request is nil")
|
||||
}
|
||||
points := srv.cache.QueryTrend(req.Filter, req.Window)
|
||||
resp := &pb.TrendResponse{Source: srv.source}
|
||||
for _, p := range points {
|
||||
resp.Points = append(resp.Points, &pb.TrendPoint{
|
||||
TimestampUnix: p.Timestamp.Unix(),
|
||||
Count: p.Count,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (srv *Server) StreamSnapshots(_ *pb.SnapshotRequest, stream grpc.ServerStreamingServer[pb.Snapshot]) error {
|
||||
ch := srv.cache.Subscribe()
|
||||
defer srv.cache.Unsubscribe(ch)
|
||||
|
||||
log.Printf("server: new StreamSnapshots subscriber")
|
||||
for {
|
||||
select {
|
||||
case <-stream.Context().Done():
|
||||
log.Printf("server: StreamSnapshots subscriber disconnected")
|
||||
return nil
|
||||
case snap, ok := <-ch:
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
msg := &pb.Snapshot{Source: srv.source, Timestamp: snap.Timestamp.Unix()}
|
||||
for _, e := range snap.Entries {
|
||||
msg.Entries = append(msg.Entries, &pb.TopNEntry{Label: e.Label, Count: e.Count})
|
||||
}
|
||||
if err := stream.Send(msg); err != nil {
|
||||
return err
|
||||
}
|
||||
case <-time.After(30 * time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user