'This will delete all the vulnerability_aggregate records and then
rebuild them by triggering off a request for each
vulnerability_data record.
This function is normally only run during prepop or postpop so we
don\'t need to worry about the aggregate data being unavailable for
any length of time'

@staticmethod
def vulnerability_rebuild_all_aggregates():

    db = current.db
    ttable = db.scheduler_task
    rtable = db.scheduler_run
    wtable = db.scheduler_worker
    query = (((ttable.task_name == 'vulnerability_update_aggregates') & (rtable.task_id == ttable.id)) & (rtable.status == 'RUNNING'))
    rows = db(query).select(rtable.id, rtable.task_id, rtable.worker_name)
    now = current.request.utcnow
    for row in rows:
        db((wtable.worker_name == row.worker_name)).update(status='KILL')
        db((rtable.id == row.id)).update(stop_time=now, status='STOPPED')
        db((ttable.id == row.task_id)).update(stop_time=now, status='STOPPED')
    current.s3db.vulnerability_aggregate.truncate()
    dtable = db.vulnerability_data
    query = ((dtable.deleted != True) & (dtable.approved_by != None))
    records = db(query).select(dtable.data_id, dtable.parameter_id, dtable.date, dtable.location_id, dtable.value)
    current.s3task.async('vulnerability_update_aggregates', vars=dict(records=records.json()), timeout=21600)
