From c0428c3316405a31f7e495368ced0587f1f5432e Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 7 May 2023 23:02:36 +0200 Subject: [PATCH] Simplify Editor analytics code --- Source/Editor/Analytics/EditorAnalytics.cpp | 94 ++++++++++++------- Source/Editor/Analytics/EditorAnalytics.h | 2 - .../Analytics/EditorAnalyticsController.cpp | 69 -------------- .../Analytics/EditorAnalyticsController.h | 21 ----- 4 files changed, 61 insertions(+), 125 deletions(-) delete mode 100644 Source/Editor/Analytics/EditorAnalyticsController.cpp delete mode 100644 Source/Editor/Analytics/EditorAnalyticsController.h diff --git a/Source/Editor/Analytics/EditorAnalytics.cpp b/Source/Editor/Analytics/EditorAnalytics.cpp index 8e5ac4126..bd96436d7 100644 --- a/Source/Editor/Analytics/EditorAnalytics.cpp +++ b/Source/Editor/Analytics/EditorAnalytics.cpp @@ -1,19 +1,20 @@ // Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. #include "EditorAnalytics.h" -#include "EditorAnalyticsController.h" +#include "Editor/Editor.h" +#include "Editor/ProjectInfo.h" +#include "Editor/Cooker/GameCooker.h" #include "Engine/Threading/Threading.h" #include "Engine/Platform/FileSystem.h" #include "Engine/Core/Log.h" #include "Engine/Core/Math/Vector2.h" #include "Engine/Core/Types/DateTime.h" #include "Engine/Core/Types/TimeSpan.h" -#include "Editor/Editor.h" -#include "Editor/ProjectInfo.h" #include "Engine/Engine/EngineService.h" #include "Engine/Engine/Globals.h" #include "Engine/Graphics/GPUDevice.h" #include "Engine/Utilities/StringConverter.h" +#include "Engine/ShadowsOfMordor/Builder.h" #include "FlaxEngine.Gen.h" #include @@ -34,8 +35,53 @@ namespace EditorAnalyticsImpl CriticalSection Locker; bool IsSessionActive = false; - EditorAnalyticsController Controller; - Array TmpBuffer; +} + +void RegisterGameCookingStart(GameCooker::EventType type) +{ + auto& data = *GameCooker::GetCurrentData(); + auto platform = ToString(data.Platform); + if (type == GameCooker::EventType::BuildStarted) + { + EditorAnalytics::SendEvent("Actions", "GameCooker.Start", platform); + } + else if (type == GameCooker::EventType::BuildFailed) + { + EditorAnalytics::SendEvent("Actions", "GameCooker.Failed", platform); + } + else if (type == GameCooker::EventType::BuildDone) + { + EditorAnalytics::SendEvent("Actions", "GameCooker.End", platform); + } +} + +void RegisterLightmapsBuildingStart() +{ + EditorAnalytics::SendEvent("Actions", "ShadowsOfMordor.Build", "ShadowsOfMordor.Build"); +} + +void RegisterError(LogType type, const StringView& msg) +{ + if (type == LogType::Error && false) + { + String value = msg.ToString(); + const int32 MaxLength = 300; + if (msg.Length() > MaxLength) + value = value.Substring(0, MaxLength); + value.Replace('\n', ' '); + value.Replace('\r', ' '); + EditorAnalytics::SendEvent("Errors", "Log.Error", value); + } + else if (type == LogType::Fatal) + { + String value = msg.ToString(); + const int32 MaxLength = 300; + if (msg.Length() > MaxLength) + value = value.Substring(0, MaxLength); + value.Replace('\n', ' '); + value.Replace('\r', ' '); + EditorAnalytics::SendEvent("Errors", "Log.Fatal", value); + } } using namespace EditorAnalyticsImpl; @@ -43,7 +89,6 @@ using namespace EditorAnalyticsImpl; class EditorAnalyticsService : public EngineService { public: - EditorAnalyticsService() : EngineService(TEXT("Editor Analytics")) { @@ -63,7 +108,6 @@ bool EditorAnalytics::IsSessionActive() void EditorAnalytics::StartSession() { ScopeLock lock(Locker); - if (EditorAnalyticsImpl::IsSessionActive) return; @@ -140,7 +184,10 @@ void EditorAnalytics::StartSession() EditorAnalyticsImpl::IsSessionActive = true; - Controller.Init(); + // Bind events + GameCooker::OnEvent.Bind(); + ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Bind(); + Log::Logger::OnError.Bind(); // Report GPU model if (GPU.HasChars()) @@ -152,11 +199,13 @@ void EditorAnalytics::StartSession() void EditorAnalytics::EndSession() { ScopeLock lock(Locker); - if (!EditorAnalyticsImpl::IsSessionActive) return; - Controller.Cleanup(); + // Unbind events + GameCooker::OnEvent.Unbind(); + ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Unbind(); + Log::Logger::OnError.Unbind(); StringAnsi sessionLength = StringAnsi::Format("{0}", (int32)(DateTime::Now() - SessionStartTime).GetTotalSeconds()); @@ -197,7 +246,6 @@ void EditorAnalytics::EndSession() void EditorAnalytics::SendEvent(const char* category, const char* name, const char* label) { ScopeLock lock(Locker); - if (!EditorAnalyticsImpl::IsSessionActive) return; @@ -209,7 +257,6 @@ void EditorAnalytics::SendEvent(const char* category, const char* name, const ch { UA_EVENT_LABEL, 0, (char*)label }, } }; - sendTracking(Tracker, UA_EVENT, &opts); } @@ -221,28 +268,10 @@ void EditorAnalytics::SendEvent(const char* category, const char* name, const St void EditorAnalytics::SendEvent(const char* category, const char* name, const Char* label) { ScopeLock lock(Locker); - if (!EditorAnalyticsImpl::IsSessionActive) return; - - ASSERT(category && name && label); - - const int32 labelLength = StringUtils::Length(label); - TmpBuffer.Clear(); - TmpBuffer.Resize(labelLength + 1); - StringUtils::ConvertUTF162ANSI(label, TmpBuffer.Get(), labelLength); - TmpBuffer[labelLength] = 0; - - UAOptions opts = - { - { - { UA_EVENT_CATEGORY, 0, (char*)category }, - { UA_EVENT_ACTION, 0, (char*)name }, - { UA_EVENT_LABEL, 0, (char*)TmpBuffer.Get() }, - } - }; - - sendTracking(Tracker, UA_EVENT, &opts); + const StringAsANSI<> labelAnsi(label); + SendEvent(category, name, labelAnsi.Get()); } bool EditorAnalyticsService::Init() @@ -265,7 +294,6 @@ bool EditorAnalyticsService::Init() } LOG(Info, "Editor analytics service is enabled. Curl version: {0}", TEXT(LIBCURL_VERSION)); - EditorAnalytics::StartSession(); return false; diff --git a/Source/Editor/Analytics/EditorAnalytics.h b/Source/Editor/Analytics/EditorAnalytics.h index 6a16020f2..ac693aa13 100644 --- a/Source/Editor/Analytics/EditorAnalytics.h +++ b/Source/Editor/Analytics/EditorAnalytics.h @@ -10,11 +10,9 @@ class EditorAnalytics { public: - /// /// Determines whether analytics session is active. /// - /// true if there is active analytics session running; otherwise, false. static bool IsSessionActive(); /// diff --git a/Source/Editor/Analytics/EditorAnalyticsController.cpp b/Source/Editor/Analytics/EditorAnalyticsController.cpp deleted file mode 100644 index 0e1e461cf..000000000 --- a/Source/Editor/Analytics/EditorAnalyticsController.cpp +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. - -#include "EditorAnalyticsController.h" -#include "Editor/Cooker/GameCooker.h" -#include "EditorAnalytics.h" -#include "Engine/ShadowsOfMordor/Builder.h" - -void RegisterGameCookingStart(GameCooker::EventType type) -{ - auto& data = *GameCooker::GetCurrentData(); - auto platform = ToString(data.Platform); - if (type == GameCooker::EventType::BuildStarted) - { - EditorAnalytics::SendEvent("Actions", "GameCooker.Start", platform); - } - else if (type == GameCooker::EventType::BuildFailed) - { - EditorAnalytics::SendEvent("Actions", "GameCooker.Failed", platform); - } - else if (type == GameCooker::EventType::BuildDone) - { - EditorAnalytics::SendEvent("Actions", "GameCooker.End", platform); - } -} - -void RegisterLightmapsBuildingStart() -{ - EditorAnalytics::SendEvent("Actions", "ShadowsOfMordor.Build", "ShadowsOfMordor.Build"); -} - -void RegisterError(LogType type, const StringView& msg) -{ - if (type == LogType::Error && false) - { - String value = msg.ToString(); - const int32 MaxLength = 300; - if (msg.Length() > MaxLength) - value = value.Substring(0, MaxLength); - value.Replace('\n', ' '); - value.Replace('\r', ' '); - - EditorAnalytics::SendEvent("Errors", "Log.Error", value); - } - else if (type == LogType::Fatal) - { - String value = msg.ToString(); - const int32 MaxLength = 300; - if (msg.Length() > MaxLength) - value = value.Substring(0, MaxLength); - value.Replace('\n', ' '); - value.Replace('\r', ' '); - - EditorAnalytics::SendEvent("Errors", "Log.Fatal", value); - } -} - -void EditorAnalyticsController::Init() -{ - GameCooker::OnEvent.Bind(); - ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Bind(); - Log::Logger::OnError.Bind(); -} - -void EditorAnalyticsController::Cleanup() -{ - GameCooker::OnEvent.Unbind(); - ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Unbind(); - Log::Logger::OnError.Unbind(); -} diff --git a/Source/Editor/Analytics/EditorAnalyticsController.h b/Source/Editor/Analytics/EditorAnalyticsController.h deleted file mode 100644 index 71656caee..000000000 --- a/Source/Editor/Analytics/EditorAnalyticsController.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. - -#pragma once - -/// -/// The controller object for the tracking events for the editor analytics. -/// -class EditorAnalyticsController -{ -public: - - /// - /// Starts the service (registers to event handlers). - /// - void Init(); - - /// - /// Ends the service (unregisters to event handlers). - /// - void Cleanup(); -};