Chartjs-Chartkick cumulative chart by date never descending

0๐Ÿ‘

โœ…

I think it is not possible for cumulative data if you only have first occurred timestamp and current status.

  • If your errors will never be deleted and the only action is resolve (Change from active -> resolved status), you could add a new field, indicate when the error was resolved.

The error will be considered active on a day if they are created before or on that day, and not resolved or resolved after that day. So you query would be:

# Don't filter by status here
errors = Error.where('first_occurrence_date <= ?', Time.zone.today - 30.days)

day_data = (29.days.ago.to_date..Time.zone.today).to_h { |day| [day, 0] }

errors.find_each do |error|
  resolved_date = error.resolved_at&.to_date || Time.zone.tomorrow
  (error.first_occurrence_date..resolved_date).each do |day|
    day_data[day] += 1
  end
end
  • If the error can be reopened/resolved multiple times, can be deleted, or having multiple statuses, this approach will be invalid though. You may consider storing the history amount for each day in a new model via a daily cronjob or something like that, and just query those amounts. For example

Cronjob: run at the end of the day

# first_occurrence_date doesn't matter, we only count the errors still active by the end of the day
DayReport.create(day: Time.zone.today, active_errors_count: Error.where(status: "active").count)

Then simply query for the chart

line_chart DayReport.pluck(:day, :active_errors_count).to_h

Leave a comment