This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
/
image.go
180 lines (164 loc) · 3.66 KB
/
image.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package controller
import (
"net/http"
"strconv"
"time"
lru "github.com/hashicorp/golang-lru"
"github.com/vicanso/cod"
"github.com/vicanso/diving/router"
"github.com/vicanso/diving/service"
"github.com/vicanso/hes"
"go.uber.org/zap"
)
var (
// imageInfoCache image basic info
imageInfoCache *lru.Cache
)
const (
analysisDoing = iota
analysisFail
analysisDone
)
const (
// imageInfoTTL ttl for image info
imageInfoTTL = 60 * 10
)
type (
// imageCtrl image ctrl
imageCtrl struct{}
imageInfo struct {
// CreatedAt create time
CreatedAt int64 `json:"createdAt,omitempty"`
// Status status
Status int `json:"status,omitempty"`
// Err error
Err error `json:"err,omitempty"`
// TimeConsuming time consuming
TimeConsuming time.Duration `json:"timeConsuming,omitempty"`
// Analysis image analysis information
Analysis *service.ImageAnalysis `json:"-"`
}
)
func init() {
c, err := lru.New(32)
if err != nil {
panic(err)
}
imageInfoCache = c
g := router.NewAPIGroup("/images")
ctrl := imageCtrl{}
g.GET("/tree/*name", ctrl.getTree)
g.GET("/detail/*name", ctrl.getBasicInfo)
g.GET("/caches", ctrl.getCacheList)
}
func doAnalyze(name string) {
startedAt := time.Now()
analysis, err := service.Analyze(name)
if err != nil {
imageInfoCache.Add(name, &imageInfo{
CreatedAt: startedAt.Unix(),
Status: analysisFail,
Err: err,
TimeConsuming: time.Since(startedAt),
})
logger.Error("analyze fail",
zap.String("name", name),
zap.Error(err),
)
return
}
imageInfoCache.Add(name, &imageInfo{
CreatedAt: startedAt.Unix(),
Status: analysisDone,
Analysis: analysis,
TimeConsuming: time.Since(startedAt),
})
}
// getBasicInfo get basic info of image
func (ctrl imageCtrl) getBasicInfo(c *cod.Context) (err error) {
name := c.Param("name")[1:]
var info *imageInfo
v, ok := imageInfoCache.Get(name)
if ok {
info = v.(*imageInfo)
// 如果已过期
if info.CreatedAt+imageInfoTTL < time.Now().Unix() {
info = nil
}
}
if info == nil {
info = &imageInfo{
CreatedAt: time.Now().Unix(),
Status: analysisDoing,
}
imageInfoCache.Add(name, info)
go doAnalyze(name)
}
// 如果正在处理中,则直接返回
if info.Status == analysisDoing {
c.StatusCode = http.StatusAccepted
return
}
if info.Status == analysisFail {
err = hes.NewWithError(info.Err)
return
}
if !service.IsDev() {
c.CacheMaxAge("5m")
}
c.Body = info.Analysis
return
}
func (ctrl imageCtrl) getTree(c *cod.Context) (err error) {
layer := c.QueryParam("layer")
if layer == "" {
err = hes.New("layer can not be null")
return
}
index, e := strconv.Atoi(layer)
if e != nil {
err = hes.NewWithErrorStatusCode(e, http.StatusBadRequest)
return
}
name := c.Param("name")[1:]
v, ok := imageInfoCache.Get(name)
if !ok {
err = hes.New("can not get tree of image")
return
}
info := v.(*imageInfo)
if info.Err != nil {
err = info.Err
return
}
if info.Status != analysisDone {
err = hes.New("the image is analysising, please wait for a moment")
return
}
if index >= len(info.Analysis.LayerAnalysisList) {
err = hes.New("the layer index is too big")
return
}
if !service.IsDev() {
c.CacheMaxAge("5m")
}
c.Body = service.GetFileAnalysis(info.Analysis, index)
return
}
// getCacheList get cache list
func (ctrl imageCtrl) getCacheList(c *cod.Context) (err error) {
keys := imageInfoCache.Keys()
result := make(map[string]*imageInfo)
for _, key := range keys {
v, ok := imageInfoCache.Get(key)
if ok {
info := v.(*imageInfo)
// 只返回未过期的
if info.CreatedAt+imageInfoTTL > time.Now().Unix() {
result[key.(string)] = info
}
}
}
c.Body = result
return
}