Skip to content

Commit

Permalink
Ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Goulven.Furet authored and Goulven.Furet committed Oct 23, 2023
1 parent 5e1ac94 commit dc58fde
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
import org.open4goods.model.product.AggregatedFeature;
import org.open4goods.model.product.Product;
import org.open4goods.services.BrandService;
import org.open4goods.services.VerticalsConfigService;

public class AttributeRealtimeAggregationService extends AbstractRealTimeAggregationService {

private final AttributesConfig attributesConfig;


private final BrandService brandService;

public AttributeRealtimeAggregationService(final AttributesConfig attributesConfig, BrandService brandService, final String logsFolder,boolean toConsole) {
private VerticalsConfigService verticalConfigService;

public AttributeRealtimeAggregationService(final VerticalsConfigService verticalConfigService, BrandService brandService, final String logsFolder,boolean toConsole) {
super(logsFolder,toConsole);
this.attributesConfig = attributesConfig;
this.verticalConfigService = verticalConfigService;
this.brandService = brandService;
}

Expand All @@ -50,7 +53,13 @@ public AttributeRealtimeAggregationService(final AttributesConfig attributesConf
@Override
public void onDataFragment(final DataFragment dataFragment, final Product product) {

AttributesConfig attributesConfig = verticalConfigService.getConfigById(product.getVertical() == null ? "all" : product.getVertical() ).get().getAttributesConfig() ;

Set<String> toRemoveFromUnmatched = new HashSet<>();
// Adding the list of "to be removed" attributes
toRemoveFromUnmatched.addAll(attributesConfig.getExclusions());


/////////////////////////////////////////
// Converting to AggregatedAttributes for matches from config
/////////////////////////////////////////
Expand All @@ -62,7 +71,6 @@ public void onDataFragment(final DataFragment dataFragment, final Product produc
all.addAll(product.getAttributes().getUnmapedAttributes().stream().map(e -> new Attribute(e.getName(),e.getValue(),e.getLanguage())).toList());



for (Attribute attr : dataFragment.getAttributes()) {

// Checking if a potential AggregatedAttribute
Expand Down Expand Up @@ -110,7 +118,7 @@ public void onDataFragment(final DataFragment dataFragment, final Product produc
/////////////////////////////////////////

List<Attribute> matchedFeatures = dataFragment.getAttributes().stream()
.filter(this::isFeatureAttribute)
.filter(e -> isFeatureAttribute(e, attributesConfig))
.collect(Collectors.toList());

toRemoveFromUnmatched.addAll(matchedFeatures.stream().map(e->e.getName()).collect(Collectors.toSet()));
Expand Down Expand Up @@ -337,7 +345,7 @@ private Collection<AggregatedFeature> aggregateFeatures(List<Attribute> matchedF
* @param e
* @return
*/
private boolean isFeatureAttribute(Attribute e) {
private boolean isFeatureAttribute(Attribute e, AttributesConfig attributesConfig) {
return attributesConfig.getFeaturedValues().contains(e.getRawValue().toString().trim().toUpperCase());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void shutdown() {

/**
* List of services in the aggregator
*
*TODO : Replace confif with verticalConfigService, to have hot vertical config onDataFragment
* @param config
* @return
*/
Expand All @@ -131,15 +131,16 @@ RealTimeAggregator getAggregator(VerticalConfig config) {

services.add(new BarCodeAggregationService(apiProperties.logsFolder(), gs1prefixService,barcodeValidationService, apiProperties.isDedicatedLoggerToConsole()));

services.add(new AttributeRealtimeAggregationService(config.getAttributesConfig(), brandService, apiProperties.logsFolder(), apiProperties.isDedicatedLoggerToConsole()));
services.add(new VerticalRealTimeAggregationService( apiProperties.logsFolder(), verticalConfigService, apiProperties.isDedicatedLoggerToConsole()));

services.add(new AttributeRealtimeAggregationService(verticalConfigService, brandService, apiProperties.logsFolder(), apiProperties.isDedicatedLoggerToConsole()));


services.add(new NamesAggregationService(apiProperties.logsFolder(), apiProperties.isDedicatedLoggerToConsole()));

// services.add(new CategoryService(apiProperties.logsFolder(), taxonomyService));


services.add(new VerticalRealTimeAggregationService( apiProperties.logsFolder(), verticalConfigService, apiProperties.isDedicatedLoggerToConsole()));

services.add(new IdAggregationService( apiProperties.logsFolder(), apiProperties.isDedicatedLoggerToConsole()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

public class AttributesConfig {

// Local cache
private Map<String, Map<String, String>> cacheHashedSynonyms;
/**
* The specific configs configurations
*/
Expand Down Expand Up @@ -56,6 +58,9 @@ public class AttributesConfig {
@JsonIgnore
private Map<String, AttributeConfig> hashedAttributesByKey;




public AttributesConfig(Set<AttributeConfig> configs) {
this.configs = configs;

Expand All @@ -72,23 +77,27 @@ public AttributesConfig() {
*
* @return A map-ProviderKey,Synonym, Translated Key
*/
@Cacheable(cacheNames = CacheConstants.FOREVER_LOCAL_CACHE_NAME)
// Spring cache ineffectiv on internal calls
// @Cacheable(cacheNames = CacheConstants.FOREVER_LOCAL_CACHE_NAME)
public Map<String, Map<String, String>> synonyms() {

final Map<String, Map<String, String>> hashedSynonyms = new HashMap<>();

for (final AttributeConfig ac : configs) {
for (final Entry<String, Set<String>> entry : ac.getSynonyms().entrySet()) {
if (!hashedSynonyms.containsKey(entry.getKey())) {
hashedSynonyms.put(entry.getKey(), new HashMap<>());
}
for (final String val : entry.getValue()) {
hashedSynonyms.get(entry.getKey()).put(val, ac.getKey());
if (cacheHashedSynonyms == null) {

final Map<String, Map<String, String>> hashedSynonyms = new HashMap<>();

for (final AttributeConfig ac : configs) {
for (final Entry<String, Set<String>> entry : ac.getSynonyms().entrySet()) {
if (!hashedSynonyms.containsKey(entry.getKey())) {
hashedSynonyms.put(entry.getKey(), new HashMap<>());
}
for (final String val : entry.getValue()) {
hashedSynonyms.get(entry.getKey()).put(val, ac.getKey());
}
}
}
cacheHashedSynonyms = hashedSynonyms;
}

return hashedSynonyms;
return cacheHashedSynonyms;
}

public Attribute translateAttribute(final Attribute a, final String provider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.open4goods.config.yml.attributes.AttributeConfig;
import org.open4goods.exceptions.ValidationException;
import org.open4goods.model.attribute.Attribute;
Expand Down Expand Up @@ -63,6 +64,23 @@ public int sourcesCount() {
public long distinctValues () {
return sources.stream().map(e-> e.getValue()).distinct().count();
}

/**
* For UI, a String representation of all providers names
* @return
*/
public String providersToString() {
return StringUtils.join( sources.stream().map(e-> e.getKey()).toArray(),", ");
}

/**
* For UI, a String representation of all providers names and values
* @return
*/
public String sourcesToString() {
return StringUtils.join( sources.stream().map(e-> e.getKey() + ":"+e.getValue()).toArray(),", ");

}


public boolean hasConflicts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class AggregatedPrice extends Price {

@Field(index = false, store = false, type = FieldType.Keyword)
private String datasourceName;

@Field(index = false, store = false, type = FieldType.Keyword)
private String offerName;
@Field(index = false, store = false, type = FieldType.Keyword)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,11 @@ public VerticalSearchResponse verticalSearch(VerticalConfig vertical, VerticalSe
for (AttributeConfig attrConfig : customAttrFilters) {
esQuery = esQuery
// TODO : size from conf
.withAggregation(attrConfig.getKey(), Aggregation.of(a -> a.terms(ta -> ta.field("attributes.aggregatedAttributes."+attrConfig.getKey()+".value").missing(OTHER_BUCKET).size(100) )) );
.withAggregation(attrConfig.getKey(), Aggregation.of(a -> a.terms(ta -> ta.field("attributes.aggregatedAttributes."+attrConfig.getKey()+".value.keyword").missing(OTHER_BUCKET).size(100) )) );
}



// Adding custom range aggregations
for (NumericRangeFilter filter: request.getNumericFilters()) {
esQuery = esQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class VerticalsConfigService {
public static final Logger logger = LoggerFactory.getLogger(VerticalsConfigService.class);

private static final String DEFAULT_CONFIG_FILENAME = "_default.yml";
private static final String CLASSPATH_VERTICALS = "classpath:**verticals/*.yml";
private static final String CLASSPATH_VERTICAL_PREFIX = "classpath:verticals/";
// private static final String CLASSPATH_VERTICALS = "classpath:**verticals/*.yml";
// private static final String CLASSPATH_VERTICAL_PREFIX = "classpath:verticals/";

private SerialisationService serialisationService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.open4goods.services.SerialisationService;
import org.open4goods.services.VerticalsConfigService;
import org.open4goods.ui.config.yml.UiConfig;
import org.open4goods.ui.helper.UiHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -187,7 +188,9 @@ private ModelAndView buildProductView(String id, String vertical, final HttpServ


mv.addObject("product", data);
mv.addObject("verticalConfig", verticalConfigService.getVerticalForPath(vertical));

VerticalConfig verticalConfig = verticalConfigService.getVerticalForPath(vertical);
mv.addObject("verticalConfig", verticalConfig);



Expand All @@ -200,6 +203,9 @@ private ModelAndView buildProductView(String id, String vertical, final HttpServ
// Adding the brand informations
mv.addObject("hasBrandLogo", brandService.hasLogo(data.brand()));

// Adding the UiHelper class
mv.addObject("helper", new UiHelper(request, verticalConfig));

// Adding the images resource

return mv;
Expand Down
39 changes: 39 additions & 0 deletions ui/src/main/java/org/open4goods/ui/helper/UiHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.open4goods.ui.helper;

import org.open4goods.config.yml.ui.VerticalConfig;
import org.open4goods.services.VerticalsConfigService;

import jakarta.servlet.http.HttpServletRequest;

public class UiHelper {


private HttpServletRequest request;
private VerticalConfig verticalConfig;

public UiHelper(HttpServletRequest request, VerticalConfig verticalConfig) {
super();
this.request = request;
this.verticalConfig = verticalConfig;
}




/**
* Return the i18n for an attribute
* @param key
* @return
*/
public String attributeName(String key) {

return verticalConfig.getAttributesConfig().getAttributeConfigByKey(key).i18n(request.getLocale().getLanguage());


}





}
7 changes: 6 additions & 1 deletion ui/src/main/resources/i18n/messages_fr.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@

aria.navigation.primary = Navigation principale

attribute.sourcing = {0} source(s) de donn\u00E9e(s), {1} conflit(s)
attribute.sourcing.conflict = {1} conflits
attribute.sourcing.noconflict = Aucun conflit

attribute.sourcing.list = {0} source(s) de donn\u00E9e(s) ({2})



category.meta.description = {0} produits dans la cat\u00E9gorie {1} sont \u00E9ligibles \u00E0 la compensation carbone
category.meta.title = Produits de la cat\u00E9gorie {0}
Expand Down
4 changes: 3 additions & 1 deletion ui/src/main/resources/static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ CUSTOM
width: 90%;
}


.help-pointer:hover {
cursor : help;
}
/***********************************************
Xwiki css
**************************************************/
Expand Down
Binary file added ui/src/main/resources/static/icons/ko.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/src/main/resources/static/icons/ok.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions ui/src/main/resources/templates/inc/attribute-sourcing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<span th:with="conflicts = ${attribute.hasConflicts()}, sourcesCount=${attribute.sourcesCount()}, distinctValues=${attribute.distinctValues()}">

<!-- /** Conflicts **/ -->
<img th:if="${conflicts}" class="help-pointer" height="16" src="/icons/ko.png" data-bs-toggle="tooltip" data-bs-placement="top" th:title="#{attribute.sourcing.conflict(${attribute.sourcesCount()}, ${disctinctValues})}">
<!-- /** No conflicts, 1 source **/ -->
<img th:unless="${conflicts}" class="help-pointer" height="16" src="/icons/ok.png" data-bs-toggle="tooltip" data-bs-placement="top" th:title="#{attribute.sourcing.noconflict(${attribute.sourcesCount()}, ${disctinctValues})}">


<!-- /** 1 source **/ -->
<span th:if="${sourcesCount == 1}" class="help-pointer badge rounded-pill bg-info" th:text="${sourcesCount}" data-bs-toggle="tooltip" data-bs-placement="top" th:title="#{attribute.sourcing.list(${attribute.sourcesCount()}, ${disctinctValues}, ${attribute.providersToString()})}">1</span>
<!-- /** multiple source **/ -->
<span th:unless="${sourcesCount == 1}" class="help-pointer badge rounded-pill bg-success" th:text="${sourcesCount}" data-bs-toggle="tooltip" data-bs-placement="top" th:title="#{attribute.sourcing.list(${attribute.sourcesCount()}, ${disctinctValues}, ${attribute.sourcesToString()})})}">1</span>
</span>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 144 73" class="repair-label" th:attr="data-rate=${item.value}" role="img" aria-labelledby="repairTitle" focusable="false">
<title id="repairTitle" th:text="'Indice de réparabilité : '+${item.value}+' sur 10'"></title>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 144 73" class="repair-label" th:attr="data-rate=${attribute.value}" role="img" aria-labelledby="repairTitle" focusable="false">
<title id="repairTitle" th:text="'Indice de réparabilité : '+${attribute.value}+' sur 10'"></title>

<path fill="currentcolor" d="M131 .3c6.8 0 12.2 5.4 12.2 12.1v48.3c0 6.7-5.4 12.2-12.1 12.2H12.4C5.7 72.9.3 67.4.3 60.7V12.4C.3 5.7 5.7.3 12.4.3zm0 2H65.7a11.2 11.2 0 016 9.9v48.3c0 4.7-3 8.8-7.1 10.4H131c5.6 0 10.1-4.6 10.1-10.2V12.4c0-5.6-4.5-10.1-10.1-10.1z"></path>
<path fill="#fff" d="M57 49.5a5.5 5.5 0 010 7.9 5.5 5.5 0 01-7.8 0L38.5 46.6a10.4 10.4 0 01-9.7-2.8c-3-3-3.6-7.2-2.4-10.6 0-.3.5-.4.7-.2l6.2 6.2c.8.9 2.2.9 3 0l2.6-2.6c.9-.8.9-2.2 0-3l-6.2-6.2c-.2-.2-.1-.6.2-.7 3.4-1.2 7.6-.5 10.6 2.5a10.4 10.4 0 012.8 9.6l10.8 10.7zm6-17.8l-5-.8a22.3 22.3 0 00-2.7-6.5l3.2-4.3c.4-.5.3-1.4-.2-1.8L54 13.9c-.5-.4-1.3-.5-1.8-.1l-4.4 3.1a22.3 22.3 0 00-6.4-2.6l-.8-5c-.1-.8-.7-1.3-1.4-1.3H33c-.7 0-1.3.5-1.4 1.2l-.8 5a22.3 22.3 0 00-6.5 2.7l-4.3-3.1c-.5-.4-1.4-.3-1.8.1l-4.4 4.4c-.4.4-.5 1.3-.1 1.8l3.1 4.3a22.4 22.4 0 00-2.6 6.5l-5 .8c-.8 0-1.3.7-1.3 1.4v6c0 .8.5 1.4 1.2 1.5l5 .8a22.4 22.4 0 002.7 6.4l-3.1 4.4c-.4.5-.3 1.3.1 1.8l4.4 4.3c.4.5 1.3.6 1.8.2l4.3-3.2A22.3 22.3 0 0031 58l.8 5c0 .7.7 1.3 1.4 1.3h6c.8 0 1.4-.6 1.5-1.2l.8-5.1 2.3-.7-4.9-4.9a16.5 16.5 0 1113.5-13l4.8 4.8.9-2.8 5-.8c.7-.1 1.3-.7 1.3-1.4V33c0-.7-.6-1.3-1.2-1.4z"></path>
Expand Down
21 changes: 13 additions & 8 deletions ui/src/main/resources/templates/inc/product-attributes.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<div th:if="${product.attributes.aggregatedAttributes.size()} > 0" class="card shadow mb-3 my-3">
<div class="card-header"><h2 class="h5">Caractéristiques principales</h2></div>
<div th:if="${product.attributes.aggregatedAttributes.size()} > 0" class="card shadow">
<div class="card-header"><h2 class="h5"><span class="fa-solid fa-rectangle-list me-2"></span>Caractéristiques principales</h2></div>
<div class="card-body">
<table class="table table-hover">
<tbody>
<th:block th:each="item : ${product.attributes.aggregatedAttributes.values()}">
<tr data-bs-toggle="tooltip" data-bs-placement="top" th:title="#{attribute.sourcing(${item.sourcesCount()}, ${item.distinctValues()-1})}">
<th class="help-pointer" scope="row" th:text="${item.name}"></th>
<th:block th:switch="${item.name}">
<td class="help-pointer" th:case="'REPAIRABILITY_INDEX'" th:insert="~{inc/attributes/class_energy.html}"> </td>
<td class="help-pointer" th:case="*" th:text="${item.value}"></td>
<th:block th:each="attribute : ${product.attributes.aggregatedAttributes.values()}">
<tr>
<th class="help-pointer" scope="row" th:text="${helper.attributeName(attribute.name)}"></th>


<th:block th:switch="${attribute.name}">
<td class="lead help-pointer" th:case="'REPAIRABILITY_INDEX'" th:insert="~{inc/attributes/class_energy.html}"> </td>
<td class="lead help-pointer" th:case="*" th:text="${attribute.value}"></td>
</th:block>
<td class="float-end">
<th:block th:insert="~{inc/attribute-sourcing.html}"></th:block>
</td>
</tr>
</th:block>
</tbody>
Expand Down
3 changes: 1 addition & 2 deletions ui/src/main/resources/templates/inc/product-ecoscore.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@



<div class="nav-wrapper position-relative mb-2">
<ul class="nav nav-tabs nav-fill "
id="tabs-icons-text" role="tablist">
<li class="nav-item"><a class="nav-link mb-sm-3 mb-md-0 active"
Expand All @@ -30,7 +29,7 @@


</ul>
</div>




Expand Down
Loading

0 comments on commit dc58fde

Please sign in to comment.