我有一个问题,在使用累积图表与夏特奎克。目前,下面是在Ruby中构建图表的方式:
errors = Error.where(status: 'active')
sum = errors.where('first_occurrence_date <= ?', Time.zone.today - 30.days).count
line_chart errors.group_by_day(:first_occurrence_date, last: 30).count.map { |x,y| { x => (sum += y)} }.reduce({}, :merge)我有一个外部服务,为我提供Rails应用程序上发生的错误,我使用API调用收集它们,并将它们存储在数据库中。
问题是,这些错误可能具有状态Resolved或Active。在外部平台上,当我认为我已经处理了错误时,我有可能“解决”错误。因此状态从active -> resolved开始。
所有这些错误都有第一次出现的时间戳,我在上面构建我的图表。让我们想象一下以下场景:
Monday => 0 errors
Tuesday => 10 errors occurring for the first time
Wednesday => 2 errors (which occurred on tuesday) resolved (active -> resolved) => total of 8 active errors
Thursday => 4 errors occurring for the first time
Friday => 1 error which occurred on Thursday solved, and 1 on Tuesday solved周三,我的图表将有以下值
Monday => 0
Tuesday => 7
Wednesday => 7
Thursday => 10
Friday => 10(因为我只接受在图表中具有活动状态的错误)
我想要的是:
Monday => 0
Tuesday => 10
Wednesday => 8
Thursday => 12
Friday =>10我想了一会儿该怎么做,却没办法找到解决办法,有谁知道怎么解决这个问题吗?
非常感谢!
发布于 2022-06-08 07:44:20
我认为,如果仅首次发生时间戳和当前状态,则不可能获得累积数据。
如果您的错误永远不会被删除,唯一的操作是“解决”(从active
如果在某一天之前或在某一天创建了错误,则该错误将被认为是active,而不是在当天之后解决或解决。因此,您的查询是:
# 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如果错误可以重新打开/解决多次,可以删除,或者具有多个状态,则
克隆约伯:在一天结束时跑步
# 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)然后简单地查询图表
line_chart DayReport.pluck(:day, :active_errors_count).to_hhttps://stackoverflow.com/questions/72532670
复制相似问题