github.com/unclejack/drone@v0.2.1-0.20140918182345-831b034aa33b/pkg/model/build.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  const (
     9  	StatusNone    = "None"
    10  	StatusEnqueue = "Pending"
    11  	StatusStarted = "Started"
    12  	StatusSuccess = "Success"
    13  	StatusFailure = "Failure"
    14  	StatusError   = "Error"
    15  )
    16  
    17  type Build struct {
    18  	ID          int64     `meddler:"id,pk"            json:"id"`
    19  	CommitID    int64     `meddler:"commit_id"        json:"-"`
    20  	Slug        string    `meddler:"slug"             json:"slug"`
    21  	Status      string    `meddler:"status"           json:"status"`
    22  	Started     time.Time `meddler:"started,utctime"  json:"started"`
    23  	Finished    time.Time `meddler:"finished,utctime" json:"finished"`
    24  	Duration    int64     `meddler:"duration"         json:"duration"`
    25  	Created     time.Time `meddler:"created,utctime"  json:"created"`
    26  	Updated     time.Time `meddler:"updated,utctime"  json:"updated"`
    27  	Stdout      string    `meddler:"stdout"           json:"-"`
    28  	BuildScript string    `meddler:"buildscript"      json:"-"`
    29  }
    30  
    31  // HumanDuration returns a human-readable approximation of a duration
    32  // (eg. "About a minute", "4 hours ago", etc.)
    33  func (b *Build) HumanDuration() string {
    34  	d := time.Duration(b.Duration)
    35  	if seconds := int(d.Seconds()); seconds < 1 {
    36  		return "Less than a second"
    37  	} else if seconds < 60 {
    38  		return fmt.Sprintf("%d seconds", seconds)
    39  	} else if minutes := int(d.Minutes()); minutes == 1 {
    40  		return "About a minute"
    41  	} else if minutes < 60 {
    42  		return fmt.Sprintf("%d minutes", minutes)
    43  	} else if hours := int(d.Hours()); hours == 1 {
    44  		return "About an hour"
    45  	} else if hours < 48 {
    46  		return fmt.Sprintf("%d hours", hours)
    47  	} else if hours < 24*7*2 {
    48  		return fmt.Sprintf("%d days", hours/24)
    49  	} else if hours < 24*30*3 {
    50  		return fmt.Sprintf("%d weeks", hours/24/7)
    51  	} else if hours < 24*365*2 {
    52  		return fmt.Sprintf("%d months", hours/24/30)
    53  	}
    54  	return fmt.Sprintf("%f years", d.Hours()/24/365)
    55  }
    56  
    57  // Returns the Started Date as an ISO8601
    58  // formatted string.
    59  func (b *Build) StartedString() string {
    60  	return b.Started.Format("2006-01-02T15:04:05Z")
    61  }
    62  
    63  // Returns the Started Date as an ISO8601
    64  // formatted string.
    65  func (b *Build) FinishedString() string {
    66  	return b.Finished.Format("2006-01-02T15:04:05Z")
    67  }
    68  
    69  // Returns true if the Build statis is Started
    70  // or Pending, indicating it is currently running.
    71  func (b *Build) IsRunning() bool {
    72  	return (b.Status == StatusStarted || b.Status == StatusEnqueue)
    73  }