-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding some smarter defaults for persistence of data #25
Changes from 3 commits
e0d7e94
4262c34
5bf056c
738f5b9
6265d81
eb89092
6350e75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
| ||
using System; | ||
using System.IO; | ||
using Segment.Analytics.Utilities; | ||
using Segment.Concurrent; | ||
|
||
|
@@ -33,11 +35,12 @@ public class Configuration | |
/// </summary> | ||
/// <param name="writeKey">the Segment writeKey</param> | ||
/// <param name="persistentDataPath"> | ||
/// path where analytics stores data. for example: | ||
/// path where analytics stores data when using a storage provider that writes to disk. for example: | ||
/// <list type="bullet"> | ||
/// <item><description>Xamarin: <c>Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)</c></description></item> | ||
/// <item><description>Unity: <c>Application.persistentDataPath</c></description></item> | ||
/// </list> | ||
/// defaults to Local Application Data | ||
/// </param> | ||
/// <param name="flushAt">count of events at which we flush events, defaults to <c>20</c></param> | ||
/// <param name="flushInterval">interval in seconds at which we flush events, defaults to <c>30 seconds</c></param> | ||
|
@@ -49,12 +52,13 @@ public class Configuration | |
/// <param name="exceptionHandler">set an exception handler to handle errors happened in async methods within the analytics scope</param> | ||
/// <param name="storageProvider">set a storage provide to tell the analytics where to store your data: | ||
/// <list type="bullet"> | ||
/// <item><description><see cref="InMemoryStorageProvider"/> stores data only in memory</description></item> | ||
/// <item><description><see cref="InMemoryStorageProvider"/> stores data only in memory and ignores the persistentDataPath</description></item> | ||
/// <item><description><see cref="DefaultStorageProvider"/> persists data in local disk. This is used by default</description></item> | ||
/// </list> | ||
/// defaults to DefaultStorageProvider on Unity (Mono) and Xamarin, or to InMemoryStorageProvider on .Net Core | ||
/// </param> | ||
public Configuration(string writeKey, | ||
string persistentDataPath, | ||
string persistentDataPath = null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now that we have a way to provide |
||
int flushAt = 20, | ||
int flushInterval = 30, | ||
Settings defaultSettings = new Settings(), | ||
|
@@ -65,8 +69,10 @@ public Configuration(string writeKey, | |
ICoroutineExceptionHandler exceptionHandler = null, | ||
IStorageProvider storageProvider = default) | ||
{ | ||
var platform = SystemInfo.getPlatform(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like this variable is unused? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, thanks. |
||
|
||
this.writeKey = writeKey; | ||
this.persistentDataPath = persistentDataPath; | ||
this.persistentDataPath = persistentDataPath ?? SystemInfo.getAppFolder(); | ||
this.flushAt = flushAt; | ||
this.flushInterval = flushInterval; | ||
this.defaultSettings = defaultSettings; | ||
|
@@ -75,7 +81,19 @@ public Configuration(string writeKey, | |
this.apiHost = apiHost; | ||
this.cdnHost = cdnHost; | ||
this.exceptionHandler = exceptionHandler; | ||
this.storageProvider = storageProvider ?? new DefaultStorageProvider(); | ||
|
||
if (storageProvider != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the more I think the more I feel we probably only need to smart default persist data path. we should always to default to use storage unless the user specifies to use in memory. what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm in favor. More consistent behavior, fewer assumptions and surprises. |
||
{ | ||
this.storageProvider = storageProvider; | ||
} | ||
else if (platform.Contains("Mono") || platform.Contains("Xamarin")) | ||
{ | ||
this.storageProvider = new DefaultStorageProvider(); | ||
} | ||
else | ||
{ | ||
this.storageProvider = new InMemoryStorageProvider(); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Segment.Analytics.Utilities | ||
{ | ||
public class SystemInfo | ||
{ | ||
public static string getAppFolder() | ||
{ | ||
var type = Type.GetType("UnityEngine.Application, UnityEngine"); | ||
string unityPath = type?.GetProperty("persistentDataPath").GetValue(null,null).ToString(); | ||
if(unityPath != null) | ||
{ | ||
return unityPath; | ||
} | ||
|
||
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); | ||
} | ||
|
||
public static string getPlatform() | ||
{ | ||
var type = ""; | ||
|
||
if (Type.GetType("Xamarin.Forms.Device") != null) | ||
{ | ||
type = "Xamarin"; | ||
} | ||
else if (Type.GetType("UnityEngine.Device.SystemInfo, UnityEngine") != null) | ||
{ | ||
type = "Unity"; | ||
} | ||
else | ||
{ | ||
var descr = RuntimeInformation.FrameworkDescription; | ||
var platf = descr.Substring(0, descr.LastIndexOf(' ')); | ||
|
||
type = platf; | ||
} | ||
return type; | ||
} | ||
|
||
public static string getOS() | ||
{ | ||
OperatingSystem os = Environment.OSVersion; | ||
var vs = os.Version; | ||
|
||
string operatingSystem = ""; | ||
|
||
switch (os.Platform) | ||
{ | ||
|
||
case PlatformID.Win32S: | ||
operatingSystem = "Win32S"; | ||
break; | ||
case PlatformID.Win32Windows: | ||
operatingSystem = "Win32Windows"; | ||
break; | ||
case PlatformID.Win32NT: | ||
operatingSystem = "Win32NT"; | ||
break; | ||
case PlatformID.WinCE: | ||
operatingSystem = "WinCE"; | ||
break; | ||
case PlatformID.Unix: | ||
operatingSystem = "Unix"; | ||
break; | ||
case PlatformID.Xbox: | ||
operatingSystem = "Xbox"; | ||
break; | ||
case PlatformID.MacOSX: | ||
operatingSystem = "MacOSX"; | ||
break; | ||
default: | ||
break; | ||
} | ||
return operatingSystem + " - " + vs.Major + "." + vs.Minor; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using Segment.Analytics.Utilities; | ||
using Xunit; | ||
|
||
namespace Tests.Utilities | ||
{ | ||
public class SystemInfoTest | ||
{ | ||
|
||
[Fact] | ||
public void GetPlatformTest() | ||
{ | ||
var sysinfo = SystemInfo.getPlatform(); | ||
|
||
Assert.NotNull(sysinfo); | ||
Assert.Contains(".NET", sysinfo); | ||
} | ||
|
||
[Fact] | ||
public void GetOSTest() | ||
{ | ||
var sysinfo = SystemInfo.getOS(); | ||
|
||
Assert.NotNull(sysinfo); | ||
} | ||
|
||
[Fact] | ||
public void GetAppFolderTest() | ||
{ | ||
var path = SystemInfo.getAppFolder(); | ||
|
||
Assert.Equal(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), path); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to remove this comment block too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done