forked from raystack/optimus
-
Notifications
You must be signed in to change notification settings - Fork 2
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: get job run from Optimus DB #278
Open
Mryashbhardwaj
wants to merge
1
commit into
release-v0.16
Choose a base branch
from
getJobRuns
base: release-v0.16
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"github.com/goto/optimus/internal/lib/interval" | ||
"github.com/goto/optimus/internal/lib/window" | ||
"github.com/goto/optimus/internal/models" | ||
"github.com/goto/optimus/internal/utils/filter" | ||
) | ||
|
||
type metricType string | ||
|
@@ -54,6 +55,8 @@ type JobRepository interface { | |
type JobRunRepository interface { | ||
GetByID(ctx context.Context, id scheduler.JobRunID) (*scheduler.JobRun, error) | ||
GetByScheduledAt(ctx context.Context, tenant tenant.Tenant, name scheduler.JobName, scheduledAt time.Time) (*scheduler.JobRun, error) | ||
GetLatestRun(ctx context.Context, project tenant.ProjectName, name scheduler.JobName, status *scheduler.State) (*scheduler.JobRun, error) | ||
GetRunsByTimeRange(ctx context.Context, project tenant.ProjectName, jobName scheduler.JobName, status *scheduler.State, since, until time.Time) ([]*scheduler.JobRun, error) | ||
GetByScheduledTimes(ctx context.Context, tenant tenant.Tenant, jobName scheduler.JobName, scheduledTimes []time.Time) ([]*scheduler.JobRun, error) | ||
Create(ctx context.Context, tenant tenant.Tenant, name scheduler.JobName, scheduledAt time.Time, slaDefinitionInSec int64) error | ||
Update(ctx context.Context, jobRunID uuid.UUID, endTime time.Time, jobRunStatus scheduler.State) error | ||
|
@@ -146,6 +149,27 @@ func (s *JobRunService) JobRunInput(ctx context.Context, projectName tenant.Proj | |
return s.compiler.Compile(ctx, details, config, executedAt) | ||
} | ||
|
||
func (s *JobRunService) GetJobRunsByFilter(ctx context.Context, projectName tenant.ProjectName, jobName scheduler.JobName, filters ...filter.FilterOpt) ([]*scheduler.JobRun, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also add test for this function as well |
||
f := filter.NewFilter(filters...) | ||
var runState *scheduler.State | ||
state, err := scheduler.StateFromString(f.GetStringValue(filter.RunState)) | ||
if err == nil { | ||
runState = &state | ||
} | ||
|
||
if f.Contains(filter.StartDate) && f.Contains(filter.EndDate) { | ||
// get job run by scheduled at between start date and end date, filter by runState if applicable | ||
return s.repo.GetRunsByTimeRange(ctx, projectName, jobName, runState, | ||
f.GetTimeValue(filter.StartDate), f.GetTimeValue(filter.EndDate)) | ||
} | ||
|
||
jobRun, err := s.repo.GetLatestRun(ctx, projectName, jobName, runState) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []*scheduler.JobRun{jobRun}, nil | ||
} | ||
|
||
func (s *JobRunService) GetJobRuns(ctx context.Context, projectName tenant.ProjectName, jobName scheduler.JobName, criteria *scheduler.JobRunsCriteria) ([]*scheduler.JobRunStatus, error) { | ||
jobWithDetails, err := s.jobRepo.GetJobDetails(ctx, projectName, jobName) | ||
if err != 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package scheduler | |
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -90,6 +91,56 @@ func (j *JobRunRepository) GetByID(ctx context.Context, id scheduler.JobRunID) ( | |
return jr.toJobRun() | ||
} | ||
|
||
func (j *JobRunRepository) GetLatestRun(ctx context.Context, project tenant.ProjectName, jobName scheduler.JobName, runState *scheduler.State) (*scheduler.JobRun, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please also provide test |
||
var jr jobRun | ||
var stateClause string | ||
if runState != nil { | ||
stateClause = fmt.Sprintf(" and status = '%s'", runState) | ||
} | ||
getLatestRun := fmt.Sprintf("SELECT %s, created_at FROM job_run j where project_name = $1 and job_name = $2 %s order by scheduled_at desc limit 1", jobRunColumns, stateClause) | ||
err := j.db.QueryRow(ctx, getLatestRun, project, jobName). | ||
Scan(&jr.ID, &jr.JobName, &jr.NamespaceName, &jr.ProjectName, &jr.ScheduledAt, &jr.StartTime, &jr.EndTime, | ||
&jr.Status, &jr.SLADefinition, &jr.SLAAlert, &jr.Monitoring, &jr.CreatedAt) | ||
if err != nil { | ||
if errors.Is(err, pgx.ErrNoRows) { | ||
return nil, errors.NotFound(scheduler.EntityJobRun, "no record for job:"+jobName.String()+stateClause) | ||
} | ||
return nil, errors.Wrap(scheduler.EntityJobRun, "error while getting run", err) | ||
} | ||
return jr.toJobRun() | ||
} | ||
|
||
func (j *JobRunRepository) GetRunsByTimeRange(ctx context.Context, project tenant.ProjectName, jobName scheduler.JobName, runState *scheduler.State, since, until time.Time) ([]*scheduler.JobRun, error) { | ||
var jobRunList []*scheduler.JobRun | ||
var stateClause string | ||
if runState != nil { | ||
stateClause = fmt.Sprintf(" and status = '%s'", runState) | ||
} | ||
getLatestRun := fmt.Sprintf("SELECT %s, created_at FROM job_run j where project_name = $1 and job_name = $2 and scheduled_at >= $3 and scheduled_at <= $4 %s", jobRunColumns, stateClause) | ||
rows, err := j.db.Query(ctx, getLatestRun, project, jobName, since, until) | ||
if err != nil { | ||
return nil, errors.Wrap(scheduler.EntityJobRun, "error while getting job runs", err) | ||
} | ||
for rows.Next() { | ||
var jr jobRun | ||
err := rows.Scan(&jr.ID, &jr.JobName, &jr.NamespaceName, &jr.ProjectName, &jr.ScheduledAt, &jr.StartTime, &jr.EndTime, | ||
&jr.Status, &jr.SLADefinition, &jr.SLAAlert, &jr.Monitoring, &jr.CreatedAt) | ||
if err != nil { | ||
if errors.Is(err, pgx.ErrNoRows) { | ||
errMsg := fmt.Sprintf("no record for job: %s, scheduled between : %s -> %s, %s", jobName, since, until, stateClause) | ||
return nil, errors.NotFound(scheduler.EntityJobRun, errMsg) | ||
} | ||
return nil, errors.Wrap(scheduler.EntityJobRun, "error while getting run", err) | ||
} | ||
jobRun, err := jr.toJobRun() | ||
if err != nil { | ||
return nil, errors.Wrap(scheduler.EntityJobRun, "error while getting job runs", err) | ||
} | ||
jobRunList = append(jobRunList, jobRun) | ||
} | ||
return jobRunList, nil | ||
} | ||
|
||
func (j *JobRunRepository) GetByScheduledAt(ctx context.Context, t tenant.Tenant, jobName scheduler.JobName, scheduledAt time.Time) (*scheduler.JobRun, error) { | ||
var jr jobRun | ||
// todo: check if `order by created_at desc limit 1` is required | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we provide test for this handler?