2019-06-01 16:57:33 +08:00
|
|
|
|
using Longbow.Web.Mvc;
|
2019-03-16 20:24:33 +08:00
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using PetaPoco;
|
|
|
|
|
using System;
|
2019-05-25 14:52:14 +08:00
|
|
|
|
using System.Collections.Generic;
|
2019-03-16 20:24:33 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.MongoDB
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Trace : DataAccess.Trace
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2019-05-25 14:52:14 +08:00
|
|
|
|
public override Page<DataAccess.Trace> RetrievePages(PaginationOption po, DateTime? startTime, DateTime? endTime, string ip)
|
2019-03-16 20:24:33 +08:00
|
|
|
|
{
|
|
|
|
|
// 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));
|
2019-05-04 17:13:29 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(ip)) filter = filterBuilder.Eq("Ip", ip);
|
2019-03-18 17:26:12 +08:00
|
|
|
|
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()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-25 14:52:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="startTime"></param>
|
|
|
|
|
/// <param name="endTime"></param>
|
|
|
|
|
/// <param name="ip"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override IEnumerable<DataAccess.Trace> RetrieveAll(DateTime? startTime, DateTime? endTime, string ip)
|
|
|
|
|
{
|
|
|
|
|
var filterBuilder = Builders<DataAccess.Trace>.Filter;
|
|
|
|
|
var filter = filterBuilder.Empty;
|
|
|
|
|
if (startTime.HasValue) filter = filterBuilder.Gt("LogTime", startTime.Value);
|
|
|
|
|
if (endTime.HasValue) filter = filterBuilder.Lt("LogTime", endTime.Value.AddDays(1).AddSeconds(-1));
|
|
|
|
|
if (!string.IsNullOrEmpty(ip)) filter = filterBuilder.Eq("Ip", ip);
|
|
|
|
|
|
|
|
|
|
// sort
|
|
|
|
|
var sort = Builders<DataAccess.Trace>.Sort.Ascending(t => t.LogTime);
|
|
|
|
|
return DbManager.Traces.Find(filter).Sort(sort).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-16 20:24:33 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override bool Save(DataAccess.Trace p)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|