Skip to content

Commit 38dffbc

Browse files
authored
Remove all old analytics defines. (#5168)
1 parent 564d51e commit 38dffbc

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

com.unity.ml-agents/Runtime/Analytics/InferenceAnalytics.cs

+7-11
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,34 @@ internal class InferenceAnalytics
4444
const int k_MaxNumberOfElements = 1000;
4545

4646

47+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
4748
/// <summary>
4849
/// Models that we've already sent events for.
4950
/// </summary>
5051
private static HashSet<NNModel> s_SentModels;
52+
#endif
5153

5254
static bool EnableAnalytics()
5355
{
56+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
5457
if (s_EventRegistered)
5558
{
5659
return true;
5760
}
5861

59-
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
6062
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey, k_EventVersion);
6163
if (result == AnalyticsResult.Ok)
6264
{
6365
s_EventRegistered = true;
6466
}
65-
#elif MLA_UNITY_ANALYTICS_MODULE
66-
AnalyticsResult result = AnalyticsResult.UnsupportedPlatform;
67-
if (result == AnalyticsResult.Ok)
68-
{
69-
s_EventRegistered = true;
70-
}
71-
#endif
7267
if (s_EventRegistered && s_SentModels == null)
7368
{
7469
s_SentModels = new HashSet<NNModel>();
7570
}
7671

72+
#else // no editor, no analytics
73+
s_EventRegistered = false;
74+
#endif
7775
return s_EventRegistered;
7876
}
7977

@@ -108,6 +106,7 @@ public static void InferenceModelSet(
108106
IList<IActuator> actuators
109107
)
110108
{
109+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
111110
// The event shouldn't be able to report if this is disabled but if we know we're not going to report
112111
// Lets early out and not waste time gathering all the data
113112
if (!IsAnalyticsEnabled())
@@ -127,13 +126,10 @@ IList<IActuator> actuators
127126
var data = GetEventForModel(nnModel, behaviorName, inferenceDevice, sensors, actionSpec, actuators);
128127
// Note - to debug, use JsonUtility.ToJson on the event.
129128
// Debug.Log(JsonUtility.ToJson(data, true));
130-
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
131129
if (AnalyticsUtils.s_SendEditorAnalytics)
132130
{
133131
EditorAnalytics.SendEventWithLimit(k_EventName, data, k_EventVersion);
134132
}
135-
#else
136-
return;
137133
#endif
138134
}
139135

com.unity.ml-agents/Runtime/Analytics/TrainingAnalytics.cs

+13-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace Unity.MLAgents.Analytics
1919
{
20-
internal class TrainingAnalytics
20+
internal static class TrainingAnalytics
2121
{
2222
const string k_VendorKey = "unity.ml-agents";
2323
const string k_TrainingEnvironmentInitializedEventName = "ml_agents_training_environment_initialized";
@@ -31,11 +31,6 @@ internal class TrainingAnalytics
3131
k_RemotePolicyInitializedEventName
3232
};
3333

34-
/// <summary>
35-
/// Whether or not we've registered this particular event yet
36-
/// </summary>
37-
static bool s_EventsRegistered = false;
38-
3934
/// <summary>
4035
/// Hourly limit for this event name
4136
/// </summary>
@@ -47,36 +42,40 @@ internal class TrainingAnalytics
4742
const int k_MaxNumberOfElements = 1000;
4843

4944
private static bool s_SentEnvironmentInitialized;
45+
46+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
47+
/// <summary>
48+
/// Whether or not we've registered this particular event yet
49+
/// </summary>
50+
static bool s_EventsRegistered = false;
51+
5052
/// <summary>
5153
/// Behaviors that we've already sent events for.
5254
/// </summary>
5355
private static HashSet<string> s_SentRemotePolicyInitialized;
5456
private static HashSet<string> s_SentTrainingBehaviorInitialized;
57+
#endif
5558

5659
private static Guid s_TrainingSessionGuid;
5760

5861
// These are set when the RpcCommunicator connects
5962
private static string s_TrainerPackageVersion = "";
6063
private static string s_TrainerCommunicationVersion = "";
6164

62-
static bool EnableAnalytics()
65+
internal static bool EnableAnalytics()
6366
{
64-
#if MLA_UNITY_ANALYTICS_MODULE
67+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
6568
if (s_EventsRegistered)
6669
{
6770
return true;
6871
}
6972
foreach (var eventName in s_EventNames)
7073
{
71-
#if UNITY_EDITOR
7274
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(eventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey);
7375
if (result != AnalyticsResult.Ok)
7476
{
7577
return false;
7678
}
77-
#else
78-
return false;
79-
#endif // UNITY_EDITOR
8079
}
8180
s_EventsRegistered = true;
8281

@@ -152,6 +151,7 @@ public static void RemotePolicyInitialized(
152151
IList<IActuator> actuators
153152
)
154153
{
154+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
155155
if (!IsAnalyticsEnabled())
156156
return;
157157

@@ -173,7 +173,6 @@ IList<IActuator> actuators
173173
// Debug.Log(
174174
// $"Would send event {k_RemotePolicyInitializedEventName} with body {JsonUtility.ToJson(data, true)}"
175175
// );
176-
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
177176
if (AnalyticsUtils.s_SendEditorAnalytics)
178177
{
179178
EditorAnalytics.SendEventWithLimit(k_RemotePolicyInitializedEventName, data);
@@ -196,6 +195,7 @@ internal static string ParseBehaviorName(string fullyQualifiedBehaviorName)
196195
[Conditional("MLA_UNITY_ANALYTICS_MODULE")]
197196
public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent tbiEvent)
198197
{
198+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
199199
if (!IsAnalyticsEnabled())
200200
return;
201201

@@ -219,13 +219,10 @@ public static void TrainingBehaviorInitialized(TrainingBehaviorInitializedEvent
219219
// Debug.Log(
220220
// $"Would send event {k_TrainingBehaviorInitializedEventName} with body {JsonUtility.ToJson(tbiEvent, true)}"
221221
// );
222-
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
223222
if (AnalyticsUtils.s_SendEditorAnalytics)
224223
{
225224
EditorAnalytics.SendEventWithLimit(k_TrainingBehaviorInitializedEventName, tbiEvent);
226225
}
227-
#else
228-
return;
229226
#endif
230227
}
231228

com.unity.ml-agents/Tests/Editor/Analytics/TrainingAnalyticsTest.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Unity.MLAgents.Actuators;
66
using Unity.MLAgents.Analytics;
77
using Unity.MLAgents.Policies;
8+
using UnityEditor;
89

910
namespace Unity.MLAgents.Tests.Analytics
1011
{
@@ -48,7 +49,6 @@ public void TestRemotePolicyEvent()
4849

4950
Assert.AreEqual(2, remotePolicyEvent.ActuatorInfos[0].NumContinuousActions);
5051
Assert.AreEqual(0, remotePolicyEvent.ActuatorInfos[0].NumDiscreteActions);
51-
5252
}
5353

5454
[Test]
@@ -68,5 +68,16 @@ public void TestRemotePolicy()
6868

6969
Academy.Instance.Dispose();
7070
}
71+
72+
[Test]
73+
public void TestEnableAnalytics()
74+
{
75+
#if UNITY_EDITOR && MLA_UNITY_ANALYTICS_MODULE
76+
Assert.IsTrue(EditorAnalytics.enabled == TrainingAnalytics.EnableAnalytics());
77+
#else
78+
Assert.IsFalse(TrainingAnalytics.EnableAnalytics());
79+
#endif
80+
81+
}
7182
}
7283
}

0 commit comments

Comments
 (0)