Merge pull request '74-add-jFuzzyLogic' (#75) from 74-add-jFuzzyLogic into master
Reviewed-on: #75
This commit is contained in:
commit
bf5220acd0
@ -35,6 +35,9 @@ compileJava {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
maven {
|
||||
url="https://repository.ow2.org/nexus/content/repositories/public"
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
@ -65,6 +68,7 @@ dependencies {
|
||||
|
||||
implementation group: 'com.ibm.icu', name: 'icu4j', version: '63.1'
|
||||
implementation group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '5.9.0.202009080501-r'
|
||||
implementation group: 'com.fuzzylite', name: 'jfuzzylite', version: '6.0.1'
|
||||
|
||||
implementation group: 'org.webjars', name: 'jquery', version: '3.6.0'
|
||||
implementation group: 'org.webjars', name: 'bootstrap', version: '4.6.0'
|
||||
|
@ -20,5 +20,5 @@ public interface AuthorRepository extends JpaRepository<Author, Integer> {
|
||||
List<Author> findByName(String name);
|
||||
|
||||
@Query("SELECT DISTINCT a.name FROM Commit c, Branch b, Author a WHERE c.author = a AND c.branch = b AND (:branchId IS NULL OR b.id = :branchId) AND a.name IS NOT NULL AND a.name <> '' ORDER BY a.name")
|
||||
List<String> findByBranchId(Integer branchId);
|
||||
List<String> findByBranchId(@Param("branchId") Integer branchId);
|
||||
}
|
||||
|
@ -11,12 +11,9 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@ControllerAdvice
|
||||
@ApiIgnore
|
||||
class GlobalDefaultExceptionHandler {
|
||||
@ -24,15 +21,15 @@ class GlobalDefaultExceptionHandler {
|
||||
public static final String DEFAULT_ERROR_VIEW = "error";
|
||||
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
|
||||
LOG.warn(e.getMessage());
|
||||
ModelAndView mav = new ModelAndView();
|
||||
mav.addObject("exception", e);
|
||||
mav.addObject("url", req.getRequestURL());
|
||||
mav.setViewName(DEFAULT_ERROR_VIEW);
|
||||
return mav;
|
||||
}
|
||||
// @ExceptionHandler(value = Exception.class)
|
||||
// public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
|
||||
// LOG.warn(e.getMessage());
|
||||
// ModelAndView mav = new ModelAndView();
|
||||
// mav.addObject("exception", e);
|
||||
// mav.addObject("url", req.getRequestURL());
|
||||
// mav.setViewName(DEFAULT_ERROR_VIEW);
|
||||
// return mav;
|
||||
// }
|
||||
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
|
@ -18,6 +18,7 @@ public class Route {
|
||||
public static final String STATISTIC = "statistic";
|
||||
public static final String LIST_RULE = "listRules";
|
||||
public static final String ADD_RULE = "addRule";
|
||||
public static final String RECOMMENDATIONS = "recommendations";
|
||||
public static final String DELETE_RULE = "deleteRule";
|
||||
|
||||
public static String getLIST_INDEXED_REPOSITORIES() {
|
||||
@ -39,4 +40,8 @@ public class Route {
|
||||
public static String getSTATISTIC() {
|
||||
return STATISTIC;
|
||||
}
|
||||
|
||||
public static String getRECOMMENDATIONS() {
|
||||
return RECOMMENDATIONS;
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,8 @@ public class IndexService {
|
||||
commits = gitRepositoryService.getCommits(repositoryUrl, branchName, commitsFrom, commitsTo, false);
|
||||
}
|
||||
Integer repositoryId = gitRepository.getId();
|
||||
timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(repositoryId, branchName));
|
||||
final Branch branchForSave = branch;
|
||||
timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(repositoryId, branchForSave));
|
||||
LOG.debug("Complete indexing {} branch", branchName);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
package ru.ulstu.extractor.http;
|
||||
|
||||
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||
|
||||
public class SmoothingTimeSeries {
|
||||
private JsonTimeSeries originalTimeSeries;
|
||||
private String methodClassName;
|
||||
|
||||
public SmoothingTimeSeries(TimeSeries timeSeries) {
|
||||
originalTimeSeries = new JsonTimeSeries(timeSeries);
|
||||
this.methodClassName = "FTransform";
|
||||
}
|
||||
|
||||
public JsonTimeSeries getOriginalTimeSeries() {
|
||||
return originalTimeSeries;
|
||||
}
|
||||
|
||||
public void setOriginalTimeSeries(JsonTimeSeries originalTimeSeries) {
|
||||
this.originalTimeSeries = originalTimeSeries;
|
||||
}
|
||||
|
||||
public String getMethodClassName() {
|
||||
return methodClassName;
|
||||
}
|
||||
|
||||
public void setMethodClassName(String methodClassName) {
|
||||
this.methodClassName = methodClassName;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package ru.ulstu.extractor.recommendation.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import ru.ulstu.extractor.branch.service.BranchService;
|
||||
import ru.ulstu.extractor.recommendation.model.FilterBranchForm;
|
||||
import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static ru.ulstu.extractor.core.Route.RECOMMENDATIONS;
|
||||
|
||||
@Controller
|
||||
@ApiIgnore
|
||||
public class RecommendationController {
|
||||
private final FuzzyInferenceService fuzzyInferenceService;
|
||||
private final BranchService branchService;
|
||||
|
||||
public RecommendationController(FuzzyInferenceService fuzzyInferenceService,
|
||||
BranchService branchService) {
|
||||
this.fuzzyInferenceService = fuzzyInferenceService;
|
||||
this.branchService = branchService;
|
||||
}
|
||||
|
||||
@GetMapping(RECOMMENDATIONS)
|
||||
public String getRecommendations(Model model, @RequestParam Optional<Integer> branchId) {
|
||||
model.addAttribute("branches", branchService.findAll());
|
||||
if (branchId.isPresent()) {
|
||||
model.addAttribute("recommendations", fuzzyInferenceService.getRecommendations(branchId.get()));
|
||||
model.addAttribute("filterBranchForm", new FilterBranchForm(branchId.get()));
|
||||
} else {
|
||||
model.addAttribute("filterBranchForm", new FilterBranchForm());
|
||||
}
|
||||
return RECOMMENDATIONS;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package ru.ulstu.extractor.recommendation.model;
|
||||
|
||||
public class FilterBranchForm {
|
||||
private Integer branchId;
|
||||
|
||||
public FilterBranchForm() {
|
||||
}
|
||||
|
||||
public FilterBranchForm(Integer branchId) {
|
||||
this.branchId = branchId;
|
||||
}
|
||||
|
||||
public Integer getBranchId() {
|
||||
return branchId;
|
||||
}
|
||||
|
||||
public void setBranchId(Integer branchId) {
|
||||
this.branchId = branchId;
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
import ru.ulstu.extractor.rule.model.AddRuleForm;
|
||||
import ru.ulstu.extractor.rule.service.AntecedentValueService;
|
||||
import ru.ulstu.extractor.rule.service.RuleService;
|
||||
import ru.ulstu.extractor.rule.service.DbRuleService;
|
||||
import ru.ulstu.extractor.ts.service.TimeSeriesService;
|
||||
import springfox.documentation.annotations.ApiIgnore;
|
||||
|
||||
@ -21,14 +21,14 @@ import static ru.ulstu.extractor.core.Route.LIST_RULE;
|
||||
@Controller
|
||||
@ApiIgnore
|
||||
public class RuleController {
|
||||
private final RuleService ruleService;
|
||||
private final DbRuleService ruleService;
|
||||
private final AntecedentValueService antecedentValueService;
|
||||
private final TimeSeriesService timeSeriesService;
|
||||
|
||||
public RuleController(RuleService ruleService,
|
||||
public RuleController(DbRuleService dbRuleService,
|
||||
AntecedentValueService antecedentValueService,
|
||||
TimeSeriesService timeSeriesService) {
|
||||
this.ruleService = ruleService;
|
||||
this.ruleService = dbRuleService;
|
||||
this.antecedentValueService = antecedentValueService;
|
||||
this.timeSeriesService = timeSeriesService;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public class AddRuleForm {
|
||||
public AddRuleForm() {
|
||||
}
|
||||
|
||||
public AddRuleForm(Rule rule) {
|
||||
public AddRuleForm(DbRule rule) {
|
||||
this.ruleId = rule.getId();
|
||||
this.firstAntecedentId = rule.getFirstAntecedent().name();
|
||||
this.secondAntecedentId = rule.getSecondAntecedent().name();
|
||||
|
@ -7,9 +7,11 @@ import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
public class Rule extends BaseEntity {
|
||||
@Table(name = "rule")
|
||||
public class DbRule extends BaseEntity {
|
||||
@ManyToOne
|
||||
private AntecedentValue firstAntecedentValue;
|
||||
|
||||
@ -24,14 +26,14 @@ public class Rule extends BaseEntity {
|
||||
|
||||
private String consequent;
|
||||
|
||||
public Rule() {
|
||||
public DbRule() {
|
||||
}
|
||||
|
||||
public Rule(AntecedentValue firstAntecedentValue,
|
||||
TimeSeriesType firstAntecedent,
|
||||
AntecedentValue secondAntecedentValue,
|
||||
TimeSeriesType secondAntecedent,
|
||||
String consequent) {
|
||||
public DbRule(AntecedentValue firstAntecedentValue,
|
||||
TimeSeriesType firstAntecedent,
|
||||
AntecedentValue secondAntecedentValue,
|
||||
TimeSeriesType secondAntecedent,
|
||||
String consequent) {
|
||||
this.firstAntecedentValue = firstAntecedentValue;
|
||||
this.firstAntecedent = firstAntecedent;
|
||||
this.secondAntecedentValue = secondAntecedentValue;
|
@ -1,7 +1,7 @@
|
||||
package ru.ulstu.extractor.rule.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.ulstu.extractor.rule.model.Rule;
|
||||
import ru.ulstu.extractor.rule.model.DbRule;
|
||||
|
||||
public interface RuleRepository extends JpaRepository<Rule, Integer> {
|
||||
public interface RuleRepository extends JpaRepository<DbRule, Integer> {
|
||||
}
|
||||
|
@ -2,34 +2,35 @@ package ru.ulstu.extractor.rule.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.extractor.rule.model.AddRuleForm;
|
||||
import ru.ulstu.extractor.rule.model.Rule;
|
||||
import ru.ulstu.extractor.rule.model.DbRule;
|
||||
import ru.ulstu.extractor.rule.repository.RuleRepository;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeriesType;
|
||||
import ru.ulstu.extractor.ts.service.TimeSeriesService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class RuleService {
|
||||
public class DbRuleService {
|
||||
private final RuleRepository ruleRepository;
|
||||
private final TimeSeriesService timeSeriesService;
|
||||
private final AntecedentValueService antecedentValueService;
|
||||
|
||||
public RuleService(RuleRepository ruleRepository,
|
||||
TimeSeriesService timeSeriesService,
|
||||
AntecedentValueService antecedentValueService) {
|
||||
public DbRuleService(RuleRepository ruleRepository,
|
||||
TimeSeriesService timeSeriesService,
|
||||
AntecedentValueService antecedentValueService) {
|
||||
this.ruleRepository = ruleRepository;
|
||||
this.timeSeriesService = timeSeriesService;
|
||||
this.antecedentValueService = antecedentValueService;
|
||||
}
|
||||
|
||||
public List<Rule> getList() {
|
||||
public List<DbRule> getList() {
|
||||
return ruleRepository.findAll();
|
||||
}
|
||||
|
||||
public void saveRule(AddRuleForm addRuleForm) {
|
||||
if (addRuleForm.getRuleId() != null) {
|
||||
Rule rule = ruleRepository.getOne(addRuleForm.getRuleId());
|
||||
DbRule rule = ruleRepository.getOne(addRuleForm.getRuleId());
|
||||
rule.setConsequent(addRuleForm.getConsequent());
|
||||
rule.setFirstAntecedent(TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()));
|
||||
rule.setFirstAntecedentValue(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()));
|
||||
@ -37,7 +38,7 @@ public class RuleService {
|
||||
rule.setSecondAntecedentValue(antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()));
|
||||
ruleRepository.save(rule);
|
||||
} else {
|
||||
ruleRepository.save(new Rule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()),
|
||||
ruleRepository.save(new DbRule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()),
|
||||
TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()),
|
||||
antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()),
|
||||
TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId()),
|
||||
@ -45,7 +46,7 @@ public class RuleService {
|
||||
}
|
||||
}
|
||||
|
||||
public Rule findById(Integer id) {
|
||||
public DbRule findById(Integer id) {
|
||||
return ruleRepository.getOne(id);
|
||||
}
|
||||
|
||||
@ -60,4 +61,8 @@ public class RuleService {
|
||||
public void deleteById(Integer id) {
|
||||
ruleRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public List<String> getConsequentList() {
|
||||
return ruleRepository.findAll().stream().map(DbRule::getConsequent).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,150 @@
|
||||
package ru.ulstu.extractor.rule.service;
|
||||
|
||||
import com.fuzzylite.Engine;
|
||||
import com.fuzzylite.activation.Highest;
|
||||
import com.fuzzylite.defuzzifier.Centroid;
|
||||
import com.fuzzylite.norm.s.BoundedSum;
|
||||
import com.fuzzylite.norm.s.Maximum;
|
||||
import com.fuzzylite.norm.t.AlgebraicProduct;
|
||||
import com.fuzzylite.rule.Rule;
|
||||
import com.fuzzylite.rule.RuleBlock;
|
||||
import com.fuzzylite.term.Triangle;
|
||||
import com.fuzzylite.variable.InputVariable;
|
||||
import com.fuzzylite.variable.OutputVariable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
|
||||
import ru.ulstu.extractor.rule.model.AntecedentValue;
|
||||
import ru.ulstu.extractor.rule.model.DbRule;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||
import ru.ulstu.extractor.ts.service.TimeSeriesService;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class FuzzyInferenceService {
|
||||
private final static String OUTPUT_VARIABLE_NAME = "state";
|
||||
private final static String RULE_TEMPLATE = "if %s is %s and %s is %s then "
|
||||
+ OUTPUT_VARIABLE_NAME
|
||||
+ " is %s";
|
||||
private final DbRuleService ruleService;
|
||||
private final AntecedentValueService antecedentValueService;
|
||||
private final GitRepositoryService gitRepositoryService;
|
||||
private final TimeSeriesService timeSeriesService;
|
||||
|
||||
public FuzzyInferenceService(DbRuleService ruleService,
|
||||
AntecedentValueService antecedentValueService,
|
||||
GitRepositoryService gitRepositoryService,
|
||||
TimeSeriesService timeSeriesService) {
|
||||
this.ruleService = ruleService;
|
||||
this.antecedentValueService = antecedentValueService;
|
||||
this.gitRepositoryService = gitRepositoryService;
|
||||
this.timeSeriesService = timeSeriesService;
|
||||
}
|
||||
|
||||
public List<String> getRulesFromDb(Map<String, Double> variableValues) {
|
||||
List<DbRule> dbDbRules = ruleService.getList();
|
||||
validateVariables(variableValues, dbDbRules);
|
||||
return dbDbRules.stream().map(this::getFuzzyRule).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String getFuzzyRule(DbRule dbRule) {
|
||||
return String.format(RULE_TEMPLATE,
|
||||
dbRule.getFirstAntecedent().name(),
|
||||
dbRule.getFirstAntecedentValue().getAntecedentValue(),
|
||||
dbRule.getSecondAntecedent().name(),
|
||||
dbRule.getSecondAntecedentValue().getAntecedentValue(),
|
||||
dbRule.getConsequent());
|
||||
}
|
||||
|
||||
private RuleBlock getRuleBlock(Engine engine,
|
||||
Map<String, Double> variableValues,
|
||||
List<AntecedentValue> antecedentValues,
|
||||
List<String> consequentValues) {
|
||||
variableValues.forEach((key, value) -> {
|
||||
InputVariable input = new InputVariable();
|
||||
input.setName(key);
|
||||
input.setDescription("");
|
||||
input.setEnabled(true);
|
||||
input.setRange(-0.1, antecedentValues.size() + 1.1);
|
||||
input.setLockValueInRange(false);
|
||||
for (int i = 0; i < antecedentValues.size(); i++) {
|
||||
input.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i - 0.1, i + 2.1));
|
||||
}
|
||||
engine.addInputVariable(input);
|
||||
});
|
||||
|
||||
OutputVariable output = new OutputVariable();
|
||||
output.setName(OUTPUT_VARIABLE_NAME);
|
||||
output.setDescription("");
|
||||
output.setEnabled(true);
|
||||
output.setRange(-0.1, antecedentValues.size() + 0.1);
|
||||
output.setAggregation(new Maximum());
|
||||
output.setDefuzzifier(new Centroid(100));
|
||||
output.setDefaultValue(Double.NaN);
|
||||
output.setLockValueInRange(false);
|
||||
for (int i = 0; i < consequentValues.size(); i++) {
|
||||
output.addTerm(new Triangle(consequentValues.get(i), i - 0.1, i + 2.1));
|
||||
}
|
||||
engine.addOutputVariable(output);
|
||||
|
||||
RuleBlock mamdani = new RuleBlock();
|
||||
mamdani.setName("mamdani");
|
||||
mamdani.setDescription("");
|
||||
mamdani.setEnabled(true);
|
||||
mamdani.setConjunction(new AlgebraicProduct());
|
||||
mamdani.setDisjunction(new BoundedSum());
|
||||
mamdani.setImplication(new AlgebraicProduct());
|
||||
mamdani.setActivation(new Highest());
|
||||
getRulesFromDb(variableValues).forEach(r -> mamdani.addRule(Rule.parse(r, engine)));
|
||||
return mamdani;
|
||||
}
|
||||
|
||||
private Engine getFuzzyEngine() {
|
||||
Engine engine = new Engine();
|
||||
engine.setName("Git rules");
|
||||
engine.setDescription("");
|
||||
return engine;
|
||||
}
|
||||
|
||||
public String getRecommendations(Integer branchId) {
|
||||
List<TimeSeries> timeSeries = timeSeriesService.getByBranch(branchId);
|
||||
Engine engine = getFuzzyEngine();
|
||||
List<AntecedentValue> antecedentValues = antecedentValueService.getList();
|
||||
List<String> consequentValues = ruleService.getConsequentList();
|
||||
Map<String, Double> variableValues = new HashMap<>();
|
||||
timeSeries.forEach(ts -> variableValues.put(ts.getTimeSeriesType().name(), timeSeriesService.getLastTimeSeriesTendency(ts)));
|
||||
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues, consequentValues));
|
||||
return getConsequent(engine, variableValues);
|
||||
}
|
||||
|
||||
private void validateVariables(Map<String, Double> variableValues, List<DbRule> dbDbRules) {
|
||||
for (DbRule dbRule : dbDbRules) {
|
||||
if (!variableValues.containsKey(dbRule.getFirstAntecedent().name())) {
|
||||
throw new RuntimeException(String.format("Переменной в правиле не задано значение (нет временного ряда): %s ",
|
||||
dbRule.getFirstAntecedent().name()));
|
||||
}
|
||||
if (!variableValues.containsKey(dbRule.getSecondAntecedent().name())) {
|
||||
throw new RuntimeException(String.format("Переменной в правиле не задано значение (нет временного ряда): %s ",
|
||||
dbRule.getSecondAntecedent().name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getConsequent(Engine engine, Map<String, Double> variableValues) {
|
||||
OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME);
|
||||
for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
|
||||
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
|
||||
inputVariable.setValue(variableValue.getValue());
|
||||
}
|
||||
engine.process();
|
||||
if (outputVariable != null) {
|
||||
outputVariable.defuzzify();
|
||||
}
|
||||
return (outputVariable == null || Double.isNaN(outputVariable.getValue()))
|
||||
? "Нет рекомендаций"
|
||||
: outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package ru.ulstu.extractor.ts.creator;
|
||||
|
||||
import ru.ulstu.extractor.branch.model.Branch;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeriesType;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
|
||||
@ -29,15 +30,15 @@ public abstract class AbstractTimeSeriesCreator {
|
||||
* Сохранить извлеченные временные ряды
|
||||
*
|
||||
* @param repositoryId
|
||||
* @param branchName
|
||||
* @param branch
|
||||
*/
|
||||
public void addTimeSeries(Integer repositoryId, String branchName) {
|
||||
public void addTimeSeries(Integer repositoryId, Branch branch) {
|
||||
// извлеченные временных рядов
|
||||
List<TimeSeries> timeSeries = getTimeSeries(repositoryId, branchName);
|
||||
List<TimeSeries> timeSeries = getTimeSeries(repositoryId, branch.getName());
|
||||
|
||||
// сгруппированные по временным интервалам точки временных рядов
|
||||
timeSeries.forEach(ts -> ts.setValues(mapTimeSeriesToInterval(getTimeSeriesService().getTimeSeriesInterval(), ts.getValues())));
|
||||
getTimeSeriesService().save(sortTimeSeries(timeSeries));
|
||||
getTimeSeriesService().save(sortTimeSeries(timeSeries), branch);
|
||||
}
|
||||
|
||||
private List<TimeSeries> sortTimeSeries(List<TimeSeries> timeSeries) {
|
||||
|
@ -1,7 +1,9 @@
|
||||
package ru.ulstu.extractor.ts.creator;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.ulstu.extractor.branch.service.BranchService;
|
||||
import ru.ulstu.extractor.commit.service.CommitService;
|
||||
import ru.ulstu.extractor.gitrepository.model.GitRepository;
|
||||
import ru.ulstu.extractor.gitrepository.service.GitRepositoryService;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeriesType;
|
||||
@ -17,24 +19,29 @@ public class CommitsTS extends AbstractTimeSeriesCreator {
|
||||
private final TimeSeriesService timeSeriesService;
|
||||
private final CommitService commitService;
|
||||
private final GitRepositoryService gitRepositoryService;
|
||||
private final BranchService branchService;
|
||||
|
||||
public CommitsTS(TimeSeriesService timeSeriesService,
|
||||
CommitService commitService,
|
||||
GitRepositoryService gitRepositoryService) {
|
||||
GitRepositoryService gitRepositoryService,
|
||||
BranchService branchService) {
|
||||
this.timeSeriesService = timeSeriesService;
|
||||
this.commitService = commitService;
|
||||
this.gitRepositoryService = gitRepositoryService;
|
||||
this.branchService = branchService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TimeSeries> getTimeSeries(Integer repositoryId, String branchName) {
|
||||
GitRepository gitRepository = gitRepositoryService.findById(repositoryId);
|
||||
//TODO: добавить постраничное чтение
|
||||
return Collections.singletonList(
|
||||
new TimeSeries(
|
||||
String.format("%s %s %s",
|
||||
gitRepositoryService.findById(repositoryId).getName(),
|
||||
gitRepository.getName(),
|
||||
branchName,
|
||||
getTimeSeriesType().getDescription()),
|
||||
branchService.findByRepositoryAndName(gitRepository, branchName),
|
||||
getTimeSeriesType(),
|
||||
commitService.findByRepositoryIdAndName(repositoryId, branchName)
|
||||
.stream()
|
||||
|
@ -2,6 +2,7 @@ package ru.ulstu.extractor.ts.model;
|
||||
|
||||
import org.hibernate.annotations.Fetch;
|
||||
import org.hibernate.annotations.FetchMode;
|
||||
import ru.ulstu.extractor.branch.model.Branch;
|
||||
import ru.ulstu.extractor.core.BaseEntity;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
@ -10,6 +11,7 @@ import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -25,22 +27,21 @@ public class TimeSeries extends BaseEntity {
|
||||
@Enumerated(EnumType.STRING)
|
||||
private TimeSeriesType timeSeriesType;
|
||||
|
||||
@ManyToOne
|
||||
private Branch branch;
|
||||
|
||||
public TimeSeries() {
|
||||
}
|
||||
|
||||
public TimeSeries(String name) {
|
||||
this.name = name;
|
||||
public TimeSeries(String name, Branch branch, TimeSeriesType timeSeriesType) {
|
||||
this(name, branch, timeSeriesType, new ArrayList<>());
|
||||
}
|
||||
|
||||
public TimeSeries(String name, List<TimeSeriesValue> values) {
|
||||
this.name = name;
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
public TimeSeries(String name, TimeSeriesType timeSeriesType, List<TimeSeriesValue> values) {
|
||||
public TimeSeries(String name, Branch branch, TimeSeriesType timeSeriesType, List<TimeSeriesValue> values) {
|
||||
this.name = name;
|
||||
this.values = values;
|
||||
this.timeSeriesType = timeSeriesType;
|
||||
this.branch = branch;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@ -66,4 +67,12 @@ public class TimeSeries extends BaseEntity {
|
||||
public void setTimeSeriesType(TimeSeriesType timeSeriesType) {
|
||||
this.timeSeriesType = timeSeriesType;
|
||||
}
|
||||
|
||||
public Branch getBranch() {
|
||||
return branch;
|
||||
}
|
||||
|
||||
public void setBranch(Branch branch) {
|
||||
this.branch = branch;
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,11 @@ package ru.ulstu.extractor.ts.repository;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface TimeSeriesRepository extends JpaRepository<TimeSeries, Integer> {
|
||||
Optional<TimeSeries> findByName(String name);
|
||||
|
||||
List<TimeSeries> getTimeSeriesByBranchId(Integer branchId);
|
||||
}
|
||||
|
@ -9,8 +9,10 @@ import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.extractor.branch.model.Branch;
|
||||
import ru.ulstu.extractor.http.HttpService;
|
||||
import ru.ulstu.extractor.http.JsonTimeSeries;
|
||||
import ru.ulstu.extractor.http.SmoothingTimeSeries;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeries;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeriesType;
|
||||
import ru.ulstu.extractor.ts.model.TimeSeriesValue;
|
||||
@ -31,7 +33,8 @@ public class TimeSeriesService {
|
||||
private final TimeSeriesValueRepository timeSeriesValueRepository;
|
||||
private final TimeSeriesDateMapper.TimeSeriesInterval timeSeriesInterval = TimeSeriesDateMapper.TimeSeriesInterval.HOUR;
|
||||
private final HttpService httpService;
|
||||
private final static String TIME_SERIES_SERVICE_URL = "http://time-series.athene.tech/api/1.0/add-time-series?setKey=git-extractor";
|
||||
private final static String TIME_SERIES_SAVE_SERVICE_URL = "http://time-series.athene.tech/api/1.0/add-time-series?setKey=git-extractor";
|
||||
private final static String TIME_SERIES_TENDENCY_URL = "http://time-series.athene.tech/api/1.0/getSpecificMethodSmoothed";
|
||||
|
||||
public TimeSeriesService(TimeSeriesRepository timeSeriesRepository,
|
||||
TimeSeriesValueRepository timeSeriesValueRepository,
|
||||
@ -42,12 +45,11 @@ public class TimeSeriesService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TimeSeries save(String timeSeriesName, TimeSeriesType timeSeriesType, List<TimeSeriesValue> timeSeriesValues) {
|
||||
public TimeSeries save(String timeSeriesName, Branch branch, TimeSeriesType timeSeriesType, List<TimeSeriesValue> timeSeriesValues) {
|
||||
LOG.debug("Start save {} time series with {} time series values ", timeSeriesName, timeSeriesValues.size());
|
||||
final TimeSeries timeSeries = findOrCreate(timeSeriesName);
|
||||
final TimeSeries timeSeries = findOrCreate(timeSeriesName, branch, timeSeriesType);
|
||||
List<TimeSeriesValue> timeSeriesValuesToRemove = timeSeries.getValues();
|
||||
timeSeries.setValues(timeSeriesValues);
|
||||
timeSeries.setTimeSeriesType(timeSeriesType);
|
||||
LOG.debug("Save time series {} ", timeSeries.getName());
|
||||
TimeSeries savedTimeSeries = timeSeriesRepository.save(timeSeries);
|
||||
LOG.debug("Clear {} time series values ", timeSeriesValuesToRemove.size());
|
||||
@ -56,13 +58,13 @@ public class TimeSeriesService {
|
||||
return savedTimeSeries;
|
||||
}
|
||||
|
||||
public TimeSeries findOrCreate(String timeSeriesName) {
|
||||
public TimeSeries findOrCreate(String timeSeriesName, Branch branch, TimeSeriesType timeSeriesType) {
|
||||
Optional<TimeSeries> maybeTimeSeries = timeSeriesRepository.findByName(timeSeriesName);
|
||||
if (maybeTimeSeries.isPresent()) {
|
||||
LOG.debug("TimeSeries {} exists.", maybeTimeSeries.get().getName());
|
||||
return maybeTimeSeries.get();
|
||||
}
|
||||
return timeSeriesRepository.save(new TimeSeries(timeSeriesName));
|
||||
return timeSeriesRepository.save(new TimeSeries(timeSeriesName, branch, timeSeriesType));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -72,10 +74,10 @@ public class TimeSeriesService {
|
||||
* @return сохраненный список ВР
|
||||
*/
|
||||
@Transactional
|
||||
public List<TimeSeries> save(List<TimeSeries> timeSeries) {
|
||||
public List<TimeSeries> save(List<TimeSeries> timeSeries, Branch branch) {
|
||||
List<TimeSeries> results = new ArrayList<>();
|
||||
for (TimeSeries ts : timeSeries) {
|
||||
results.add(save(ts.getName(), ts.getTimeSeriesType(), ts.getValues()));
|
||||
results.add(save(ts.getName(), branch, ts.getTimeSeriesType(), ts.getValues()));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
@ -87,7 +89,7 @@ public class TimeSeriesService {
|
||||
private void sendToTimeSeriesService(TimeSeries timeSeries) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
httpService.post(TIME_SERIES_SERVICE_URL, new JSONObject(new JsonTimeSeries(timeSeries)));
|
||||
httpService.post(TIME_SERIES_SAVE_SERVICE_URL, new JSONObject(new JsonTimeSeries(timeSeries)));
|
||||
LOG.debug("Успешно отправлен на сервис");
|
||||
} catch (Exception ex) {
|
||||
LOG.debug(ex.getMessage());
|
||||
@ -107,4 +109,15 @@ public class TimeSeriesService {
|
||||
public List<TimeSeriesType> getAllTimeSeriesTypes() {
|
||||
return Arrays.asList(TimeSeriesType.values());
|
||||
}
|
||||
|
||||
public List<TimeSeries> getByBranch(Integer branchId) {
|
||||
return timeSeriesRepository.getTimeSeriesByBranchId(branchId);
|
||||
}
|
||||
|
||||
public Double getLastTimeSeriesTendency(TimeSeries ts) {
|
||||
JSONObject response = httpService.post(TIME_SERIES_TENDENCY_URL, new JSONObject(new SmoothingTimeSeries(ts)));
|
||||
LOG.debug("Успешно отправлен на сервис сглаживания");
|
||||
response.get("timeSeries");
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
@ -2,12 +2,14 @@
|
||||
# Copyright (C) 2021 Anton Romanov - All Rights Reserved
|
||||
# You may use, distribute and modify this code, please write to: romanov73@gmail.com.
|
||||
#
|
||||
|
||||
spring.main.banner-mode=off
|
||||
server.port=8080
|
||||
server.jetty.connection-idle-timeout=1000s
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.ru.ulstu=DEBUG
|
||||
logging.level.sun.rmi.transport=off
|
||||
logging.level.javax.management.remote.rmi=off
|
||||
logging.level.java.rmi.server=off
|
||||
extractor.custom-projects-dir=
|
||||
# Thymeleaf Settings
|
||||
spring.thymeleaf.cache=false
|
||||
|
@ -80,4 +80,23 @@
|
||||
<column name="antecedent_value" value="рост"/>
|
||||
</insert>
|
||||
</changeSet>
|
||||
<changeSet author="orion" id="20221127-170000-1">
|
||||
<addColumn tableName="time_series">
|
||||
<column name="branch_id" type="integer"/>
|
||||
</addColumn>
|
||||
<addForeignKeyConstraint baseTableName="time_series" baseColumnNames="branch_id"
|
||||
constraintName="fk_time_series_branch" referencedTableName="branch"
|
||||
referencedColumnNames="id"/>
|
||||
</changeSet>
|
||||
<changeSet author="orion" id="20221127-170000-2">
|
||||
<sql>
|
||||
delete
|
||||
from time_series_value
|
||||
where time_series_id in (select id from time_series where time_series_type is null OR branch_id is null);
|
||||
delete
|
||||
from time_series
|
||||
where time_series_type is null
|
||||
OR branch_id is null;
|
||||
</sql>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
File diff suppressed because one or more lines are too long
@ -38,10 +38,10 @@
|
||||
<a class="nav-link" href="/statistic" th:text="#{messages.menu.statistic}">Link</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/recommendations" th:text="Рекомендации">Link</a>
|
||||
<a class="nav-link" href="/listRules" th:text="Правила">Link</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/listRules" th:text="Правила">Link</a>
|
||||
<a class="nav-link" href="/recommendations" th:text="Рекомендации">Link</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
@ -14,22 +14,22 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="rule: ${rules}">
|
||||
<tr th:each="dbRule: ${rules}">
|
||||
<td><span class="badge badge-info">Если</span></td>
|
||||
<td><span class="badge badge-success" th:text="${rule.firstAntecedent.description}"></span></td>
|
||||
<td><span class="badge badge-success" th:text="${rule.firstAntecedentValue.antecedentValue}"></span></td>
|
||||
<td><span class="badge badge-success" th:text="${dbRule.firstAntecedent.description}"></span></td>
|
||||
<td><span class="badge badge-success" th:text="${dbRule.firstAntecedentValue.antecedentValue}"></span></td>
|
||||
<td><span class="badge badge-info">и</span></td>
|
||||
<td><span class="badge badge-success" th:text="${rule.secondAntecedent.description }"></span></td>
|
||||
<td><span class="badge badge-success" th:text="${rule.secondAntecedentValue.antecedentValue}"></span></td>
|
||||
<td><span class="badge badge-success" th:text="${dbRule.secondAntecedent.description }"></span></td>
|
||||
<td><span class="badge badge-success" th:text="${dbRule.secondAntecedentValue.antecedentValue}"></span></td>
|
||||
<td><span class="badge badge-info">то</span></td>
|
||||
<td><span class="badge badge-warning" th:text="${rule.consequent}"></span></td>
|
||||
<td><span class="badge badge-warning" th:text="${dbRule.consequent}"></span></td>
|
||||
<td>
|
||||
<a role="button" class="btn btn-info" th:href="@{${@route.ADD_RULE} + '?ruleId=' + ${rule.id}}">
|
||||
<a role="button" class="btn btn-info" th:href="@{${@route.ADD_RULE} + '?ruleId=' + ${dbRule.id}}">
|
||||
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
<a role="button" class="btn btn-danger" th:href="@{'deleteRule?id=' + ${rule.id}}"
|
||||
<a role="button" class="btn btn-danger" th:href="@{'deleteRule?id=' + ${dbRule.id}}"
|
||||
onclick="return confirm('Удалить правило?')">
|
||||
<i class="fa fa-times" aria-hidden="true"></i>
|
||||
</a>
|
||||
|
@ -1,28 +1,42 @@
|
||||
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
|
||||
<html
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
|
||||
layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Простая обработка формы на Spring MVC</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||
</head>
|
||||
<div class="container" layout:fragment="content">
|
||||
<table class="table table-striped">
|
||||
<thead class="thead-dark">
|
||||
<tr>
|
||||
<th scope="col">Рекомендации</th>
|
||||
<th width="10%"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Увеличить колличество авторов</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Оценнить эффективность авторов</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<input type="submit" class="btn btn-outline-success" value="Применить правила"/>
|
||||
<form action="#" th:action="${@route.RECOMMENDATIONS}" th:object="${filterBranchForm}" method="get">
|
||||
<div class="row">
|
||||
<div class="col-md-2 col-sm-12">
|
||||
Репозиторий-ветка
|
||||
</div>
|
||||
<div class="col-md-6 col-sm-12">
|
||||
<select id="select-branch" class="selectpicker" data-live-search="true" th:field="*{branchId}"
|
||||
data-width="90%">
|
||||
<option value="">Все ветки</option>
|
||||
<option th:each="branch : ${branches}"
|
||||
th:value="${branch.id}"
|
||||
th:utext="${branch.gitRepository.url} + ' - '+ ${branch.name}">
|
||||
</option>
|
||||
</select>
|
||||
<script th:inline="javascript">
|
||||
$('#select-branch').val([[ * {branchId}]
|
||||
])
|
||||
;
|
||||
$('#select-branch').selectpicker('refresh');
|
||||
|
||||
</script>
|
||||
</div>
|
||||
<input type="submit" class="btn btn-outline-success w-100" value="Применить фильтр"/>
|
||||
</div>
|
||||
<div th:if="*{branchId == null}">Выбрерите ветку для получения рекомендаций</div>
|
||||
|
||||
<input type="hidden" th:field="*{branchId}">
|
||||
</form>
|
||||
<div th:each="recommendation: ${recommendations}">
|
||||
<div th:text="${recommendation}"></div>
|
||||
</div>
|
||||
</div>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user