Topic: Yuck.

This is pretty hideous; it's actually causing WEBrick to, well, brick.  It freezes up and the site is unresponsive until I restart the console sad

I'm using a graphing utility to keep track of a project's backlog. I put the code on Pastie:

http://pastie.org/566922

Any help would be great.  Thank you tongue

Last edited by mikben (2009-07-31 12:58:31)

Re: Yuck.

This is what I have right now:

def backlog_graph

    @project = Project.find(params[:id], :include => {:assignments => :employee})
    @assignments = @project.assignments
   
        #cool, so right now we have the project and all of its assignments.  Let's create an array that holds each day's value (initialized to 0)

    backlog = Array.new(((@project.end_date - @project.start_date).to_i), 0) 

        #the first value in the array will be the full value of the project
    burn_carry = @project.mswa_value
    backlog[0] = burn_carry
    i = 1

        #now we'll iterate through each day and collect every assignment's data, if the assignment is active during that day

    backlog.each do |day|
   
        day_burn = 0  #day's total burn
        current_day = @project.start_date.advance(:days => i)
        debugger
       
       
        @assignments.each do |assignment|
       
            if (assignment.start_date..assignment.end_date).to_a.include?(current_day)
           
                day_burn += assignment.daily_burn # we're adding the cost of that assignment (which is returned by the daily_burn method in the assignment model)
           
            end
        end

     
      backlog[i] = burn_carry-day_burn unless day_burn == nil
      
      burn_carry = backlog[i] #this value will be used by the next iteration to decrease the total backlog

      i = i + 1
   
   
  end

    line = Line.new
    line.set_values(backlog)
chart = OpenFlashChart.new
  chart.add_element(line)
    chart.bg_colour = "#F0F0F0"
   
    y = YAxis.new
    y.set_offset(true)
    y.set_labels([1000,2000,3000,4000])
    chart.set_y_axis(y)
   
    render :text => chart.to_s

end


I'm using ruby-debug and I'm watching the array fill up past the constraints of the project's start_date and end_date.  How would I prevent this array from "expanding"? 

Also, when an infinite loop occurs and continues to fill an array with data indefinitely, main memory gets filled first and then virtual memory gets filled (on the hard drive) and then the OS or application crash due to insufficient space, correct? 

Cheers smile

Re: Yuck.

I just ended up using an if statement to solve the overflow issue, but I'm still curious as to what was physically happening when this array was going nuts.

Last edited by mikben (2009-08-03 16:59:53)

Re: Yuck.

I haven't really assimilated all of this as it's pretty hard to read and understand but i spied a few things:

1) (date1 - date2), if the dates are DateTime objects, gives you the difference between those dates in *seconds*, as a float.  There are 86400 seconds in a day - if you want the time in days then divide the result by that.  Maybe when you were doing Array.new and passing it the difference between two dates then the array was 86400x bigger than you wanted.

2) array iteration using an index (like 'i') is NOT the ruby way.  don't do it.  If you really have to use an index variable inside the array then do .each_with_index |thing, i| instead.  But, avoid using the i variable to jump around the array.  You do this where, inside backlog, you're changing the value of i and also accessing backlog[i].  Don't do this, it makes the code unpredictable and very hard to mentally step through.

3) this

if (assignment.start_date..assignment.end_date).to_a.include?(current_day)

can be rewritten as

if (assignment.start_date..assignment.end_date) === current_day

Last edited by Max Williams (2009-08-05 12:52:43)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Yuck.

Cool!  Good stuff, as always.