2019-03-18 17:26:12 +08:00
|
|
|
using Longbow.Web.Mvc;
|
2019-03-16 20:24:33 +08:00
|
|
|
using MongoDB.Driver;
|
|
|
|
using PetaPoco;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.MongoDB
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
public class Trace : DataAccess.Trace
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override Page<DataAccess.Trace> Retrieves(PaginationOption po, DateTime? startTime, DateTime? endTime)
|
|
|
|
{
|
|
|
|
// filter
|
2019-03-18 17:26:12 +08:00
|
|
|
var filterBuilder = Builders<DataAccess.Trace>.Filter;
|
2019-03-16 20:24:33 +08:00
|
|
|
var filter = filterBuilder.Empty;
|
2019-03-18 17:26:12 +08:00
|
|
|
if (startTime.HasValue) filter = filterBuilder.Gt("LogTime", startTime.Value);
|
|
|
|
if (endTime.HasValue) filter = filterBuilder.Lt("LogTime", endTime.Value.AddDays(1).AddSeconds(-1));
|
|
|
|
if (startTime == null && endTime == null) filter = filterBuilder.Gt("LogTime", DateTime.Today.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod()));
|
|
|
|
|
|
|
|
// sort
|
|
|
|
var sortBuilder = Builders<DataAccess.Trace>.Sort;
|
|
|
|
SortDefinition<DataAccess.Trace> sort = null;
|
|
|
|
switch (po.Sort)
|
|
|
|
{
|
|
|
|
case "LogTime":
|
2019-03-16 20:24:33 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.LogTime) : sortBuilder.Descending(t => t.LogTime);
|
2019-03-18 17:26:12 +08:00
|
|
|
break;
|
|
|
|
case "IP":
|
2019-03-16 20:24:33 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.Ip) : sortBuilder.Descending(t => t.Ip);
|
2019-03-18 17:26:12 +08:00
|
|
|
break;
|
|
|
|
case "UserName":
|
2019-03-16 20:24:33 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.UserName) : sortBuilder.Descending(t => t.UserName);
|
2019-03-18 17:26:12 +08:00
|
|
|
break;
|
|
|
|
case "City":
|
2019-03-16 20:24:33 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.City) : sortBuilder.Descending(t => t.City);
|
2019-03-18 17:26:12 +08:00
|
|
|
break;
|
|
|
|
case "Browser":
|
2019-03-16 20:24:33 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.Browser) : sortBuilder.Descending(t => t.Browser);
|
2019-03-18 17:26:12 +08:00
|
|
|
break;
|
|
|
|
case "OS":
|
2019-03-16 20:24:33 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.OS) : sortBuilder.Descending(t => t.OS);
|
2019-03-18 17:26:12 +08:00
|
|
|
break;
|
|
|
|
case "RequestUrl":
|
2019-03-16 20:24:33 +08:00
|
|
|
sort = po.Order == "asc" ? sortBuilder.Ascending(t => t.RequestUrl) : sortBuilder.Descending(t => t.RequestUrl);
|
2019-03-18 17:26:12 +08:00
|
|
|
break;
|
2019-03-16 20:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var traces = DbManager.Traces.Find(filter).Sort(sort).ToList();
|
|
|
|
return new Page<DataAccess.Trace>()
|
|
|
|
{
|
|
|
|
Context = traces,
|
|
|
|
CurrentPage = po.PageIndex,
|
|
|
|
ItemsPerPage = po.Limit,
|
|
|
|
TotalItems = traces.Count,
|
|
|
|
TotalPages = (long)Math.Ceiling(traces.Count * 1.0 / po.Limit),
|
|
|
|
Items = traces.Skip(po.Offset).Take(po.Limit).ToList()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="p"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override bool Save(DataAccess.Trace p)
|
|
|
|
{
|
|
|
|
p.Id = null;
|
|
|
|
DbManager.Traces.InsertOne(p);
|
2019-03-18 17:26:12 +08:00
|
|
|
ClearTraces();
|
2019-03-16 20:24:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-18 17:26:12 +08:00
|
|
|
|
|
|
|
private static void ClearTraces()
|
|
|
|
{
|
|
|
|
System.Threading.Tasks.Task.Run(() =>
|
|
|
|
{
|
|
|
|
DbManager.Traces.DeleteMany(t => t.LogTime < DateTime.Now.AddMonths(0 - DictHelper.RetrieveAccessLogPeriod()));
|
|
|
|
});
|
|
|
|
}
|
2019-03-16 20:24:33 +08:00
|
|
|
}
|
|
|
|
}
|