9 changed files with 138 additions and 153 deletions
@ -0,0 +1,65 @@ |
|||
using BBWY.Common.Models; |
|||
using BBWY.Server.Model.Db.Statistics; |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Linq; |
|||
using Yitter.IdGenerator; |
|||
|
|||
namespace BBWY.Server.Business |
|||
{ |
|||
public class APIExecutionTimesRecorder : BaseBusiness, IDenpendency |
|||
{ |
|||
private ConcurrentDictionary<string, int> apiDictionary; |
|||
public APIExecutionTimesRecorder(IFreeSql fsql, |
|||
NLogManager nLogManager, |
|||
IIdGenerator idGenerator) : base(fsql, nLogManager, idGenerator) |
|||
{ |
|||
this.apiDictionary = new ConcurrentDictionary<string, int>(); |
|||
} |
|||
|
|||
public void Record(string api) |
|||
{ |
|||
try |
|||
{ |
|||
apiDictionary.AddOrUpdate(api, 1, (k, v) => v + 1); |
|||
} |
|||
catch { } |
|||
} |
|||
|
|||
public void Flush() |
|||
{ |
|||
var currentRoundList = apiDictionary.Select(kv => new { Api = kv.Key, Count = kv.Value }); |
|||
apiDictionary.Clear(); |
|||
var currentRoundKeyList = currentRoundList.Select(x => x.Api).ToList(); |
|||
var today = DateTime.Now.Date; |
|||
var dbList = fsql.Select<ApiExecutionTimes>().Where(x => currentRoundKeyList.Contains(x.Api) && x.Date == today).ToList(); |
|||
|
|||
var insertList = currentRoundList.Where(x => !dbList.Any(y => y.Api == x.Api)).Select(x => new ApiExecutionTimes |
|||
{ |
|||
Id = idGenerator.NewLong(), |
|||
Api = x.Api, |
|||
Count = x.Count, |
|||
CreateTime = DateTime.Now, |
|||
Date = today, |
|||
UpdateTime = DateTime.Now |
|||
}).ToList(); |
|||
|
|||
var updateList = currentRoundList.Where(x => dbList.Any(y => y.Api == x.Api)) |
|||
.Select(x => fsql.Update<ApiExecutionTimes>() |
|||
.Set(a => a.Count + x.Count) |
|||
.Where(a => a.Api == x.Api && a.Date == today)) |
|||
.ToList(); |
|||
|
|||
fsql.Transaction(() => |
|||
{ |
|||
if (insertList.Count() > 0) |
|||
fsql.Insert(insertList).ExecuteAffrows(); |
|||
if (updateList.Count() > 0) |
|||
{ |
|||
foreach (var update in updateList) |
|||
update.ExecuteAffrows(); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,37 @@ |
|||
using FreeSql.DataAnnotations; |
|||
using System; |
|||
|
|||
namespace BBWY.Server.Model.Db.Statistics |
|||
{ |
|||
|
|||
[Table(Name = "apiexecutiontimes", DisableSyncStructure = true)] |
|||
public partial class ApiExecutionTimes |
|||
{ |
|||
|
|||
[Column(DbType = "bigint", IsPrimary = true)] |
|||
public long Id { get; set; } |
|||
|
|||
[Column(StringLength = 100)] |
|||
public string Api { get; set; } |
|||
|
|||
[Column(DbType = "bigint")] |
|||
public long? Count { get; set; } |
|||
|
|||
[Column(DbType = "datetime")] |
|||
public DateTime? CreateTime { get; set; } |
|||
|
|||
[Column(DbType = "datetime")] |
|||
public DateTime? Date { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 扩展信息
|
|||
/// </summary>
|
|||
[Column(StringLength = 100)] |
|||
public string ExtendInfo { get; set; } |
|||
|
|||
[Column(DbType = "datetime")] |
|||
public DateTime? UpdateTime { get; set; } |
|||
|
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue