Skip to content
Snippets Groups Projects
Commit 6ac9b85d authored by Powers, Peter M.'s avatar Powers, Peter M.
Browse files

new service for mfd by setting and type

parent 116708da
No related branches found
No related tags found
2 merge requests!687Production Release | nshmp-haz,!686MFD service
package gov.usgs.earthquake.nshmp.www.source;
import java.util.Optional;
import gov.usgs.earthquake.nshmp.model.SourceType;
import gov.usgs.earthquake.nshmp.model.TectonicSetting;
import gov.usgs.earthquake.nshmp.www.NshmpMicronautServlet;
import gov.usgs.earthquake.nshmp.www.ResponseBody;
import gov.usgs.earthquake.nshmp.www.ServletUtil;
import gov.usgs.earthquake.nshmp.www.source.MfdsService.Metadata;
import gov.usgs.earthquake.nshmp.www.source.MfdsService.RequestData;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.inject.Inject;
/**
* Micronaut web service controller for summed MFDs in the installed model.
* These endpoints return the magnitude frequency distributions (MFDs)
* associated with the different tectonic settings and source types present in
* the model. MFDs for requested tetonic settings and source types are scaled by
* their branch weights and then summed.
*
* <p>See src/main/resources/application.yml nshmp-haz.model-path for installed
* model.
*
* <p>To run the Micronaut jar file with a model: java -jar
* path/to/nshmp-haz.jar --model=<path/to/model>
*
* @author U.S. Geological Survey
*/
@Tag(
name = MfdsService.NAME,
description = "USGS NSHM source model MFD service")
@Controller("/mfds")
public class MfdsController {
@Inject
private NshmpMicronautServlet servlet;
@Operation(
summary = "Source model tectonic settings and source types",
description = "Returns the tectonic settings and source types in the model",
operationId = "source-mfd-metadata")
@ApiResponse(
description = "Source MFD metadata",
responseCode = "200",
content = @Content(
schema = @Schema(
implementation = MetadataResponse.class)))
@Get
public HttpResponse<String> doGetMetadata(HttpRequest<?> http) {
try {
return LogicTreesService.getMetadata(http);
} catch (Exception e) {
return ServletUtil.error(
LogicTreesService.LOG, e,
LogicTreesService.NAME,
http.getUri().toString());
}
}
/**
* @param setting model tectonic setting
*/
@Operation(
summary = "Get a source model MFD for a tectonic setting",
description = "Returns the total MFD for the supplied tectonic setting",
operationId = "source-setting-mfd")
@ApiResponse(
description = "NSHM source MFD",
responseCode = "200",
content = @Content(
schema = @Schema(implementation = Response.class)))
@Get(uri = "/{setting}")
public HttpResponse<String> doGetMfdSettingGroup(
HttpRequest<?> http,
@PathVariable TectonicSetting setting) {
try {
return MfdsService.getMfdGroup(http, setting, Optional.empty());
} catch (Exception e) {
return ServletUtil.error(
LogicTreesService.LOG, e,
LogicTreesService.NAME,
http.getUri().toString());
}
}
/**
* @param setting model tectonic setting
* @param type model source type}
*/
@Operation(
summary = "Get a source model MFD for a tectonic setting and source type",
description = "Returns the total MFD for the supplied tectonic setting and source type",
operationId = "source-setting-type-mfd")
@ApiResponse(
description = "NSHM source MFD",
responseCode = "200",
content = @Content(
schema = @Schema(implementation = Response.class)))
@Get(uri = "/{setting}/{type}")
public HttpResponse<String> doGetMfdTypeGroup(
HttpRequest<?> http,
@PathVariable TectonicSetting setting,
@PathVariable SourceType type) {
try {
return MfdsService.getMfdGroup(http, setting, Optional.of(type));
} catch (Exception e) {
return ServletUtil.error(
LogicTreesService.LOG, e,
LogicTreesService.NAME,
http.getUri().toString());
}
}
// Swagger schema
private static class MetadataResponse extends ResponseBody<String, Metadata> {}
// Swagger schema
private static class Response extends ResponseBody<RequestData, Object> {}
}
package gov.usgs.earthquake.nshmp.www.source;
import java.util.Optional;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import gov.usgs.earthquake.nshmp.model.HazardModel;
import gov.usgs.earthquake.nshmp.model.Models;
import gov.usgs.earthquake.nshmp.model.SourceType;
import gov.usgs.earthquake.nshmp.model.TectonicSetting;
import gov.usgs.earthquake.nshmp.www.HazVersion;
import gov.usgs.earthquake.nshmp.www.ResponseBody;
import gov.usgs.earthquake.nshmp.www.ResponseMetadata;
import gov.usgs.earthquake.nshmp.www.ServletUtil;
import gov.usgs.earthquake.nshmp.www.source.SourceService.SourceModel;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import jakarta.inject.Singleton;
/**
* Source model MFD handler {@link MfdsController}
*
* @author U.S. Geological Survey
*/
@Singleton
public class MfdsService {
static final String NAME = "Model MFDs";
static final Logger LOG = LoggerFactory.getLogger(LogicTreesService.class);
public static HttpResponse<String> getMetadata(HttpRequest<?> request) {
var url = request.getUri().toString();
var metadata = new Metadata(ServletUtil.model());
var response = ResponseBody.usage()
.name(NAME)
.url(url)
.metadata(new ResponseMetadata(HazVersion.appVersions()))
.request(url)
.response(metadata)
.build();
return HttpResponse.ok(ServletUtil.GSON2.toJson(response));
}
public static HttpResponse<String> getMfdGroup(
HttpRequest<?> request,
TectonicSetting setting,
Optional<SourceType> type) {
var url = request.getUri().toString();
var mfd = Models.mfd(ServletUtil.model(), setting, type);
var requestData = new RequestData(setting, type.orElse(null));
var response = ResponseBody.success()
.name(NAME)
.url(url)
.metadata(new ResponseMetadata(HazVersion.appVersions()))
.request(requestData)
.response(mfd)
.build();
return HttpResponse.ok(ServletUtil.GSON2.toJson(response));
}
static class Metadata {
final SourceModel model;
final Set<TectonicSetting> settings;
final Set<SourceType> types;
Metadata(HazardModel model) {
this.model = new SourceModel(model);
this.settings = model.settings();
this.types = model.types();
}
public SourceModel getModel() {
return model;
}
public Set<TectonicSetting> getSettings() {
return settings;
}
public Set<SourceType> getTypes() {
return types;
}
}
static class RequestData {
final TectonicSetting setting;
final SourceType type;
RequestData(TectonicSetting setting, SourceType type) {
this.setting = setting;
this.type = type;
}
public TectonicSetting getSetting() {
return setting;
}
public SourceType getType() {
return type;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment