-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GROUNDWORK-3440: add clusters services
- Loading branch information
1 parent
c1df245
commit 7f9a63d
Showing
8 changed files
with
281 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package client | ||
|
||
const ( | ||
defaultJobsRunsListPath = "/api/2.1/jobs/runs/list" | ||
defaultJobRunGetPath = "/api/2.1/jobs/runs/get" | ||
defaultPathJobsRunsList = "/api/2.1/jobs/runs/list" | ||
defaultPathJobsRunsGet = "/api/2.1/jobs/runs/get" | ||
defaultPathClustersList = "/api/2.1/clusters/list" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func (d *DatabricksClient) GetClusters() ([]Cluster, error) { | ||
var ( | ||
result = make([]Cluster, 0) | ||
hasMore = true | ||
nextPageToken = "" | ||
) | ||
|
||
for hasMore { | ||
resp, err := d.cl.R().Get( | ||
fmt.Sprintf( | ||
"%s?next_page_token=%s", | ||
defaultPathClustersList, | ||
nextPageToken, | ||
), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode() != 200 { | ||
return nil, fmt.Errorf("bad status code: %d", resp.StatusCode()) | ||
} | ||
|
||
var clustersResp ClustersResponse | ||
if err = json.Unmarshal(resp.Body(), &clustersResp); err != nil { | ||
return nil, err | ||
} | ||
|
||
if clustersResp.Clusters != nil { | ||
result = append(result, clustersResp.Clusters...) | ||
} | ||
|
||
hasMore = clustersResp.HasMore | ||
nextPageToken = clustersResp.NextPageToken | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func (d *DatabricksClient) GetJobsLatency(from, to time.Time) (map[int64][]Run, error) { | ||
var ( | ||
result = make(map[int64][]Run) | ||
|
||
hasMore = true | ||
nextPageToken = "" | ||
) | ||
|
||
for hasMore { | ||
resp, err := d.cl.R().Get( | ||
fmt.Sprintf( | ||
"%s?next_page_token=%s&start_time_from=%d&start_time_to=%d", | ||
defaultPathJobsRunsList, | ||
nextPageToken, | ||
from.UnixMilli(), | ||
to.UnixMilli(), | ||
), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode() != 200 { | ||
return nil, fmt.Errorf("bad status code: %d", resp.StatusCode()) | ||
} | ||
|
||
var runsResp RunsResponse | ||
if err = json.Unmarshal(resp.Body(), &runsResp); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, run := range runsResp.Runs { | ||
result[run.JobID] = append(result[run.JobID], run) | ||
} | ||
|
||
hasMore = runsResp.HasMore | ||
nextPageToken = runsResp.NextPageToken | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/gwos/tcg/connectors" | ||
"github.com/gwos/tcg/connectors/databricks/client" | ||
"github.com/gwos/tcg/sdk/transit" | ||
) | ||
|
||
const ( | ||
defaultResourceNameClusters = "Clusters" | ||
) | ||
|
||
func GetClustersResource(databricksClient *client.DatabricksClient) (*transit.MonitoredResource, error) { | ||
clusters, err := databricksClient.GetClusters() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get jobs latencies: %w", err) | ||
} | ||
|
||
services := make([]transit.MonitoredService, 0, len(clusters)) | ||
for _, cluster := range clusters { | ||
service, err := connectors.CreateService(cluster.Name, defaultResourceNameClusters) | ||
if err != nil { | ||
log.Error().Err(err).Str("cluster_name", cluster.Name).Msg("failed to create service") | ||
continue | ||
} | ||
switch cluster.State { | ||
case "PENDING", "RESTARTING", "RESIZING": | ||
service.Status = transit.ServicePending | ||
case "TERMINATING", "TERMINATED", "ERROR": | ||
service.Status = transit.ServiceUnscheduledCritical | ||
case "UNKNOWN": | ||
service.Status = transit.ServiceUnknown | ||
default: | ||
service.Status = transit.ServiceOk | ||
} | ||
|
||
if service.Status != transit.ServiceOk { | ||
if cluster.StateMessage != "" { | ||
service.LastPluginOutput = cluster.StateMessage | ||
} else { | ||
service.LastPluginOutput = fmt.Sprintf( | ||
"Termination reason: %s, %s", | ||
cluster.TerminationReason.Code, | ||
cluster.TerminationReason.Type, | ||
) | ||
} | ||
} | ||
|
||
services = append(services, *service) | ||
} | ||
|
||
return connectors.CreateResource(defaultResourceNameClusters, services) | ||
} |
Oops, something went wrong.