-
Notifications
You must be signed in to change notification settings - Fork 3
/
graph.go
147 lines (127 loc) · 3.01 KB
/
graph.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
package reco
import (
"errors"
"os"
"github.com/ReconfigureIO/reco/downloader"
"github.com/ReconfigureIO/reco/logger"
"github.com/ReconfigureIO/reco/printer"
humanize "github.com/dustin/go-humanize"
)
// Graph manages reco graphs.
type Graph interface {
// Generate generates a graph.
Generate(Args) (output string, err error)
// List list graphs.
List(filter M) (printer.Table, error)
// Open opens a graph.
Open(id string) (file string, err error)
}
var _ Graph = &platformGraph{}
type platformGraph struct {
*clientImpl
}
func (b platformGraph) prepareGraph() (string, error) {
projectID, err := b.projectID()
if err != nil {
return "", err
}
req := b.apiRequest(endpoints.graphs.String())
reqBody := M{"project_id": projectID}
resp, err := req.Do("POST", reqBody)
if err != nil {
return "", err
}
var respJSON struct {
Value struct {
ID string `json:"id"`
} `json:"value"`
Error string `json:"error"`
}
err = decodeJSON(resp.Body, &respJSON)
return respJSON.Value.ID, err
}
func (p platformGraph) Generate(args Args) (string, error) {
srcDir := String(args.At(0))
wait := Bool(args.At(1))
logger.Info.Println("preparing graph")
id, err := p.prepareGraph()
if err != nil {
return "", err
}
logger.Info.Println("done. Graph ID: ", id)
logger.Info.Println("archiving")
srcArchive, err := archiveDir(srcDir)
if err != nil {
return "", err
}
logger.Info.Println("done")
logger.Info.Println("uploading")
if err := p.uploadJob("graph", id, srcArchive); err != nil {
return "", err
}
logger.Info.Println("done")
if wait {
logger.Info.Println("waiting for graph generation to complete")
}
logger.Info.Println()
return id, nil
}
func (p platformGraph) List(filter M) (printer.Table, error) {
var table printer.Table
allProjects := filter.Bool("all")
graphs, err := p.clientImpl.listGraphs(filter)
if err != nil {
return table, err
}
var body [][]string
for _, graph := range graphs {
buildTime := "-"
if !graph.Time.IsZero() {
buildTime = humanize.Time(graph.Time)
}
row := []string{
graph.ID,
graph.Status,
buildTime,
}
if allProjects {
row = append(row, graph.Project)
}
body = append(body, row)
}
table = printer.Table{
Header: []string{"graph id", "status", "requested"},
Body: body,
}
if allProjects {
table.Header = append(table.Header, "project")
}
return table, nil
}
func (p platformGraph) Open(id string) (string, error) {
var req = p.apiRequest(endpoints.graphs.Graph())
req.param("id", id)
resp, err := req.Do("GET", nil)
if err != nil {
return "", err
}
switch resp.StatusCode {
case 404:
return "", errors.New("graph not found")
case 204:
return "", errors.New("no graph generated")
case 200:
break
default:
return "", errors.New("unknown error occured")
}
pdfFile, err := downloader.FromReader(resp.Body, resp.ContentLength)
if err != nil {
return "", err
}
// rename for easier pdf viewer recognition.
if err := os.Rename(pdfFile, pdfFile+".pdf"); err == nil {
pdfFile += ".pdf"
}
return pdfFile, nil
}