-
Notifications
You must be signed in to change notification settings - Fork 117
/
feedback.go
149 lines (126 loc) · 4.16 KB
/
feedback.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package feedback
// <!-- START clutchdoc -->
// description: Exposes endpoints to return survey questions for feedback components and to submit feedback submissions.
// <!-- END clutchdoc -->
import (
"context"
"errors"
"github.com/golang/protobuf/ptypes/any"
"github.com/uber-go/tally/v4"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
feedbackv1cfg "github.com/lyft/clutch/backend/api/config/module/feedback/v1"
feedbackv1 "github.com/lyft/clutch/backend/api/feedback/v1"
"github.com/lyft/clutch/backend/module"
"github.com/lyft/clutch/backend/service"
"github.com/lyft/clutch/backend/service/feedback"
)
const (
Name = "clutch.module.feedback"
)
func New(cfg *any.Any, log *zap.Logger, scope tally.Scope) (module.Module, error) {
config := &feedbackv1cfg.Config{}
err := cfg.UnmarshalTo(config)
if err != nil {
return nil, err
}
feedbackClient, ok := service.Registry["clutch.service.feedback"]
if !ok {
return nil, errors.New("could not find service")
}
c, ok := feedbackClient.(feedback.Service)
if !ok {
return nil, errors.New("service was not the correct type")
}
m := &mod{
surveyMap: newSurveyLookup(config.Origins),
client: c,
logger: log,
scope: scope,
}
return m, nil
}
// SurveyLookup stores a map of the origin to survey mapping that are
// provided in the feedback module config
type surveyLookup struct {
// key is the origin name
surveys map[string]*feedbackv1cfg.SurveyOrigin
}
type mod struct {
surveyMap surveyLookup
client feedback.Service
logger *zap.Logger
scope tally.Scope
}
func (m *mod) Register(r module.Registrar) error {
feedbackv1.RegisterFeedbackAPIServer(r.GRPCServer(), m)
return r.RegisterJSONGateway(feedbackv1.RegisterFeedbackAPIHandler)
}
func (m *mod) GetSurveys(tx context.Context, req *feedbackv1.GetSurveysRequest) (*feedbackv1.GetSurveysResponse, error) {
results := make(map[string]*feedbackv1.Survey)
// this scenario shouldn't happen as the feedback config fields cannot be empty
if len(m.surveyMap.surveys) == 0 {
m.logger.Error("survey questions were not found in the config",
zap.Any("config", m.surveyMap.surveys),
)
return nil, status.Errorf(codes.NotFound, "survey questions were not found")
}
for _, origin := range req.Origins {
v, ok := m.surveyMap.getConfigSurveys(origin)
if !ok {
continue
}
results[origin.String()] = &feedbackv1.Survey{
Prompt: v.Prompt,
FreeformPrompt: v.FreeformPrompt,
RatingLabels: v.RatingLabels,
}
}
// this scenario shouldn't happen as the request and config have to provide defined enum types
// TODO: if multiple orgins are requested at once and only some surveys are found, return partial error
if len(results) == 0 {
msg := "surveys not found for the requested origin(s)"
m.logger.Error(
msg,
zap.Any("request origins", req.Origins),
zap.Any("config origins", m.surveyMap.surveys),
)
return nil, status.Errorf(codes.InvalidArgument, msg)
}
return &feedbackv1.GetSurveysResponse{
OriginSurvey: results,
}, nil
}
func newSurveyLookup(origins []*feedbackv1cfg.SurveyOrigin) surveyLookup {
if len(origins) == 0 {
return surveyLookup{}
}
surveys := make(map[string]*feedbackv1cfg.SurveyOrigin, len(origins))
for _, origin := range origins {
surveys[origin.Origin.String()] = origin
}
return surveyLookup{
surveys,
}
}
// GetConfigSurveys uses the SurveyLookup and the origin passed in the API request to
// check if a survey exists for the origin. If found, the survey is returned.
// Otherwise ok is false.
func (sl surveyLookup) getConfigSurveys(origin feedbackv1.Origin) (*feedbackv1cfg.Survey, bool) {
if len(sl.surveys) == 0 {
return nil, false
}
v, ok := sl.surveys[origin.String()]
if !ok {
return nil, false
}
return v.Survey, true
}
func (m *mod) SubmitFeedback(ctx context.Context, req *feedbackv1.SubmitFeedbackRequest) (*feedbackv1.SubmitFeedbackResponse, error) {
if err := m.client.SubmitFeedback(ctx, req.Id, req.UserId, req.Feedback, req.Metadata); err != nil {
m.logger.Error("failed to submit feedback", zap.Error(err))
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
return &feedbackv1.SubmitFeedbackResponse{}, nil
}