silent
eval $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
try
do  
    ctf set abortedColor '#c2c2a3'
    ctf set failureColor '#ff3300'
    ctf set successColor '#00cc00'
    ctf set unstableColor '#ff9900'
    ctf set durationColor '#0000ff'
        
    ctf set after_time `echo $box.field.interval`.days.ago
    
    try
    do
        edb in_project ["$box.field.projId"];  after $after_time; restrict_to builds;
        copy as results
    catch
        ctf set nodata 1
    done
    
    if $nodata eq '1'
    do
        raise to NO_DATA
    done
    
    go results
    
    alter list add field dateCreated date as %Y-%m-%d $row.time_created
    
    pivot -n dateCreated _type function -row avg duration
    foreach
    do
        if $lineCounts ne ""
        do
            ctf set lineCounts $lineCounts ,
        done
        ctf set -e rounded expr int(`print TOTAL`)
        ctf set lineCounts $lineCounts $rounded
    done
    return
    
    pivot -n dateCreated status_name
    alter list delete field TOTAL
    copy as results
    go results
    
    # This is a bit of a hack.  I'm sure there's a better way to do this.
    # Figures out which statuses are in the pivot table by attempting to
    # add the each status field to the table.  If the add fails, it means
    # the field is already there (the status is included), so we append
    # the color for the status (with a comma separator if not the first
    # status).  If the add succeeds, it means that the field was not already
    # there, so we remove the field we just added.
    try
    do
        alter list add field Successful expr 0
        alter list delete field Successful
    catch
        ctf set colors $colors $successColor
        ctf set successfulFound 1
    done
    try
    do
        alter list add field Unstable expr 0
        alter list delete field Unstable
    catch
        if $colors ne ''
        do
            ctf set colors $colors , 
        done
        ctf set colors $colors $unstableColor
        ctf set unstableFound 1
    done
    try
    do
        alter list add field Aborted expr 0
        alter list delete field Aborted
    catch
        if $colors ne ''
        do
            ctf set colors $colors , 
        done
        ctf set colors $colors $abortedColor
        ctf set abortedFound 1
    done
    try
    do
        alter list add field Failed expr 0
        alter list delete field Failed
    catch
        if $colors ne ''
        do
            ctf set colors $colors , 
        done
        ctf set colors $colors $failureColor
        ctf set failedFound 1
    done
    
    ctf set colors $colors , $durationColor
    
    foreach
    do
        if $abortedFound eq '1'
        do
            if $abortedSeries ne ''
            do
                ctf set abortedSeries $abortedSeries ,
            done
            ctf set abortedSeries $abortedSeries `print Aborted`
        done
        if $failedFound eq '1'
        do
            if $failedSeries ne ''
            do
                ctf set failedSeries $failedSeries ,
            done
            ctf set failedSeries $failedSeries `print Failed`
        done
        if $successfulFound eq '1'
        do
            if $successfulSeries ne ''
            do
                ctf set successfulSeries $successfulSeries ,
            done
            ctf set successfulSeries $successfulSeries `print Successful`
        done
        if $unstableFound eq '1'
        do
            if $unstableSeries ne ''
            do
                ctf set unstableSeries $unstableSeries ,
            done
            ctf set unstableSeries $unstableSeries `print Unstable`
        done
    done
    
    if $successfulFound eq '1'
    do
        ctf set series { name: 'Successful', data:[ $successfulSeries ]}
    done
    if $unstableFound eq '1'
    do
        if $series ne ''
        do
            ctf set series $series ,
        done
        ctf set series $series { name: 'Unstable', data:[ $unstableSeries ]}
    done
    if $abortedFound eq '1'
    do
        if $series ne ''
        do
            ctf set series $series ,
        done
        ctf set series $series { name: 'Aborted', data:[ $abortedSeries ]}
    done
    if $failedFound eq '1'
    do
        if $series ne ''
        do
            ctf set series $series ,
        done
        ctf set series $series { name: 'Failed', data:[ $failedSeries ]}
    done
    
    ctf set stacked true
    ctf set decimals false
    ctf set columnOne dateCreated
catch NO_DATA
   message info No data.
done