Fixed: History grid loads faster (lazy loaded)

This commit is contained in:
Mark McDowall 2012-07-27 23:37:47 -07:00
commit d44c07b27b
23 changed files with 245 additions and 39 deletions

View file

@ -1,6 +1,9 @@
using System.Linq;
using System;
using System.Linq;
using System.Linq.Dynamic;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using DataTables.Mvc.Core.Models;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Providers;
@ -24,15 +27,46 @@ namespace NzbDrone.Web.Controllers
return View();
}
public JsonResult AjaxBinding()
//public JsonResult AjaxBinding()
//{
// var history = _historyProvider.AllItemsWithRelationships().Select(h => new HistoryModel
// {
// HistoryId = h.HistoryId,
// SeriesId = h.SeriesId,
// EpisodeNumbering = string.Format("{0}x{1:00}", h.Episode.SeasonNumber, h.Episode.EpisodeNumber),
// EpisodeTitle = h.Episode.Title,
// EpisodeOverview = h.Episode.Overview,
// SeriesTitle = h.SeriesTitle,
// SeriesTitleSorter = SortHelper.SkipArticles(h.SeriesTitle),
// NzbTitle = h.NzbTitle,
// Quality = h.Quality.ToString(),
// IsProper = h.IsProper,
// Date = h.Date.ToString(),
// DateSorter = h.Date.ToString("MM/dd/yyyy h:mm:ss tt"),
// Indexer = h.Indexer,
// EpisodeId = h.EpisodeId,
// NzbInfoUrl = h.NzbInfoUrl
// }).OrderByDescending(h => h.Date).ToList();
// return Json(new
// {
// aaData = history
// },
// JsonRequestBehavior.AllowGet);
//}
public ActionResult AjaxBinding(DataTablesPageRequest pageRequest)
{
var history = _historyProvider.AllItemsWithRelationships().Select(h => new HistoryModel
var pageResult = _historyProvider.GetPagedItems(pageRequest);
var totalItems = _historyProvider.Count();
var items = pageResult.Items.Select(h => new HistoryModel
{
HistoryId = h.HistoryId,
SeriesId = h.SeriesId,
EpisodeNumbering = string.Format("{0}x{1:00}", h.Episode.SeasonNumber, h.Episode.EpisodeNumber),
EpisodeTitle = h.Episode.Title,
EpisodeOverview = h.Episode.Overview,
EpisodeNumbering = string.Format("{0}x{1:00}", h.SeasonNumber, h.EpisodeNumber),
EpisodeTitle = h.EpisodeTitle,
EpisodeOverview = h.EpisodeOverview,
SeriesTitle = h.SeriesTitle,
SeriesTitleSorter = SortHelper.SkipArticles(h.SeriesTitle),
NzbTitle = h.NzbTitle,
@ -43,11 +77,14 @@ namespace NzbDrone.Web.Controllers
Indexer = h.Indexer,
EpisodeId = h.EpisodeId,
NzbInfoUrl = h.NzbInfoUrl
}).OrderByDescending(h => h.Date).ToList();
});
return Json(new
{
aaData = history
sEcho = pageRequest.Echo,
iTotalRecords = totalItems,
iTotalDisplayRecords = pageResult.TotalItems,
aaData = items
},
JsonRequestBehavior.AllowGet);
}

View file

@ -52,36 +52,36 @@ namespace NzbDrone.Web.Controllers
return JsonNotificationResult.Info("Logs Cleared");
}
public ActionResult AjaxBinding(DataTablesParams dataTablesParams)
public ActionResult AjaxBinding(DataTablesPageRequest pageRequest)
{
var logs = _logProvider.GetAllLogs();
var totalCount = logs.Count();
IQueryable<Log> q = logs;
if (!string.IsNullOrEmpty(dataTablesParams.sSearch))
if (!string.IsNullOrEmpty(pageRequest.Search))
{
q = q.Where(b => b.Logger.Contains(dataTablesParams.sSearch)
|| b.Exception.Contains(dataTablesParams.sSearch)
|| b.Message.Contains(dataTablesParams.sSearch));
q = q.Where(b => b.Logger.Contains(pageRequest.Search)
|| b.Exception.Contains(pageRequest.Search)
|| b.Message.Contains(pageRequest.Search));
}
int filteredCount = q.Count();
IQueryable<Log> sorted = q;
for (int i = 0; i < dataTablesParams.iSortingCols; i++)
for (int i = 0; i < pageRequest.SortingCols; i++)
{
int sortCol = dataTablesParams.iSortCol[i];
int sortCol = pageRequest.SortCol[i];
var sortColName = sortCol == 0 ? "Time" : sortCol == 1 ? "Level" : "Logger";
var sortExpression = String.Format("{0} {1}", sortColName, dataTablesParams.sSortDir[i]);
var sortExpression = String.Format("{0} {1}", sortColName, pageRequest.SortDir[i]);
sorted = sorted.OrderBy(sortExpression);
}
IQueryable<Log> filteredAndSorted = sorted;
if (filteredCount > dataTablesParams.iDisplayLength)
if (filteredCount > pageRequest.DisplayLength)
{
filteredAndSorted = sorted.Skip(dataTablesParams.iDisplayStart).Take(dataTablesParams.iDisplayLength);
filteredAndSorted = sorted.Skip(pageRequest.DisplayStart).Take(pageRequest.DisplayLength);
}
var logModels = filteredAndSorted.ToList().Select(s => new LogModel
@ -97,7 +97,7 @@ namespace NzbDrone.Web.Controllers
return Json(new
{
sEcho = dataTablesParams.sEcho,
sEcho = pageRequest.Echo,
iTotalRecords = totalCount,
iTotalDisplayRecords = filteredCount,
aaData = logModels