-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renaming to sustainalytics agg service + metadatas forwarding
- Loading branch information
goulven
authored and
goulven
committed
Dec 9, 2024
1 parent
816c1b4
commit 05ea121
Showing
3 changed files
with
116 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 0 additions & 76 deletions
76
...n4goods/api/services/aggregation/services/batch/scores/Brand2ScoreAggregationService.java
This file was deleted.
Oops, something went wrong.
114 changes: 114 additions & 0 deletions
114
...oods/api/services/aggregation/services/batch/scores/SustainalyticsAggregationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |