From f41ae7e53615f920b7f208072f60cad17ccb6962 Mon Sep 17 00:00:00 2001 From: BarminaA Date: Tue, 26 Apr 2022 13:02:26 +0400 Subject: [PATCH 01/10] #58 - Add TS storage --- .../ru/ulstu/extractor/model/TimeSeries.java | 39 ++++++++++++++++ .../extractor/model/TimeSeriesValue.java | 43 ++++++++++++++++++ .../db/changelog-20220422_120000-schema.xml | 44 +++++++++++++++++++ src/main/resources/db/changelog-master.xml | 1 + 4 files changed, 127 insertions(+) create mode 100644 src/main/java/ru/ulstu/extractor/model/TimeSeries.java create mode 100644 src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java create mode 100644 src/main/resources/db/changelog-20220422_120000-schema.xml diff --git a/src/main/java/ru/ulstu/extractor/model/TimeSeries.java b/src/main/java/ru/ulstu/extractor/model/TimeSeries.java new file mode 100644 index 0000000..2d90523 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/TimeSeries.java @@ -0,0 +1,39 @@ +package ru.ulstu.extractor.model; + +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Entity +public class TimeSeries extends BaseEntity { + private String name; + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @JoinColumn(name = "time_series_id") + @Fetch(FetchMode.SUBSELECT) + private List values = new ArrayList<>(); + + + public TimeSeries(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getValues() { + return values; + } + + public void setValues(List values) { + this.values = values; + } + +} diff --git a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java new file mode 100644 index 0000000..544db71 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java @@ -0,0 +1,43 @@ +package ru.ulstu.extractor.model; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.ManyToOne; +import java.util.Date; + +@Entity +public class TimeSeriesValue extends BaseEntity { + private Date date; + private Integer value; + @ManyToOne(fetch = FetchType.LAZY) + private TimeSeries timeSeries; + + public TimeSeriesValue(Date date, Integer value) { + this.date = date; + this.value = value; + } + + public Date getDate() { + return date; + } + + public Integer getValue() { + return value; + } + + public void setDate(Date date) { + this.date = date; + } + + public void setValue(Integer value) { + this.value = value; + } + + public TimeSeries getTimeSeriesType() { + return timeSeries; + } + + public void setTimeSeriesType(TimeSeries timeSeries) { + this.timeSeries = timeSeries; + } +} diff --git a/src/main/resources/db/changelog-20220422_120000-schema.xml b/src/main/resources/db/changelog-20220422_120000-schema.xml new file mode 100644 index 0000000..bec0b8d --- /dev/null +++ b/src/main/resources/db/changelog-20220422_120000-schema.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 514d679..b9c9b69 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -12,4 +12,5 @@ + -- 2.34.1 From 9a38056a94f5709c46bc4c82968b20b07d455833 Mon Sep 17 00:00:00 2001 From: BarminaA Date: Wed, 8 Jun 2022 15:44:53 +0400 Subject: [PATCH 02/10] #58-add AbstractTSExtractor --- .../extractor/model/TimeSeriesValue.java | 3 +- .../repository/TimeSeriesRepository.java | 10 +++ .../repository/TimeSeriesValueRepository.java | 13 ++++ .../extractor/service/TimeSeriesService.java | 75 +++++++++++++++++++ .../extractor/ts/AbstractTSExtractor.java | 28 +++++++ .../java/ru/ulstu/extractor/ts/AuthorTS.java | 34 +++++++++ 6 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/extractor/repository/TimeSeriesRepository.java create mode 100644 src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java create mode 100644 src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/AuthorTS.java diff --git a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java index 544db71..f4daaf4 100644 --- a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java +++ b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java @@ -12,7 +12,8 @@ public class TimeSeriesValue extends BaseEntity { @ManyToOne(fetch = FetchType.LAZY) private TimeSeries timeSeries; - public TimeSeriesValue(Date date, Integer value) { + public TimeSeriesValue(TimeSeries timeSeries, Date date, Integer value) { + this.timeSeries = timeSeries; this.date = date; this.value = value; } diff --git a/src/main/java/ru/ulstu/extractor/repository/TimeSeriesRepository.java b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesRepository.java new file mode 100644 index 0000000..e324774 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesRepository.java @@ -0,0 +1,10 @@ +package ru.ulstu.extractor.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.extractor.model.TimeSeries; + +import java.util.Optional; + +public interface TimeSeriesRepository extends JpaRepository { + Optional findByName(String name); +} diff --git a/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java new file mode 100644 index 0000000..8289b55 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java @@ -0,0 +1,13 @@ +package ru.ulstu.extractor.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.extractor.model.TimeSeries; +import ru.ulstu.extractor.model.TimeSeriesValue; + +import java.util.List; + +public interface TimeSeriesValueRepository extends JpaRepository { + TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name); + + List findTimeSeriesValueById(Integer repositoryId); +} diff --git a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java new file mode 100644 index 0000000..1054ce9 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + +package ru.ulstu.extractor.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.BaseEntity; +import ru.ulstu.extractor.model.TimeSeries; +import ru.ulstu.extractor.model.TimeSeriesValue; +import ru.ulstu.extractor.repository.TimeSeriesRepository; +import ru.ulstu.extractor.repository.TimeSeriesValueRepository; + +import java.util.Date; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service +public class TimeSeriesService { + private final static Logger LOG = LoggerFactory.getLogger(TimeSeriesService.class); + private final TimeSeriesRepository timeSeriesRepository; + private final TimeSeriesValueRepository timeSeriesValueRepository; + + + public TimeSeriesService(TimeSeriesRepository timeSeriesRepository, TimeSeriesValueRepository timeSeriesValueRepository) { + this.timeSeriesRepository = timeSeriesRepository; + this.timeSeriesValueRepository = timeSeriesValueRepository; + } + + public TimeSeries save(TimeSeries timeSeries, List timeSeriesValues) { + LOG.debug("Start save {} time series with {} time series values ", timeSeries.getName(), timeSeriesValues.size()); + List timeSeriesValuesToRemoveIds = timeSeries.getValues().stream().map(BaseEntity::getId).collect(Collectors.toList()); + timeSeries.setValues(timeSeriesValues); + LOG.debug("Save time series {} ", timeSeries.getName()); + timeSeries = timeSeriesRepository.save(timeSeries); + LOG.debug("Clear {} ime series values ", timeSeriesValuesToRemoveIds.size()); + return timeSeries; + } + + public TimeSeries findOrCreate(String timeSeriesName) { + Optional maybeTimeSeries = timeSeriesRepository.findByName(timeSeriesName); + if (maybeTimeSeries.isPresent()) { + LOG.debug("TimeSeries {} exists.", maybeTimeSeries.get().getName()); + return maybeTimeSeries.get(); + } + return timeSeriesRepository.save(new TimeSeries(timeSeriesName)); + } + + public List save(List timeSeriesValues) { + return timeSeriesValues.stream() + .map(timeSeriesValue -> { + timeSeriesValue.setValue((timeSeriesValue.getValue())); + timeSeriesValue.setDate((timeSeriesValue.getDate())); + return timeSeriesValueRepository.save(timeSeriesValue); + }).collect(Collectors.toList()); + } + + public void addTimeSeriesValue(String timeSeriesName, Date date, Integer value) { + LOG.debug("Start add time series values to {} time series values ", timeSeriesName); + TimeSeries timeSeries = findOrCreate(timeSeriesName); + timeSeriesValueRepository.save(new TimeSeriesValue(timeSeries, date, value)); + } + + public TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name) { + return timeSeriesValueRepository.findByTimeSeriesAndName(timeSeries, name); + } + + public List findAll() { + return timeSeriesValueRepository.findAll(); + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java new file mode 100644 index 0000000..e211df7 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java @@ -0,0 +1,28 @@ +package ru.ulstu.extractor.ts; + +public abstract class AbstractTSExtractor { + private String name; + private int value; + + public abstract void getAbstractTSName(); + + public abstract void addPoint(int value); + + public abstract void addTS(String name, int value); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java b/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java new file mode 100644 index 0000000..ef7014f --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java @@ -0,0 +1,34 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class AuthorTS extends AbstractTSExtractor { + private static String name = "AuthorTimeSeries"; + private static TimeSeriesService timeSeriesService; + + public static void setTimeSeriesService(TimeSeriesService timeSeriesService) { + AuthorTS.timeSeriesService = timeSeriesService; + } + + @Override + public void getAbstractTSName() { + timeSeriesService.findOrCreate(name); + } + + @Override + public void addPoint(int value) { + + } + + @Override + public void addTS(String name, int value) { + + } + + @Override + public String getName() { + return name; + } +} -- 2.34.1 From 5426253b390b4afe267124673bc96f768a1d534f Mon Sep 17 00:00:00 2001 From: BarminaA Date: Tue, 14 Jun 2022 16:19:41 +0400 Subject: [PATCH 03/10] #58-add new TS classes --- .../extractor/ts/AbstractTSExtractor.java | 26 +++++++------------ .../java/ru/ulstu/extractor/ts/AuthorTS.java | 25 +++++------------- .../ulstu/extractor/ts/AuthorsCommentTS.java | 23 ++++++++++++++++ .../extractor/ts/AuthorsCompletedIssueTS.java | 23 ++++++++++++++++ .../ru/ulstu/extractor/ts/AuthorsIssueTS.java | 23 ++++++++++++++++ .../java/ru/ulstu/extractor/ts/BranchTS.java | 23 ++++++++++++++++ .../java/ru/ulstu/extractor/ts/ClassTS.java | 23 ++++++++++++++++ .../java/ru/ulstu/extractor/ts/CommitsTS.java | 23 ++++++++++++++++ .../ru/ulstu/extractor/ts/DependenceTS.java | 23 ++++++++++++++++ .../java/ru/ulstu/extractor/ts/EntityTS.java | 23 ++++++++++++++++ .../java/ru/ulstu/extractor/ts/FileTS.java | 23 ++++++++++++++++ .../ru/ulstu/extractor/ts/InterfaceTS.java | 23 ++++++++++++++++ .../java/ru/ulstu/extractor/ts/IssuesTS.java | 23 ++++++++++++++++ .../java/ru/ulstu/extractor/ts/ProcessTS.java | 23 ++++++++++++++++ .../java/ru/ulstu/extractor/ts/StarTS.java | 23 ++++++++++++++++ 15 files changed, 316 insertions(+), 34 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/ts/AuthorsCommentTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/AuthorsCompletedIssueTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/AuthorsIssueTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/BranchTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/ClassTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/CommitsTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/DependenceTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/EntityTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/FileTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/InterfaceTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/IssuesTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/ProcessTS.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/StarTS.java diff --git a/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java index e211df7..e460ba4 100644 --- a/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java +++ b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java @@ -1,28 +1,22 @@ package ru.ulstu.extractor.ts; +import ru.ulstu.extractor.model.TimeSeriesValue; +import ru.ulstu.extractor.service.TimeSeriesService; + +import java.util.List; + public abstract class AbstractTSExtractor { - private String name; - private int value; - public abstract void getAbstractTSName(); + public abstract String getTSName(); - public abstract void addPoint(int value); + public abstract TimeSeriesService getTimeSeriesService(); - public abstract void addTS(String name, int value); - - public String getName() { - return name; + public void addPoint(int value) { } - public void setName(String name) { - this.name = name; + public void addTS(List values) { + getTimeSeriesService().findOrCreate(getTSName()); } - public int getValue() { - return value; - } - public void setValue(int value) { - this.value = value; - } } diff --git a/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java b/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java index ef7014f..a25a835 100644 --- a/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/AuthorTS.java @@ -5,30 +5,19 @@ import ru.ulstu.extractor.service.TimeSeriesService; @Component public class AuthorTS extends AbstractTSExtractor { - private static String name = "AuthorTimeSeries"; - private static TimeSeriesService timeSeriesService; + private final TimeSeriesService timeSeriesService; - public static void setTimeSeriesService(TimeSeriesService timeSeriesService) { - AuthorTS.timeSeriesService = timeSeriesService; + public AuthorTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; } @Override - public void getAbstractTSName() { - timeSeriesService.findOrCreate(name); + public String getTSName() { + return "Количество Авторов"; } @Override - public void addPoint(int value) { - - } - - @Override - public void addTS(String name, int value) { - - } - - @Override - public String getName() { - return name; + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; } } diff --git a/src/main/java/ru/ulstu/extractor/ts/AuthorsCommentTS.java b/src/main/java/ru/ulstu/extractor/ts/AuthorsCommentTS.java new file mode 100644 index 0000000..0a2feea --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/AuthorsCommentTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class AuthorsCommentTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public AuthorsCommentTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество коммитов авторов"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/AuthorsCompletedIssueTS.java b/src/main/java/ru/ulstu/extractor/ts/AuthorsCompletedIssueTS.java new file mode 100644 index 0000000..d6df385 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/AuthorsCompletedIssueTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class AuthorsCompletedIssueTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public AuthorsCompletedIssueTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество выполненных issues авторов"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/AuthorsIssueTS.java b/src/main/java/ru/ulstu/extractor/ts/AuthorsIssueTS.java new file mode 100644 index 0000000..a780b4f --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/AuthorsIssueTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class AuthorsIssueTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public AuthorsIssueTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество issues авторов"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/BranchTS.java b/src/main/java/ru/ulstu/extractor/ts/BranchTS.java new file mode 100644 index 0000000..6f6200c --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/BranchTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class BranchTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public BranchTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество веток"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/ClassTS.java b/src/main/java/ru/ulstu/extractor/ts/ClassTS.java new file mode 100644 index 0000000..4d68d4c --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/ClassTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class ClassTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public ClassTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество классов"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java new file mode 100644 index 0000000..af15290 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class CommitsTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public CommitsTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество коммитов во времени"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/DependenceTS.java b/src/main/java/ru/ulstu/extractor/ts/DependenceTS.java new file mode 100644 index 0000000..0df8357 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/DependenceTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class DependenceTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public DependenceTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество зависимостей"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/EntityTS.java b/src/main/java/ru/ulstu/extractor/ts/EntityTS.java new file mode 100644 index 0000000..dc687bf --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/EntityTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class EntityTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public EntityTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество сущностей"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/FileTS.java b/src/main/java/ru/ulstu/extractor/ts/FileTS.java new file mode 100644 index 0000000..5325dd4 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/FileTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class FileTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public FileTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество файлов"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/InterfaceTS.java b/src/main/java/ru/ulstu/extractor/ts/InterfaceTS.java new file mode 100644 index 0000000..9899c64 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/InterfaceTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class InterfaceTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public InterfaceTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество интерфейсов"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/IssuesTS.java b/src/main/java/ru/ulstu/extractor/ts/IssuesTS.java new file mode 100644 index 0000000..e565172 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/IssuesTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class IssuesTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public IssuesTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество issues созданных во времени"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return null; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/ProcessTS.java b/src/main/java/ru/ulstu/extractor/ts/ProcessTS.java new file mode 100644 index 0000000..03198fa --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/ProcessTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class ProcessTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public ProcessTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество процессов"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/StarTS.java b/src/main/java/ru/ulstu/extractor/ts/StarTS.java new file mode 100644 index 0000000..839e039 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/StarTS.java @@ -0,0 +1,23 @@ +package ru.ulstu.extractor.ts; + +import org.springframework.stereotype.Component; +import ru.ulstu.extractor.service.TimeSeriesService; + +@Component +public class StarTS extends AbstractTSExtractor { + private final TimeSeriesService timeSeriesService; + + public StarTS(TimeSeriesService timeSeriesService) { + this.timeSeriesService = timeSeriesService; + } + + @Override + public String getTSName() { + return "Количество звезд"; + } + + @Override + public TimeSeriesService getTimeSeriesService() { + return timeSeriesService; + } +} -- 2.34.1 From 08751b94523a3e85664665d5c52219a45c3efb1d Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 17 Jun 2022 16:33:53 +0400 Subject: [PATCH 04/10] #58 -- add scheduler --- .../extractor/GitExtractorApplication.java | 2 + .../extractor/branch/BranchRepository.java | 21 ++++++ .../{service => branch}/BranchService.java | 16 ++++- .../controller/BranchController.java | 14 ++-- .../controller/GitIndexingController.java | 2 +- .../extractor/controller/RepoController.java | 2 +- .../controller/RepositoryController.java | 14 ++-- .../controller/StatisticController.java | 2 +- .../GitRepositoryRepository.java} | 4 +- .../GitRepositoryService.java | 37 +++++----- .../controller/StructuralUnitController.java | 2 +- .../ulstu/extractor/loader/GitScheduler.java | 69 +++++++++++++++++++ .../repository/BranchRepository.java | 13 ---- .../repository/TimeSeriesValueRepository.java | 7 +- .../extractor/service/FilteringService.java | 12 ++-- .../ulstu/extractor/service/IndexService.java | 16 +++-- .../extractor/service/TimeSeriesService.java | 8 +-- .../extractor/ts/AbstractTSExtractor.java | 17 +++-- .../java/ru/ulstu/extractor/ts/CommitsTS.java | 11 ++- 19 files changed, 186 insertions(+), 83 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/branch/BranchRepository.java rename src/main/java/ru/ulstu/extractor/{service => branch}/BranchService.java (78%) rename src/main/java/ru/ulstu/extractor/{repository/RepositoryRepository.java => gitrepository/GitRepositoryRepository.java} (53%) rename src/main/java/ru/ulstu/extractor/{service => gitrepository}/GitRepositoryService.java (91%) create mode 100644 src/main/java/ru/ulstu/extractor/loader/GitScheduler.java delete mode 100644 src/main/java/ru/ulstu/extractor/repository/BranchRepository.java diff --git a/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java b/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java index 425424f..f9a272d 100644 --- a/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java +++ b/src/main/java/ru/ulstu/extractor/GitExtractorApplication.java @@ -2,8 +2,10 @@ package ru.ulstu.extractor; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class GitExtractorApplication { public static void main(String[] args) { SpringApplication.run(GitExtractorApplication.class, args); diff --git a/src/main/java/ru/ulstu/extractor/branch/BranchRepository.java b/src/main/java/ru/ulstu/extractor/branch/BranchRepository.java new file mode 100644 index 0000000..6034cb3 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/branch/BranchRepository.java @@ -0,0 +1,21 @@ +package ru.ulstu.extractor.branch; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.model.Repository; + +import java.util.List; + +public interface BranchRepository extends JpaRepository { + Branch findByRepositoryAndName(Repository repository, String name); + + @Query("select count(c) from Commit c LEFT JOIN c.branch b LEFT JOIN Repository r where r.id = ?1 AND b.name = ?2") + int getCommitsCount(Integer repositoryId, String name); + + List findByRepositoryId(Integer repositoryId); + + Page findByRepository(Repository repository, Pageable pageable); +} diff --git a/src/main/java/ru/ulstu/extractor/service/BranchService.java b/src/main/java/ru/ulstu/extractor/branch/BranchService.java similarity index 78% rename from src/main/java/ru/ulstu/extractor/service/BranchService.java rename to src/main/java/ru/ulstu/extractor/branch/BranchService.java index 8ce9353..8cae8e6 100644 --- a/src/main/java/ru/ulstu/extractor/service/BranchService.java +++ b/src/main/java/ru/ulstu/extractor/branch/BranchService.java @@ -3,16 +3,19 @@ * You may use, distribute and modify this code, please write to: romanov73@gmail.com. */ -package ru.ulstu.extractor.service; +package ru.ulstu.extractor.branch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.model.BaseEntity; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Repository; -import ru.ulstu.extractor.repository.BranchRepository; +import ru.ulstu.extractor.service.CommitService; import java.util.List; import java.util.stream.Collectors; @@ -29,6 +32,7 @@ public class BranchService { this.commitService = commitService; } + @Transactional public Branch save(Branch branch, List commits) { LOG.debug("Start save {} branch with {} commits ", branch.getName(), commits.size()); List commitsToRemoveIds = branch.getCommits().stream().map(BaseEntity::getId).collect(Collectors.toList()); @@ -52,6 +56,14 @@ public class BranchService { return branchRepository.findByRepositoryAndName(repository, branchName); } + public int getCommitsCount(Integer repositoryId, String branchName) { + return branchRepository.getCommitsCount(repositoryId, branchName); + } + + public Page findByRepository(Repository repository, Pageable pageable) { + return branchRepository.findByRepository(repository, pageable); + } + public List findAll() { return branchRepository.findAll(); } diff --git a/src/main/java/ru/ulstu/extractor/controller/BranchController.java b/src/main/java/ru/ulstu/extractor/controller/BranchController.java index 5e0f845..64ccecc 100644 --- a/src/main/java/ru/ulstu/extractor/controller/BranchController.java +++ b/src/main/java/ru/ulstu/extractor/controller/BranchController.java @@ -9,8 +9,8 @@ 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.repository.BranchRepository; -import ru.ulstu.extractor.repository.RepositoryRepository; +import ru.ulstu.extractor.branch.BranchRepository; +import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; import springfox.documentation.annotations.ApiIgnore; import static ru.ulstu.extractor.controller.Route.DELETE_BRANCH; @@ -19,11 +19,11 @@ import static ru.ulstu.extractor.controller.Route.LIST_REPOSITORY_BRANCHES; @Controller @ApiIgnore public class BranchController { - private final RepositoryRepository repositoryRepository; + private final GitRepositoryRepository gitRepositoryRepository; private final BranchRepository branchRepository; - public BranchController(RepositoryRepository repositoryRepository, BranchRepository branchRepository) { - this.repositoryRepository = repositoryRepository; + public BranchController(GitRepositoryRepository gitRepositoryRepository, BranchRepository branchRepository) { + this.gitRepositoryRepository = gitRepositoryRepository; this.branchRepository = branchRepository; } @@ -32,7 +32,7 @@ public class BranchController { Model model, @RequestParam int repositoryId) { model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId)); - model.addAttribute("repository", repositoryRepository.findById(repositoryId).get()); + model.addAttribute("repository", gitRepositoryRepository.findById(repositoryId).get()); return LIST_REPOSITORY_BRANCHES; } @@ -42,7 +42,7 @@ public class BranchController { @RequestParam Integer id) { branchRepository.deleteById(id); model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId)); - model.addAttribute("repository", repositoryRepository.findById(repositoryId).get()); + model.addAttribute("repository", gitRepositoryRepository.findById(repositoryId).get()); return LIST_REPOSITORY_BRANCHES; } } diff --git a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java index ce2433f..3102ba5 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java @@ -15,10 +15,10 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import ru.ulstu.extractor.gitrepository.GitRepositoryService; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.model.mvc.RepoForm; -import ru.ulstu.extractor.service.GitRepositoryService; import ru.ulstu.extractor.service.IndexService; import springfox.documentation.annotations.ApiIgnore; diff --git a/src/main/java/ru/ulstu/extractor/controller/RepoController.java b/src/main/java/ru/ulstu/extractor/controller/RepoController.java index 9be6c93..847003d 100644 --- a/src/main/java/ru/ulstu/extractor/controller/RepoController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RepoController.java @@ -12,8 +12,8 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.extractor.gitrepository.GitRepositoryService; import ru.ulstu.extractor.model.Commit; -import ru.ulstu.extractor.service.GitRepositoryService; import ru.ulstu.extractor.service.IndexService; import ru.ulstu.extractor.util.HttpUtils; diff --git a/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java b/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java index dd58781..ee5de82 100644 --- a/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java @@ -9,7 +9,7 @@ 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.repository.RepositoryRepository; +import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; import springfox.documentation.annotations.ApiIgnore; import static ru.ulstu.extractor.controller.Route.DELETE_INDEXED_REPOSITORY; @@ -18,23 +18,23 @@ import static ru.ulstu.extractor.controller.Route.LIST_INDEXED_REPOSITORIES; @Controller @ApiIgnore public class RepositoryController { - private final RepositoryRepository repositoryRepository; + private final GitRepositoryRepository gitRepositoryRepository; - public RepositoryController(RepositoryRepository repositoryRepository) { - this.repositoryRepository = repositoryRepository; + public RepositoryController(GitRepositoryRepository gitRepositoryRepository) { + this.gitRepositoryRepository = gitRepositoryRepository; } @GetMapping(LIST_INDEXED_REPOSITORIES) public String indexNewRepo(Model model) { - model.addAttribute("repositories", repositoryRepository.findAll()); + model.addAttribute("repositories", gitRepositoryRepository.findAll()); return LIST_INDEXED_REPOSITORIES; } @GetMapping(DELETE_INDEXED_REPOSITORY) public String deleteRepo(Model model, @RequestParam Integer id) { - repositoryRepository.deleteById(id); - model.addAttribute("repositories", repositoryRepository.findAll()); + gitRepositoryRepository.deleteById(id); + model.addAttribute("repositories", gitRepositoryRepository.findAll()); return "redirect:/" + LIST_INDEXED_REPOSITORIES; } } diff --git a/src/main/java/ru/ulstu/extractor/controller/StatisticController.java b/src/main/java/ru/ulstu/extractor/controller/StatisticController.java index e522c04..fd729ef 100644 --- a/src/main/java/ru/ulstu/extractor/controller/StatisticController.java +++ b/src/main/java/ru/ulstu/extractor/controller/StatisticController.java @@ -9,9 +9,9 @@ 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.BranchService; import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.repository.CommitRepository; -import ru.ulstu.extractor.service.BranchService; import ru.ulstu.extractor.service.FilteringService; import springfox.documentation.annotations.ApiIgnore; diff --git a/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java b/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryRepository.java similarity index 53% rename from src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java rename to src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryRepository.java index 21263f4..a94a59a 100644 --- a/src/main/java/ru/ulstu/extractor/repository/RepositoryRepository.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryRepository.java @@ -1,8 +1,8 @@ -package ru.ulstu.extractor.repository; +package ru.ulstu.extractor.gitrepository; import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.extractor.model.Repository; -public interface RepositoryRepository extends JpaRepository { +public interface GitRepositoryRepository extends JpaRepository { Repository findByUrl(String url); } diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryService.java similarity index 91% rename from src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java rename to src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryService.java index 6f85552..61eebf9 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryService.java @@ -1,9 +1,4 @@ -/* - * Copyright (C) 2021 Anton Romanov - All Rights Reserved - * You may use, distribute and modify this code, please write to: romanov73@gmail.com. - */ - -package ru.ulstu.extractor.service; +package ru.ulstu.extractor.gitrepository; import org.apache.commons.io.FileUtils; import org.eclipse.jgit.api.CreateBranchCommand; @@ -15,12 +10,13 @@ import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.ObjectReader; -import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.treewalk.TreeWalk; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit; import ru.ulstu.extractor.heuristic.model.EntityUnit; @@ -31,6 +27,7 @@ import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.FileChange; import ru.ulstu.extractor.model.LineChange; +import ru.ulstu.extractor.model.Repository; import java.io.ByteArrayOutputStream; import java.io.File; @@ -61,18 +58,20 @@ public class GitRepositoryService { private String customProjectsDir; private final ExecutorService executorService = Executors.newFixedThreadPool(4); private final ExecutorService executorServiceCommits = Executors.newFixedThreadPool(4); - private final StructuralUnitService structuralUnitService; + private final GitRepositoryRepository gitRepositoryRepository; - public GitRepositoryService(StructuralUnitService structuralUnitService) { + public GitRepositoryService(StructuralUnitService structuralUnitService, + GitRepositoryRepository gitRepositoryRepository) { this.structuralUnitService = structuralUnitService; + this.gitRepositoryRepository = gitRepositoryRepository; } public List getRemoteBranches(String url) throws GitAPIException, IOException { LOG.debug("Get remote branches of {}. Clone", url); cloneOrUpdateRepo(url); LOG.debug("Get remote branches of {}. Get branches", url); - Repository localRepo = new FileRepository(getProjectGitDirectory(url)); + org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(url)); Git git = new Git(localRepo); List branches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE) .call() @@ -88,7 +87,7 @@ public class GitRepositoryService { LOG.debug("Get local branches of {}. Clone", url); cloneOrUpdateRepo(url); LOG.debug("Get local branches of {}. Get branches", url); - Repository localRepo = new FileRepository(getProjectGitDirectory(url)); + org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(url)); Git git = new Git(localRepo); List branches = git.branchList() .call() @@ -105,7 +104,7 @@ public class GitRepositoryService { if (needUpdate) { cloneOrUpdateRepo(repositoryUrl, branchName); } - Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); + org.eclipse.jgit.lib.Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); Git git = new Git(localRepo); List commits = new ArrayList<>(); @@ -144,7 +143,7 @@ public class GitRepositoryService { return list; } - private void checkoutBranch(String repositoryUrl, Git git, Repository localRepo, String branchName) throws GitAPIException, IOException { + private void checkoutBranch(String repositoryUrl, Git git, org.eclipse.jgit.lib.Repository localRepo, String branchName) throws GitAPIException, IOException { LOG.debug("Checkout branch {} {}", repositoryUrl, branchName); git.pull().call(); if (!localRepo.getBranch().equals(branchName)) { @@ -192,7 +191,7 @@ public class GitRepositoryService { private void cloneOrUpdateRepo(String repositoryUrl, String branchName) throws GitAPIException, IOException { Git git; - Repository localRepo; + org.eclipse.jgit.lib.Repository localRepo; if (projectDirExists(getProjectDirectoryFile(repositoryUrl))) { localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl)); git = new Git(localRepo); @@ -240,7 +239,7 @@ public class GitRepositoryService { return file.exists(); } - private List findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) { + private List findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, org.eclipse.jgit.lib.Repository localRepo) { if (laterCommit == null || earlierCommit == null) { return null; } @@ -256,7 +255,7 @@ public class GitRepositoryService { return parseOutputDiff(output, localRepo, laterCommit); } - private List parseOutputDiff(String output, Repository repository, RevCommit commit) { + private List parseOutputDiff(String output, org.eclipse.jgit.lib.Repository repository, RevCommit commit) { List changes = new ArrayList<>(); String[] strings = output.split("\n"); FileChange fileChange = new FileChange(); @@ -322,7 +321,7 @@ public class GitRepositoryService { return changes; } - private String getContent(Repository repository, RevCommit commit, String path) { + private String getContent(org.eclipse.jgit.lib.Repository repository, RevCommit commit, String path) { try (TreeWalk treeWalk = TreeWalk.forPath(repository, path, commit.getTree())) { if (treeWalk != null) { ObjectId blobId = treeWalk.getObjectId(0); @@ -346,4 +345,8 @@ public class GitRepositoryService { } return Optional.empty(); } + + public Page findAll(Pageable pageable) { + return gitRepositoryRepository.findAll(pageable); + } } diff --git a/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java b/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java index 0939e08..ccedc05 100644 --- a/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java +++ b/src/main/java/ru/ulstu/extractor/heuristic/controller/StructuralUnitController.java @@ -11,10 +11,10 @@ import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import ru.ulstu.extractor.gitrepository.GitRepositoryService; import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit; import ru.ulstu.extractor.heuristic.model.EntityUnit; import ru.ulstu.extractor.heuristic.model.ResourceUnit; -import ru.ulstu.extractor.service.GitRepositoryService; import ru.ulstu.extractor.util.HttpUtils; import javax.servlet.http.HttpServletRequest; diff --git a/src/main/java/ru/ulstu/extractor/loader/GitScheduler.java b/src/main/java/ru/ulstu/extractor/loader/GitScheduler.java new file mode 100644 index 0000000..4a16fdc --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/loader/GitScheduler.java @@ -0,0 +1,69 @@ +package ru.ulstu.extractor.loader; + +import org.eclipse.jgit.api.errors.GitAPIException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.domain.Page; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.branch.BranchService; +import ru.ulstu.extractor.gitrepository.GitRepositoryService; +import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.model.OffsetablePageRequest; +import ru.ulstu.extractor.model.Repository; +import ru.ulstu.extractor.service.IndexService; +import ru.ulstu.extractor.ts.AbstractTSExtractor; + +import java.io.IOException; +import java.util.List; + +@Service +public class GitScheduler { + private final static int DEFAULT_PAGE_SIZE = 100; + private final Logger log = LoggerFactory.getLogger(GitScheduler.class); + private final GitRepositoryService gitRepositoryService; + private final BranchService branchService; + private final IndexService indexService; + private final List tsExtractors; + + public GitScheduler(GitRepositoryService gitRepositoryService, + BranchService branchService, + IndexService indexService, + List tsExtractors) { + this.gitRepositoryService = gitRepositoryService; + this.branchService = branchService; + this.indexService = indexService; + this.tsExtractors = tsExtractors; + } + + @Scheduled(cron = "* */5 * * * *") + public void getCommitsCount() { + log.debug("Load commits count started"); + int repositoryPageNumber = 0; + Page repositoryPage; + do { + int branchPageNumber = 0; + repositoryPage = gitRepositoryService.findAll( + new OffsetablePageRequest(repositoryPageNumber, DEFAULT_PAGE_SIZE)); + Page branchPage; + for (Repository repository : repositoryPage.getContent()) { + do { + branchPage = branchService.findByRepository(repository, + new OffsetablePageRequest(branchPageNumber, DEFAULT_PAGE_SIZE)); + for (Branch branch : branchPage.getContent()) { + try { + indexService.index(repository.getUrl(), branch.getName()); + tsExtractors.forEach(tsExtractor -> tsExtractor.addPoint(repository.getId(), branch.getName())); + } catch (GitAPIException | IOException ex) { + log.warn(ex.getMessage()); + ex.printStackTrace(); + } + } + branchPageNumber++; + } while (!branchPage.isEmpty()); + } + repositoryPageNumber++; + } while (!repositoryPage.isEmpty()); + log.debug("Load commits count finished"); + } +} diff --git a/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java b/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java deleted file mode 100644 index d16b1a2..0000000 --- a/src/main/java/ru/ulstu/extractor/repository/BranchRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -package ru.ulstu.extractor.repository; - -import org.springframework.data.jpa.repository.JpaRepository; -import ru.ulstu.extractor.model.Branch; -import ru.ulstu.extractor.model.Repository; - -import java.util.List; - -public interface BranchRepository extends JpaRepository { - Branch findByRepositoryAndName(Repository repository, String name); - - List findByRepositoryId(Integer repositoryId); -} diff --git a/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java index 8289b55..cad99ca 100644 --- a/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/TimeSeriesValueRepository.java @@ -1,13 +1,10 @@ package ru.ulstu.extractor.repository; import org.springframework.data.jpa.repository.JpaRepository; -import ru.ulstu.extractor.model.TimeSeries; import ru.ulstu.extractor.model.TimeSeriesValue; -import java.util.List; - public interface TimeSeriesValueRepository extends JpaRepository { - TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name); + //TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name); - List findTimeSeriesValueById(Integer repositoryId); + //List findTimeSeriesValueById(Integer repositoryId); } diff --git a/src/main/java/ru/ulstu/extractor/service/FilteringService.java b/src/main/java/ru/ulstu/extractor/service/FilteringService.java index 221f133..51802aa 100644 --- a/src/main/java/ru/ulstu/extractor/service/FilteringService.java +++ b/src/main/java/ru/ulstu/extractor/service/FilteringService.java @@ -9,10 +9,10 @@ import com.sun.istack.NotNull; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.repository.AuthorRepository; import ru.ulstu.extractor.repository.CommitRepository; -import ru.ulstu.extractor.repository.RepositoryRepository; import java.util.List; import java.util.Map; @@ -21,20 +21,20 @@ import java.util.Map; public class FilteringService { private final AuthorRepository authorRepository; private final CommitRepository commitRepository; - private final RepositoryRepository repositoryRepository; + private final GitRepositoryRepository gitRepositoryRepository; public FilteringService(AuthorRepository authorRepository, CommitRepository commitRepository, - RepositoryRepository repositoryRepository) { + GitRepositoryRepository gitRepositoryRepository) { this.authorRepository = authorRepository; this.commitRepository = commitRepository; - this.repositoryRepository = repositoryRepository; + this.gitRepositoryRepository = gitRepositoryRepository; } public List getRepositoryAuthors(@NotNull String repositoryUrl, @NotNull String branchName) { return authorRepository.findByRepositoryAndBranch( - repositoryRepository.findByUrl(repositoryUrl), + gitRepositoryRepository.findByUrl(repositoryUrl), branchName ); } @@ -51,7 +51,7 @@ public class FilteringService { Pageable pageable) { return commitRepository.findByRepositoryAndBranch( pageable, - repositoryRepository.findByUrl(repositoryUrl), + gitRepositoryRepository.findByUrl(repositoryUrl), branchName, author, filter, diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java index 3cd9644..86f9454 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -10,10 +10,13 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.extractor.branch.BranchService; +import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; +import ru.ulstu.extractor.gitrepository.GitRepositoryService; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Repository; -import ru.ulstu.extractor.repository.RepositoryRepository; import java.io.IOException; import java.util.Collections; @@ -24,21 +27,22 @@ public class IndexService { private final static Logger LOG = LoggerFactory.getLogger(IndexService.class); private final static int COMMITS_PAGE_SIZE = 10; private final GitRepositoryService gitRepositoryService; - private final RepositoryRepository repositoryRepository; + private final GitRepositoryRepository gitRepositoryRepository; private final BranchService branchService; public IndexService(GitRepositoryService gitRepositoryService, - RepositoryRepository repositoryRepository, + GitRepositoryRepository gitRepositoryRepository, BranchService branchService) { this.gitRepositoryService = gitRepositoryService; - this.repositoryRepository = repositoryRepository; + this.gitRepositoryRepository = gitRepositoryRepository; this.branchService = branchService; } + @Transactional public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { - Repository repository = repositoryRepository.findByUrl(repositoryUrl); + Repository repository = gitRepositoryRepository.findByUrl(repositoryUrl); if (repository == null) { - repository = repositoryRepository.save(new Repository(repositoryUrl)); + repository = gitRepositoryRepository.save(new Repository(repositoryUrl)); } Branch branch = branchService.findByRepositoryAndName(repository, branchName); if (branch == null) { diff --git a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java index 1054ce9..5653c3e 100644 --- a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java @@ -64,10 +64,10 @@ public class TimeSeriesService { TimeSeries timeSeries = findOrCreate(timeSeriesName); timeSeriesValueRepository.save(new TimeSeriesValue(timeSeries, date, value)); } - - public TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name) { - return timeSeriesValueRepository.findByTimeSeriesAndName(timeSeries, name); - } +// +// public TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name) { +// return timeSeriesValueRepository.findByTimeSeriesAndName(timeSeries, name); +// } public List findAll() { return timeSeriesValueRepository.findAll(); diff --git a/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java index e460ba4..14271ee 100644 --- a/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java +++ b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java @@ -1,22 +1,21 @@ package ru.ulstu.extractor.ts; -import ru.ulstu.extractor.model.TimeSeriesValue; import ru.ulstu.extractor.service.TimeSeriesService; -import java.util.List; +import java.util.Date; public abstract class AbstractTSExtractor { public abstract String getTSName(); + public abstract int getNewTSValue(Integer repositoryId, String branchName); + public abstract TimeSeriesService getTimeSeriesService(); - public void addPoint(int value) { + public void addPoint(Integer repositoryId, String branchName) { + getTimeSeriesService().addTimeSeriesValue( + String.format("%s %s %s", getTSName(), repositoryId, branchName), + new Date(), + getNewTSValue(repositoryId, branchName)); } - - public void addTS(List values) { - getTimeSeriesService().findOrCreate(getTSName()); - } - - } diff --git a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java index af15290..a50f988 100644 --- a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java @@ -1,14 +1,18 @@ package ru.ulstu.extractor.ts; import org.springframework.stereotype.Component; +import ru.ulstu.extractor.branch.BranchService; import ru.ulstu.extractor.service.TimeSeriesService; @Component public class CommitsTS extends AbstractTSExtractor { private final TimeSeriesService timeSeriesService; + private final BranchService branchService; - public CommitsTS(TimeSeriesService timeSeriesService) { + public CommitsTS(TimeSeriesService timeSeriesService, + BranchService branchService) { this.timeSeriesService = timeSeriesService; + this.branchService = branchService; } @Override @@ -16,6 +20,11 @@ public class CommitsTS extends AbstractTSExtractor { return "Количество коммитов во времени"; } + @Override + public int getNewTSValue(Integer repositoryId, String branchName) { + return branchService.getCommitsCount(repositoryId, branchName); + } + @Override public TimeSeriesService getTimeSeriesService() { return timeSeriesService; -- 2.34.1 From 4f8d25e56e07304261e90ce98efe48e70d755e38 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 20 Jun 2022 23:45:40 +0400 Subject: [PATCH 05/10] #58 -- add tests, add time series mapper --- build.gradle | 1 + .../ru/ulstu/extractor/model/TimeSeries.java | 13 ++- .../extractor/model/TimeSeriesValue.java | 13 ++- .../extractor/ts/TimeSeriesDateMapper.java | 59 ++++++++++ .../java/ru/ulstu/TimeSeriesMapperTest.java | 103 ++++++++++++++++++ 5 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java create mode 100644 src/test/java/ru/ulstu/TimeSeriesMapperTest.java diff --git a/build.gradle b/build.gradle index 1d713ee..04deaca 100644 --- a/build.gradle +++ b/build.gradle @@ -61,6 +61,7 @@ dependencies { implementation group: 'commons-io', name: 'commons-io', version: '2.6' implementation group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.35.0' implementation group: 'com.github.javaparser', name: 'javaparser-core', version: '3.20.2' + implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0' implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0' diff --git a/src/main/java/ru/ulstu/extractor/model/TimeSeries.java b/src/main/java/ru/ulstu/extractor/model/TimeSeries.java index 2d90523..d04db07 100644 --- a/src/main/java/ru/ulstu/extractor/model/TimeSeries.java +++ b/src/main/java/ru/ulstu/extractor/model/TimeSeries.java @@ -3,7 +3,11 @@ package ru.ulstu.extractor.model; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; -import javax.persistence.*; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; import java.util.ArrayList; import java.util.List; @@ -15,11 +19,18 @@ public class TimeSeries extends BaseEntity { @Fetch(FetchMode.SUBSELECT) private List values = new ArrayList<>(); + public TimeSeries() { + } public TimeSeries(String name) { this.name = name; } + public TimeSeries(String name, List values) { + this.name = name; + this.values = values; + } + public String getName() { return name; } diff --git a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java index f4daaf4..3efd621 100644 --- a/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java +++ b/src/main/java/ru/ulstu/extractor/model/TimeSeriesValue.java @@ -12,6 +12,15 @@ public class TimeSeriesValue extends BaseEntity { @ManyToOne(fetch = FetchType.LAZY) private TimeSeries timeSeries; + public TimeSeriesValue() { + } + + public TimeSeriesValue(Date date, Integer value) { + this.timeSeries = timeSeries; + this.date = date; + this.value = value; + } + public TimeSeriesValue(TimeSeries timeSeries, Date date, Integer value) { this.timeSeries = timeSeries; this.date = date; @@ -34,11 +43,11 @@ public class TimeSeriesValue extends BaseEntity { this.value = value; } - public TimeSeries getTimeSeriesType() { + public TimeSeries getTimeSeries() { return timeSeries; } - public void setTimeSeriesType(TimeSeries timeSeries) { + public void setTimeSeries(TimeSeries timeSeries) { this.timeSeries = timeSeries; } } diff --git a/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java b/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java new file mode 100644 index 0000000..d192b53 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java @@ -0,0 +1,59 @@ +package ru.ulstu.extractor.ts; + +import org.apache.commons.lang3.time.DateUtils; +import ru.ulstu.extractor.model.TimeSeries; +import ru.ulstu.extractor.model.TimeSeriesValue; + +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Класс для регулировки дискретности временного ряда. + * Можно создать временной ряд с точками с секундными отметками, затем суммировать значения, + * применив одно из значений TimeSeriesInterval + */ +public class TimeSeriesDateMapper { + + public TimeSeries mapTimeSeriesToInterval(TimeSeriesInterval timeSeriesInterval, TimeSeries timeSeries) { + List trimmedTimeSeriesValues = timeSeries.getValues() + .stream() + .map(timeSeriesValue -> new TimeSeriesValue(trimTo(timeSeriesInterval, timeSeriesValue.getDate()), + timeSeriesValue.getValue())) + .collect(Collectors.toList()); + Map groupedTimeSeriesValues = trimmedTimeSeriesValues + .stream() + .collect(Collectors.groupingBy(TimeSeriesValue::getDate, + Collectors.summingInt(TimeSeriesValue::getValue))); + + return new TimeSeries(timeSeries.getName(), groupedTimeSeriesValues.entrySet() + .stream() + .map(e -> new TimeSeriesValue(e.getKey(), e.getValue())) + .collect(Collectors.toList())); + } + + private Date trimTo(TimeSeriesInterval timeSeriesInterval, Date date) { + return DateUtils.truncate(date, timeSeriesInterval.calendarField); + } + + /** + * Интервальность временного ряда при преобразовании + */ + public enum TimeSeriesInterval { + SECOND(Calendar.SECOND), + MINUTE(Calendar.MINUTE), + HOUR(Calendar.HOUR_OF_DAY), + DAY(Calendar.DAY_OF_MONTH), + WEEK(Calendar.WEEK_OF_MONTH), + MONTH(Calendar.MONTH), + YEAR(Calendar.YEAR); + + private final int calendarField; + + TimeSeriesInterval(int calendarField) { + this.calendarField = calendarField; + } + } +} diff --git a/src/test/java/ru/ulstu/TimeSeriesMapperTest.java b/src/test/java/ru/ulstu/TimeSeriesMapperTest.java new file mode 100644 index 0000000..91030f9 --- /dev/null +++ b/src/test/java/ru/ulstu/TimeSeriesMapperTest.java @@ -0,0 +1,103 @@ +package ru.ulstu; + +import org.junit.Assert; +import org.junit.Test; +import ru.ulstu.extractor.model.TimeSeries; +import ru.ulstu.extractor.model.TimeSeriesValue; +import ru.ulstu.extractor.ts.TimeSeriesDateMapper; + +import java.util.Arrays; +import java.util.Calendar; +import java.util.GregorianCalendar; + +public class TimeSeriesMapperTest { + @Test + public void testMappingByDay() { + Calendar c1 = GregorianCalendar.getInstance(); + c1.set(2020, 5, 1, 1, 1, 1); + Calendar c2 = GregorianCalendar.getInstance(); + c2.set(2020, 5, 2, 2, 1, 1); + TimeSeries timeSeries = new TimeSeries("Тестовый", + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), + new TimeSeriesValue(c2.getTime(), 10))); + TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); + timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); + Assert.assertEquals(1, timeSeries.getValues().size()); + Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue()); + } + + @Test + public void testMappingByDayDifferent() { + Calendar c1 = GregorianCalendar.getInstance(); + c1.set(2020, 5, 1, 1, 1, 1); + Calendar c2 = GregorianCalendar.getInstance(); + c2.set(2020, 5, 2, 1, 1, 1); + TimeSeries timeSeries = new TimeSeries("Тестовый", + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), + new TimeSeriesValue(c2.getTime(), 10))); + TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); + timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); + Assert.assertEquals(1, timeSeries.getValues().size()); + Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue()); + } + + @Test + public void testMappingByMonth() { + Calendar c1 = GregorianCalendar.getInstance(); + c1.set(2020, 5, 1, 1, 1, 1); + Calendar c2 = GregorianCalendar.getInstance(); + c2.set(2020, 5, 2, 1, 1, 1); + TimeSeries timeSeries = new TimeSeries("Тестовый", + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), + new TimeSeriesValue(c2.getTime(), 10))); + TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); + timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); + Assert.assertEquals(1, timeSeries.getValues().size()); + Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue()); + } + + @Test + public void testMappingByMonthDifferent() { + Calendar c1 = GregorianCalendar.getInstance(); + c1.set(2020, 5, 1, 1, 1, 1); + Calendar c2 = GregorianCalendar.getInstance(); + c2.set(2020, 6, 2, 1, 1, 1); + TimeSeries timeSeries = new TimeSeries("Тестовый", + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), + new TimeSeriesValue(c2.getTime(), 10))); + TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); + timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); + Assert.assertEquals(2, timeSeries.getValues().size()); + Assert.assertEquals(Integer.valueOf(10), timeSeries.getValues().get(0).getValue()); + } + + @Test + public void testMappingByYear() { + Calendar c1 = GregorianCalendar.getInstance(); + c1.set(2020, 5, 1, 1, 1, 1); + Calendar c2 = GregorianCalendar.getInstance(); + c2.set(2020, 5, 2, 1, 1, 1); + TimeSeries timeSeries = new TimeSeries("Тестовый", + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), + new TimeSeriesValue(c2.getTime(), 10))); + TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); + timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries); + Assert.assertEquals(1, timeSeries.getValues().size()); + Assert.assertEquals(Integer.valueOf(20), timeSeries.getValues().get(0).getValue()); + } + + @Test + public void testMappingByYearDifferent() { + Calendar c1 = GregorianCalendar.getInstance(); + c1.set(2020, 5, 1, 1, 1, 1); + Calendar c2 = GregorianCalendar.getInstance(); + c2.set(2021, 5, 2, 1, 1, 1); + TimeSeries timeSeries = new TimeSeries("Тестовый", + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), + new TimeSeriesValue(c2.getTime(), 10))); + TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); + timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries); + Assert.assertEquals(2, timeSeries.getValues().size()); + Assert.assertEquals(Integer.valueOf(10), timeSeries.getValues().get(0).getValue()); + } +} -- 2.34.1 From 156cdbc67230bbbfc16f2e9c5a78224d750056d2 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 21 Jun 2022 00:51:52 +0400 Subject: [PATCH 06/10] #58 -- save time series on indexing --- .../BranchController.java | 3 +- .../ulstu/extractor/branch/BranchService.java | 10 --- .../ulstu/extractor/loader/GitScheduler.java | 69 ------------------- .../repository/CommitRepository.java | 3 + .../extractor/service/CommitService.java | 5 ++ .../ulstu/extractor/service/IndexService.java | 10 ++- .../extractor/service/TimeSeriesService.java | 21 +++--- .../extractor/ts/AbstractTSExtractor.java | 21 ------ .../ts/AbstractTimeSeriesCreator.java | 22 ++++++ .../java/ru/ulstu/extractor/ts/CommitsTS.java | 23 ++++--- .../extractor/ts/TimeSeriesDateMapper.java | 15 ++-- 11 files changed, 75 insertions(+), 127 deletions(-) rename src/main/java/ru/ulstu/extractor/{controller => branch}/BranchController.java (95%) delete mode 100644 src/main/java/ru/ulstu/extractor/loader/GitScheduler.java delete mode 100644 src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java create mode 100644 src/main/java/ru/ulstu/extractor/ts/AbstractTimeSeriesCreator.java diff --git a/src/main/java/ru/ulstu/extractor/controller/BranchController.java b/src/main/java/ru/ulstu/extractor/branch/BranchController.java similarity index 95% rename from src/main/java/ru/ulstu/extractor/controller/BranchController.java rename to src/main/java/ru/ulstu/extractor/branch/BranchController.java index 64ccecc..47f823e 100644 --- a/src/main/java/ru/ulstu/extractor/controller/BranchController.java +++ b/src/main/java/ru/ulstu/extractor/branch/BranchController.java @@ -3,13 +3,12 @@ * You may use, distribute and modify this code, please write to: romanov73@gmail.com. */ -package ru.ulstu.extractor.controller; +package ru.ulstu.extractor.branch; 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.BranchRepository; import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; import springfox.documentation.annotations.ApiIgnore; diff --git a/src/main/java/ru/ulstu/extractor/branch/BranchService.java b/src/main/java/ru/ulstu/extractor/branch/BranchService.java index 8cae8e6..e047bcb 100644 --- a/src/main/java/ru/ulstu/extractor/branch/BranchService.java +++ b/src/main/java/ru/ulstu/extractor/branch/BranchService.java @@ -7,8 +7,6 @@ package ru.ulstu.extractor.branch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.model.BaseEntity; @@ -56,14 +54,6 @@ public class BranchService { return branchRepository.findByRepositoryAndName(repository, branchName); } - public int getCommitsCount(Integer repositoryId, String branchName) { - return branchRepository.getCommitsCount(repositoryId, branchName); - } - - public Page findByRepository(Repository repository, Pageable pageable) { - return branchRepository.findByRepository(repository, pageable); - } - public List findAll() { return branchRepository.findAll(); } diff --git a/src/main/java/ru/ulstu/extractor/loader/GitScheduler.java b/src/main/java/ru/ulstu/extractor/loader/GitScheduler.java deleted file mode 100644 index 4a16fdc..0000000 --- a/src/main/java/ru/ulstu/extractor/loader/GitScheduler.java +++ /dev/null @@ -1,69 +0,0 @@ -package ru.ulstu.extractor.loader; - -import org.eclipse.jgit.api.errors.GitAPIException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.data.domain.Page; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; -import ru.ulstu.extractor.branch.BranchService; -import ru.ulstu.extractor.gitrepository.GitRepositoryService; -import ru.ulstu.extractor.model.Branch; -import ru.ulstu.extractor.model.OffsetablePageRequest; -import ru.ulstu.extractor.model.Repository; -import ru.ulstu.extractor.service.IndexService; -import ru.ulstu.extractor.ts.AbstractTSExtractor; - -import java.io.IOException; -import java.util.List; - -@Service -public class GitScheduler { - private final static int DEFAULT_PAGE_SIZE = 100; - private final Logger log = LoggerFactory.getLogger(GitScheduler.class); - private final GitRepositoryService gitRepositoryService; - private final BranchService branchService; - private final IndexService indexService; - private final List tsExtractors; - - public GitScheduler(GitRepositoryService gitRepositoryService, - BranchService branchService, - IndexService indexService, - List tsExtractors) { - this.gitRepositoryService = gitRepositoryService; - this.branchService = branchService; - this.indexService = indexService; - this.tsExtractors = tsExtractors; - } - - @Scheduled(cron = "* */5 * * * *") - public void getCommitsCount() { - log.debug("Load commits count started"); - int repositoryPageNumber = 0; - Page repositoryPage; - do { - int branchPageNumber = 0; - repositoryPage = gitRepositoryService.findAll( - new OffsetablePageRequest(repositoryPageNumber, DEFAULT_PAGE_SIZE)); - Page branchPage; - for (Repository repository : repositoryPage.getContent()) { - do { - branchPage = branchService.findByRepository(repository, - new OffsetablePageRequest(branchPageNumber, DEFAULT_PAGE_SIZE)); - for (Branch branch : branchPage.getContent()) { - try { - indexService.index(repository.getUrl(), branch.getName()); - tsExtractors.forEach(tsExtractor -> tsExtractor.addPoint(repository.getId(), branch.getName())); - } catch (GitAPIException | IOException ex) { - log.warn(ex.getMessage()); - ex.printStackTrace(); - } - } - branchPageNumber++; - } while (!branchPage.isEmpty()); - } - repositoryPageNumber++; - } while (!repositoryPage.isEmpty()); - log.debug("Load commits count finished"); - } -} diff --git a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java index a1845b4..8bf5e98 100644 --- a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java @@ -39,4 +39,7 @@ public interface CommitRepository extends JpaRepository { List getCommitTimeEntityStatistic(@Param("branchId") Integer branchId, @Param("author") String author); void deleteByBranchIsNull(); + + @Query("SELECT b.commits FROM Branch b WHERE b.name = ?2 and b.repository.id = ?1") + List findByRepositoryIdAndBranchName(Integer repositoryId, String name); } diff --git a/src/main/java/ru/ulstu/extractor/service/CommitService.java b/src/main/java/ru/ulstu/extractor/service/CommitService.java index ed75ee1..8da7bda 100644 --- a/src/main/java/ru/ulstu/extractor/service/CommitService.java +++ b/src/main/java/ru/ulstu/extractor/service/CommitService.java @@ -49,6 +49,11 @@ public class CommitService { updateQuery = String.format(updateQuery, String.join(",", commitIds)); jdbcTemplate.update(updateQuery, branchId); } + + + public List findByRepositoryIdAndName(Integer repositoryId, String branchName) { + return commitRepository.findByRepositoryIdAndBranchName(repositoryId, branchName); + } } diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java index 86f9454..875f357 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -10,13 +10,13 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.branch.BranchService; import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; import ru.ulstu.extractor.gitrepository.GitRepositoryService; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Repository; +import ru.ulstu.extractor.ts.AbstractTimeSeriesCreator; import java.io.IOException; import java.util.Collections; @@ -29,16 +29,18 @@ public class IndexService { private final GitRepositoryService gitRepositoryService; private final GitRepositoryRepository gitRepositoryRepository; private final BranchService branchService; + private final List timeSeriesCreators; public IndexService(GitRepositoryService gitRepositoryService, GitRepositoryRepository gitRepositoryRepository, - BranchService branchService) { + BranchService branchService, + List timeSeriesCreators) { this.gitRepositoryService = gitRepositoryService; this.gitRepositoryRepository = gitRepositoryRepository; this.branchService = branchService; + this.timeSeriesCreators = timeSeriesCreators; } - @Transactional public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { Repository repository = gitRepositoryRepository.findByUrl(repositoryUrl); if (repository == null) { @@ -60,6 +62,8 @@ public class IndexService { commitsTo += COMMITS_PAGE_SIZE; commits = gitRepositoryService.getCommits(repositoryUrl, branchName, commitsFrom, commitsTo, false); } + Integer repositoryId = repository.getId(); + timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(repositoryId, branchName)); LOG.debug("Complete indexing {} branch", branchName); } } diff --git a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java index 5653c3e..30a8327 100644 --- a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java @@ -13,6 +13,7 @@ import ru.ulstu.extractor.model.TimeSeries; import ru.ulstu.extractor.model.TimeSeriesValue; import ru.ulstu.extractor.repository.TimeSeriesRepository; import ru.ulstu.extractor.repository.TimeSeriesValueRepository; +import ru.ulstu.extractor.ts.TimeSeriesDateMapper; import java.util.Date; import java.util.List; @@ -24,21 +25,23 @@ public class TimeSeriesService { private final static Logger LOG = LoggerFactory.getLogger(TimeSeriesService.class); private final TimeSeriesRepository timeSeriesRepository; private final TimeSeriesValueRepository timeSeriesValueRepository; - + private final TimeSeriesDateMapper.TimeSeriesInterval timeSeriesInterval = TimeSeriesDateMapper.TimeSeriesInterval.HOUR; public TimeSeriesService(TimeSeriesRepository timeSeriesRepository, TimeSeriesValueRepository timeSeriesValueRepository) { this.timeSeriesRepository = timeSeriesRepository; this.timeSeriesValueRepository = timeSeriesValueRepository; } - public TimeSeries save(TimeSeries timeSeries, List timeSeriesValues) { - LOG.debug("Start save {} time series with {} time series values ", timeSeries.getName(), timeSeriesValues.size()); + public TimeSeries save(String timeSeriesName, List timeSeriesValues) { + LOG.debug("Start save {} time series with {} time series values ", timeSeriesName, timeSeriesValues.size()); + final TimeSeries timeSeries = findOrCreate(timeSeriesName); List timeSeriesValuesToRemoveIds = timeSeries.getValues().stream().map(BaseEntity::getId).collect(Collectors.toList()); + timeSeriesValues.forEach(v -> v.setTimeSeries(timeSeries)); timeSeries.setValues(timeSeriesValues); LOG.debug("Save time series {} ", timeSeries.getName()); - timeSeries = timeSeriesRepository.save(timeSeries); + TimeSeries savedTimeSeries = timeSeriesRepository.save(timeSeries); LOG.debug("Clear {} ime series values ", timeSeriesValuesToRemoveIds.size()); - return timeSeries; + return savedTimeSeries; } public TimeSeries findOrCreate(String timeSeriesName) { @@ -64,12 +67,12 @@ public class TimeSeriesService { TimeSeries timeSeries = findOrCreate(timeSeriesName); timeSeriesValueRepository.save(new TimeSeriesValue(timeSeries, date, value)); } -// -// public TimeSeriesValue findByTimeSeriesAndName(TimeSeries timeSeries, String name) { -// return timeSeriesValueRepository.findByTimeSeriesAndName(timeSeries, name); -// } public List findAll() { return timeSeriesValueRepository.findAll(); } + + public TimeSeriesDateMapper.TimeSeriesInterval getTimeSeriesInterval() { + return timeSeriesInterval; + } } diff --git a/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java b/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java deleted file mode 100644 index 14271ee..0000000 --- a/src/main/java/ru/ulstu/extractor/ts/AbstractTSExtractor.java +++ /dev/null @@ -1,21 +0,0 @@ -package ru.ulstu.extractor.ts; - -import ru.ulstu.extractor.service.TimeSeriesService; - -import java.util.Date; - -public abstract class AbstractTSExtractor { - - public abstract String getTSName(); - - public abstract int getNewTSValue(Integer repositoryId, String branchName); - - public abstract TimeSeriesService getTimeSeriesService(); - - public void addPoint(Integer repositoryId, String branchName) { - getTimeSeriesService().addTimeSeriesValue( - String.format("%s %s %s", getTSName(), repositoryId, branchName), - new Date(), - getNewTSValue(repositoryId, branchName)); - } -} diff --git a/src/main/java/ru/ulstu/extractor/ts/AbstractTimeSeriesCreator.java b/src/main/java/ru/ulstu/extractor/ts/AbstractTimeSeriesCreator.java new file mode 100644 index 0000000..2cc9f4f --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/ts/AbstractTimeSeriesCreator.java @@ -0,0 +1,22 @@ +package ru.ulstu.extractor.ts; + +import ru.ulstu.extractor.model.TimeSeriesValue; +import ru.ulstu.extractor.service.TimeSeriesService; + +import java.util.List; + +public abstract class AbstractTimeSeriesCreator { + + public abstract String getTimeSeriesName(); + + public abstract List getTimeSeriesValues(Integer repositoryId, String branchName); + + public abstract TimeSeriesService getTimeSeriesService(); + + public void addTimeSeries(Integer repositoryId, String branchName) { + getTimeSeriesService().save( + String.format("%s %s %s", getTimeSeriesName(), repositoryId, branchName), + TimeSeriesDateMapper.mapTimeSeriesToInterval(getTimeSeriesService().getTimeSeriesInterval(), + getTimeSeriesValues(repositoryId, branchName))); + } +} diff --git a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java index a50f988..6a2b6f2 100644 --- a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java @@ -1,28 +1,35 @@ package ru.ulstu.extractor.ts; import org.springframework.stereotype.Component; -import ru.ulstu.extractor.branch.BranchService; +import ru.ulstu.extractor.model.TimeSeriesValue; +import ru.ulstu.extractor.service.CommitService; import ru.ulstu.extractor.service.TimeSeriesService; +import java.util.List; +import java.util.stream.Collectors; + @Component -public class CommitsTS extends AbstractTSExtractor { +public class CommitsTS extends AbstractTimeSeriesCreator { private final TimeSeriesService timeSeriesService; - private final BranchService branchService; + private final CommitService commitService; public CommitsTS(TimeSeriesService timeSeriesService, - BranchService branchService) { + CommitService commitService) { this.timeSeriesService = timeSeriesService; - this.branchService = branchService; + this.commitService = commitService; } @Override - public String getTSName() { + public String getTimeSeriesName() { return "Количество коммитов во времени"; } @Override - public int getNewTSValue(Integer repositoryId, String branchName) { - return branchService.getCommitsCount(repositoryId, branchName); + public List getTimeSeriesValues(Integer repositoryId, String branchName) { + return commitService.findByRepositoryIdAndName(repositoryId, branchName) + .stream() + .map(c -> new TimeSeriesValue(c.getDate(), 1)) + .collect(Collectors.toList()); } @Override diff --git a/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java b/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java index d192b53..19298a0 100644 --- a/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java +++ b/src/main/java/ru/ulstu/extractor/ts/TimeSeriesDateMapper.java @@ -17,8 +17,8 @@ import java.util.stream.Collectors; */ public class TimeSeriesDateMapper { - public TimeSeries mapTimeSeriesToInterval(TimeSeriesInterval timeSeriesInterval, TimeSeries timeSeries) { - List trimmedTimeSeriesValues = timeSeries.getValues() + public static List mapTimeSeriesToInterval(TimeSeriesInterval timeSeriesInterval, List timeSeriesValues) { + List trimmedTimeSeriesValues = timeSeriesValues .stream() .map(timeSeriesValue -> new TimeSeriesValue(trimTo(timeSeriesInterval, timeSeriesValue.getDate()), timeSeriesValue.getValue())) @@ -28,13 +28,18 @@ public class TimeSeriesDateMapper { .collect(Collectors.groupingBy(TimeSeriesValue::getDate, Collectors.summingInt(TimeSeriesValue::getValue))); - return new TimeSeries(timeSeries.getName(), groupedTimeSeriesValues.entrySet() + return groupedTimeSeriesValues.entrySet() .stream() .map(e -> new TimeSeriesValue(e.getKey(), e.getValue())) - .collect(Collectors.toList())); + .collect(Collectors.toList()); } - private Date trimTo(TimeSeriesInterval timeSeriesInterval, Date date) { + public static TimeSeries mapTimeSeriesToInterval(TimeSeriesInterval timeSeriesInterval, TimeSeries timeSeries) { + timeSeries.setValues(mapTimeSeriesToInterval(timeSeriesInterval, timeSeries.getValues())); + return timeSeries; + } + + private static Date trimTo(TimeSeriesInterval timeSeriesInterval, Date date) { return DateUtils.truncate(date, timeSeriesInterval.calendarField); } -- 2.34.1 From a7add46348bf4f3a4bb0556ec4eb79377d88d3d0 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 21 Jun 2022 10:39:53 +0400 Subject: [PATCH 07/10] #58 -- fix models naming, refactor --- .../extractor/branch/BranchRepository.java | 21 --------- .../{ => controller}/BranchController.java | 7 +-- .../extractor/{ => branch}/model/Branch.java | 19 ++++---- .../branch/repository/BranchRepository.java | 21 +++++++++ .../branch/{ => service}/BranchService.java | 13 +++--- .../extractor/{ => commit}/model/Commit.java | 1 + .../model/CommitAuthorStatistic.java | 2 +- .../model/CommitEntityStatistic.java | 2 +- .../model/CommitTimeStatistic.java | 2 +- .../model/CommitUrlStatistic.java | 2 +- .../commit/repository/CommitRepository.java | 45 +++++++++++++++++++ .../{ => commit}/service/CommitService.java | 5 ++- .../controller/GitIndexingController.java | 2 +- .../controller/StatisticController.java | 4 +- .../GitRepositoryRepository.java | 6 +-- .../gitrepository/GitRepositoryService.java | 6 +-- .../model/GitRepository.java} | 10 +++-- .../repository/AuthorRepository.java | 6 +-- .../repository/CommitRepository.java | 45 ------------------- .../extractor/service/FilteringService.java | 2 +- .../ulstu/extractor/service/IndexService.java | 18 ++++---- .../extractor/service/TimeSeriesService.java | 2 +- .../java/ru/ulstu/extractor/ts/CommitsTS.java | 3 +- .../db/changelog-20220621_120000-schema.xml | 11 +++++ src/main/resources/db/changelog-master.xml | 1 + src/main/resources/templates/statistic.html | 2 +- 26 files changed, 140 insertions(+), 118 deletions(-) delete mode 100644 src/main/java/ru/ulstu/extractor/branch/BranchRepository.java rename src/main/java/ru/ulstu/extractor/branch/{ => controller}/BranchController.java (85%) rename src/main/java/ru/ulstu/extractor/{ => branch}/model/Branch.java (70%) create mode 100644 src/main/java/ru/ulstu/extractor/branch/repository/BranchRepository.java rename src/main/java/ru/ulstu/extractor/branch/{ => service}/BranchService.java (81%) rename src/main/java/ru/ulstu/extractor/{ => commit}/model/Commit.java (98%) rename src/main/java/ru/ulstu/extractor/{ => commit}/model/CommitAuthorStatistic.java (90%) rename src/main/java/ru/ulstu/extractor/{ => commit}/model/CommitEntityStatistic.java (90%) rename src/main/java/ru/ulstu/extractor/{ => commit}/model/CommitTimeStatistic.java (92%) rename src/main/java/ru/ulstu/extractor/{ => commit}/model/CommitUrlStatistic.java (89%) create mode 100644 src/main/java/ru/ulstu/extractor/commit/repository/CommitRepository.java rename src/main/java/ru/ulstu/extractor/{ => commit}/service/CommitService.java (92%) rename src/main/java/ru/ulstu/extractor/{model/Repository.java => gitrepository/model/GitRepository.java} (51%) delete mode 100644 src/main/java/ru/ulstu/extractor/repository/CommitRepository.java create mode 100644 src/main/resources/db/changelog-20220621_120000-schema.xml diff --git a/src/main/java/ru/ulstu/extractor/branch/BranchRepository.java b/src/main/java/ru/ulstu/extractor/branch/BranchRepository.java deleted file mode 100644 index 6034cb3..0000000 --- a/src/main/java/ru/ulstu/extractor/branch/BranchRepository.java +++ /dev/null @@ -1,21 +0,0 @@ -package ru.ulstu.extractor.branch; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import ru.ulstu.extractor.model.Branch; -import ru.ulstu.extractor.model.Repository; - -import java.util.List; - -public interface BranchRepository extends JpaRepository { - Branch findByRepositoryAndName(Repository repository, String name); - - @Query("select count(c) from Commit c LEFT JOIN c.branch b LEFT JOIN Repository r where r.id = ?1 AND b.name = ?2") - int getCommitsCount(Integer repositoryId, String name); - - List findByRepositoryId(Integer repositoryId); - - Page findByRepository(Repository repository, Pageable pageable); -} diff --git a/src/main/java/ru/ulstu/extractor/branch/BranchController.java b/src/main/java/ru/ulstu/extractor/branch/controller/BranchController.java similarity index 85% rename from src/main/java/ru/ulstu/extractor/branch/BranchController.java rename to src/main/java/ru/ulstu/extractor/branch/controller/BranchController.java index 47f823e..22432bd 100644 --- a/src/main/java/ru/ulstu/extractor/branch/BranchController.java +++ b/src/main/java/ru/ulstu/extractor/branch/controller/BranchController.java @@ -3,12 +3,13 @@ * You may use, distribute and modify this code, please write to: romanov73@gmail.com. */ -package ru.ulstu.extractor.branch; +package ru.ulstu.extractor.branch.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.repository.BranchRepository; import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; import springfox.documentation.annotations.ApiIgnore; @@ -30,7 +31,7 @@ public class BranchController { public String indexBranch( Model model, @RequestParam int repositoryId) { - model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId)); + model.addAttribute("branches", branchRepository.findByGitRepositoryId(repositoryId)); model.addAttribute("repository", gitRepositoryRepository.findById(repositoryId).get()); return LIST_REPOSITORY_BRANCHES; } @@ -40,7 +41,7 @@ public class BranchController { @RequestParam int repositoryId, @RequestParam Integer id) { branchRepository.deleteById(id); - model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId)); + model.addAttribute("branches", branchRepository.findByGitRepositoryId(repositoryId)); model.addAttribute("repository", gitRepositoryRepository.findById(repositoryId).get()); return LIST_REPOSITORY_BRANCHES; } diff --git a/src/main/java/ru/ulstu/extractor/model/Branch.java b/src/main/java/ru/ulstu/extractor/branch/model/Branch.java similarity index 70% rename from src/main/java/ru/ulstu/extractor/model/Branch.java rename to src/main/java/ru/ulstu/extractor/branch/model/Branch.java index 30c30dd..6837771 100644 --- a/src/main/java/ru/ulstu/extractor/model/Branch.java +++ b/src/main/java/ru/ulstu/extractor/branch/model/Branch.java @@ -3,10 +3,13 @@ * You may use, distribute and modify this code, please write to: romanov73@gmail.com. */ -package ru.ulstu.extractor.model; +package ru.ulstu.extractor.branch.model; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; +import ru.ulstu.extractor.gitrepository.model.GitRepository; +import ru.ulstu.extractor.model.BaseEntity; +import ru.ulstu.extractor.model.Commit; import javax.persistence.CascadeType; import javax.persistence.Entity; @@ -22,7 +25,7 @@ public class Branch extends BaseEntity { private String name; @ManyToOne - private Repository repository; + private GitRepository gitRepository; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "branch_id") @@ -36,8 +39,8 @@ public class Branch extends BaseEntity { this.name = name; } - public Branch(Repository repository, String branchName) { - this.repository = repository; + public Branch(GitRepository gitRepository, String branchName) { + this.gitRepository = gitRepository; this.name = branchName; } @@ -49,12 +52,12 @@ public class Branch extends BaseEntity { this.name = name; } - public Repository getRepository() { - return repository; + public GitRepository getGitRepository() { + return gitRepository; } - public void setRepository(Repository repository) { - this.repository = repository; + public void setGitRepository(GitRepository gitRepository) { + this.gitRepository = gitRepository; } public List getCommits() { diff --git a/src/main/java/ru/ulstu/extractor/branch/repository/BranchRepository.java b/src/main/java/ru/ulstu/extractor/branch/repository/BranchRepository.java new file mode 100644 index 0000000..af14f53 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/branch/repository/BranchRepository.java @@ -0,0 +1,21 @@ +package ru.ulstu.extractor.branch.repository; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import ru.ulstu.extractor.branch.model.Branch; +import ru.ulstu.extractor.gitrepository.model.GitRepository; + +import java.util.List; + +public interface BranchRepository extends JpaRepository { + Branch findByGitRepositoryAndName(GitRepository gitRepository, String name); + + @Query("select count(c) from Commit c LEFT JOIN c.branch b LEFT JOIN GitRepository r where r.id = ?1 AND b.name = ?2") + int getCommitsCount(Integer repositoryId, String name); + + List findByGitRepositoryId(Integer repositoryId); + + Page findByGitRepository(GitRepository gitRepository, Pageable pageable); +} diff --git a/src/main/java/ru/ulstu/extractor/branch/BranchService.java b/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java similarity index 81% rename from src/main/java/ru/ulstu/extractor/branch/BranchService.java rename to src/main/java/ru/ulstu/extractor/branch/service/BranchService.java index e047bcb..3d6fd28 100644 --- a/src/main/java/ru/ulstu/extractor/branch/BranchService.java +++ b/src/main/java/ru/ulstu/extractor/branch/service/BranchService.java @@ -3,17 +3,18 @@ * You may use, distribute and modify this code, please write to: romanov73@gmail.com. */ -package ru.ulstu.extractor.branch; +package ru.ulstu.extractor.branch.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import ru.ulstu.extractor.branch.model.Branch; +import ru.ulstu.extractor.branch.repository.BranchRepository; +import ru.ulstu.extractor.commit.service.CommitService; +import ru.ulstu.extractor.gitrepository.model.GitRepository; import ru.ulstu.extractor.model.BaseEntity; -import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; -import ru.ulstu.extractor.model.Repository; -import ru.ulstu.extractor.service.CommitService; import java.util.List; import java.util.stream.Collectors; @@ -50,8 +51,8 @@ public class BranchService { return branch; } - public Branch findByRepositoryAndName(Repository repository, String branchName) { - return branchRepository.findByRepositoryAndName(repository, branchName); + public Branch findByRepositoryAndName(GitRepository gitRepository, String branchName) { + return branchRepository.findByGitRepositoryAndName(gitRepository, branchName); } public List findAll() { diff --git a/src/main/java/ru/ulstu/extractor/model/Commit.java b/src/main/java/ru/ulstu/extractor/commit/model/Commit.java similarity index 98% rename from src/main/java/ru/ulstu/extractor/model/Commit.java rename to src/main/java/ru/ulstu/extractor/commit/model/Commit.java index daf9087..699ff94 100644 --- a/src/main/java/ru/ulstu/extractor/model/Commit.java +++ b/src/main/java/ru/ulstu/extractor/commit/model/Commit.java @@ -7,6 +7,7 @@ package ru.ulstu.extractor.model; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; +import ru.ulstu.extractor.branch.model.Branch; import javax.persistence.CascadeType; import javax.persistence.Entity; diff --git a/src/main/java/ru/ulstu/extractor/model/CommitAuthorStatistic.java b/src/main/java/ru/ulstu/extractor/commit/model/CommitAuthorStatistic.java similarity index 90% rename from src/main/java/ru/ulstu/extractor/model/CommitAuthorStatistic.java rename to src/main/java/ru/ulstu/extractor/commit/model/CommitAuthorStatistic.java index 5587cb2..c3d98f4 100644 --- a/src/main/java/ru/ulstu/extractor/model/CommitAuthorStatistic.java +++ b/src/main/java/ru/ulstu/extractor/commit/model/CommitAuthorStatistic.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.model; +package ru.ulstu.extractor.commit.model; public class CommitAuthorStatistic { private String author; diff --git a/src/main/java/ru/ulstu/extractor/model/CommitEntityStatistic.java b/src/main/java/ru/ulstu/extractor/commit/model/CommitEntityStatistic.java similarity index 90% rename from src/main/java/ru/ulstu/extractor/model/CommitEntityStatistic.java rename to src/main/java/ru/ulstu/extractor/commit/model/CommitEntityStatistic.java index 61dc428..2a1a677 100644 --- a/src/main/java/ru/ulstu/extractor/model/CommitEntityStatistic.java +++ b/src/main/java/ru/ulstu/extractor/commit/model/CommitEntityStatistic.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.model; +package ru.ulstu.extractor.commit.model; public class CommitEntityStatistic { private Boolean entity; diff --git a/src/main/java/ru/ulstu/extractor/model/CommitTimeStatistic.java b/src/main/java/ru/ulstu/extractor/commit/model/CommitTimeStatistic.java similarity index 92% rename from src/main/java/ru/ulstu/extractor/model/CommitTimeStatistic.java rename to src/main/java/ru/ulstu/extractor/commit/model/CommitTimeStatistic.java index a9ca75c..4cdb369 100644 --- a/src/main/java/ru/ulstu/extractor/model/CommitTimeStatistic.java +++ b/src/main/java/ru/ulstu/extractor/commit/model/CommitTimeStatistic.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.model; +package ru.ulstu.extractor.commit.model; public class CommitTimeStatistic { private final static String DATE_TEMPLATE = "%s.%s"; diff --git a/src/main/java/ru/ulstu/extractor/model/CommitUrlStatistic.java b/src/main/java/ru/ulstu/extractor/commit/model/CommitUrlStatistic.java similarity index 89% rename from src/main/java/ru/ulstu/extractor/model/CommitUrlStatistic.java rename to src/main/java/ru/ulstu/extractor/commit/model/CommitUrlStatistic.java index 3eb4b37..c075074 100644 --- a/src/main/java/ru/ulstu/extractor/model/CommitUrlStatistic.java +++ b/src/main/java/ru/ulstu/extractor/commit/model/CommitUrlStatistic.java @@ -1,4 +1,4 @@ -package ru.ulstu.extractor.model; +package ru.ulstu.extractor.commit.model; public class CommitUrlStatistic { private String url; diff --git a/src/main/java/ru/ulstu/extractor/commit/repository/CommitRepository.java b/src/main/java/ru/ulstu/extractor/commit/repository/CommitRepository.java new file mode 100644 index 0000000..d0558d8 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/commit/repository/CommitRepository.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + +package ru.ulstu.extractor.commit.repository; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import ru.ulstu.extractor.commit.model.CommitAuthorStatistic; +import ru.ulstu.extractor.commit.model.CommitEntityStatistic; +import ru.ulstu.extractor.commit.model.CommitTimeStatistic; +import ru.ulstu.extractor.commit.model.CommitUrlStatistic; +import ru.ulstu.extractor.gitrepository.model.GitRepository; +import ru.ulstu.extractor.model.Commit; + +import java.util.List; + +public interface CommitRepository extends JpaRepository { + @Query("SELECT DISTINCT c FROM Commit c LEFT JOIN c.branch b LEFT JOIN c.fileChanges f LEFT JOIN c.author a LEFT JOIN b.gitRepository r WHERE r = :repository AND b.name = :branchName AND (:author IS NULL OR :author = '' OR a.name = :author) AND (:filter IS NULL OR :filter = '' OR lower(c.message) LIKE lower(concat('%', :filter,'%'))) AND (:entity IS NULL OR f.containsEntity = :entity)") + Page findByRepositoryAndBranch(Pageable pageable, @Param("repository") GitRepository gitRepository, @Param("branchName") String branchName, @Param("author") String author, @Param("filter") String filter, @Param("entity") Boolean entity); + + @Query("SELECT new ru.ulstu.extractor.commit.model.CommitAuthorStatistic(c.author.name, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by c.author.name") + List getCommitAuthorStatistic(@Param("branchId") Integer branchId, @Param("author") String author); + + @Query("SELECT new ru.ulstu.extractor.commit.model.CommitUrlStatistic(c.branch.gitRepository.url, COUNT(DISTINCT c.hash)) FROM Commit c GROUP by c.branch.gitRepository.url") + List getCommitUrlStatistic(); + + @Query("SELECT new ru.ulstu.extractor.commit.model.CommitTimeStatistic(extract(month FROM c.date) as month, extract(year FROM c.date) as year, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by extract(month from c.date), extract(year from c.date) ORDER by extract(year from c.date), extract(month from c.date)") + List getCommitTimeStatistic(@Param("branchId") Integer branchId, @Param("author") String author); + + @Query("SELECT new ru.ulstu.extractor.commit.model.CommitEntityStatistic(f.containsEntity, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a LEFT JOIN c.fileChanges f WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by f.containsEntity") + List getCommitEntityStatistic(@Param("branchId") Integer branchId, @Param("author") String author); + + @Query("SELECT new ru.ulstu.extractor.commit.model.CommitTimeStatistic(EXTRACT(MONTH FROM c.date), EXTRACT(YEAR FROM c.date), COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a LEFT JOIN c.fileChanges f WHERE f.containsEntity = true AND (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by extract(month from c.date), extract(year from c.date) ORDER by extract(year from c.date), extract(month from c.date)") + List getCommitTimeEntityStatistic(@Param("branchId") Integer branchId, @Param("author") String author); + + void deleteByBranchIsNull(); + + @Query("SELECT b.commits FROM Branch b WHERE b.name = ?2 and b.gitRepository.id = ?1") + List findByRepositoryIdAndBranchName(Integer repositoryId, String name); +} diff --git a/src/main/java/ru/ulstu/extractor/service/CommitService.java b/src/main/java/ru/ulstu/extractor/commit/service/CommitService.java similarity index 92% rename from src/main/java/ru/ulstu/extractor/service/CommitService.java rename to src/main/java/ru/ulstu/extractor/commit/service/CommitService.java index 8da7bda..fd1c449 100644 --- a/src/main/java/ru/ulstu/extractor/service/CommitService.java +++ b/src/main/java/ru/ulstu/extractor/commit/service/CommitService.java @@ -3,12 +3,13 @@ * You may use, distribute and modify this code, please write to: romanov73@gmail.com. */ -package ru.ulstu.extractor.service; +package ru.ulstu.extractor.commit.service; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.commit.repository.CommitRepository; import ru.ulstu.extractor.model.Commit; -import ru.ulstu.extractor.repository.CommitRepository; +import ru.ulstu.extractor.service.AuthorService; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java index 3102ba5..7708a1f 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitIndexingController.java @@ -15,8 +15,8 @@ import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import ru.ulstu.extractor.branch.model.Branch; import ru.ulstu.extractor.gitrepository.GitRepositoryService; -import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.mvc.FilterForm; import ru.ulstu.extractor.model.mvc.RepoForm; import ru.ulstu.extractor.service.IndexService; diff --git a/src/main/java/ru/ulstu/extractor/controller/StatisticController.java b/src/main/java/ru/ulstu/extractor/controller/StatisticController.java index fd729ef..771a5a0 100644 --- a/src/main/java/ru/ulstu/extractor/controller/StatisticController.java +++ b/src/main/java/ru/ulstu/extractor/controller/StatisticController.java @@ -9,9 +9,9 @@ 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.BranchService; +import ru.ulstu.extractor.branch.service.BranchService; +import ru.ulstu.extractor.commit.repository.CommitRepository; import ru.ulstu.extractor.model.mvc.FilterForm; -import ru.ulstu.extractor.repository.CommitRepository; import ru.ulstu.extractor.service.FilteringService; import springfox.documentation.annotations.ApiIgnore; diff --git a/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryRepository.java b/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryRepository.java index a94a59a..ae9e821 100644 --- a/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryRepository.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryRepository.java @@ -1,8 +1,8 @@ package ru.ulstu.extractor.gitrepository; import org.springframework.data.jpa.repository.JpaRepository; -import ru.ulstu.extractor.model.Repository; +import ru.ulstu.extractor.gitrepository.model.GitRepository; -public interface GitRepositoryRepository extends JpaRepository { - Repository findByUrl(String url); +public interface GitRepositoryRepository extends JpaRepository { + GitRepository findByUrl(String url); } diff --git a/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryService.java index 61eebf9..f05a91a 100644 --- a/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/GitRepositoryService.java @@ -18,16 +18,16 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.branch.model.Branch; +import ru.ulstu.extractor.gitrepository.model.GitRepository; import ru.ulstu.extractor.heuristic.model.BusinessLogicUnit; import ru.ulstu.extractor.heuristic.model.EntityUnit; import ru.ulstu.extractor.heuristic.model.ResourceUnit; import ru.ulstu.extractor.heuristic.service.StructuralUnitService; import ru.ulstu.extractor.model.Author; -import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.FileChange; import ru.ulstu.extractor.model.LineChange; -import ru.ulstu.extractor.model.Repository; import java.io.ByteArrayOutputStream; import java.io.File; @@ -346,7 +346,7 @@ public class GitRepositoryService { return Optional.empty(); } - public Page findAll(Pageable pageable) { + public Page findAll(Pageable pageable) { return gitRepositoryRepository.findAll(pageable); } } diff --git a/src/main/java/ru/ulstu/extractor/model/Repository.java b/src/main/java/ru/ulstu/extractor/gitrepository/model/GitRepository.java similarity index 51% rename from src/main/java/ru/ulstu/extractor/model/Repository.java rename to src/main/java/ru/ulstu/extractor/gitrepository/model/GitRepository.java index 651bfca..edabd3e 100644 --- a/src/main/java/ru/ulstu/extractor/model/Repository.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/model/GitRepository.java @@ -1,15 +1,17 @@ -package ru.ulstu.extractor.model; +package ru.ulstu.extractor.gitrepository.model; + +import ru.ulstu.extractor.model.BaseEntity; import javax.persistence.Entity; @Entity -public class Repository extends BaseEntity { +public class GitRepository extends BaseEntity { private String url; - public Repository() { + public GitRepository() { } - public Repository(String repositoryUrl) { + public GitRepository(String repositoryUrl) { url = repositoryUrl; } diff --git a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java index 26b5f45..0ae8a20 100644 --- a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java @@ -8,14 +8,14 @@ package ru.ulstu.extractor.repository; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import ru.ulstu.extractor.gitrepository.model.GitRepository; import ru.ulstu.extractor.model.Author; -import ru.ulstu.extractor.model.Repository; import java.util.List; public interface AuthorRepository extends JpaRepository { - @Query("SELECT DISTINCT a.name FROM Commit c, Repository r, Branch b, Author a WHERE c.author = a AND c.branch = b AND r = b.repository AND (:repository IS NULL OR r = :repository) AND (:branchName IS NULL OR :branchName = '' OR b.name = :branchName) AND a.name IS NOT NULL AND a.name <> '' ORDER BY a.name") - List findByRepositoryAndBranch(@Param("repository") Repository repository, @Param("branchName") String branchName); + @Query("SELECT DISTINCT a.name FROM Commit c, GitRepository r, Branch b, Author a WHERE c.author = a AND c.branch = b AND r = b.gitRepository AND (:repository IS NULL OR r = :repository) AND (:branchName IS NULL OR :branchName = '' OR b.name = :branchName) AND a.name IS NOT NULL AND a.name <> '' ORDER BY a.name") + List findByRepositoryAndBranch(@Param("repository") GitRepository gitRepository, @Param("branchName") String branchName); List findByName(String name); diff --git a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java deleted file mode 100644 index 8bf5e98..0000000 --- a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2021 Anton Romanov - All Rights Reserved - * You may use, distribute and modify this code, please write to: romanov73@gmail.com. - */ - -package ru.ulstu.extractor.repository; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; -import ru.ulstu.extractor.model.Commit; -import ru.ulstu.extractor.model.CommitAuthorStatistic; -import ru.ulstu.extractor.model.CommitEntityStatistic; -import ru.ulstu.extractor.model.CommitTimeStatistic; -import ru.ulstu.extractor.model.CommitUrlStatistic; -import ru.ulstu.extractor.model.Repository; - -import java.util.List; - -public interface CommitRepository extends JpaRepository { - @Query("SELECT DISTINCT c FROM Commit c LEFT JOIN c.branch b LEFT JOIN c.fileChanges f LEFT JOIN c.author a LEFT JOIN b.repository r WHERE r = :repository AND b.name = :branchName AND (:author IS NULL OR :author = '' OR a.name = :author) AND (:filter IS NULL OR :filter = '' OR lower(c.message) LIKE lower(concat('%', :filter,'%'))) AND (:entity IS NULL OR f.containsEntity = :entity)") - Page findByRepositoryAndBranch(Pageable pageable, @Param("repository") Repository repository, @Param("branchName") String branchName, @Param("author") String author, @Param("filter") String filter, @Param("entity") Boolean entity); - - @Query("SELECT new ru.ulstu.extractor.model.CommitAuthorStatistic(c.author.name, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by c.author.name") - List getCommitAuthorStatistic(@Param("branchId") Integer branchId, @Param("author") String author); - - @Query("SELECT new ru.ulstu.extractor.model.CommitUrlStatistic(c.branch.repository.url, COUNT(DISTINCT c.hash)) FROM Commit c GROUP by c.branch.repository.url") - List getCommitUrlStatistic(); - - @Query("SELECT new ru.ulstu.extractor.model.CommitTimeStatistic(extract(month FROM c.date) as month, extract(year FROM c.date) as year, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by extract(month from c.date), extract(year from c.date) ORDER by extract(year from c.date), extract(month from c.date)") - List getCommitTimeStatistic(@Param("branchId") Integer branchId, @Param("author") String author); - - @Query("SELECT new ru.ulstu.extractor.model.CommitEntityStatistic(f.containsEntity, COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a LEFT JOIN c.fileChanges f WHERE (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by f.containsEntity") - List getCommitEntityStatistic(@Param("branchId") Integer branchId, @Param("author") String author); - - @Query("SELECT new ru.ulstu.extractor.model.CommitTimeStatistic(EXTRACT(MONTH FROM c.date), EXTRACT(YEAR FROM c.date), COUNT(DISTINCT c.hash)) FROM Commit c LEFT JOIN c.branch LEFT JOIN c.author a LEFT JOIN c.fileChanges f WHERE f.containsEntity = true AND (:branchId IS NULL OR c.branch.id = :branchId) AND (:author IS NULL OR :author = '' OR a.name = :author) GROUP by extract(month from c.date), extract(year from c.date) ORDER by extract(year from c.date), extract(month from c.date)") - List getCommitTimeEntityStatistic(@Param("branchId") Integer branchId, @Param("author") String author); - - void deleteByBranchIsNull(); - - @Query("SELECT b.commits FROM Branch b WHERE b.name = ?2 and b.repository.id = ?1") - List findByRepositoryIdAndBranchName(Integer repositoryId, String name); -} diff --git a/src/main/java/ru/ulstu/extractor/service/FilteringService.java b/src/main/java/ru/ulstu/extractor/service/FilteringService.java index 51802aa..ddeda0c 100644 --- a/src/main/java/ru/ulstu/extractor/service/FilteringService.java +++ b/src/main/java/ru/ulstu/extractor/service/FilteringService.java @@ -9,10 +9,10 @@ import com.sun.istack.NotNull; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.commit.repository.CommitRepository; import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.repository.AuthorRepository; -import ru.ulstu.extractor.repository.CommitRepository; import java.util.List; import java.util.Map; diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java index 875f357..d5415e2 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -10,12 +10,12 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import ru.ulstu.extractor.branch.BranchService; +import ru.ulstu.extractor.branch.model.Branch; +import ru.ulstu.extractor.branch.service.BranchService; import ru.ulstu.extractor.gitrepository.GitRepositoryRepository; import ru.ulstu.extractor.gitrepository.GitRepositoryService; -import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.gitrepository.model.GitRepository; import ru.ulstu.extractor.model.Commit; -import ru.ulstu.extractor.model.Repository; import ru.ulstu.extractor.ts.AbstractTimeSeriesCreator; import java.io.IOException; @@ -42,13 +42,13 @@ public class IndexService { } public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { - Repository repository = gitRepositoryRepository.findByUrl(repositoryUrl); - if (repository == null) { - repository = gitRepositoryRepository.save(new Repository(repositoryUrl)); + GitRepository gitRepository = gitRepositoryRepository.findByUrl(repositoryUrl); + if (gitRepository == null) { + gitRepository = gitRepositoryRepository.save(new GitRepository(repositoryUrl)); } - Branch branch = branchService.findByRepositoryAndName(repository, branchName); + Branch branch = branchService.findByRepositoryAndName(gitRepository, branchName); if (branch == null) { - branch = new Branch(repository, branchName); + branch = new Branch(gitRepository, branchName); } branchService.save(branch, Collections.emptyList()); int commitsFrom = 0; @@ -62,7 +62,7 @@ public class IndexService { commitsTo += COMMITS_PAGE_SIZE; commits = gitRepositoryService.getCommits(repositoryUrl, branchName, commitsFrom, commitsTo, false); } - Integer repositoryId = repository.getId(); + Integer repositoryId = gitRepository.getId(); timeSeriesCreators.forEach(tsCreator -> tsCreator.addTimeSeries(repositoryId, branchName)); LOG.debug("Complete indexing {} branch", branchName); } diff --git a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java index 30a8327..644f6aa 100644 --- a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java @@ -40,7 +40,7 @@ public class TimeSeriesService { timeSeries.setValues(timeSeriesValues); LOG.debug("Save time series {} ", timeSeries.getName()); TimeSeries savedTimeSeries = timeSeriesRepository.save(timeSeries); - LOG.debug("Clear {} ime series values ", timeSeriesValuesToRemoveIds.size()); + LOG.debug("Clear {} time series values ", timeSeriesValuesToRemoveIds.size()); return savedTimeSeries; } diff --git a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java index 6a2b6f2..8f7dae0 100644 --- a/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/CommitsTS.java @@ -1,8 +1,8 @@ package ru.ulstu.extractor.ts; import org.springframework.stereotype.Component; +import ru.ulstu.extractor.commit.service.CommitService; import ru.ulstu.extractor.model.TimeSeriesValue; -import ru.ulstu.extractor.service.CommitService; import ru.ulstu.extractor.service.TimeSeriesService; import java.util.List; @@ -26,6 +26,7 @@ public class CommitsTS extends AbstractTimeSeriesCreator { @Override public List getTimeSeriesValues(Integer repositoryId, String branchName) { + //TODO: добавить постраничное чтение return commitService.findByRepositoryIdAndName(repositoryId, branchName) .stream() .map(c -> new TimeSeriesValue(c.getDate(), 1)) diff --git a/src/main/resources/db/changelog-20220621_120000-schema.xml b/src/main/resources/db/changelog-20220621_120000-schema.xml new file mode 100644 index 0000000..2df5c58 --- /dev/null +++ b/src/main/resources/db/changelog-20220621_120000-schema.xml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index b9c9b69..e2afa54 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -13,4 +13,5 @@ + diff --git a/src/main/resources/templates/statistic.html b/src/main/resources/templates/statistic.html index e89c1a8..2ad0eb2 100644 --- a/src/main/resources/templates/statistic.html +++ b/src/main/resources/templates/statistic.html @@ -193,7 +193,7 @@