Skip to content

Commit

Permalink
Categories updater feature
Browse files Browse the repository at this point in the history
  • Loading branch information
goulven authored and goulven committed Dec 6, 2024
1 parent 5855a28 commit 14c63ca
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,31 @@ public void generateCategoryMappingsFragment(
}




@GetMapping(path="/update/verticals/categoriesmapping")
@Operation(summary="Update the categories mapping directly in the files !")
@PreAuthorize("hasAuthority('"+RolesConstants.ROLE_ADMIN+"')")
public void updateVerticalsWithMappings(
@RequestParam (defaultValue = "3") Integer minOffers) throws ResourceNotFoundException, IOException {

//TODO(p2,conf) : from conf
verticalsGenService.updateVerticalsWithMappings("/home/goulven/git/open4goods/verticals/src/main/resources/verticals/",minOffers);

}

@GetMapping(path="/update/verticals/categoriesmapping/{vertical}")
@Operation(summary="Update the categories mapping for a given vertical directly in the file !")
@PreAuthorize("hasAuthority('"+RolesConstants.ROLE_ADMIN+"')")
public void updateVerticalWithMappings(
@RequestParam (defaultValue = "tv") String vertical,
@RequestParam (defaultValue = "3") Integer minOffers) throws ResourceNotFoundException, IOException {

//TODO(p2,conf) : from conf
verticalsGenService.updateVerticalFile(minOffers, "/home/goulven/git/open4goods/verticals/src/main/resources/verticals/"+vertical+".yml");

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

import com.fasterxml.jackson.core.type.TypeReference;

import io.micrometer.core.instrument.util.IOUtils;

public class VerticalsGenerationService {

private static final Logger LOGGER = LoggerFactory.getLogger(VerticalsGenerationService.class);
Expand Down Expand Up @@ -182,7 +184,6 @@ public void removeByAssociatedcategoryThreshold() {
if (entry.getValue() < threshold) {
iterator.remove(); // Safely remove entry from the map
}

}
});
}
Expand Down Expand Up @@ -483,4 +484,48 @@ public void verticalTemplatetoFile(String googleTaxonomyId, String matchingCateg
}
}

/**
* A hacky method that hard updates the categorys from predicted ones in the vertical yaml files
* @param verticalFolderPath
* @param minOffers
*/
public void updateVerticalsWithMappings(String verticalFolderPath, Integer minOffers) {
LOGGER.warn("Will update categories in vertical files. Be sure to review before publishing on github !");
List<File> files = Arrays.asList(new File(verticalFolderPath).listFiles());
files.stream().filter(e->e.getName().endsWith("yml")).forEach(file -> {
updateVerticalFile(minOffers, file.getAbsolutePath());
});

}

/**
* Update a vertical file with categorys from predicted ones in the vertical yaml files
* @param minOffers
* @param fileName
*/
public void updateVerticalFile(Integer minOffers, String fileName) {
File file = new File(fileName);
try {
VerticalConfig vc = verticalConfigservice.getConfigById(file.getName().substring(0, file.getName().length()-4));
LOGGER.warn("Will update {}",file.getName());
String originalContent = FileUtils.readFileToString(file);

int startIndex = originalContent.indexOf("matchingCategories:");
int endIndex = originalContent.indexOf("\n\n", startIndex);

String newContent = originalContent.substring(0, startIndex);
newContent += generateMapping(vc, minOffers);
newContent += originalContent.substring(endIndex);

FileUtils.writeStringToFile(file, newContent, Charset.defaultCharset());

} catch (IOException e1) {
LOGGER.error("Error while updaing vertical file {}",file,e1);
}
}





}

0 comments on commit 14c63ca

Please sign in to comment.