-
Notifications
You must be signed in to change notification settings - Fork 4
[FEATURE] Add telemetry CAT-66 #28
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6e2a1ca
[FEATURE] Add SDK telemetry functionality CAT-66
norvin-yecla-lrn d3cfc68
[TEST] Add integration tests for SDK Telemetry CAT-66
norvin-yecla-lrn 8a8d523
[FEATURE] Extract correct information for the SDK field CAT-66
norvin-yecla-lrn a7b1e35
[POLISH] Remove unneccessary lines and update strings CAT-66
norvin-yecla-lrn 14a45ab
[POLISH] Remove redundant function CAT-66
norvin-yecla-lrn 027b130
[TEST] Add more tests CAT-66
norvin-yecla-lrn 30e2527
[REFACTOR] Improve variable naming CAT-66
norvin-yecla-lrn 937adcc
[REFACTOR] Reduce code duplication CAT-66
norvin-yecla-lrn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using LearnositySDK.Utils; | ||
| using System.Web; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace LearnositySDK.Request | ||
| { | ||
|
|
@@ -77,6 +77,11 @@ public class Init | |
| /// </summary> | ||
| private string algorithm; | ||
|
|
||
| /// <summary> | ||
| /// Determines if telemetry is enabled; default is true | ||
| /// </summary> | ||
| private static bool telemetryEnabled = true; | ||
|
|
||
| /// <summary> | ||
| /// Instantiate this class with all security and request data. It will be used to create a signature. | ||
| /// </summary> | ||
|
|
@@ -153,6 +158,7 @@ private void Initialize(string service, JsonObject securityPacket, string secret | |
| //try | ||
| //{ | ||
| this.validate(this.service, ref this.securityPacket, this.secret, this.requestPacket, this.action); | ||
| this.addTelemetryData(); | ||
| this.requestString = this.generateRequestString(); | ||
| this.setServiceOptions(); | ||
| this.securityPacket.set("signature", this.generateSignature()); | ||
|
|
@@ -180,7 +186,7 @@ private string generateRequestString() | |
| { | ||
| throw new Exception("Invalid data, please check you request packet."); | ||
| } | ||
|
|
||
| return request; | ||
| } | ||
|
|
||
|
|
@@ -191,7 +197,7 @@ private string generateRequestString() | |
| /// - the `action` value if passed | ||
| /// </summary> | ||
| /// <returns>A signature hash for the request authentication</returns> | ||
| private string generateSignature() | ||
| public string generateSignature() | ||
| { | ||
| List<string> signatureList = new List<string>(); | ||
| string temp = null; | ||
|
|
@@ -256,7 +262,7 @@ public string generate() | |
| { | ||
| return this.generateData(output); | ||
| } | ||
| else if(this.service == "assess") | ||
| else if (this.service == "assess") | ||
| { | ||
| output = this.generateAssess(output); | ||
| } | ||
|
|
@@ -277,7 +283,7 @@ public string generate() | |
| // do nothing | ||
| break; | ||
| } | ||
|
|
||
| return output.toJson(); | ||
| } | ||
|
|
||
|
|
@@ -291,7 +297,7 @@ private string generateData(JsonObject output) | |
| StringBuilder sb = new StringBuilder(); | ||
| JsonObject obj = null; | ||
| string str = ""; | ||
|
|
||
| obj = output.getJsonObject("security"); | ||
| if (!Tools.empty(obj)) | ||
| { | ||
|
|
@@ -380,7 +386,7 @@ private void setServiceOptions() | |
| case "assess": | ||
| // fall-through | ||
| case "questions": | ||
|
|
||
| this.signRequestData = false; | ||
|
|
||
| if (this.service == "assess" && Tools.array_key_exists("questionsApiActivity", this.requestPacket)) | ||
|
|
@@ -456,18 +462,21 @@ private void setServiceOptions() | |
| this.signRequestData = false; | ||
|
|
||
| JsonObject requestPackageUsers = this.requestPacket.getJsonObject("users"); | ||
| if (requestPackageUsers != null) { | ||
| string[] users = requestPackageUsers.getValuesArray(); | ||
| if (users != null && users.Length > 0) { | ||
| if (requestPackageUsers != null) | ||
| { | ||
| string[] users = requestPackageUsers.getValuesArray(); | ||
| if (users != null && users.Length > 0) | ||
| { | ||
| hashedUsers = new JsonObject(); | ||
| for (int i = 0; i < users.Length; i++) { | ||
| for (int i = 0; i < users.Length; i++) | ||
| { | ||
| string user_id = users[i]; | ||
| hashedUsers.set(user_id, Tools.hash(this.algorithm, user_id + this.secret)); | ||
| } | ||
| this.requestPacket.set("users", hashedUsers); | ||
| } | ||
| } | ||
|
|
||
| break; | ||
| default: | ||
| // do nothing | ||
|
|
@@ -518,5 +527,111 @@ public void validate(string service, ref JsonObject securityPacket, string secre | |
| throw new Exception("The `secret` argument must be a valid string"); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a meta field with SDK information in it to the requestPacket | ||
| /// </summary> | ||
| public void addTelemetryData() | ||
| { | ||
| if (this.isTelemetryEnabled()) | ||
| { | ||
| JsonObject meta; | ||
| if (this.requestPacket.getJsonObject("meta") != null) | ||
| { | ||
| meta = this.requestPacket.getJsonObject("meta"); | ||
| } | ||
| else | ||
| { | ||
| meta = new JsonObject(); | ||
| } | ||
flakeparadigm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| meta.set("sdk", this.getSdkMeta()); | ||
| this.requestPacket.set("meta", meta); | ||
| } | ||
| } | ||
|
|
||
| public bool isTelemetryEnabled() | ||
| { | ||
| return telemetryEnabled; | ||
| } | ||
|
|
||
| public JsonObject getSdkMeta() | ||
| { | ||
| JsonObject sdkMeta = new JsonObject(); | ||
| sdkMeta.set("version", this.getSdkVersion()); | ||
| sdkMeta.set("lang", "asp.net"); | ||
| sdkMeta.set("lang_version", this.getLanguageVersion()); | ||
| sdkMeta.set("platform", this.getPlatform()); | ||
| sdkMeta.set("platform_version", this.getPlatformVersion()); | ||
|
|
||
| return sdkMeta; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the version number of ASP.NET | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public string getLanguageVersion() | ||
| { | ||
| return Environment.Version.ToString(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the name of the platform the code is running on | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public string getPlatform() | ||
| { | ||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
| { | ||
| return "darwin"; | ||
| } | ||
|
|
||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| { | ||
| return "win"; | ||
| } | ||
|
|
||
| if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
| { | ||
| return "linux"; | ||
| } | ||
|
|
||
| return Environment.OSVersion.Platform.ToString(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the version number of the platform the code is running on | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public string getPlatformVersion() | ||
| { | ||
| var os = Environment.OSVersion; | ||
| return string.Concat(os.Version.Major, '.', os.Version.Minor); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extracts the version number of this SDK from the project meta data | ||
| /// </summary> | ||
| /// <returns>The version number</returns> | ||
| public string getSdkVersion() | ||
| { | ||
| return GetType().Assembly.GetName().Version.ToString(); | ||
|
Contributor
Author
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. This will return a string with four sections separated by periods. i.e. 0.0.8.0 |
||
| } | ||
|
|
||
| /// <summary> | ||
| /// We use telemetry to enable better support and feature planning. | ||
| /// It will not interfere with any usage. | ||
| /// However, it is not advised to disable it. | ||
| /// </summary> | ||
| public static void disableTelemetry() | ||
| { | ||
| telemetryEnabled = false; | ||
| } | ||
|
|
||
| public static void enableTelemetry() | ||
| { | ||
| telemetryEnabled = true; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Turned to
publicso we can use it in the tests. In Java SDK this is also apublicmethod.