feat(#IVYSF): 登录日志使用分页查询

comment #IVYSF
使用后台分页提高数据量大时程序性能

close https://gitee.com/LongbowEnterprise/dashboard/issues?id=IVYSF
This commit is contained in:
Argo Zhang 2019-05-01 12:07:00 +08:00
parent e1ea247549
commit 15a6b23564
5 changed files with 105 additions and 86 deletions

View File

@ -1,61 +1,69 @@
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Longbow.Web;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// </summary>
/// <summary>
///
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class LoginController : ControllerBase
{
/// <summary>
/// 获得登录历史记录
/// </summary>
/// <returns></returns>
[HttpGet]
public IEnumerable<LoginUser> Get() => LoginHelper.Retrieves();
/// <summary>
///
/// </summary>
/// <param name="onlineUserSvr"></param>
/// <param name="ipLocator"></param>
/// <param name="value"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost]
public string Post([FromServices]IOnlineUsers onlineUserSvr, [FromServices]IIPLocatorProvider ipLocator, [FromBody]JObject value)
{
string token = null;
dynamic user = value;
string userName = user.userName;
string password = user.password;
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password, loginUser => AccountController.CreateLoginUser(onlineUserSvr, ipLocator, HttpContext, loginUser)))
{
token = BootstrapAdminJwtTokenHandler.CreateToken(userName);
}
return token;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpOptions]
public string Options()
{
return null;
}
}
}
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Longbow.Web;
using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// </summary>
/// <summary>
///
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class LoginController : ControllerBase
{
/// <summary>
/// 获得登录历史记录
/// </summary>
/// <param name="po"></param>
/// <returns></returns>
[HttpGet]
public QueryData<LoginUser> Get([FromQuery]PaginationOption po)
{
var data = LoginHelper.Retrieves(po);
var ret = new QueryData<LoginUser>();
ret.total = data.TotalItems;
ret.rows = data.Items;
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="onlineUserSvr"></param>
/// <param name="ipLocator"></param>
/// <param name="value"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost]
public string Post([FromServices]IOnlineUsers onlineUserSvr, [FromServices]IIPLocatorProvider ipLocator, [FromBody]JObject value)
{
string token = null;
dynamic user = value;
string userName = user.userName;
string password = user.password;
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password) && UserHelper.Authenticate(userName, password, loginUser => AccountController.CreateLoginUser(onlineUserSvr, ipLocator, HttpContext, loginUser)))
{
token = BootstrapAdminJwtTokenHandler.CreateToken(userName);
}
return token;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[AllowAnonymous]
[HttpOptions]
public string Options()
{
return null;
}
}
}

View File

@ -3,8 +3,6 @@ $(function () {
var apiUrl = "api/Login";
var $table = $('table').smartTable({
url: apiUrl,
method: "get",
sidePagination: "client",
showToggle: false,
showRefresh: false,
showColumns: false,

View File

@ -1,6 +1,10 @@
using MongoDB.Driver;
using Longbow.Web.Mvc;
using MongoDB.Driver;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
/// <summary>
@ -22,7 +26,24 @@ namespace Bootstrap.DataAccess.MongoDB
/// <summary>
///
/// </summary>
/// <param name="po"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.LoginUser> Retrieves() => DbManager.LoginUsers.Find(FilterDefinition<DataAccess.LoginUser>.Empty).SortByDescending(user => user.LoginTime).ToList();
public override Page<DataAccess.LoginUser> Retrieves(PaginationOption po)
{
var logs = DbManager.LoginUsers
.Find(Builders<DataAccess.LoginUser>.Filter.Empty)
.Sort(Builders<DataAccess.LoginUser>.Sort.Descending(t => t.LoginTime))
.ToList();
return new Page<DataAccess.LoginUser>()
{
Context = logs,
CurrentPage = po.PageIndex,
ItemsPerPage = po.Limit,
TotalItems = logs.Count,
TotalPages = (long)Math.Ceiling(logs.Count * 1.0 / po.Limit),
Items = logs.Skip(po.Offset).Take(po.Limit).ToList()
};
}
}
}

View File

@ -1,7 +1,7 @@
using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic;
using Longbow.Data;
using Longbow.Web.Mvc;
using PetaPoco;
namespace Bootstrap.DataAccess
{
/// <summary>
@ -9,26 +9,17 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class LoginHelper
{
/// <summary>
///
/// </summary>
public const string RetrieveLoginLogsDataKey = "LoginHelper-Retrieves";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static bool Log(LoginUser user)
{
var ret = DbContextManager.Create<LoginUser>().Log(user);
if (ret) CacheManager.Clear(RetrieveLoginLogsDataKey);
return ret;
}
public static bool Log(LoginUser user) => DbContextManager.Create<LoginUser>().Log(user);
/// <summary>
/// 查询一个月内所有登录信息
/// 查询所有登录日志
/// </summary>
public static IEnumerable<LoginUser> Retrieves() => CacheManager.GetOrAdd(RetrieveLoginLogsDataKey, key => DbContextManager.Create<LoginUser>().Retrieves());
/// <param name="po"></param>
public static Page<LoginUser> Retrieves(PaginationOption po) => DbContextManager.Create<LoginUser>().Retrieves(po);
}
}

View File

@ -1,6 +1,6 @@
using PetaPoco;
using Longbow.Web.Mvc;
using PetaPoco;
using System;
using System.Collections.Generic;
namespace Bootstrap.DataAccess
{
@ -70,7 +70,8 @@ namespace Bootstrap.DataAccess
/// <summary>
///
/// </summary>
/// <param name="po"></param>
/// <returns></returns>
public virtual IEnumerable<LoginUser> Retrieves() => DbManager.Create().Fetch<LoginUser>("Where LoginTime > @0 Order by LoginTime desc", DateTime.Today.AddMonths(0 - DictHelper.RetrieveLoginLogsPeriod()));
public virtual Page<LoginUser> Retrieves(PaginationOption po) => DbManager.Create().Page<LoginUser>(po.PageIndex, po.Limit, "select UserName, LoginTime, Ip, Browser, OS, City, Result from LoginLogs Order by LoginTime desc");
}
}