-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewrelic-api.el
136 lines (110 loc) · 4.07 KB
/
newrelic-api.el
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
;;; newrelic-api.el --- Utils for interacting with New Relic APIs -*- lexical-binding: t -*-
;;; Copyright (C) 2021 Maciej Smolinski
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; A set of functions responsible for interacting with New Relic
;; through the official GraphQL API (api.newrelic.com/graphiql)
;;; Code:
;;;; Requirements
(require 'request)
;;;; Variables
(defconst newrelic-gql-get-chart-link "
query GetChartLink($accountId: Int!, $nrql: Nrql!) {
actor {
account(id: $accountId) {
nrql(query: $nrql) {
nrql
staticChartUrl
}
}
}
}
" "A GraphQL Query used by the client to get a link to the chart image for a given query.")
(defconst newrelic-gql-get-accounts "
query GetAccounts {
actor {
accounts {
id
name
}
}
}
" "A GraphQL Query used by the client to get a list of account name and id pairs.")
(defconst newrelic-gql-get-dashboards "
query GetDashboards {
actor {
entitySearch(queryBuilder: {type: DASHBOARD}) {
results {
entities {
... on DashboardEntityOutline {
name
accountId
guid
dashboardParentGuid
}
}
}
}
}
}
" "A GraphQL Query used by the client to get a list of dashboards including their name, guid and other details.")
;;;; Functions
(defun newrelic-get-chart-link (nrql)
(newrelic--query
`(:query ,newrelic-gql-get-chart-link
:variables ,`(:accountId ,newrelic-active-account-id :nrql ,nrql))
'newrelic--get-chart-link-callback))
(defun newrelic--get-chart-link-callback (data)
(let* ((chartLink (let-alist data .data.actor.account.nrql.staticChartUrl))
(nrql (let-alist data .data.actor.account.nrql.nrql))
(buffer (get-buffer-create (concat "* NRQL @ " (number-to-string newrelic-active-account-id) " *"))))
(pop-to-buffer buffer)
(with-current-buffer buffer
(goto-char (point-min))
(save-excursion
(insert "\n" nrql "\n\n")
(insert-image-from-url chartLink)
(insert "\n")))))
(defun newrelic-load-accounts ()
(newrelic--query
`(:query ,newrelic-gql-get-accounts)
'newrelic--get-accounts-callback))
(defun newrelic--get-accounts-callback (data)
(let* ((accounts-data (let-alist data .data.actor.accounts))
(account-items (seq-map (lambda (a) (list (cdadr a) (cdar a))) accounts-data)))
(setq newrelic-accounts-list account-items)))
(defun newrelic-load-dashboards ()
(newrelic--query
`(:query ,newrelic-gql-get-dashboards)
'newrelic--load-dashboards-callback))
(defun newrelic--load-dashboards-callback (data)
(let ((dashboards-data (let-alist data .data.actor.entitySearch.results.entities)))
(setq newrelic-dashboards-list dashboards-data)))
(defun newrelic--query (payload on-success)
(newrelic--graphql
"https://api.newrelic.com/graphql"
payload
on-success
`(:headers (("Api-Key" . ,newrelic-api-key)))))
(defun newrelic--graphql (endpoint payload success-handler &optional options)
(request
endpoint
:type "POST"
:headers (append '(("Content-Type" . "application/json")) (plist-get options :headers))
:data (json-encode payload)
:parser 'json-read
:success (cl-function (lambda (&key data &allow-other-keys) (funcall success-handler data)))
:error (cl-function (lambda (&key error-thrown data &allow-other-keys) (message (format "%s %s" error-thrown data))))
:sync t))
(provide 'newrelic-api)
;;; newrelic-api.el ends here