Search in sources :

Example 1 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class ImpactHandler method getLocationContributions.

@Rpc("get/impacts/contributions/locations")
public RpcResponse getLocationContributions(RpcRequest req) {
    return utils.contributionImpact(req, (result, impact, cache) -> {
        LocationResult r = new LocationResult(result, cache.db);
        List<Contribution<LocationDescriptor>> contributions = utils.toDescriptors(r.getContributions(impact));
        contributions = utils.filter(contributions, contribution -> contribution.amount != 0);
        return JsonRpc.encode(contributions, cache, json -> json.addProperty("unit", impact.referenceUnit));
    });
}
Also used : UpstreamNode(org.openlca.core.results.UpstreamNode) RpcRequest(org.openlca.ipc.RpcRequest) ImpactValue(org.openlca.core.results.ImpactValue) UpstreamTree(org.openlca.core.results.UpstreamTree) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) Rpc(org.openlca.ipc.Rpc) HashMap(java.util.HashMap) LocationResult(org.openlca.core.results.LocationResult) RpcResponse(org.openlca.ipc.RpcResponse) RootDescriptor(org.openlca.core.model.descriptors.RootDescriptor) ArrayList(java.util.ArrayList) StringPair(org.openlca.ipc.handlers.Upstream.StringPair) List(java.util.List) JsonArray(com.google.gson.JsonArray) EnviFlow(org.openlca.core.matrix.index.EnviFlow) ContributionResult(org.openlca.core.results.ContributionResult) Contribution(org.openlca.core.results.Contribution) Map(java.util.Map) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) Contribution(org.openlca.core.results.Contribution) LocationResult(org.openlca.core.results.LocationResult) Rpc(org.openlca.ipc.Rpc)

Example 2 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class ImpactHandler method getUpstream.

@Rpc("get/impacts/upstream")
public RpcResponse getUpstream(RpcRequest req) {
    return utils.fullImpact(req, (result, impact, cache) -> {
        List<StringPair> products = utils.parseProducts(req);
        UpstreamTree tree = result.getTree(impact);
        List<UpstreamNode> results = Upstream.calculate(tree, products);
        return JsonRpc.encode(results, tree, cache, json -> {
            json.addProperty("unit", impact.referenceUnit);
            json.add("upstream", json.remove("amount"));
        });
    });
}
Also used : StringPair(org.openlca.ipc.handlers.Upstream.StringPair) UpstreamTree(org.openlca.core.results.UpstreamTree) UpstreamNode(org.openlca.core.results.UpstreamNode) Rpc(org.openlca.ipc.Rpc)

Example 3 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class InventoryHandler method getUpstream.

@Rpc("get/inventory/upstream")
public RpcResponse getUpstream(RpcRequest req) {
    return utils.fullFlow(req, (result, flow, cache) -> {
        List<StringPair> products = utils.parseProducts(req);
        UpstreamTree tree = result.getTree(flow);
        List<UpstreamNode> results = Upstream.calculate(tree, products);
        String unit = utils.getUnit(flow, cache);
        return JsonRpc.encode(results, tree, cache, json -> {
            json.addProperty("unit", unit);
            json.add("upstream", json.remove("amount"));
        });
    });
}
Also used : StringPair(org.openlca.ipc.handlers.Upstream.StringPair) UpstreamTree(org.openlca.core.results.UpstreamTree) UpstreamNode(org.openlca.core.results.UpstreamNode) Rpc(org.openlca.ipc.Rpc)

Example 4 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class ModelHandler method getAll.

@Rpc("get/models")
public RpcResponse getAll(RpcRequest req) {
    if (req.params == null || !req.params.isJsonObject())
        return Responses.invalidParams("params must be an object with" + " valid @type attribute", req);
    var type = getType(req.params.getAsJsonObject());
    if (type == null)
        return Responses.invalidParams("params must be an object with" + " valid @type attribute", req);
    try {
        var store = new MemStore();
        var exp = new JsonExport(db, store).withReferences(false);
        Daos.refDao(db, type).getAll().forEach(exp::write);
        var array = new JsonArray();
        store.getAll(type).forEach(array::add);
        return Responses.ok(array, req);
    } catch (Exception e) {
        return Responses.serverError(e, req);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) MemStore(org.openlca.jsonld.MemStore) JsonExport(org.openlca.jsonld.output.JsonExport) Rpc(org.openlca.ipc.Rpc)

Example 5 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class CacheHandler method dispose.

@Rpc("dispose")
public RpcResponse dispose(RpcRequest req) {
    if (req.params == null || !req.params.isJsonObject())
        return Responses.error(400, "No object with '@id' provided", req);
    JsonObject param = req.params.getAsJsonObject();
    String id = Json.getString(param, "@id");
    if (id == null)
        return Responses.error(400, "No '@id' provided", req);
    boolean removed = cache.remove(id) != null;
    if (removed) {
        log.info("Removed {} from memory", id);
        return Responses.ok(req);
    }
    return Responses.ok("Did not find something with @id=" + id + "in memory; did nothing", req);
}
Also used : JsonObject(com.google.gson.JsonObject) Rpc(org.openlca.ipc.Rpc)

Aggregations

Rpc (org.openlca.ipc.Rpc)13 JsonArray (com.google.gson.JsonArray)6 JsonObject (com.google.gson.JsonObject)6 UpstreamNode (org.openlca.core.results.UpstreamNode)5 UpstreamTree (org.openlca.core.results.UpstreamTree)5 StringPair (org.openlca.ipc.handlers.Upstream.StringPair)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 FlowDescriptor (org.openlca.core.model.descriptors.FlowDescriptor)3 LocationDescriptor (org.openlca.core.model.descriptors.LocationDescriptor)3 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)3 RootDescriptor (org.openlca.core.model.descriptors.RootDescriptor)3 Contribution (org.openlca.core.results.Contribution)3 LocationResult (org.openlca.core.results.LocationResult)3 RpcRequest (org.openlca.ipc.RpcRequest)3 RpcResponse (org.openlca.ipc.RpcResponse)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EnviFlow (org.openlca.core.matrix.index.EnviFlow)2 ImpactDescriptor (org.openlca.core.model.descriptors.ImpactDescriptor)2