cz.xprees.event-logging 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Threading;
4
+ using Cysharp.Threading.Tasks;
5
+ using UnityEngine;
6
+ using UnityEngine.Networking;
7
+ using Xprees.EventLogging.Api.Model;
8
+ using Xprees.EventLogging.Extensions;
9
+
10
+ namespace Xprees.EventLogging.Api
11
+ {
12
+ public class EventLoggingApi : IEventLoggingApi
13
+ {
14
+ public const string DefaultEndpoint = "https://eventlog-service-phkfk465ha-ew.a.run.app";
15
+
16
+ private readonly string _baseUrl;
17
+ private string LogsUri => $"{_baseUrl}/logs";
18
+ private string LogsBatchUri => $"{_baseUrl}/logs/batch";
19
+
20
+ public EventLoggingApi(string baseUrl = DefaultEndpoint)
21
+ {
22
+ if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out _))
23
+ {
24
+ Debug.LogError($"{nameof(EventLoggingApi)} - Created with invalid URL: \"{baseUrl}\"\n"
25
+ + $"Fallback to default Endpoint: \"{DefaultEndpoint}\"");
26
+ baseUrl = DefaultEndpoint; // Fall back to default Endpoint
27
+ }
28
+
29
+ _baseUrl = baseUrl;
30
+ }
31
+
32
+ public async UniTask<bool> Warmup(CancellationToken cancellationToken = default)
33
+ {
34
+ try
35
+ {
36
+ var request = UnityWebRequest.Get(_baseUrl);
37
+ await request.SendWebRequestAsync(cancellationToken);
38
+ return request.result == UnityWebRequest.Result.Success;
39
+ }
40
+ catch
41
+ {
42
+ // ignore
43
+ }
44
+
45
+ return false;
46
+ }
47
+
48
+ public async UniTask<bool> SendEventLog(EventLog log, CancellationToken cancellationToken = default)
49
+ {
50
+ var request = new UnityWebRequest(LogsUri, UnityWebRequest.kHttpVerbPOST);
51
+ request.AddJsonBody(log);
52
+ await request.SendWebRequestAsync(cancellationToken);
53
+ return request.result == UnityWebRequest.Result.Success;
54
+ }
55
+
56
+ public async UniTask<bool> SendEventLogsBatch(List<EventLog> logs, CancellationToken cancellationToken = default)
57
+ {
58
+ var batchRequest = new UnityWebRequest(LogsBatchUri, UnityWebRequest.kHttpVerbPOST);
59
+ batchRequest.AddJsonBody(logs);
60
+ await batchRequest.SendWebRequestAsync(cancellationToken);
61
+ return batchRequest.result == UnityWebRequest.Result.Success;
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: fd34413abc7046d5b998358c126e4ba8
3
+ timeCreated: 1678722796
@@ -0,0 +1,20 @@
1
+ using System.Collections.Generic;
2
+ using System.Threading;
3
+ using Cysharp.Threading.Tasks;
4
+ using Xprees.EventLogging.Api.Model;
5
+
6
+ namespace Xprees.EventLogging.Api
7
+ {
8
+ public interface IEventLoggingApi
9
+ {
10
+ /// <summary>
11
+ /// Call this method before logging start to warm up the connection (otherwise the first log will be slow)
12
+ /// </summary>
13
+ /// <returns>Success</returns>
14
+ UniTask<bool> Warmup(CancellationToken cancellationToken = default);
15
+
16
+ UniTask<bool> SendEventLog(EventLog log, CancellationToken cancellationToken = default);
17
+
18
+ UniTask<bool> SendEventLogsBatch(List<EventLog> logs, CancellationToken cancellationToken = default);
19
+ }
20
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 0b922702b1d040efa873d2c7e01cdfe8
3
+ timeCreated: 1678721541
@@ -0,0 +1,17 @@
1
+ using System;
2
+
3
+ namespace Xprees.EventLogging.Api.Model
4
+ {
5
+ [Serializable]
6
+ public class EventLog
7
+ {
8
+ public long? id = null;
9
+ public string scenario;
10
+ public DateTime timestamp;
11
+
12
+ public string @event;
13
+ public string eventData = null;
14
+
15
+ public User user;
16
+ }
17
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: b51d2313275b43a08a18706acc45d1bd
3
+ timeCreated: 1678721725
@@ -0,0 +1,20 @@
1
+ using System;
2
+ using UnityEngine;
3
+
4
+ namespace Xprees.EventLogging.Api.Model
5
+ {
6
+ [Serializable]
7
+ public class User
8
+ {
9
+ public long? id = null;
10
+
11
+ [Tooltip("Can be left blank and be filled individually later by the UserSetupService.")]
12
+ public string formId;
13
+
14
+ [HideInInspector]
15
+ public bool formSubmitted = false;
16
+
17
+ public string name;
18
+ public string email = null;
19
+ }
20
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 102b46811ee146388f2daa04a5dc616c
3
+ timeCreated: 1678721742
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: b6bd40d80c3849698f9686c74c59bdc1
3
+ timeCreated: 1678721711
@@ -0,0 +1,201 @@
1
+ ---
2
+ openapi: 3.0.3
3
+ info:
4
+ title: '"event-log-collector-service" API'
5
+ version: "1"
6
+ paths:
7
+ /export/{scenario}:
8
+ get:
9
+ tags:
10
+ - Event Log Export Resource
11
+ summary: Export logs of scenario to CSV
12
+ parameters:
13
+ - name: scenario
14
+ in: path
15
+ required: true
16
+ schema:
17
+ type: string
18
+ responses:
19
+ "200":
20
+ description: OK
21
+ /logs:
22
+ get:
23
+ tags:
24
+ - Event Log Resource
25
+ summary: Get all EventLogs
26
+ responses:
27
+ "200":
28
+ description: OK
29
+ content:
30
+ application/json:
31
+ schema:
32
+ type: array
33
+ items:
34
+ $ref: '#/components/schemas/EventLogEntity'
35
+ post:
36
+ tags:
37
+ - Event Log Resource
38
+ summary: Add new EventLog
39
+ requestBody:
40
+ content:
41
+ application/json:
42
+ schema:
43
+ $ref: '#/components/schemas/EventLogEntity'
44
+ responses:
45
+ "201":
46
+ description: Event log created
47
+ "409":
48
+ description: Event log already exists
49
+ /logs/scenarios:
50
+ get:
51
+ tags:
52
+ - Event Log Resource
53
+ summary: Get all scenarios
54
+ responses:
55
+ "200":
56
+ description: OK
57
+ content:
58
+ application/json:
59
+ schema:
60
+ type: array
61
+ items:
62
+ type: string
63
+ /logs/{id}:
64
+ get:
65
+ tags:
66
+ - Event Log Resource
67
+ summary: Get EventLog by id
68
+ parameters:
69
+ - name: id
70
+ in: path
71
+ required: true
72
+ schema:
73
+ format: int64
74
+ type: integer
75
+ responses:
76
+ "200":
77
+ description: OK
78
+ delete:
79
+ tags:
80
+ - Event Log Resource
81
+ summary: Delete EventLog by id
82
+ parameters:
83
+ - name: id
84
+ in: path
85
+ required: true
86
+ schema:
87
+ format: int64
88
+ type: integer
89
+ responses:
90
+ "200":
91
+ description: OK
92
+ /users:
93
+ get:
94
+ tags:
95
+ - User Resource
96
+ summary: Get all users
97
+ responses:
98
+ "200":
99
+ description: OK
100
+ content:
101
+ application/json:
102
+ schema:
103
+ type: array
104
+ items:
105
+ $ref: '#/components/schemas/UserEntity'
106
+ /users/count:
107
+ get:
108
+ tags:
109
+ - User Resource
110
+ summary: Get user count
111
+ responses:
112
+ "200":
113
+ description: OK
114
+ content:
115
+ application/json:
116
+ schema:
117
+ format: int64
118
+ type: integer
119
+ /users/{id}:
120
+ get:
121
+ tags:
122
+ - User Resource
123
+ summary: Get user by id
124
+ parameters:
125
+ - name: id
126
+ in: path
127
+ required: true
128
+ schema:
129
+ format: int64
130
+ type: integer
131
+ responses:
132
+ "200":
133
+ description: OK
134
+ put:
135
+ tags:
136
+ - User Resource
137
+ summary: Update user by id
138
+ parameters:
139
+ - name: id
140
+ in: path
141
+ required: true
142
+ schema:
143
+ format: int64
144
+ type: integer
145
+ requestBody:
146
+ content:
147
+ application/json:
148
+ schema:
149
+ $ref: '#/components/schemas/UserEntity'
150
+ responses:
151
+ "200":
152
+ description: OK
153
+ components:
154
+ schemas:
155
+ EventLogEntity:
156
+ required:
157
+ - timestamp
158
+ - scenario
159
+ - event
160
+ - user
161
+ type: object
162
+ properties:
163
+ id:
164
+ format: int64
165
+ type: integer
166
+ nullable: true
167
+ timestamp:
168
+ $ref: '#/components/schemas/Instant'
169
+ scenario:
170
+ type: string
171
+ event:
172
+ type: string
173
+ eventData:
174
+ type: string
175
+ nullable: true
176
+ user:
177
+ $ref: '#/components/schemas/UserEntity'
178
+ Instant:
179
+ format: date-time
180
+ type: string
181
+ example: 2022-03-10T16:15:50Z
182
+ UserEntity:
183
+ required:
184
+ - formId
185
+ - name
186
+ type: object
187
+ properties:
188
+ id:
189
+ format: int64
190
+ type: integer
191
+ nullable: true
192
+ formId:
193
+ type: string
194
+ formSubmitted:
195
+ type: boolean
196
+ nullable: true
197
+ name:
198
+ type: string
199
+ email:
200
+ type: string
201
+ nullable: true
@@ -0,0 +1,7 @@
1
+ fileFormatVersion: 2
2
+ guid: d356127a998312d43859c2288eeca78f
3
+ TextScriptImporter:
4
+ externalObjects: {}
5
+ userData:
6
+ assetBundleName:
7
+ assetBundleVariant:
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 7d010b3b8a084e2aa9365ba3586c990f
3
+ timeCreated: 1678702037
@@ -0,0 +1,18 @@
1
+ using System;
2
+ using Xprees.EventLogging.Api.Model;
3
+ using Xprees.EventLogging.ScriptableObjects;
4
+
5
+ namespace Xprees.EventLogging.Extensions
6
+ {
7
+ public static class EventLoggingApiExtension
8
+ {
9
+ public static EventLog GenerateEventLog(this EventSO @event, User user, DateTime timestamp) => new()
10
+ {
11
+ timestamp = timestamp,
12
+ user = user,
13
+ scenario = @event.scenario?.Trim(),
14
+ @event = @event.eventName.Trim(),
15
+ eventData = @event.eventData?.Trim(),
16
+ };
17
+ }
18
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: af3476fe77b344d5a737a21a375f22e0
3
+ timeCreated: 1678722511
@@ -0,0 +1,73 @@
1
+ using System;
2
+ using System.Text;
3
+ using System.Threading;
4
+ using Cysharp.Threading.Tasks;
5
+ using Newtonsoft.Json;
6
+ using UnityEngine;
7
+ using UnityEngine.Networking;
8
+
9
+ namespace Xprees.EventLogging.Extensions
10
+ {
11
+ public static class UnityWebRequestExtensions
12
+ {
13
+ public async static UniTask SendWebRequestAsync(
14
+ this UnityWebRequest request,
15
+ CancellationToken cancellationToken = default,
16
+ IProgress<float> progress = null
17
+ )
18
+ {
19
+ try
20
+ {
21
+ await request.SendWebRequest().ToUniTask(cancellationToken: cancellationToken, progress: progress);
22
+ }
23
+ catch (OperationCanceledException)
24
+ {
25
+ // ignore
26
+ }
27
+ catch (UnityWebRequestException e)
28
+ {
29
+ if (e.Result == UnityWebRequest.Result.ConnectionError)
30
+ {
31
+ Debug.LogError($"Connection error: {e.Message} - URL: {e.UnityWebRequest.url}");
32
+ return;
33
+ }
34
+
35
+ Debug.LogError($"Web request failed: {e.Message} with code {e.ResponseCode}");
36
+ }
37
+ }
38
+
39
+ public static void AddJsonBody(this UnityWebRequest request, object body)
40
+ {
41
+ var json = JsonConvert.SerializeObject(body);
42
+ var jsonBytes = Encoding.UTF8.GetBytes(json);
43
+
44
+ request.uploadHandler = new UploadHandlerRaw(jsonBytes);
45
+ request.downloadHandler = new DownloadHandlerBuffer();
46
+ request.SetRequestHeader("Content-Type", "application/json");
47
+ }
48
+
49
+ public async static UniTask<UnityWebRequest.Result> TestConnection(CancellationToken cancellationToken = default)
50
+ {
51
+ var request = UnityWebRequest.Get("http://neverssl.com");
52
+ try
53
+ {
54
+ await request.SendWebRequest().WithCancellation(cancellationToken);
55
+ }
56
+ catch (OperationCanceledException)
57
+ {
58
+ // ignore
59
+ }
60
+ catch (UnityWebRequestException e)
61
+ {
62
+ Debug.LogError($"Webex: {e.Message}");
63
+ throw;
64
+ }
65
+ catch (Exception e)
66
+ {
67
+ Debug.LogError(e);
68
+ }
69
+
70
+ return request.result;
71
+ }
72
+ }
73
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: c4d9c209ae324826ba1e6974a97f15d0
3
+ timeCreated: 1678745693
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 321dca0e204e41c288a2548d8477aea6
3
+ timeCreated: 1679505964
@@ -0,0 +1,12 @@
1
+ using System.Threading;
2
+ using Cysharp.Threading.Tasks;
3
+ using Xprees.EventLogging.ScriptableObjects;
4
+
5
+ namespace Xprees.EventLogging
6
+ {
7
+ public interface IEventSenderService
8
+ {
9
+ void LogEvent(EventSO loggedEvent);
10
+ UniTask<bool> UploadEvents(CancellationToken cancellationToken = default);
11
+ }
12
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: d4724818fc954803bfe427cbf18a58ae
3
+ timeCreated: 1679318392
@@ -0,0 +1,12 @@
1
+ using Xprees.EventLogging.Api.Model;
2
+
3
+ namespace Xprees.EventLogging
4
+ {
5
+ public interface IEventUserSetupService
6
+ {
7
+ User CurrentUser { get; set; }
8
+ void SetFormId(string formId);
9
+ void SetName(string username);
10
+ void SetEmail(string email);
11
+ }
12
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 85bdd3c6e0554e6381fd5bda79c58f7a
3
+ timeCreated: 1679319340
@@ -0,0 +1,28 @@
1
+ using UnityEngine;
2
+ using Xprees.Core;
3
+
4
+ namespace Xprees.EventLogging.ScriptableObjects
5
+ {
6
+ [CreateAssetMenu(menuName = "EventLogging/New Event", fileName = "Event")]
7
+ public class EventSO : DescriptionBaseSO
8
+ {
9
+ [Tooltip("Scenario name")]
10
+ public string scenario;
11
+
12
+ [Tooltip("Name of the event.")]
13
+ public string eventName;
14
+
15
+ [Tooltip("Event data/arguments (optional)")]
16
+ public string eventData;
17
+
18
+ #if UNITY_EDITOR
19
+ private void OnValidate()
20
+ {
21
+ if (string.IsNullOrEmpty(eventName))
22
+ {
23
+ Debug.LogWarning($"{nameof(EventSO)} - {name} has null/empty {nameof(eventName)}", this);
24
+ }
25
+ }
26
+ #endif
27
+ }
28
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 8139761af4654f9e8b1b522ed64d2347
3
+ timeCreated: 1678719413
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 61750183ed1749519e2c87c2b84075a1
3
+ timeCreated: 1678700781
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "cz.xprees.event-logging",
3
+ "rootNamespace": "Xprees.EventLogging",
4
+ "references": [
5
+ "GUID:f51ebe6a0ceec4240a699833d6309b23",
6
+ "GUID:999c2ca78ab34358a1222750376a501c"
7
+ ],
8
+ "includePlatforms": [],
9
+ "excludePlatforms": [],
10
+ "allowUnsafeCode": false,
11
+ "overrideReferences": false,
12
+ "precompiledReferences": [],
13
+ "autoReferenced": true,
14
+ "defineConstraints": [],
15
+ "versionDefines": [],
16
+ "noEngineReferences": false
17
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 2bc606c18a1a4f2cb45a7c51038804cf
3
+ timeCreated: 1679505773
package/Runtime.meta ADDED
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 4ceb1d8111a54a6d803686b1b632ff94
3
+ timeCreated: 1679505735
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "cz.xprees.event-logging",
3
+ "displayName": "Event Logging",
4
+ "version": "1.0.0",
5
+ "unity": "2021.3",
6
+ "description": "This package provides a simple way to log events in your game.",
7
+ "license": "MIT",
8
+ "category": "Core",
9
+ "dependencies": {
10
+ "com.cysharp.unitask": "2.5.10",
11
+ "cz.xprees.core": "^1.0.0"
12
+ },
13
+ "keywords": [
14
+ "Event Logging",
15
+ "ScriptableObjects"
16
+ ],
17
+ "author": {
18
+ "name": "xprees",
19
+ "email": "xprees.developer@gmail.com",
20
+ "url": "https://xprees.com"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/xprees/unity-event-logging/issues"
24
+ },
25
+ "repository": {
26
+ "url": "https://github.com/xprees/unity-event-logging"
27
+ }
28
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: d9c9e5db5936479985b7dc0248e9f992
3
+ timeCreated: 1684880940