Skip to content

Instantly share code, notes, and snippets.

@jairojunior
Last active November 12, 2018 17:10
Show Gist options
  • Save jairojunior/2adaaaa42fad21eed2ba9e8836496dea to your computer and use it in GitHub Desktop.
Save jairojunior/2adaaaa42fad21eed2ba9e8836496dea to your computer and use it in GitHub Desktop.
def missing_hours(vm_id, day)
day_hours=*(0..23)
present_hours = MetricRollup.where(:resource_id => vm_id, :resource_type => 'VmOrTemplate', :capture_interval_name => 'hourly').where("date_trunc('day', timestamp) = '#{day}'").group("resource_id").group("timestamp").select("timestamp").order("timestamp").collect { |rollup| rollup.timestamp.hour }
day_hours - present_hours
end
def generate_missing(vm_id, day)
objs = []
rollup_template = MetricRollup.where(:resource_id => vm_id, :resource_type => 'VmOrTemplate', :capture_interval_name => 'hourly').where("date_trunc('day', timestamp) = '#{day}'").order("timestamp").first
missing_hours(vm_id, day).each do |missing_hour|
obj = rollup_template.dup
obj.timestamp = day.to_time + missing_hour.hours
obj.created_on = '2018-11-08 00:00:00 UTC'.to_time
objs << obj.attributes
end
MetricRollup.create(objs)
end
days = ['2018-10-31 00:00:00 UTC', '2018-11-01 00:00:00 UTC']
days.each do |day|
vms_with_missing_hours = MetricRollup.where(:resource_type => 'VmOrTemplate', :capture_interval_name => 'hourly').where("date_trunc('day', timestamp) = '#{day}'").group("resource_id").order("resource_id").having('count(distinct timestamp) < 24').select('resource_id')
vms_with_missing_hours.each { |vm_with_missing_hours| generate_missing(vm_with_missing_hours.resource_id, day) }
end
# MetricRollup.where("date_trunc('second', created_on) = '2018-11-08 00:00:00 UTC'").size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment