Skip to content

Commit

Permalink
Renaming to sustainalytics agg service + metadatas forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
goulven authored and goulven committed Dec 9, 2024
1 parent 816c1b4 commit 05ea121
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import org.open4goods.api.services.aggregation.aggregator.ScoringBatchedAggregator;
import org.open4goods.api.services.aggregation.aggregator.StandardAggregator;
import org.open4goods.api.services.aggregation.services.batch.scores.Attribute2ScoreAggregationService;
import org.open4goods.api.services.aggregation.services.batch.scores.Brand2ScoreAggregationService;
import org.open4goods.api.services.aggregation.services.batch.scores.CleanScoreAggregationService;
import org.open4goods.api.services.aggregation.services.batch.scores.DataCompletion2ScoreAggregationService;
import org.open4goods.api.services.aggregation.services.batch.scores.EcoScoreAggregationService;
import org.open4goods.api.services.aggregation.services.batch.scores.SustainalyticsAggregationService;
import org.open4goods.api.services.aggregation.services.realtime.AttributeRealtimeAggregationService;
import org.open4goods.api.services.aggregation.services.realtime.IdentityAggregationService;
import org.open4goods.api.services.aggregation.services.realtime.MediaAggregationService;
Expand Down Expand Up @@ -399,7 +399,7 @@ public ScoringBatchedAggregator getScoringAggregator() {

services.add(new CleanScoreAggregationService(logger));
services.add(new Attribute2ScoreAggregationService(logger));
services.add(new Brand2ScoreAggregationService( logger, brandService, verticalConfigService,brandScoreService));
services.add(new SustainalyticsAggregationService( logger, brandService, verticalConfigService,brandScoreService));
services.add(new DataCompletion2ScoreAggregationService(logger));
services.add(new EcoScoreAggregationService( logger));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package org.open4goods.api.services.aggregation.services.batch.scores;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.open4goods.commons.config.yml.ui.VerticalConfig;
import org.open4goods.commons.model.data.Brand;
import org.open4goods.commons.model.data.BrandScore;
import org.open4goods.commons.model.data.Score;
import org.open4goods.commons.model.product.Product;
import org.open4goods.commons.services.BrandScoreService;
import org.open4goods.commons.services.BrandService;
import org.open4goods.commons.services.VerticalsConfigService;
import org.slf4j.Logger;

/**
* Create a score based on brand sustainality evaluations
* TODO : Needs evolution to handle multiple brand score providers. (have to go through an intermediate score)
* @author goulven
*
*/
public class SustainalyticsAggregationService extends AbstractScoreAggregationService {

private static final String BRAND_SUSTAINABILITY_SCORENAME = "BRAND_SUSTAINABILITY";

public static final String RATING = "rating";

public static final String RISK_LEVEL = "risk-level";

private BrandService brandService;

private BrandScoreService brandScoreService;


public SustainalyticsAggregationService(final Logger logger, BrandService brandService, VerticalsConfigService verticalsConfigService, BrandScoreService brandScoreService) {
super(logger);
this.brandService = brandService;
this.brandScoreService = brandScoreService;
}


@Override
public void onProduct(Product data, VerticalConfig vConf) {

// Enforce score removing
data.getScores().remove(BRAND_SUSTAINABILITY_SCORENAME);

if (StringUtils.isEmpty(data.brand())) {
return;
}

try {

Brand brand = brandService.resolve(data.brand());
if (null == brand || StringUtils.isEmpty(brand.getCompanyName())) {
brandService.incrementUnknown(data.brand());
dedicatedLogger.warn("Cannot resolve brand or company for {}",data.brand());
return;
}

BrandScore brandResult = brandScoreService.getBrandScore(brand.getCompanyName(),"sustainalytics.com");
if (null == brandResult) {
dedicatedLogger.error("No score found for {} - {}",data.brand(), brand.getCompanyName());
return;
}
Double score = brandResult.getNormalized();

// Processing cardinality
incrementCardinality(BRAND_SUSTAINABILITY_SCORENAME,score);
Score s = new Score(BRAND_SUSTAINABILITY_SCORENAME, score);

// Setting metadatas
Map<String, String> metadatas = new HashMap<>();
metadatas.put(RATING, brandResult.getScoreValue());
metadatas.put(RISK_LEVEL, getRiskLevel(brandResult));

// Saving in product
data.getScores().put(s.getName(),s);
} catch (Exception e) {
dedicatedLogger.warn("Brand to score fail for {}",data,e);
}
}

/**
* Compute the sustainalytics risk level from sustainalytics range.
* For the official scale, see: https://www.sustainalytics.com/corporate-solutions/esg-solutions/esg-risk-ratings
*
* @param brandResult the result object containing the score value
* @return the risk level as a lowercase single word (e.g., negligible, low, medium, high, severe)
*/
private String getRiskLevel(BrandScore brandResult) {
Double sustainalyticsRating = Double.valueOf(brandResult.getScoreValue());

if (sustainalyticsRating == null) {
return "unknown";
}

if (sustainalyticsRating >= 0 && sustainalyticsRating <= 9.9) {
return "negligible";
} else if (sustainalyticsRating >= 10 && sustainalyticsRating <= 19.9) {
return "low";
} else if (sustainalyticsRating >= 20 && sustainalyticsRating <= 29.9) {
return "medium";
} else if (sustainalyticsRating >= 30 && sustainalyticsRating <= 39.9) {
return "high";
} else if (sustainalyticsRating >= 40) {
return "severe";
}

return "unknown";
}

}

0 comments on commit 05ea121

Please sign in to comment.