Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit f12b92f

Browse files
Functionality to submit Usage through NodeJS
1 parent 9385c5b commit f12b92f

File tree

7 files changed

+47
-31
lines changed

7 files changed

+47
-31
lines changed

octorun/src/bin/app-usage.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@ var package = require('../../package.json')
33
var endOfLine = require('os').EOL;
44
var fs = require('fs');
55
var util = require('util');
6-
var https = require('https');
76

87
commander
98
.version(package.version)
9+
.option('-s, --scheme <scheme>')
1010
.option('-h, --host <host>')
1111
.option('-p, --port <port>')
1212
.parse(process.argv);
1313

1414
var host = commander.host;
1515
var port = 443;
16+
var scheme = 'https';
1617

1718
if (commander.port) {
18-
port = commander.port;
19+
port = parseInt(commander.port);
20+
}
21+
22+
if (commander.scheme) {
23+
scheme = commander.scheme;
1924
}
2025

2126
var fileContents = null;
@@ -27,9 +32,10 @@ if (commander.args.length == 1) {
2732
}
2833
}
2934

30-
if (fileContents && host && util.isNumber(port)) {
35+
if (fileContents && host) {
36+
var https = require(scheme);
3137
var options = {
32-
protocol: "https:",
38+
protocol: scheme + ':',
3339
hostname: host,
3440
port: port,
3541
path: '/api/usage/unity',
@@ -40,18 +46,34 @@ if (fileContents && host && util.isNumber(port)) {
4046
};
4147

4248
var req = https.request(options, function (res) {
49+
var success = res.statusCode == 200;
50+
4351
res.on('data', function (d) {
44-
process.stdout.write(d);
45-
process.stdout.write(endOfLine);
52+
if (success) {
53+
process.stdout.write("Success");
54+
process.stdout.write(endOfLine);
55+
process.stdout.write(d);
56+
}
57+
else {
58+
process.stdout.write("Error");
59+
process.stdout.write(endOfLine);
60+
process.stdout.write(d);
61+
}
4662
});
4763

4864
res.on('end', function (d) {
49-
process.exit(res.statusCode == 200 ? 0 : -1);
65+
process.exit(success ? 0 : -1);
5066
});
5167
});
5268

5369
req.on('error', function (e) {
54-
console.log(e);
70+
process.stdout.write("Error");
71+
process.stdout.write(endOfLine);
72+
73+
if (e) {
74+
process.stdout.write(e.toString());
75+
}
76+
5577
process.exit(-1);
5678
});
5779

script

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public static IApiClient Create(UriString repositoryUrl, IKeychain keychain, IPr
3030
private readonly IGitHubClient githubClient;
3131
private readonly IProcessManager processManager;
3232
private readonly ITaskManager taskManager;
33-
private readonly NPath octorunScriptPath;
3433
private readonly ILoginManager loginManager;
3534

3635
public ApiClient(UriString hostUrl, IKeychain keychain, IGitHubClient githubClient, IProcessManager processManager, ITaskManager taskManager, NPath nodeJsExecutablePath, NPath octorunScriptPath)
@@ -45,7 +44,6 @@ public ApiClient(UriString hostUrl, IKeychain keychain, IGitHubClient githubClie
4544
this.githubClient = githubClient;
4645
this.processManager = processManager;
4746
this.taskManager = taskManager;
48-
this.octorunScriptPath = octorunScriptPath;
4947
loginManager = new LoginManager(keychain, ApplicationInfo.ClientId, ApplicationInfo.ClientSecret,
5048
processManager: processManager,
5149
taskManager: taskManager,

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ protected void Initialize()
4040
ProcessManager = new ProcessManager(Environment, Platform.GitEnvironment, CancellationToken);
4141
Platform.Initialize(ProcessManager, TaskManager);
4242
GitClient = new GitClient(Environment, ProcessManager, TaskManager.Token);
43-
SetupMetrics();
4443
}
4544

4645
public void Run(bool firstRun)
@@ -156,7 +155,14 @@ protected void SetupMetrics(string unityVersion, bool firstRun)
156155
UserSettings.Set(Constants.GuidKey, id);
157156
}
158157

159-
UsageTracker = new UsageTracker(UserSettings, usagePath, id, unityVersion);
158+
var metricsService = new MetricsService(ProcessManager,
159+
TaskManager,
160+
Environment.FileSystem,
161+
Environment.NodeJsExecutablePath,
162+
Environment.OctorunScriptPath,
163+
ApplicationConfiguration.ProductHeader);
164+
165+
UsageTracker = new UsageTracker(metricsService, UserSettings, usagePath, id, unityVersion);
160166

161167
if (firstRun)
162168
{
@@ -181,6 +187,7 @@ private void InitializeEnvironment(NPath gitExecutablePath, NPath octorunScriptP
181187
Environment.GitExecutablePath = gitExecutablePath;
182188
Environment.OctorunScriptPath = octorunScriptPath;
183189
Environment.User.Initialize(GitClient);
190+
SetupMetrics();
184191

185192
if (Environment.IsWindows)
186193
{

src/GitHub.Api/Metrics/UsageTracker.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,25 @@ namespace GitHub.Unity
1313
class UsageTracker : IUsageTracker
1414
{
1515
private static ILogging Logger { get; } = LogHelper.GetLogger<UsageTracker>();
16-
private static IMetricsService metricsService;
1716

1817
private readonly NPath storePath;
1918
private readonly ISettings userSettings;
19+
private readonly IMetricsService metricsService;
2020
private readonly string guid;
2121
private readonly string unityVersion;
2222
private Timer timer;
2323

24-
public UsageTracker(ISettings userSettings, NPath storePath, string guid, string unityVersion)
24+
public UsageTracker(IMetricsService metricsService, ISettings userSettings, NPath storePath, string guid, string unityVersion)
2525
{
2626
this.userSettings = userSettings;
27+
this.metricsService = metricsService;
2728
this.guid = guid;
2829
this.storePath = storePath;
2930
this.unityVersion = unityVersion;
3031

3132
Logger.Trace("guid:{0}", guid);
3233
if (Enabled)
33-
RunTimer(3*60);
34+
RunTimer(3*10);
3435
}
3536

3637
private UsageStore LoadUsage()
@@ -108,8 +109,6 @@ private void RunTimer(int seconds)
108109

109110
private async Task SendUsage()
110111
{
111-
Logger.Trace("SendUsage");
112-
113112
var usage = LoadUsage();
114113

115114
if (metricsService == null)
@@ -174,12 +173,6 @@ public void IncrementLaunchCount()
174173
SaveUsage(usageStore);
175174
}
176175

177-
public static void SetMetricsService(IMetricsService instance)
178-
{
179-
Logger.Trace("SetMetricsService instance:{0}", instance?.ToString() ?? "Null");
180-
metricsService = instance;
181-
}
182-
183176
public bool Enabled
184177
{
185178
get

src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,7 @@
116116
<Compile Include="UI\Window.cs" />
117117
</ItemGroup>
118118
<Choose>
119-
<When Condition="$(Buildtype) == 'Internal'">
120-
<ItemGroup>
121-
<Compile Include="$(SolutionDir)\script\src\MetricsInitialization.cs">
122-
<Link>Metrics\MetricsInitialization.cs</Link>
123-
</Compile>
124-
</ItemGroup>
125-
</When>
119+
<When Condition="$(Buildtype) == 'Internal'" />
126120
</Choose>
127121
<ItemGroup>
128122
<ProjectReference Include="..\..\..\..\GitHub.Api\GitHub.Api.csproj">

src/tests/IntegrationTests/IntegrationTestEnvironment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public NPath GitExecutablePath
104104
}
105105
}
106106

107+
public NPath NodeJsExecutablePath => defaultEnvironment.NodeJsExecutablePath;
108+
107109
public NPath OctorunScriptPath { get; set; }
108110

109111
public bool IsWindows => defaultEnvironment.IsWindows;

0 commit comments

Comments
 (0)