Skip to content

Commit

Permalink
Merge pull request #38 from Shuffle/0x0elliot/force-ops-and-read-api
Browse files Browse the repository at this point in the history
Fix: Ops Dashboard bug fixes + stats + force run
  • Loading branch information
0x0elliot authored Oct 11, 2023
2 parents 146d936 + 0645e5d commit 8ee5738
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 226 deletions.
248 changes: 140 additions & 108 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 @@ -3849,114 +3904,6 @@ func GetUser(ctx context.Context, username string) (*User, error) {
return curUser, nil
}

func GetOpsDashboardCacheHitStat(ctx context.Context, limit int) ([]OpsDashboardStats, error) {
// get last 30 runs
nameKey := "opsdashboardstats"
var stats []OpsDashboardStats

if project.DbType == "opensearch" {
var buf bytes.Buffer
query := map[string]interface{}{
"size": limit,
"sort": map[string]interface{}{
"timestamp": map[string]interface{}{
"order": "desc",
},
},
}

if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Printf("[WARNING] Error encoding find app query: %s", err)
return stats, 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 (find app by name): %s", err)
return stats, err
}

defer res.Body.Close()
if res.StatusCode == 404 {
return stats, nil
}

defer res.Body.Close()
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 stats, err
}
}

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

respBody, err := ioutil.ReadAll(res.Body)

var wrapped OpsDashboardStatSearchWrapper

// put into struct
err = json.Unmarshal(respBody, &wrapped)

stats = []OpsDashboardStats{}
for _, hit := range wrapped.Hits.Hits {
stats = append(stats, hit.Source)
}

if err != nil {
log.Printf("RespBody is: %s", respBody)
return stats, err
}

log.Printf("[INFO] Successfully got ops dashboard stats")

} else {
q := datastore.NewQuery(nameKey).Order("-Timestamp").Limit(limit)
_, err := project.Dbclient.GetAll(ctx, q, &stats)
if err != nil {
log.Printf("[WARNING] Failed getting stats for ops dashboard: %s", err)
return stats, err
}
}

return stats, nil
}

func SetOpsDashboardCacheHitStat(ctx context.Context, cacheHit bool) error {
nameKey := "opsdashboardstats"
stat := OpsDashboardStats{}
stat.Timestamp = time.Now().Unix()
stat.CacheHit = cacheHit

data, err := json.Marshal(stat)

if project.DbType == "opensearch" {
err = indexEs(ctx, nameKey, fmt.Sprintf("%d", stat.Timestamp), data)
if err != nil {
return err
} else {
log.Printf("[INFO] Successfully updated ops dashboard stats")
}
} else {
k := datastore.NameKey(nameKey, fmt.Sprintf("%d", stat.Timestamp), nil)
if _, err := project.Dbclient.Put(ctx, k, &stat); err != nil {
log.Printf("[WARNING] Error updating ops dashboard stats: %s", err)
return err
}
}

return nil

}

func SetUser(ctx context.Context, user *User, updateOrg bool) error {
log.Printf("[INFO] Updating a user (%s) that has the role %s with %d apps and %d orgs. Org updater: %t", user.Username, user.Role, len(user.PrivateApps), len(user.Orgs), updateOrg)
parsedKey := user.Id
Expand Down Expand Up @@ -5341,6 +5288,91 @@ func SetNewValue(ctx context.Context, newvalue NewValue) error {
return nil
}

func GetPlatformHealth(ctx context.Context, limit int) ([]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",
},
},
"size": limit,
}

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").Limit(limit)

_, 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

0 comments on commit 8ee5738

Please sign in to comment.