-
Notifications
You must be signed in to change notification settings - Fork 12
/
log_entry_service.go
100 lines (84 loc) · 4.67 KB
/
log_entry_service.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package service
import (
"context"
"github.com/google/uuid"
"github.com/openline-ai/openline-customer-os/packages/server/customer-os-common-module/utils"
commonmodel "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/common/model"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/log_entry/command"
logentrycmdhandler "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/log_entry/command_handler"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/domain/log_entry/model"
grpcerr "github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/grpc_errors"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/logger"
"github.com/openline-ai/openline-customer-os/packages/server/events-processing-platform/tracing"
logentrypb "github.com/openline-ai/openline-customer-os/packages/server/events-processing-proto/gen/proto/go/api/grpc/v1/log_entry"
"strings"
)
type logEntryService struct {
logentrypb.UnimplementedLogEntryGrpcServiceServer
log logger.Logger
logEntryCommands *logentrycmdhandler.CommandHandlers
}
func NewLogEntryService(log logger.Logger, logEntryCommands *logentrycmdhandler.CommandHandlers) *logEntryService {
return &logEntryService{
log: log,
logEntryCommands: logEntryCommands,
}
}
func (s *logEntryService) UpsertLogEntry(ctx context.Context, request *logentrypb.UpsertLogEntryGrpcRequest) (*logentrypb.LogEntryIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "LogEntryService.UpsertLogEntry")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.UserId)
tracing.LogObjectAsJson(span, "request", request)
logEntryId := request.Id
if strings.TrimSpace(logEntryId) == "" {
logEntryId = uuid.New().String()
}
dataFields := model.LogEntryDataFields{
Content: request.Content,
ContentType: request.ContentType,
StartedAt: utils.TimestampProtoToTimePtr(request.StartedAt),
AuthorUserId: request.AuthorUserId,
LoggedOrganizationId: request.LoggedOrganizationId,
}
source := commonmodel.Source{}
source.FromGrpc(request.SourceFields)
externalSystem := commonmodel.ExternalSystem{}
externalSystem.FromGrpc(request.ExternalSystemFields)
cmd := command.NewUpsertLogEntryCommand(logEntryId, request.Tenant, request.UserId, source, externalSystem, dataFields, utils.TimestampProtoToTimePtr(request.CreatedAt), utils.TimestampProtoToTimePtr(request.UpdatedAt))
if err := s.logEntryCommands.UpsertLogEntry.Handle(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("(UpsertLogEntryCommand.Handle) tenant:{%s}, logEntryId:{%s} , err: %s", request.Tenant, request.Id, err.Error())
return nil, s.errResponse(err)
}
s.log.Infof("Upserted logEntry %s", logEntryId)
return &logentrypb.LogEntryIdGrpcResponse{Id: logEntryId}, nil
}
func (s *logEntryService) AddTag(ctx context.Context, request *logentrypb.AddTagGrpcRequest) (*logentrypb.LogEntryIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "LogEntryService.Addtag")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.UserId)
tracing.LogObjectAsJson(span, "request", request)
cmd := command.NewAddTagCommand(request.Id, request.Tenant, request.UserId, request.TagId, utils.TimePtr(utils.Now()))
if err := s.logEntryCommands.AddTag.Handle(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("(AddTag.Handle) tenant:%s, logEntryId: %s, tagId , err: %s", request.Tenant, request.Id, request.TagId, err.Error())
return nil, s.errResponse(err)
}
return &logentrypb.LogEntryIdGrpcResponse{Id: request.Id}, nil
}
func (s *logEntryService) RemoveTag(ctx context.Context, request *logentrypb.RemoveTagGrpcRequest) (*logentrypb.LogEntryIdGrpcResponse, error) {
ctx, span := tracing.StartGrpcServerTracerSpan(ctx, "LogEntryService.RemoveTag")
defer span.Finish()
tracing.SetServiceSpanTags(ctx, span, request.Tenant, request.UserId)
tracing.LogObjectAsJson(span, "request", request)
cmd := command.NewRemoveTagCommand(request.Id, request.Tenant, request.UserId, request.TagId)
if err := s.logEntryCommands.RemoveTag.Handle(ctx, cmd); err != nil {
tracing.TraceErr(span, err)
s.log.Errorf("(RemoveTag.Handle) tenant:%s, logEntryId: %s, tagId , err: %s", request.Tenant, request.Id, request.TagId, err.Error())
return nil, s.errResponse(err)
}
return &logentrypb.LogEntryIdGrpcResponse{Id: request.Id}, nil
}
func (s *logEntryService) errResponse(err error) error {
return grpcerr.ErrResponse(err)
}