Skip to content

Commit

Permalink
Service analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Jun 26, 2024
1 parent 57a8583 commit 129d795
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
14 changes: 9 additions & 5 deletions platforms/core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct Settings {
pub license_key: String,
#[serde(rename = "licenseActivated", default = "default_string")]
pub license_activated: String,
#[serde(rename = "userId", default = "default_connection_code")]
pub user_id: String,
pub version: Option<u8>,
}

Expand All @@ -55,6 +57,7 @@ fn check_if_settings_exits() {
launch_on_startup: false,
remote_connections: false,
connection_code: default_connection_code(),
user_id: default_connection_code(),
license_key: "".to_string(),
license_activated: "".to_string(),
};
Expand All @@ -63,7 +66,7 @@ fn check_if_settings_exits() {

// Check if folder exists
if !program_data.join("Cores").exists() {
std::fs::create_dir(program_data.join("Cores")).unwrap();
std::fs::create_dir(program_data.join("Cores")).expect("Failed to create settings folder");
}

// Check if file exists
Expand All @@ -72,7 +75,7 @@ fn check_if_settings_exits() {
program_data.join("Cores").join("settings.json"),
serde_json::to_string(&sample_settings).unwrap(),
)
.unwrap();
.expect("Failed to create settings file");
}
}

Expand All @@ -85,6 +88,7 @@ pub fn get_settings() -> Settings {
launch_on_startup: false,
remote_connections: false,
connection_code: default_connection_code(),
user_id: default_connection_code(),
license_key: "".to_string(),
license_activated: "".to_string(),
};
Expand All @@ -95,7 +99,7 @@ pub fn get_settings() -> Settings {

check_if_settings_exits();

let file = std::fs::read_to_string(program_data.join("Cores").join("settings.json")).unwrap();
let file = std::fs::read_to_string(program_data.join("Cores").join("settings.json")).expect("Failed to read settings file");
let settings: Result<Settings, _> = serde_json::from_str(&file);

match settings {
Expand All @@ -107,7 +111,7 @@ pub fn get_settings() -> Settings {
program_data.join("Cores").join("settings.json"),
serde_json::to_string(&sample_settings).unwrap(),
)
.unwrap();
.expect("Failed to create a missing settings file");

return sample_settings;
}
Expand All @@ -122,5 +126,5 @@ pub fn set_settings(settings: String) {

check_if_settings_exits();

std::fs::write(program_data.join("Cores").join("settings.json"), settings).unwrap();
std::fs::write(program_data.join("Cores").join("settings.json"), settings).expect("Failed to write settings file");
}
36 changes: 36 additions & 0 deletions platforms/windows/lib/Analytics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Text;
using System.Text.Json;

namespace lib {
public class AnalyticsPayload {
public string api_key { get; set; }

Check warning on line 6 in platforms/windows/lib/Analytics.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'api_key' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string @event { get; set; }

Check warning on line 7 in platforms/windows/lib/Analytics.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'event' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string distinct_id { get; set; }

Check warning on line 8 in platforms/windows/lib/Analytics.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'distinct_id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class Analytics {
public static async Task SendEvent(Settings settings) {
string url = "https://eu.i.posthog.com/capture/";

var payload = new AnalyticsPayload {
api_key = "phc_2zbUPXXhnelCYP2VLXeWZvKy0hykzQA7edSOsFrYZaa",
distinct_id = settings.userId,
@event = "service"
};

try {
using var httpClient = new HttpClient();
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");

var response = await httpClient.PostAsync(url, content);

response.EnsureSuccessStatusCode();

var responseContent = await response.Content.ReadAsStringAsync();
}
catch (Exception ex) {
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}
6 changes: 4 additions & 2 deletions platforms/windows/lib/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace lib;

public class ConnectionCode {
public class DefaultValues {
public static string Generate() {
// get first 10 characters of guid
var id = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 10);
Expand All @@ -17,9 +17,10 @@ public class DefaultSettings {
public bool launchOnStartup { get; set; } = false;
public bool remoteConnections { get; set; } = false;
public bool optionalAnalytics { get; set; } = true;
public string connectionCode { get; set; } = ConnectionCode.Generate();
public string connectionCode { get; set; } = DefaultValues.Generate();
public string licenseKey { get; set; } = "";
public string licenseActivated { get; set; } = "";
public string userId { get; set; } = DefaultValues.Generate();
public int version { get; set; } = 2;
}

Expand Down Expand Up @@ -63,6 +64,7 @@ public Settings() {
connectionCode = settings?.connectionCode ?? defaultSettings.connectionCode;
licenseKey = settings?.licenseKey ?? defaultSettings.licenseKey;
licenseActivated = settings?.licenseActivated ?? defaultSettings.licenseActivated;
userId = settings?.userId ?? defaultSettings.userId;
version = settings?.version ?? defaultSettings.version;
}
catch (Exception e) {
Expand Down
8 changes: 7 additions & 1 deletion platforms/windows/service/WindowsBackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ public sealed class WindowsBackgroundService : BackgroundService {
internal static HTTPServer HTTPServer = new();
internal static WSServer WSServer = new();
internal static RTCServer RTCServer = new();
internal static Analytics Analytics = new();

protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
Log.Information("Starting Cores service");
HardwareInfo.GetInfo();
HTTPServer.Start(HardwareInfo);
WSServer.Start(HardwareInfo);
WSServer.Start(HardwareInfo)

Check failure on line 17 in platforms/windows/service/WindowsBackgroundService.cs

View workflow job for this annotation

GitHub Actions / build (Release)

; expected

// Send analytics
_ = Task.Run(async () => {
await Analytics.SendEvent(Program.Settings);
});

// Start remote connection
if (Program.Settings.remoteConnections) {
Expand Down

0 comments on commit 129d795

Please sign in to comment.