Skip to content

Commit

Permalink
performed some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas345 committed Nov 13, 2023
1 parent 9c5cb40 commit cdf28d7
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import de.hbt.pwr.model.*;
import de.hbt.pwr.model.clustering.ClusteredNetwork;
import de.hbt.pwr.model.clustering.ConsultantClusteringInfo;
import de.hbt.pwr.model.clustering.MetricType;
import de.hbt.pwr.model.profile.Consultant;
import de.hbt.pwr.model.profile.NameEntityType;
import de.hbt.pwr.service.AsyncInformationService;
import de.hbt.pwr.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -29,24 +27,10 @@ public class ProfileStatisticsController {

private final StatisticsService statisticsService;

private final AsyncInformationService asyncInformationService;


@Autowired
public ProfileStatisticsController(StatisticsService statisticsService, AsyncInformationService asyncInformationService) {
public ProfileStatisticsController(StatisticsService statisticsService) {
this.statisticsService = statisticsService;
this.asyncInformationService = asyncInformationService;
}

@RequestMapping(value = "", method = RequestMethod.HEAD)
public ResponseEntity<Void> available() {
return ResponseEntity.noContent().build();
}

@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<Void> refresh() {
asyncInformationService.invokeConsultantDataRefresh();
return ResponseEntity.noContent().build();
}

@RequestMapping(value = "/skill/usage/absolute", produces = "application/json")
Expand All @@ -69,11 +53,6 @@ public ResponseEntity<List<RelativeSkillUsage>> getRelativeMostUsedSkills(Intege
return ResponseEntity.ok(statisticsService.getRelativeMostUsedSkills(maxSkills));
}

@RequestMapping(value = "/skill/common", produces = "application/json")
public ResponseEntity<List<String>> getCommonSkills() {
return ResponseEntity.ok(statisticsService.getCommonSkills());
}

@RequestMapping(value = "/skill/common/{initials}", produces = "application/json")
public ResponseEntity<Map<String, List<String>>> getProfileSkillMetrics(@PathVariable("initials") String initials) {
List<String> common = new ArrayList<>();
Expand All @@ -95,39 +74,22 @@ public ResponseEntity<ClusteredNetwork> getClusteredNetwork() {
return ResponseEntity.ok(statisticsService.getClusteredNetwork());
}

/**
* Updates the parameters for the clustering process that is automated and invokes a re-clustering
* @param iterations the amount of iterations used for the k-medoid algorithm
* @param clusters the amount of expected clusters
* @param metric type to be used
* @return the newly clustered network.
*/
@RequestMapping(value = "/network/kmed", produces = "application/json", method = RequestMethod.POST)
public ResponseEntity<ClusteredNetwork> updateClusteredNetworkParams(
@RequestParam(value = "iterations", required = true) Integer iterations,
@RequestParam(value = "clusters", required = true) Integer clusters,
@RequestParam(value = "metric", required = true) MetricType metric
) {
statisticsService.updateConfigAndRenewNetwork(iterations, clusters, metric);
return ResponseEntity.ok(statisticsService.getClusteredNetwork());
}

@RequestMapping(value = "/network/consultant/{initials}", produces = "application/json")
public ResponseEntity<ConsultantClusteringInfo> getConsultantClusteringInfo(@PathVariable("initials") String initials){
return ResponseEntity.ok(statisticsService.getConsultantInfo(initials));
}

@GetMapping(value = "/entries/referencing", produces = "application/json")
public ResponseEntity<List<ConsultantInfo>> getConsultantsReferencingNameEntity(
@RequestParam(value = "name-entity", required = true) String nameEntityName,
@RequestParam(value = "type", required = true) NameEntityType type) {
@RequestParam(value = "name-entity") String nameEntityName,
@RequestParam(value = "type") NameEntityType type) {
List<Consultant> consultantLost = statisticsService.getAllConsultantsReferencingNameEntity(nameEntityName, type);
return ResponseEntity.ok(consultantLost.stream().map(ConsultantInfo::new).collect(Collectors.toList()));
}

@GetMapping(value = "/skill/referencing", produces = "application/json")
public ResponseEntity<List<ConsultantInfo>> getConsultantsReferencingSkill(
@RequestParam(value = "skill", required = true) String skillName) {
@RequestParam(value = "skill") String skillName) {
List<Consultant> consultants = statisticsService.getAllConsultantsReferencingSkill(skillName);
return ResponseEntity.ok(consultants.stream().map(ConsultantInfo::new).collect(Collectors.toList()));
}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/de/hbt/pwr/model/SimRank/ProfileNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import de.hbt.pwr.model.profile.Profile;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashSet;
import java.util.Set;

@NoArgsConstructor
public class ProfileNode {

@Getter
Expand All @@ -20,17 +22,10 @@ public class ProfileNode {
@JsonIgnore
private Set<SkillNode> skillNodes = new HashSet<>();

public ProfileNode() {
}

public ProfileNode(Profile profile) {
this.profileId = profile.getId();
}

/**
* Adds a skill node and also adds the skill nodes reference to this profile node to the skill node.
* @param skillNode
*/
public void addSkillNode(SkillNode skillNode) {
skillNodes.add(skillNode);
skillNode.addProfileNode(this);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/hbt/pwr/model/SimRank/ProfileSimilarity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

@Value
public class ProfileSimilarity {
private ProfileNode node1;
private ProfileNode node2;
ProfileNode node1;
ProfileNode node2;

private double simVal;
double simVal;

public ProfileSimilarity(ProfileNode node1, ProfileNode node2, double simVal) {
this.node1 = node1;
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/de/hbt/pwr/model/SimRank/SkillNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashSet;
import java.util.Set;

@NoArgsConstructor
public class SkillNode {

@Getter
Expand All @@ -18,17 +20,10 @@ public class SkillNode {
@JsonIgnore
private Set<ProfileNode> profileNodes = new HashSet<>();

public SkillNode() {
}

public SkillNode(String skillName) {
this.skillName = skillName;
}

/**
* Adds the profile node as backreference
* @param profileNode
*/
public void addProfileNode(ProfileNode profileNode) {
profileNodes.add(profileNode);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/hbt/pwr/model/SimRank/SkillSimilarity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

@Value
public class SkillSimilarity {
private SkillNode node1;
private SkillNode node2;
SkillNode node1;
SkillNode node2;

private double simVal;
double simVal;

public SkillSimilarity(SkillNode node1, SkillNode node2, double simVal) {
this.node1 = node1;
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/de/hbt/pwr/model/StatisticsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import de.hbt.pwr.model.clustering.MetricType;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

@RedisHash("StatisticsConfig")
@Data
public class StatisticsConfig {

Expand All @@ -14,12 +10,7 @@ public class StatisticsConfig {
public static final Integer DEFAULT_CLUSTERS = 2;
public static final MetricType DEFAULT_METRIC_TYPE = MetricType.SIM_RANK;


@Id
private String id;

private Integer currentKMedIterations = DEFAULT_ITERATIONS;
private Integer currentKMedClusters = DEFAULT_CLUSTERS;
private MetricType currentMetricType = DEFAULT_METRIC_TYPE;

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void evaluate() {
.filter(averagedSkill -> averagedSkill.getRelativeOccurance() >= 0.5)
.map(AveragedSkill::getName)
.collect(Collectors.toList());
Set<String> recommendationsSet = new HashSet<String>(commonSkills);
Set<String> recommendationsSet = new HashSet<>(commonSkills);
recommendationsSet.removeAll(consultant.getProfile().getSkills().stream().map(Skill::getName).collect(Collectors.toSet()));
recommendations = new ArrayList<>(recommendationsSet);
}
Expand All @@ -98,7 +98,7 @@ public void sort() {
private static class AveragedSkill {

@Getter
private String name;
private final String name;

@Getter
private int numOccurances;
Expand Down Expand Up @@ -130,8 +130,8 @@ public int compareRelative(AveragedSkill a) {
}

public void calc() {
this.average = (double) totalRating / (double) numOccurances;
this.average = totalRating / (double) numOccurances;
}

}
}
}
16 changes: 6 additions & 10 deletions src/main/java/de/hbt/pwr/model/clustering/ProfilePrototype.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.hbt.pwr.model.clustering;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -11,23 +10,22 @@

public class ProfilePrototype {

private static AtomicLong CURRENT_ID = new AtomicLong();
private static final AtomicLong CURRENT_ID = new AtomicLong();

@Getter
@Setter
private String name;
private final String name;

@Getter
private Long clusterId;
private final Long clusterId;

@Getter
private Set<SkillWrapper> skills = new HashSet<>();
private final Set<SkillWrapper> skills;

/**
* Used for evaluation
*/
@Getter
private List<ProfileClusterable> profileClusterables = new ArrayList<>();
private final List<ProfileClusterable> profileClusterables = new ArrayList<>();

/**
* List that keeps track of the currently assigned clusterables.
Expand Down Expand Up @@ -77,9 +75,7 @@ public ProfilePrototype evaluate() {
return empty();
}
Set<SkillWrapper> newSkills = new HashSet<>(profileClusterables.get(0).getValues());
profileClusterables.forEach(profileClusterable -> {
newSkills.retainAll(profileClusterable.getValues());
});
profileClusterables.forEach(profileClusterable -> newSkills.retainAll(profileClusterable.getValues()));
ProfilePrototype result = new ProfilePrototype(newSkills, name, clusterId);
result.assignedClusterables = profileClusterables;
return result;
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/de/hbt/pwr/model/clustering/SkillWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class SkillWrapper {

private Skill skill;
private final Skill skill;

public Skill getSkill() {
return skill;
Expand All @@ -14,10 +14,6 @@ public SkillWrapper(Skill skill) {
this.skill = skill;
}

public SkillWrapper(String name) {
this.skill = new Skill(name, 0);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ abstract public class CareerElement extends ProfileEntry {
public CareerElement() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public LocalDate getStartDate() {
return startDate;
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/de/hbt/pwr/repo/StatisticsConfigRepo.java

This file was deleted.

17 changes: 3 additions & 14 deletions src/main/java/de/hbt/pwr/service/AsyncInformationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import de.hbt.pwr.model.profile.Consultant;
import de.hbt.pwr.repo.ClusteredNetworkRepo;
import de.hbt.pwr.repo.ConsultantRepository;
import de.hbt.pwr.repo.StatisticsConfigRepo;
import de.hbt.pwr.statistics.KMedoidCommonSkillMetric;
import de.hbt.pwr.statistics.KMedoidMetric;
import de.hbt.pwr.statistics.KMedoidSimRankMetric;
Expand All @@ -33,24 +32,22 @@ public class AsyncInformationService {
private final ConsultantRepository consultantRepository;
private final PowerProfileClient powerProfileClient;
private final ClusteredNetworkRepo clusteredNetworkRepo;
private final StatisticsConfigRepo statisticsConfigRepo;


private static final Logger LOG = LoggerFactory.getLogger(AsyncInformationService.class);


@Autowired
public AsyncInformationService(ConsultantRepository consultantRepository,
PowerProfileClient powerProfileClient, ClusteredNetworkRepo clusteredNetworkRepo, StatisticsConfigRepo statisticsConfigRepo) {
PowerProfileClient powerProfileClient, ClusteredNetworkRepo clusteredNetworkRepo) {
this.consultantRepository = consultantRepository;
this.powerProfileClient = powerProfileClient;
this.clusteredNetworkRepo = clusteredNetworkRepo;
this.statisticsConfigRepo = statisticsConfigRepo;
}


private StatisticsConfig getConfig() {
return StreamUtils.asStream(statisticsConfigRepo.findAll().iterator()).findFirst().get();
return new StatisticsConfig(); // fixme@nt move this to config properties
}

@Async
Expand Down Expand Up @@ -83,7 +80,7 @@ void refreshStatistics() {
Map<Long, String> initialsByProfileId = new HashMap<>();
consultants.forEach(consultant -> initialsByProfileId.put(consultant.getProfile().getId(), consultant.getInitials()));

KMedoidMetric metric = null;
KMedoidMetric metric;
// Create the metric
switch (config.getCurrentMetricType()) {
case SIM_RANK:
Expand All @@ -110,20 +107,12 @@ void refreshStatistics() {
old.forEach(clusteredNetworkRepo::deleteById);
}

private void initConfigIfUninitialized() {
if(statisticsConfigRepo.count() <= 0) {
LOG.info("No Configuration stored in database. Storing default config.");
statisticsConfigRepo.save(new StatisticsConfig());
}
}


/**
* Invokes a refresh of the stored data
*/
@Scheduled(fixedRate = 60000L * 60L)
public void invokeConsultantDataRefresh() {
initConfigIfUninitialized();
refreshConsultantData();
refreshStatistics();
}
Expand Down
Loading

0 comments on commit cdf28d7

Please sign in to comment.