Skip to content

Commit

Permalink
Add metrics.predict_time to response
Browse files Browse the repository at this point in the history
  • Loading branch information
nevillelyh committed Dec 13, 2024
1 parent 061bf30 commit bd8d0f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions internal/server/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ func (r *Runner) handleResponses() {
log.Infow("prediction processing", "id", pr.request.Id, "status", pr.response.Status)
pr.sendWebhook(WebhookOutput)
} else if pr.response.Status.IsCompleted() {
if pr.response.Status == PredictionSucceeded {
t := util.ParseTime(pr.response.CompletedAt).Sub(util.ParseTime(pr.response.StartedAt)).Seconds()
pr.response.Metrics = PredictionMetrics{PredictTime: t}
}
log.Infow("prediction completed", "id", pr.request.Id, "status", pr.response.Status)
pr.sendWebhook(WebhookCompleted)
pr.sendResponse()
Expand Down
23 changes: 14 additions & 9 deletions internal/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,19 @@ type PredictionRequest struct {
OutputFilePrefix string `json:"output_file_prefix,omitempty"`
}

type PredictionMetrics struct {
PredictTime float64 `json:"predict_time"`
}

type PredictionResponse struct {
Input any `json:"input"`
Output any `json:"output"`
Id string `json:"id"`
CreatedAt string `json:"created_at"`
StartedAt string `json:"started_at"`
CompletedAt string `json:"completed_at"`
Logs string `json:"logs,omitempty"`
Error string `json:"error,omitempty"`
Status PredictionStatus `json:"status,omitempty"`
Input any `json:"input"`
Output any `json:"output"`
Id string `json:"id"`
CreatedAt string `json:"created_at"`
StartedAt string `json:"started_at"`
CompletedAt string `json:"completed_at"`
Logs string `json:"logs,omitempty"`
Error string `json:"error,omitempty"`
Status PredictionStatus `json:"status,omitempty"`
Metrics PredictionMetrics `json:"metrics,omitempty"`
}
8 changes: 7 additions & 1 deletion internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ func PredictionId() string {
return encoding.EncodeToString(shuffle)
}

const TimeLayout = "2006-01-02T15:04:05.999999-07:00"

func NowIso() string {
// Python: datetime.now(tz=timezone.utc).isoformat()
return time.Now().UTC().Format("2006-01-02T15:04:05.999999-07:00")
return time.Now().UTC().Format(TimeLayout)
}

func ParseTime(t string) time.Time {
return must.Get(time.Parse(TimeLayout, t))
}

func JoinLogs(logs []string) string {
Expand Down

0 comments on commit bd8d0f0

Please sign in to comment.