This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
/
annotation.go
88 lines (75 loc) · 2.41 KB
/
annotation.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
// pmm-managed
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package management
import (
"context"
"strings"
"time"
"github.com/percona/pmm/api/managementpb"
"gopkg.in/reform.v1"
"github.com/percona/pmm-managed/models"
)
// AnnotationService Annotation Service.
type AnnotationService struct {
db *reform.DB
grafanaClient grafanaClient
}
// NewAnnotationService create new Annotation Service.
func NewAnnotationService(db *reform.DB, grafanaClient grafanaClient) *AnnotationService {
return &AnnotationService{
db: db,
grafanaClient: grafanaClient,
}
}
// AddAnnotation create annotation in grafana.
//nolint:unparam
func (as *AnnotationService) AddAnnotation(
ctx context.Context,
authorizationHeaders []string,
req *managementpb.AddAnnotationRequest,
) (*managementpb.AddAnnotationResponse, error) {
tags := req.Tags
if len(req.ServiceNames) == 0 && req.NodeName == "" {
tags = append([]string{"pmm_annotation"}, tags...)
}
var postfix []string
if len(req.ServiceNames) > 0 {
for _, sn := range req.ServiceNames {
_, err := models.FindServiceByName(as.db.Querier, sn)
if err != nil {
return nil, err
}
}
tags = append(tags, req.ServiceNames...)
postfix = append(postfix, "Service Name: "+strings.Join(req.ServiceNames, ", "))
}
if req.NodeName != "" {
_, err := models.FindNodeByName(as.db.Querier, req.NodeName)
if err != nil {
return nil, err
}
tags = append(tags, req.NodeName)
postfix = append(postfix, "Node Name: "+req.NodeName)
}
if len(postfix) > 0 {
req.Text += " (" + strings.Join(postfix, ". ") + ")"
}
_, err := as.grafanaClient.CreateAnnotation(ctx, tags, time.Now(), req.Text, authorizationHeaders[0])
if err != nil {
return nil, err
}
return &managementpb.AddAnnotationResponse{}, nil
}