using Bootstrap.DataAccess;
using System.Collections.Generic;
using System.Web.Http;
using System.Linq;
using System;
namespace Bootstrap.Admin.Controllers
{
public class NotificationsController : ApiController
{
///
///
///
///
[HttpGet]
public Notifications Get()
{
var ret = new Notifications();
// New Users
var user = UserHelper.RetrieveNewUsers();
ret.Users = user.Take(6).OrderByDescending(u => u.RegisterTime).ToList();
ret.Users.AsParallel().ForAll(n =>
{
var ts = DateTime.Now - n.RegisterTime;
if (ts.TotalMinutes < 5) n.Period = "刚刚";
else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days);
else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
});
ret.NewUsersCount = user.Count();
// Tasks
var task = TaskHelper.RetrieveTasks();
ret.Tasks = task.Take(6).OrderByDescending(u => u.AssignTime).ToList();
ret.TasksCount = task.Count();
//Message
var message = MessageHelper.RetrieveMessagesHeader(User.Identity.Name);
ret.Messages = message.Take(6).ToList();
ret.Messages.AsParallel().ForAll(m => m.FromIcon = Url.Content(m.FromIcon));
ret.MessagesCount = message.Count();
//Apps
var apps = ExceptionHelper.RetrieveExceptions().Where(n => n.ExceptionType != "Longbow.Data.DBAccessException");
ret.Apps = apps.Take(6).OrderByDescending(a => a.LogTime).ToList();
ret.Apps.AsParallel().ForAll(n =>
{
n.ExceptionType = n.ExceptionType.Split('.').Last();
var ts = DateTime.Now - n.LogTime;
if (ts.TotalMinutes < 5) n.Period = "刚刚";
else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days);
else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
});
ret.AppExceptionsCount = apps.Count();
//Dbs
var dbs = ExceptionHelper.RetrieveExceptions().Where(n => n.ExceptionType == "Longbow.Data.DBAccessException");
ret.Dbs = dbs.Take(6).OrderByDescending(d => d.LogTime).ToList();
ret.Dbs.AsParallel().ForAll(n =>
{
var ts = DateTime.Now - n.LogTime;
if (ts.TotalMinutes < 5) n.Period = "刚刚";
else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days);
else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
});
ret.DbExceptionsCount = dbs.Count();
return ret;
}
///
///
///
///
///
[HttpGet]
public Notifications Get(string id)
{
var ret = new Notifications();
if (id == "newusers" || id == "all") ret.Users = UserHelper.RetrieveNewUsers().OrderByDescending(u => u.RegisterTime).ToList();
return ret;
}
public class Notifications
{
public Notifications()
{
Users = new List();
Apps = new List();
Dbs = new List();
Tasks = new List();
Messages = new List();
}
///
///
///
public List Users { get; set; }
///
///
///
public List Apps { get; set; }
///
///
///
public List Dbs { get; set; }
///
///
///
public List Tasks { get; set; }
///
///
///
public List Messages { get; set; }
///
/// 获得/设置 消息数量
///
public int MessagesCount { get; set; }
///
/// 获得/设置 新用户数量
///
public int NewUsersCount { get; set; }
///
/// 获取/设置 任务数量
///
public int TasksCount { get; set; }
///
/// 获取/设置 应用程序错误数量
///
public int AppExceptionsCount { get; set; }
///
/// 获取/设置 数据库错误数量
///
public int DbExceptionsCount { get; set; }
}
}
}