Skip to content

Commit 67a2de0

Browse files
committed
Added datadogd and removed an unused appveyor property that caused exceptions.
1 parent c18f8c7 commit 67a2de0

5 files changed

Lines changed: 129 additions & 92 deletions

File tree

RadarrAPI/Controllers/UpdateController.cs

Lines changed: 88 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using RadarrAPI.Database;
77
using RadarrAPI.Update;
88
using RadarrAPI.Update.Data;
9+
using StatsdClient;
910
using Branch = RadarrAPI.Update.Branch;
1011

1112
namespace RadarrAPI.Controllers
@@ -24,122 +25,130 @@ public UpdateController(DatabaseContext database)
2425
[HttpGet]
2526
public object GetChanges([FromRoute(Name = "branch")]Branch updateBranch, [FromQuery(Name = "version")]string urlVersion, [FromQuery(Name = "os")]OperatingSystem operatingSystem)
2627
{
27-
var updates = _database.UpdateEntities
28+
using (DogStatsd.StartTimer("controller.update.get_changes.time"))
29+
{
30+
DogStatsd.Increment("controller.update.get_changes.count");
31+
32+
var updates = _database.UpdateEntities
2833
.Include(x => x.UpdateFiles)
2934
.Where(x => x.Branch == updateBranch && x.UpdateFiles.Any(u => u.OperatingSystem == operatingSystem))
3035
.OrderByDescending(x => x.ReleaseDate)
3136
.Take(5);
3237

33-
var response = new List<UpdatePackage>();
38+
var response = new List<UpdatePackage>();
3439

35-
foreach (var update in updates)
36-
{
37-
var updateFile = update.UpdateFiles.FirstOrDefault(u => u.OperatingSystem == operatingSystem);
38-
if (updateFile == null) continue;
40+
foreach (var update in updates)
41+
{
42+
var updateFile = update.UpdateFiles.FirstOrDefault(u => u.OperatingSystem == operatingSystem);
43+
if (updateFile == null) continue;
3944

40-
UpdateChanges updateChanges = null;
45+
UpdateChanges updateChanges = null;
4146

42-
if (update.New.Count != 0 || update.Fixed.Count != 0)
43-
{
44-
updateChanges = new UpdateChanges
47+
if (update.New.Count != 0 || update.Fixed.Count != 0)
4548
{
46-
New = update.New,
47-
Fixed = update.Fixed
48-
};
49+
updateChanges = new UpdateChanges
50+
{
51+
New = update.New,
52+
Fixed = update.Fixed
53+
};
54+
}
55+
56+
response.Add(new UpdatePackage
57+
{
58+
Version = update.Version,
59+
ReleaseDate = update.ReleaseDate,
60+
Filename = updateFile.Filename,
61+
Url = updateFile.Url,
62+
Changes = updateChanges,
63+
Hash = updateFile.Hash,
64+
Branch = update.Branch.ToString().ToLower()
65+
});
4966
}
5067

51-
response.Add(new UpdatePackage
52-
{
53-
Version = update.Version,
54-
ReleaseDate = update.ReleaseDate,
55-
Filename = updateFile.Filename,
56-
Url = updateFile.Url,
57-
Changes = updateChanges,
58-
Hash = updateFile.Hash,
59-
Branch = update.Branch.ToString().ToLower()
60-
});
68+
return response;
6169
}
62-
63-
return response;
6470
}
6571

6672
[Route("{branch}")]
6773
[HttpGet]
6874
public object GetUpdates([FromRoute(Name = "branch")]Branch updateBranch, [FromQuery(Name = "version")]string urlVersion, [FromQuery(Name = "os")]OperatingSystem operatingSystem)
6975
{
70-
Version version;
71-
7276
// Check given version
73-
if (!Version.TryParse(urlVersion, out version))
77+
if (!Version.TryParse(urlVersion, out Version version))
7478
{
7579
return new
7680
{
7781
ErrorMessage = "Invalid version number specified."
7882
};
7983
}
8084

81-
// Grab latest update based on branch and operatingsystem
82-
var update = _database.UpdateEntities
83-
.Include(x => x.UpdateFiles)
84-
.Where(x => x.Branch == updateBranch && x.UpdateFiles.Any(u => u.OperatingSystem == operatingSystem))
85-
.OrderByDescending(x => x.ReleaseDate)
86-
.Take(1)
87-
.FirstOrDefault();
88-
89-
if (update == null)
85+
using (DogStatsd.StartTimer("controller.update.get_updates.time"))
9086
{
91-
return new
87+
DogStatsd.Increment("controller.update.get_updates.count");
88+
89+
// Grab latest update based on branch and operatingsystem
90+
var update = _database.UpdateEntities
91+
.Include(x => x.UpdateFiles)
92+
.Where(x => x.Branch == updateBranch && x.UpdateFiles.Any(u => u.OperatingSystem == operatingSystem))
93+
.OrderByDescending(x => x.ReleaseDate)
94+
.Take(1)
95+
.FirstOrDefault();
96+
97+
if (update == null)
9298
{
93-
ErrorMessage = "Latest update not found."
94-
};
95-
}
99+
return new
100+
{
101+
ErrorMessage = "Latest update not found."
102+
};
103+
}
96104

97-
// Check if update file is present
98-
var updateFile = update.UpdateFiles.FirstOrDefault(u => u.OperatingSystem == operatingSystem);
99-
if (updateFile == null)
100-
{
101-
return new
105+
// Check if update file is present
106+
var updateFile = update.UpdateFiles.FirstOrDefault(u => u.OperatingSystem == operatingSystem);
107+
if (updateFile == null)
102108
{
103-
ErrorMessage = "Latest update file not found."
104-
};
105-
}
109+
return new
110+
{
111+
ErrorMessage = "Latest update file not found."
112+
};
113+
}
106114

