Files
esctl/pkg/es/parallel.go

159 lines
3.7 KiB
Go
Raw Normal View History

/*
Copyright © 2026 Thomas von Dein
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/>.
*/
package es
import (
"context"
"fmt"
"codeberg.org/scip/esctl/pkg/cfg"
"github.com/elastic/go-elasticsearch/v9"
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/indices"
"github.com/elastic/go-elasticsearch/v9/typedapi/cat/tasks"
"github.com/elastic/go-elasticsearch/v9/typedapi/ccr/stats"
"github.com/elastic/go-elasticsearch/v9/typedapi/cluster/health"
clusterstats "github.com/elastic/go-elasticsearch/v9/typedapi/cluster/stats"
"github.com/elastic/go-elasticsearch/v9/typedapi/core/info"
"github.com/elastic/go-elasticsearch/v9/typedapi/ilm/explainlifecycle"
"github.com/elastic/go-elasticsearch/v9/typedapi/ilm/getlifecycle"
"github.com/elastic/go-elasticsearch/v9/typedapi/types/enums/bytes"
)
const (
ResponseHealth = iota
ResponseInfo
ResponseCcr
ResponseStats
ResponseIndices
ResponseTasks
ResponseExplain
ResponseLifecycle
ResponseHealthReport
)
type apiResponse struct {
error error
info *info.Response
health *health.Response
healthreport *HealthReport
ccr *stats.Response
stats *clusterstats.Response
indices *indices.Response
indicesbytes *indices.Response
tasks *tasks.Response
lifecycle *getlifecycle.Response
explainlifecycle *explainlifecycle.Response
which int
}
2026-07-13 14:08:10 +02:00
func getApiData(conf *cfg.Config, es *elasticsearch.TypedClient, reschan chan apiResponse, which string) {
2026-07-07 23:46:43 +02:00
apiRes := apiResponse{}
var arerr error
switch which {
case "health":
res, err := es.Cluster.Health().
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.health = res
apiRes.which = ResponseHealth
arerr = err
case "healthreport":
report, err := getHealthReport(conf)
2026-07-07 23:46:43 +02:00
apiRes.healthreport = report
apiRes.which = ResponseHealthReport
arerr = err
case "info":
res, err := es.Info().
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.info = res
apiRes.which = ResponseInfo
arerr = err
case "ccr":
res, err := es.Ccr.Stats().
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.ccr = res
apiRes.which = ResponseCcr
arerr = err
case "stats":
res, err := es.Cluster.Stats().
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.stats = res
apiRes.which = ResponseStats
arerr = err
case "indices":
res, err := es.Cat.Indices().
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.indices = &res
apiRes.which = ResponseIndices
arerr = err
case "indicesbytes":
res, err := es.Cat.
Indices().
Bytes(bytes.Bytes{Name: "b"}).
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.indicesbytes = &res
apiRes.which = ResponseIndices
arerr = err
case "tasks":
res, err := es.Cat.Tasks().
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.tasks = &res
apiRes.which = ResponseTasks
arerr = err
case "explain":
res, err := es.Ilm.
ExplainLifecycle("_all").
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.explainlifecycle = res
apiRes.which = ResponseExplain
arerr = err
case "policies":
res, err := es.Ilm.
GetLifecycle().
Do(context.Background())
2026-07-07 23:46:43 +02:00
apiRes.lifecycle = &res
apiRes.which = ResponseLifecycle
arerr = err
}
if arerr != nil {
2026-07-07 23:46:43 +02:00
apiRes.error = fmt.Errorf("failed to get data from API: %w", arerr)
}
2026-07-07 23:46:43 +02:00
reschan <- apiRes
}