/
elasticsearch.go
72 lines (58 loc) · 1.65 KB
/
elasticsearch.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
package prago
import (
"fmt"
"sort"
"github.com/hypertornado/prago/pragelastic"
)
func (app *App) initElasticsearch() {
db := sysadminBoard.Dashboard(unlocalized("Elasticsearch"))
db.AddTask(unlocalized("Reload elasticsearch client"), "sysadmin", func(ta *TaskActivity) error {
return app.createNewElasticSearchClient()
})
app.FormAction("delete-elastic-indice", func(f *Form, r *Request) {
stats, err := app.ElasticSearchClient().GetStats()
if err != nil {
panic(err)
}
var indiceNames []string
for k := range stats.Indices {
indiceNames = append(indiceNames, k)
}
sort.Strings(indiceNames)
var doubled [][2]string = [][2]string{{"", ""}}
for _, v := range indiceNames {
doubled = append(doubled, [2]string{v, v})
}
f.AddSelect("indice", "Elastic indices", doubled)
f.AddSubmit("Delete indice")
}, func(vc ValidationContext) {
id := vc.GetValue("indice")
if id == "" {
vc.AddItemError("indice", "Select indice to delete")
}
if !vc.Valid() {
return
}
err := app.ElasticSearchClient().DeleteIndex(id)
if err != nil {
vc.AddError(fmt.Sprintf("Index '%s' nelze smazat", id))
} else {
vc.AddError(fmt.Sprintf("Index '%s' úspěšně smazán", id))
}
}).Name(unlocalized("Smazat elasticsearch index")).Permission(sysadminPermission).Board(sysadminBoard)
}
var searchClient *pragelastic.Client
func (app *App) createNewElasticSearchClient() error {
ret, err := pragelastic.New(app.codeName)
if err != nil {
return err
}
searchClient = ret
return nil
}
func (app *App) ElasticSearchClient() *pragelastic.Client {
if searchClient == nil {
app.createNewElasticSearchClient()
}
return searchClient
}