Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ops Dashboard bug fixes + stats + force run #38

Merged
merged 17 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2849,6 +2849,61 @@ func GetOrg(ctx context.Context, id string) (*Org, error) {
return curOrg, nil
}

func GetFirstOrg(ctx context.Context) (*Org, error) {
nameKey := "Organizations"

curOrg := &Org{}
if project.DbType == "opensearch" {
res, err := project.Es.Search(
project.Es.Search.WithContext(ctx),
project.Es.Search.WithIndex(strings.ToLower(GetESIndexPrefix(nameKey))),
project.Es.Search.WithTrackTotalHits(true),
)
if err != nil {
log.Printf("[ERROR] Error getting response from Opensearch (get first org): %s", err)
return curOrg, err
}

defer res.Body.Close()
if res.StatusCode != 200 && res.StatusCode != 201 {
return curOrg, errors.New(fmt.Sprintf("Bad statuscode: %d", res.StatusCode))
}

respBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return curOrg, err
}

wrapped := OrgSearchWrapper{}
err = json.Unmarshal(respBody, &wrapped)
if err != nil {
return curOrg, err
}

if len(wrapped.Hits.Hits) > 0 {
curOrg = &wrapped.Hits.Hits[0].Source
} else {
return curOrg, errors.New("No orgs found")
}

} else {
query := datastore.NewQuery(nameKey).Limit(1)
allOrgs := []Org{}
_, err := project.Dbclient.GetAll(ctx, query, &allOrgs)
if err != nil {
return curOrg, err
}

if len(allOrgs) > 0 {
curOrg = &allOrgs[0]
} else {
return curOrg, errors.New("No orgs found")
}
}

return curOrg, nil
}

func indexEs(ctx context.Context, nameKey, id string, bytes []byte) error {
//req := esapi.IndexRequest{
req := opensearchapi.IndexRequest{
Expand Down Expand Up @@ -5341,6 +5396,97 @@ func SetNewValue(ctx context.Context, newvalue NewValue) error {
return nil
}

func GetPlatformHealth(ctx context.Context, getLatest bool) ([]HealthCheckDB, error) {
nameKey := "platform_health"

// sort by "updated", and get the first one
health := []HealthCheckDB{}
if project.DbType == "opensearch" {
var buf bytes.Buffer
query := map[string]interface{}{
"sort": map[string]interface{}{
"updated": map[string]interface{}{
"order": "desc",
},
},
}

if getLatest {
query["size"] = 1
}

if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Printf("[WARNING] Error encoding find user query: %s", err)
return health, err
}

res, err := project.Es.Search(
project.Es.Search.WithContext(ctx),
project.Es.Search.WithIndex(strings.ToLower(GetESIndexPrefix(nameKey))),
project.Es.Search.WithBody(&buf),
project.Es.Search.WithTrackTotalHits(true),
)
if err != nil {
log.Printf("[ERROR] Error getting response from Opensearch (get latest platform health): %s", err)
return health, err
}
defer res.Body.Close()

if res.StatusCode != 200 && res.StatusCode != 201 {
return health, errors.New(fmt.Sprintf("Bad statuscode: %d", res.StatusCode))
}

if res.IsError() {
var e map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
log.Printf("[WARNING] Error parsing the response body: %s", err)
return health, err
} else {
// Print the response status and error information.
log.Printf("[%s] %s: %s",
res.Status(),
e["error"].(map[string]interface{})["type"],
e["error"].(map[string]interface{})["reason"],
)
}
}

respBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return health, err
}

wrapped := HealthCheckSearchWrapper{}
err = json.Unmarshal(respBody, &wrapped)
if err != nil {
return health, err
}

if len(wrapped.Hits.Hits) == 0 {
return health, errors.New("No healthchecks found")
}

for _, hit := range wrapped.Hits.Hits {
health = append(health, hit.Source)
}

} else {
q := datastore.NewQuery(nameKey).Order("-updated")

if getLatest {
q = q.Limit(1)
}

_, err := project.Dbclient.GetAll(ctx, q, &health)
if err != nil {
log.Printf("[WARNING] Error getting latest platform health: %s", err)
return health, err
}
}

return health, nil
}

func SetPlatformHealth(ctx context.Context, health HealthCheckDB) error {
nameKey := "platform_health"

Expand Down
Loading
Loading