@model SQLInstance
@{
    var files = Model.Databases.Data?.SelectMany(db => db.Files).OrderBy(f => f.PhysicalName);
    var volumes = files?.GroupBy(p => p.VolumeMountPoint);
}
<h4 class="modal-title">
    Database Files
</h4>
@if (files?.Any() ?? false)
{
    <h5 class="page-header">Read/Write Delay Averages</h5>
    <div class="row small">
        @foreach (var v in volumes)
        {
            var readMs = v.Sum(f => f.StallReadMs) / (decimal)v.Sum(f => f.NumReads);
            var writeMs = v.Sum(f => f.StallWriteMs) / (decimal)v.Sum(f => f.NumWrites);
            <div class="value-block col-md-4">
                @(readMs.ToString("F"))
                <span class="text-muted">ms /</span>
                @(writeMs.ToString("F"))
                <span class="text-muted">ms</span>
                <label>@v.Key:\</label>
            </div>
        }
    </div>
    <table class="table table-striped table-hover text-nowrap table-super-condensed table-responsive js-db-files-table">
        <thead>
            <tr>
                <th>Database</th>
                <th>Type</th>
                <th>Path</th>
                <th>Size</th>
                <th>Avg Read</th>
                <th>Avg Write</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var f in files)
                {
                <tr>
                    <td>@f.DatabaseName</td>
                    <td>@f.FileType.AsString(EnumFormat.Description)</td>
                    <td title="Virtual Name: @f.FileName"><span title="@f.PhysicalName">@f.ShortPhysicalName</span></td>
                    <td data-val="@f.TotalSizeBytes.ToString()">@f.TotalSizeBytes.ToHumanReadableSize()</td>
                    <td title="Reads: @f.NumReads.ToComma()
Stalled: @f.StallReadMs.ToComma()">
                        @f.AvgReadStallMs.ToString("F") <span class="text-muted">ms</span>
                    </td>
                    <td title="Writes: @f.NumWrites.ToComma()
Stalled: @f.StallWriteMs.ToComma()">
                        @f.AvgWriteStallMs.ToString("F") <span class="text-muted">ms</span>
                    </td>
                </tr>
            }
        </tbody>
    </table>
        <script>
            $(function () {
                $('.js-db-files-table').tablesorter({
                    headers: {
                        3: { sorter: 'dataVal', sortInitialOrder: 'desc' },
                        4: { sortInitialOrder: 'desc' },
                        5: { sortInitialOrder: 'desc' }
                    }
                });
            });
        </script>
}
else
{
    <div class="no-content">No Database Files</div>
}
