69 lines
2.9 KiB
C#
69 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace AudioWallpaper.ActivityWatch {
|
|
public class ActivityWatchClient {
|
|
private readonly HttpClient _httpClient;
|
|
private readonly string _baseUrl;
|
|
private readonly string _bucket;
|
|
|
|
public ActivityWatchClient(string bucketName, string host = "http://localhost:5600") {
|
|
_httpClient = new HttpClient();
|
|
_baseUrl = $"{host}/api/0";
|
|
_bucket = bucketName;
|
|
}
|
|
|
|
public async Task<List<Dictionary<string, object>>> GetEventsAsync(string start, string end) {
|
|
var url = $"{_baseUrl}/buckets/{_bucket}/events";
|
|
var uriBuilder = new UriBuilder(url);
|
|
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
|
|
query["start"] = start;
|
|
query["end"] = end;
|
|
uriBuilder.Query = query.ToString();
|
|
|
|
var response = await _httpClient.GetAsync(uriBuilder.ToString());
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var jsonString = await response.Content.ReadAsStringAsync();
|
|
var events = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(jsonString);
|
|
return events;
|
|
}
|
|
|
|
public static Dictionary<string, Dictionary<string, string>> GenerateTimeRanges(DateTime? now = null) {
|
|
var currentTime = now ?? DateTime.UtcNow;
|
|
|
|
return new Dictionary<string, Dictionary<string, string>> {
|
|
["last_5_minutes"] = new Dictionary<string, string> {
|
|
["start"] = currentTime.AddMinutes(-5).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
|
|
["end"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
|
|
},
|
|
["last_30_minutes"] = new Dictionary<string, string> {
|
|
["start"] = currentTime.AddMinutes(-30).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
|
|
["end"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
|
|
},
|
|
["last_6_hours"] = new Dictionary<string, string> {
|
|
["start"] = currentTime.AddHours(-6).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
|
|
["end"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
|
|
},
|
|
["last_24_hours"] = new Dictionary<string, string> {
|
|
["start"] = currentTime.AddHours(-24).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
|
|
["end"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
|
|
},
|
|
["last_3_days"] = new Dictionary<string, string> {
|
|
["start"] = currentTime.AddDays(-3).ToString("yyyy-MM-ddTHH:mm:ss.fffZ"),
|
|
["end"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
|
|
}
|
|
};
|
|
}
|
|
|
|
public void Dispose() {
|
|
_httpClient?.Dispose();
|
|
}
|
|
}
|
|
}
|