DocSpace-client/web/ASC.Web.Core/Statistic/StatisticManager.cs

178 lines
6.7 KiB
C#
Raw Normal View History

2019-08-12 10:53:12 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections.Generic;
using System.Data;
2019-12-06 16:01:12 +00:00
using System.Linq;
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-12-06 16:01:12 +00:00
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Common.EF.Model;
using Microsoft.EntityFrameworkCore;
2019-08-12 10:53:12 +00:00
namespace ASC.Web.Studio.Core.Statistic
{
public class StatisticManager
{
private static DateTime lastSave = DateTime.UtcNow;
private static readonly TimeSpan cacheTime = TimeSpan.FromMinutes(2);
private static readonly IDictionary<string, UserVisit> cache = new Dictionary<string, UserVisit>();
2020-08-12 09:58:08 +00:00
private WebstudioDbContext WebstudioDbContext { get; }
2019-08-12 10:53:12 +00:00
2019-12-11 09:47:55 +00:00
public StatisticManager(DbContextManager<WebstudioDbContext> dbContextManager)
2019-10-10 08:52:21 +00:00
{
2019-12-06 16:01:12 +00:00
WebstudioDbContext = dbContextManager.Value;
2019-10-10 08:52:21 +00:00
}
public void SaveUserVisit(int tenantID, Guid userID, Guid productID)
2019-08-12 10:53:12 +00:00
{
var now = DateTime.UtcNow;
var key = string.Format("{0}|{1}|{2}|{3}", tenantID, userID, productID, now.Date);
lock (cache)
{
var visit = cache.ContainsKey(key) ?
cache[key] :
new UserVisit
{
TenantID = tenantID,
UserID = userID,
ProductID = productID,
VisitDate = now
};
visit.VisitCount++;
visit.LastVisitTime = now;
cache[key] = visit;
}
if (cacheTime < DateTime.UtcNow - lastSave)
{
FlushCache();
}
}
2019-10-10 08:52:21 +00:00
public List<Guid> GetVisitorsToday(int tenantID, Guid productID)
2019-08-12 10:53:12 +00:00
{
2019-12-06 16:01:12 +00:00
var users = WebstudioDbContext.WebstudioUserVisit
.Where(r => r.VisitDate == DateTime.UtcNow.Date)
.Where(r => r.TenantId == tenantID)
.Where(r => r.ProductId == productID)
.OrderBy(r => r.FirstVisitTime)
.GroupBy(r => r.UserId)
.Select(r => r.Key)
.ToList();
2019-08-15 15:08:40 +00:00
lock (cache)
2019-08-12 10:53:12 +00:00
{
2019-08-15 15:08:40 +00:00
foreach (var visit in cache.Values)
2019-08-12 10:53:12 +00:00
{
2019-08-15 15:08:40 +00:00
if (!users.Contains(visit.UserID) && visit.VisitDate.Date == DateTime.UtcNow.Date)
2019-08-12 10:53:12 +00:00
{
2019-08-15 15:08:40 +00:00
users.Add(visit.UserID);
2019-08-12 10:53:12 +00:00
}
}
}
2019-08-15 15:08:40 +00:00
return users;
2019-08-12 10:53:12 +00:00
}
2019-10-10 08:52:21 +00:00
public List<UserVisit> GetHitsByPeriod(int tenantID, DateTime startDate, DateTime endPeriod)
2019-08-12 10:53:12 +00:00
{
2019-12-06 16:01:12 +00:00
return WebstudioDbContext.WebstudioUserVisit
.Where(r => r.TenantId == tenantID)
.Where(r => r.VisitDate >= startDate && r.VisitDate <= endPeriod)
.OrderBy(r => r.VisitDate)
.GroupBy(r => r.VisitDate)
.Select(r => new UserVisit { VisitDate = r.Key, VisitCount = r.Sum(a => a.VisitCount) })
.ToList();
2019-08-12 10:53:12 +00:00
}
2019-10-10 08:52:21 +00:00
public List<UserVisit> GetHostsByPeriod(int tenantID, DateTime startDate, DateTime endPeriod)
2019-08-12 10:53:12 +00:00
{
2019-12-06 16:01:12 +00:00
return
WebstudioDbContext.WebstudioUserVisit
.Where(r => r.TenantId == tenantID)
.Where(r => r.VisitDate >= startDate && r.VisitDate <= endPeriod)
.OrderBy(r => r.VisitDate)
.GroupBy(r => new { r.UserId, r.VisitDate })
.Select(r => new UserVisit { VisitDate = r.Key.VisitDate, UserID = r.Key.UserId })
.ToList();
2019-08-12 10:53:12 +00:00
}
2019-10-10 08:52:21 +00:00
private void FlushCache()
2019-08-12 10:53:12 +00:00
{
if (cache.Count == 0) return;
List<UserVisit> visits;
lock (cache)
{
visits = new List<UserVisit>(cache.Values);
cache.Clear();
lastSave = DateTime.UtcNow;
}
2019-12-06 16:01:12 +00:00
using var tx = WebstudioDbContext.Database.BeginTransaction(IsolationLevel.ReadUncommitted);
2019-08-15 15:08:40 +00:00
foreach (var v in visits)
2019-08-12 10:53:12 +00:00
{
2019-12-06 16:01:12 +00:00
var w = new DbWebstudioUserVisit
2019-08-15 15:08:40 +00:00
{
TenantId = v.TenantID,
2019-12-06 16:01:12 +00:00
ProductId = v.ProductID,
UserId = v.UserID,
2019-08-15 15:08:40 +00:00
VisitDate = v.VisitDate.Date,
FirstVisitTime = v.VisitDate,
2019-12-06 16:01:12 +00:00
VisitCount = v.VisitCount
};
if (v.LastVisitTime.HasValue)
{
w.LastVisitTime = v.LastVisitTime.Value;
}
WebstudioDbContext.WebstudioUserVisit.Add(w);
WebstudioDbContext.SaveChanges();
2019-08-12 10:53:12 +00:00
}
2019-08-15 15:08:40 +00:00
tx.Commit();
2019-08-12 10:53:12 +00:00
}
}
2019-10-31 13:54:43 +00:00
public static class StatisticManagerExtension
2019-10-31 13:54:43 +00:00
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddStatisticManagerService(this DIHelper services)
2019-10-31 13:54:43 +00:00
{
2020-07-17 10:52:28 +00:00
if (services.TryAddScoped<StatisticManager>())
{
return services.AddWebstudioDbContextService();
}
2019-10-31 13:54:43 +00:00
2020-07-17 10:52:28 +00:00
return services;
2019-10-31 13:54:43 +00:00
}
}
2019-08-12 10:53:12 +00:00
}