-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from twpol/twpol/issue1
feat: Add system data collection API endpoint
- Loading branch information
Showing
5 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,2 +1,3 @@ | ||
/bin | ||
/data | ||
/obj |
This file contains 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Text.Json; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Open_Rails_Telemetry.API | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class Collect : ControllerBase | ||
{ | ||
readonly IConfiguration Configuration; | ||
readonly string DataPathCollectSystem; | ||
|
||
public Collect(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
DataPathCollectSystem = Path.Combine(Configuration["DataPath"], "collect", "system"); | ||
if (!Directory.Exists(DataPathCollectSystem)) Directory.CreateDirectory(DataPathCollectSystem); | ||
} | ||
|
||
[HttpPost("System")] | ||
[Consumes("application/json")] | ||
public async Task<IActionResult> PostSystem([FromBody] JsonElement data) | ||
{ | ||
// File's name has the date for analysis, and a random key to prevent collisions | ||
var date = DateTime.UtcNow.Date; | ||
var randomKey = Guid.NewGuid().ToString(); | ||
var file = new FileInfo(Path.Combine(DataPathCollectSystem, $"{date:yyyy-MM-dd}_{randomKey}.json")); | ||
// Write the JSON into the file | ||
await using (var stream = file.Create()) | ||
{ | ||
await JsonSerializer.SerializeAsync(stream, data); | ||
} | ||
// Wipe out the file's timestamps so we cannot correlate them with e.g. access logs | ||
file.CreationTimeUtc = date; | ||
file.LastWriteTimeUtc = date; | ||
file.LastAccessTimeUtc = date; | ||
return Ok(); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
}, | ||
"DataPath": "data" | ||
} |
This file contains 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