Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 72 additions & 3 deletions Runtime/Api/Analytics/GameTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class GameTelemetry : WrapperBase

IAccelByteDataStorage cacheStorage;
string cacheTableName;
private bool useCache;
private WaitTimeCommand telemetryLoopCommand;

internal TimeSpan TelemetryInterval
Expand All @@ -54,6 +55,7 @@ internal GameTelemetry( ClientGameTelemetryApi inApi

session = inSession;

useCache = true;
cacheStorage = session.DataStorage;
cacheTableName = $"GameTelemetryCache/{AccelByteSDK.Environment.Current}.cache";

Expand Down Expand Up @@ -100,6 +102,13 @@ public void SetBatchFrequency( TimeSpan interval )
$"Set to {minimumTelemetryInterval.TotalSeconds} seconds.");
telemetryInterval = minimumTelemetryInterval;
}

if (cancelationTokenSource != null)
{
cancelationTokenSource.Cancel();
cancelationTokenSource.Dispose();
InitializeTelemetryLoop();
}
}

/// <summary>
Expand All @@ -113,6 +122,24 @@ public void SetBatchFrequency(int intervalSeconds)
SetBatchFrequency(TimeSpan.FromSeconds(intervalSeconds));
}

/// <summary>
/// Enable or disable the usage of the file storage cache.
/// The cache stores all events in JSON before being sent in case
/// of crash or errors and attempts to re-send them when it can.
/// The cache is enabled by default.
/// </summary>
/// <param name="shouldUseCache">Enables or disables the usage of the cache.</param>
public void SetUseCache(bool shouldUseCache)
{
useCache = shouldUseCache;

if (!useCache)
{
eventList.Clear();
DeleteCache();
}
}

/// <summary>
/// Set list of event that need to be sent immediately without the needs to jobQueue it.
/// </summary>
Expand Down Expand Up @@ -147,7 +174,11 @@ public void Send( TelemetryBody telemetryBody
else
{
jobQueue.Enqueue(new Tuple<TelemetryBody, ResultCallback>(telemetryBody, callback));
AppendEventToCache(telemetryBody);

if (useCache)
{
AppendEventToCache(telemetryBody);
}

if (cancelationTokenSource == null)
{
Expand Down Expand Up @@ -203,7 +234,10 @@ public void SendTelemetryBatch(ResultCallback callback)
}
else
{
RemoveEventsFromCache();
if (useCache)
{
RemoveEventsFromCache();
}
}

foreach (var telemetryCallback in telemetryCallbacks)
Expand All @@ -222,6 +256,11 @@ internal override void SetSharedMemory(ApiSharedMemory newSharedMemory)

private void SendCachedTelemetry()
{
if (!useCache)
{
return;
}

LoadEventsFromCache(telemetryBodies =>
{
SendTelemetryRequest(telemetryBodies, cb =>
Expand Down Expand Up @@ -250,7 +289,11 @@ private void RunPeriodicTelemetry()
}

SendTelemetryBatch(callback: null);

InitializeTelemetryLoop();
}

private void InitializeTelemetryLoop()
{
cancelationTokenSource = new CancellationTokenSource();
telemetryLoopCommand = new WaitTimeCommand(waitTime: telemetryInterval.TotalSeconds
, cancellationToken: cancelationTokenSource.Token
Expand Down Expand Up @@ -290,6 +333,11 @@ private void TelemetryLoop()

internal void LoadEventsFromCache(Action<List<TelemetryBody>> onLoadCacheDone)
{
if (!useCache)
{
onLoadCacheDone?.Invoke(null);
return;
}
if (!session.IsValid())
{
onLoadCacheDone?.Invoke(null);
Expand All @@ -301,6 +349,12 @@ internal void LoadEventsFromCache(Action<List<TelemetryBody>> onLoadCacheDone)

internal void LoadEventsFromCache(string key, Action<List<TelemetryBody>> onLoadCacheDone)
{
if (!useCache)
{
onLoadCacheDone?.Invoke(null);
return;
}

cacheStorage.GetItem(
key
, tableName: cacheTableName
Expand Down Expand Up @@ -328,6 +382,11 @@ internal void LoadEventsFromCache(string key, Action<List<TelemetryBody>> onLoad

internal void AppendEventToCache(TelemetryBody telemetryBody, Action<bool> onSaveCacheDone = null)
{
if (!useCache)
{
onSaveCacheDone?.Invoke(false);
return;
}
if (!session.IsValid())
{
onSaveCacheDone?.Invoke(false);
Expand All @@ -339,6 +398,12 @@ internal void AppendEventToCache(TelemetryBody telemetryBody, Action<bool> onSav

internal void AppendEventToCache(string key, TelemetryBody telemetryBody, Action<bool> onSaveCacheDone = null)
{
if (!useCache)
{
onSaveCacheDone?.Invoke(false);
return;
}

eventList.Add(telemetryBody);
var telemetryEventsJson = System.Text.Encoding.UTF8.GetString(eventList.ToUtf8Json());
cacheStorage.SaveItem(key, telemetryEventsJson, onSaveCacheDone, tableName: cacheTableName);
Expand All @@ -351,6 +416,10 @@ internal void DeleteCache(Action<bool> onDone = null)

private void RemoveEventsFromCache()
{
if (!useCache)
{
return;
}
if (session != null && !session.IsValid())
{
return;
Expand Down
1 change: 0 additions & 1 deletion Runtime/Server/Analytics/ServerGameTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ private void RunPeriodicTelemetry()
}

SendTelemetryBatch(callback: null);

cancelationTokenSource = new System.Threading.CancellationTokenSource();
IndefiniteLoopCommand telemetryLoopCommand = new IndefiniteLoopCommand(interval: telemetryInterval.TotalSeconds
, cancellationToken: cancelationTokenSource.Token
Expand Down