def by_vat(dest, run, detail):
    chain_id = f'agorictest-{run.chain}'
    run_detail = f'{run.name:04}-{run.parent}-{run.file_id}-{run.line}'
    for (vatID, g) in detail.groupby('vatID'):
        try:
            ((dest / chain_id) / vatID).mkdir(parents=True)
        except:
            pass
        vat_dir = ((dest / chain_id) / vatID)
        f = (vat_dir / f'delivery-detail-{run_detail}.csv.gz')
        log.info('saving to %s:\n%s', f, g.set_index(['vatID', 'deliveryNum'])[['compute', 'dur']].tail(3))
        g.to_csv(f, index=False)
        f = (vat_dir / f'delivery-summary-{run_detail}.csv.gz')
        g[['vatID', 'deliveryNum', 'kd', 'syscalls', 'compute']].to_csv(f, index=False)
    return detail.assign(run=run.name).groupby(['run', 'vatID'])[['deliveryNum']].count()
def by_vat(dest, run, detail):
    chain_id = f'agorictest-{run.chain}'
    run_detail = f'{run.name:04}-{run.parent}-{run.file_id}-{run.line}'
    for (vatID, g) in detail.groupby('vatID'):
        try:
            ((dest / chain_id) / vatID).mkdir(parents=True)
        except:
            pass
        vat_dir = ((dest / chain_id) / vatID)
        f = (vat_dir / f'delivery-detail-{run_detail}.csv.gz')
        log.info('saving to %s:\n%s', f, g.set_index(['vatID', 'deliveryNum'])[['compute', 'dur']].tail(3))
        g.to_csv(f, index=False)
        f = (vat_dir / f'delivery-summary-{run_detail}.csv.gz')
        g[['vatID', 'deliveryNum', 'kd', 'syscalls', 'compute']].to_csv(f, index=False)
    return detail.assign(run=run.name).groupby(['run', 'vatID'])[['deliveryNum']].count()