mirror of
https://github.com/bitwarden/server.git
synced 2025-04-08 14:38:15 -05:00
batch events
This commit is contained in:
parent
675b22cc9f
commit
6a91fd6be9
@ -10,6 +10,7 @@ using Bit.Core.Models.Data;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
@ -295,9 +296,12 @@ namespace Bit.Core.Services
|
|||||||
|
|
||||||
await _cipherRepository.DeleteAsync(cipherIds, deletingUserId);
|
await _cipherRepository.DeleteAsync(cipherIds, deletingUserId);
|
||||||
|
|
||||||
var events = deletingCiphers.Select(c =>
|
var events = deletingCiphers.Select(c =>
|
||||||
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Deleted, null));
|
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Deleted, null));
|
||||||
await _eventService.LogCipherEventsAsync(events);
|
foreach(var eventsBatch in events.Batch(100))
|
||||||
|
{
|
||||||
|
await _eventService.LogCipherEventsAsync(eventsBatch);
|
||||||
|
}
|
||||||
|
|
||||||
// push
|
// push
|
||||||
await _pushService.PushSyncCiphersAsync(deletingUserId);
|
await _pushService.PushSyncCiphersAsync(deletingUserId);
|
||||||
@ -509,7 +513,10 @@ namespace Bit.Core.Services
|
|||||||
|
|
||||||
var events = ciphers.Select(c =>
|
var events = ciphers.Select(c =>
|
||||||
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Shared, null));
|
new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Shared, null));
|
||||||
await _eventService.LogCipherEventsAsync(events);
|
foreach(var eventsBatch in events.Batch(100))
|
||||||
|
{
|
||||||
|
await _eventService.LogCipherEventsAsync(eventsBatch);
|
||||||
|
}
|
||||||
|
|
||||||
// push
|
// push
|
||||||
await _pushService.PushSyncCiphersAsync(sharingUserId);
|
await _pushService.PushSyncCiphersAsync(sharingUserId);
|
||||||
|
@ -65,6 +65,32 @@ namespace Bit.Core.Utilities
|
|||||||
return new Guid(guidArray);
|
return new Guid(guidArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> source, int size)
|
||||||
|
{
|
||||||
|
T[] bucket = null;
|
||||||
|
var count = 0;
|
||||||
|
foreach(var item in source)
|
||||||
|
{
|
||||||
|
if(bucket == null)
|
||||||
|
{
|
||||||
|
bucket = new T[size];
|
||||||
|
}
|
||||||
|
bucket[count++] = item;
|
||||||
|
if(count != size)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
yield return bucket.Select(x => x);
|
||||||
|
bucket = null;
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
// Return the last bucket with all remaining elements
|
||||||
|
if(bucket != null && count > 0)
|
||||||
|
{
|
||||||
|
yield return bucket.Take(count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static DataTable ToGuidIdArrayTVP(this IEnumerable<Guid> ids)
|
public static DataTable ToGuidIdArrayTVP(this IEnumerable<Guid> ids)
|
||||||
{
|
{
|
||||||
return ids.ToArrayTVP("GuidId");
|
return ids.ToArrayTVP("GuidId");
|
||||||
|
@ -7,6 +7,7 @@ using Bit.Core.Enums;
|
|||||||
using Bit.Core.Models.Table;
|
using Bit.Core.Models.Table;
|
||||||
using Bit.Core.Repositories;
|
using Bit.Core.Repositories;
|
||||||
using Bit.Core.Services;
|
using Bit.Core.Services;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
using Bit.Events.Models;
|
using Bit.Events.Models;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
@ -74,7 +75,10 @@ namespace Bit.Events.Controllers
|
|||||||
}
|
}
|
||||||
if(cipherEvents.Any())
|
if(cipherEvents.Any())
|
||||||
{
|
{
|
||||||
await _eventService.LogCipherEventsAsync(cipherEvents);
|
foreach(var eventsBatch in cipherEvents.Batch(50))
|
||||||
|
{
|
||||||
|
await _eventService.LogCipherEventsAsync(eventsBatch);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return new OkResult();
|
return new OkResult();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user