107-
// Compare given version and update version
108-
var updateVersion = new Version(update.Version);
109-
if (updateVersion.CompareTo(version) <= 0)
110-
{
111-
return new UpdatePackageContainer
115+
// Compare given version and update version
116+
var updateVersion = new Version(update.Version);
117+
if (updateVersion.CompareTo(version) <= 0)
112118
{
113-
Available = false
114-
};
115-
}
119+
return new UpdatePackageContainer
120+
{
121+
Available = false
122+
};
123+
}
116124

117-
// Get the update changes
118-
UpdateChanges updateChanges = null;
125+
// Get the update changes
126+
UpdateChanges updateChanges = null;
119127

120-
if (update.New.Count != 0 || update.Fixed.Count != 0)
121-
{
122-
updateChanges = new UpdateChanges
128+
if (update.New.Count != 0 || update.Fixed.Count != 0)
129+
{
130+
updateChanges = new UpdateChanges
131+
{
132+
New = update.New,
133+
Fixed = update.Fixed
134+
};
135+
}
136+
137+
return new UpdatePackageContainer
123138
{
124-
New = update.New,
125-
Fixed = update.Fixed
139+
Available = true,
140+
UpdatePackage = new UpdatePackage
141+
{
142+
Version = update.Version,
143+
ReleaseDate = update.ReleaseDate,
144+
Filename = updateFile.Filename,
145+
Url = updateFile.Url,
146+
Changes = updateChanges,
147+
Hash = updateFile.Hash,
148+
Branch = update.Branch.ToString().ToLower()
149+
}
126150
};
127151
}
128-
129-
return new UpdatePackageContainer
130-
{
131-
Available = true,
132-
UpdatePackage = new UpdatePackage
133-
{
134-
Version = update.Version,
135-
ReleaseDate = update.ReleaseDate,
136-
Filename = updateFile.Filename,
137-
Url = updateFile.Url,
138-
Changes = updateChanges,
139-
Hash = updateFile.Hash,
140-
Branch = update.Branch.ToString().ToLower()
141-
}
142-
};
143152
}
144153
}
145154
}

RadarrAPI/RadarrAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>netcoreapp1.1</TargetFramework>
55
</PropertyGroup>
66
<ItemGroup>
7+
<PackageReference Include="DogStatsD-CSharp-Client" Version="3.0.0" />
78
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
89
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" />
910
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.0" />

RadarrAPI/Release/AppVeyor/Responses/AppVeyorProjectBuild.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ public class AppVeyorProjectBuild
2929

3030
[JsonProperty("status", Required = Required.Always)]
3131
public string Status { get; set; }
32-
33-
[JsonProperty("committerUsername", Required = Required.Always)]
34-
public string CommiterUsername { get; set; }
3532

3633
public DateTimeOffset? Started { get; set; }
3734

RadarrAPI/Startup.cs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using RadarrAPI.Release;
1212
using Microsoft.EntityFrameworkCore;
1313
using NLog.Web;
14+
using StatsdClient;
1415

1516
namespace RadarrAPI
1617
{
@@ -28,14 +29,8 @@ public Startup(IHostingEnvironment env)
2829

2930
Configuration = builder.Build();
3031

31-
// Check data path
32-
if (!Path.IsPathRooted(Configuration["DataDirectory"]))
33-
{
34-
throw new Exception("DataDirectory path must be absolute.");
35-
}
36-
37-
// Create
38-
Directory.CreateDirectory(Configuration["DataDirectory"]);
32+
SetupDataDirectory();
33+
SetupDatadog();
3934
}
4035

4136
public IConfigurationRoot Configuration { get; }
@@ -51,11 +46,41 @@ public void ConfigureServices(IServiceCollection services)
5146
services.AddDbContext<DatabaseContext>(o => o.UseMySql(Configuration["Database"]));
5247
}
5348

54-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
49+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime applicationLifetime)
5550
{
5651
loggerFactory.AddNLog();
5752
app.AddNLogWeb();
5853
app.UseMvc();
54+
55+
applicationLifetime.ApplicationStarted.Register(() => DogStatsd.Event("RadarrAPI", "RadarrAPI just started."));
56+
}
57+
58+
private void SetupDataDirectory()
59+
{
60+
// Check data path
61+
if (!Path.IsPathRooted(Configuration["DataDirectory"]))
62+
{
63+
throw new Exception("DataDirectory path must be absolute.");
64+
}
65+
66+
// Create
67+
Directory.CreateDirectory(Configuration["DataDirectory"]);
68+
}
69+
70+
private void SetupDatadog()
71+
{
72+
var server = Configuration.GetSection("DataDog")["Server"];
73+
var port = Configuration.GetSection("DataDog").GetValue<int>("Port");
74+
var prefix = Configuration.GetSection("DataDog")["Prefix"];
75+
76+
if (string.IsNullOrWhiteSpace(server) || port == 0) return;
77+
78+
DogStatsd.Configure(new StatsdConfig
79+
{
80+
StatsdServerName = server,
81+
StatsdPort = port,
82+
Prefix = prefix
83+
});
5984
}
6085
}
6186
}

RadarrAPI/appsettings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
"DataDirectory": "",
33
"Database": "server=127.0.0.1;user id=root;password=;database=radarrapi;CharSet=utf8mb4",
44
"AppVeyorApiKey": "",
5-
"ApiKey": ""
5+
"ApiKey": "",
6+
"DataDog": {
7+
"Server": "",
8+
"Port": 0,
9+
"Prefix": "radarr.api"
10+
}
611
}

0 commit comments

Comments
 (0)