-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeppelin_exporter.go
132 lines (113 loc) · 2.68 KB
/
zeppelin_exporter.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/jessevdk/go-flags"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
)
var BuildVersion string
type Options struct {
Host string `long:"host" description:"Zeppelin host" default:"127.0.0.1"`
Port int `short:"p" long:"port" description:"port" default:"8080"`
Protocol string `long:"protocol" description:"protocol" default:"http"`
Version bool `long:"version" description:"print version"`
}
type NoteBooksResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Body []Notebook `json:"body"`
}
type Notebook struct {
ID string `json:"id"`
Name string `json:"name"`
}
func main() {
opts := getOptions()
if opts.Version {
printVersion()
os.Exit(0)
}
endpoint := fmt.Sprintf("%s://%s:%d", opts.Protocol, opts.Host, opts.Port)
notebookIds, err := fetchNotebookIds(endpoint)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
notebooks, err := exportNotebooks(endpoint, notebookIds)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("[" + strings.Join(notebooks[:], ",") + "]")
}
func getOptions() Options {
opts := Options{}
psr := flags.NewParser(&opts, flags.Default)
_, err := psr.Parse()
if err != nil {
os.Exit(1)
}
return opts
}
func printVersion() {
fmt.Printf(`zeppelin-exporter %s
Compiler: %s %s
`,
BuildVersion,
runtime.Compiler,
runtime.Version())
}
func fetchNotebookIds(endpoint string) ([]string, error) {
res, err := http.Get(endpoint + "/api/notebook")
if err != nil {
return nil, err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
jsonBytes := ([]byte)(string(b))
data := new(NoteBooksResponse)
err = json.Unmarshal(jsonBytes, data)
if err != nil {
return nil, err
}
notebookIds := []string{}
for _, notebook := range data.Body {
notebookIds = append(notebookIds, notebook.ID)
}
return notebookIds, nil
}
func exportNotebooks(endpoint string, notebookIds []string) ([]string, error) {
notebooks := []string{}
for _, notebookId := range notebookIds {
res, err := http.Get(endpoint + "/api/notebook/export/" + notebookId)
if err != nil {
return nil, err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var notebook interface{}
err = json.Unmarshal(([]byte)(string(b)), ¬ebook)
if err != nil {
return nil, err
}
var notebookBody interface{}
notebookBody = notebook.(map[string]interface{})["body"]
str, ok := notebookBody.(string)
if !ok {
return nil, errors.New("Error stringify")
}
notebooks = append(notebooks, str)
}
return notebooks, nil
}