49 lines
1.9 KiB
D
49 lines
1.9 KiB
D
module analytics.api;
|
|
|
|
import handy_http_primitives;
|
|
import handy_http_handlers.path_handler : PathMapping, GetMapping;
|
|
import handy_http_data : writeJsonBody;
|
|
import std.datetime;
|
|
|
|
import profile.data;
|
|
import profile.service;
|
|
import profile.api : PROFILE_PATH;
|
|
import analytics.balances;
|
|
import util.money;
|
|
import util.data;
|
|
|
|
@PathMapping(HttpMethod.GET, PROFILE_PATH ~ "/analytics/category-spend-time-series")
|
|
void handleGetCategorySpendTimeSeries(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
auto ds = getProfileDataSource(request);
|
|
serveJsonFromProperty(response, ds, "analytics.categorySpendTimeSeries");
|
|
}
|
|
|
|
@GetMapping(PROFILE_PATH ~ "/analytics/balance-time-series")
|
|
void handleGetBalanceTimeSeriesV2(ref ServerHttpRequest request, ref ServerHttpResponse response) {
|
|
auto ds = getProfileDataSource(request);
|
|
Currency currency = Currency.ofCode(request.getParamAs!string("currency", Currencies.USD.code));
|
|
TimeRange timeRange = TimeRange(Optional!(SysTime).empty(), Optional!(SysTime).empty());
|
|
auto data = computeBalanceTimeSeriesV2(ds, currency, timeRange);
|
|
writeJsonBody(response, data);
|
|
}
|
|
|
|
/**
|
|
* Helper method to serve JSON analytics data to a client by fetching it
|
|
* directly from the user's profile properties table and writing it to the
|
|
* response.
|
|
* Params:
|
|
* response = The response to write to.
|
|
* ds = The datasource to serve data from.
|
|
* key = The name of the analytics property to read data from.
|
|
*/
|
|
private void serveJsonFromProperty(ref ServerHttpResponse response, ref ProfileDataSource ds, string key) {
|
|
PropertiesRepository propsRepo = ds.getPropertiesRepository();
|
|
string jsonStr = propsRepo.findProperty(key).orElse(null);
|
|
if (jsonStr is null || jsonStr.length == 0) {
|
|
response.status = HttpStatus.NOT_FOUND;
|
|
response.writeBodyString("No data found for " ~ key);
|
|
} else {
|
|
response.writeBodyString(jsonStr, ContentTypes.APPLICATION_JSON);
|
|
}
|
|
}
|