in metrics/metrics/platform_load.go [42:84]
func (pl *PlatformLoad) Collect() (data.DataSet, error) {
result := &loadDataSet{headers: GetColumnNames(pl.columns), ts: time.Now(), rows: make([]*loadDataRow, 0)}
for _, org := range pl.orgs {
pid := &data.PipelineID{Org: org, Slug: "all"}
builds, err := pl.client.GetMostRecentBuilds(pid, pl.builds)
if err != nil {
return nil, fmt.Errorf("Cannot get builds to determine platform load: %v", err)
}
allPlatforms := make(map[string]bool)
waiting := make(map[string]int)
running := make(map[string]int)
for _, build := range builds {
for _, job := range build.Jobs {
// Do not use getPlatform() since it may return "rbe", but here we're only interested in the actual worker OS (which would be "linux" in the rbe case).
platform := getPlatformFromAgentQueryRules(job.AgentQueryRules)
if platform == "" || job.CreatedAt == nil || job.FinishedAt != nil {
continue
}
allPlatforms[platform] = true
switch *job.State {
case "running":
running[platform] += 1
case "scheduled", "runnable":
/*
State "scheduled" / "runnable" = waiting for a worker to become available
State "waiting" / "waiting_failed" = waiting for another task to finish
We're only interested in "scheduled" and "runnable" jobs since they may indicate a shortage of workers.
*/
waiting[platform] += 1
}
}
}
for platform := range allPlatforms {
row := &loadDataRow{org: org, platform: platform, waitingJobs: waiting[platform], runningJobs: running[platform]}
result.rows = append(result.rows, row)
}
}
return result, nil
}