github.com/kvattikuti/drone@v0.2.1-0.20140603034306-d400229a327a/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  }
    29  
    30  // HumanDuration returns a human-readable approximation of a duration
    31  // (eg. "About a minute", "4 hours ago", etc.)
    32  func (b *Build) HumanDuration() string {
    33  	d := time.Duration(b.Duration)
    34  	if seconds := int(d.Seconds()); seconds < 1 {
    35  		return "Less than a second"
    36  	} else if seconds < 60 {
    37  		return fmt.Sprintf("%d seconds", seconds)
    38  	} else if minutes := int(d.Minutes()); minutes == 1 {
    39  		return "About a minute"
    40  	} else if minutes < 60 {
    41  		return fmt.Sprintf("%d minutes", minutes)
    42  	} else if hours := int(d.Hours()); hours == 1 {
    43  		return "About an hour"
    44  	} else if hours < 48 {
    45  		return fmt.Sprintf("%d hours", hours)
    46  	} else if hours < 24*7*2 {
    47  		return fmt.Sprintf("%d days", hours/24)
    48  	} else if hours < 24*30*3 {
    49  		return fmt.Sprintf("%d weeks", hours/24/7)
    50  	} else if hours < 24*365*2 {
    51  		return fmt.Sprintf("%d months", hours/24/30)
    52  	}
    53  	return fmt.Sprintf("%f years", d.Hours()/24/365)
    54  }
    55  
    56  // Returns the Started Date as an ISO8601
    57  // formatted string.
    58  func (b *Build) StartedString() string {
    59  	return b.Started.Format("2006-01-02T15:04:05Z")
    60  }
    61  
    62  // Returns the Started Date as an ISO8601
    63  // formatted string.
    64  func (b *Build) FinishedString() string {
    65  	return b.Finished.Format("2006-01-02T15:04:05Z")
    66  }
    67  
    68  // Returns true if the Build statis is Started
    69  // or Pending, indicating it is currently running.
    70  func (b *Build) IsRunning() bool {
    71  	return (b.Status == StatusStarted || b.Status == StatusEnqueue)
    72  }