94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package staterpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
type statsServer struct {
|
|
pb.UnimplementedStatsServiceServer
|
|
*server
|
|
}
|
|
|
|
func (s *statsServer) StatusCounts(ctx context.Context, _ *pb.StatusCountsRequest) (*pb.StatusCountsResponse, error) {
|
|
counts, err := s.deps.Tasks.StatusCounts(ctx)
|
|
if err != nil {
|
|
return nil, s.logErr("StatusCounts", err)
|
|
}
|
|
out := make(map[string]int64, len(counts))
|
|
for k, v := range counts {
|
|
out[k] = int64(v)
|
|
}
|
|
return &pb.StatusCountsResponse{Counts: out}, nil
|
|
}
|
|
|
|
// Events
|
|
|
|
func (s *statsServer) WorkflowStats(ctx context.Context, _ *pb.WorkflowStatsRequest) (*pb.WorkflowStatsResponse, error) {
|
|
stats, err := s.deps.Tasks.WorkflowStats(ctx)
|
|
if err != nil {
|
|
return nil, s.logErr("WorkflowStats", err)
|
|
}
|
|
return &pb.WorkflowStatsResponse{Stats: mapValues(stats, workflowStatProto)}, nil
|
|
}
|
|
|
|
func (s *statsServer) StageStats(ctx context.Context, _ *pb.StageStatsRequest) (*pb.StageStatsResponse, error) {
|
|
stats, err := s.deps.Tasks.StageStats(ctx)
|
|
if err != nil {
|
|
return nil, s.logErr("StageStats", err)
|
|
}
|
|
return &pb.StageStatsResponse{Stats: mapValues(stats, stageStatProto)}, nil
|
|
}
|
|
|
|
func (s *statsServer) TokensByDay(ctx context.Context, r *pb.TokensByDayRequest) (*pb.TokensByDayResponse, error) {
|
|
tokens, err := s.deps.Tasks.TokensByDay(ctx, int(r.Days))
|
|
if err != nil {
|
|
return nil, s.logErr("TokensByDay", err)
|
|
}
|
|
return &pb.TokensByDayResponse{Tokens: mapValues(tokens, dayTokensProto)}, nil
|
|
}
|
|
|
|
// Capture
|
|
|
|
func (c *Client) StatusCounts(ctx context.Context) (map[string]int, error) {
|
|
r, err := c.stats.StatusCounts(ctx, &pb.StatusCountsRequest{})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
out := make(map[string]int, len(r.Counts))
|
|
for k, v := range r.Counts {
|
|
out[k] = int(v)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Events
|
|
|
|
func (c *Client) WorkflowStats(ctx context.Context) ([]storecontract.WorkflowStat, error) {
|
|
r, err := c.stats.WorkflowStats(ctx, &pb.WorkflowStatsRequest{})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
return mapValues(r.Stats, workflowStatValue), nil
|
|
}
|
|
|
|
func (c *Client) StageStats(ctx context.Context) ([]storecontract.StageStat, error) {
|
|
r, err := c.stats.StageStats(ctx, &pb.StageStatsRequest{})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
return mapValues(r.Stats, stageStatValue), nil
|
|
}
|
|
|
|
func (c *Client) TokensByDay(ctx context.Context, days int) ([]storecontract.DayTokens, error) {
|
|
r, err := c.stats.TokensByDay(ctx, &pb.TokensByDayRequest{Days: int64(days)})
|
|
if err != nil {
|
|
return nil, unmapError(err)
|
|
}
|
|
return mapValues(r.Tokens, dayTokensValue), nil
|
|
}
|
|
|
|
// Capture
|