Skip to content

Commit

Permalink
change
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSaini101 committed Mar 28, 2024
1 parent be0e73d commit 6a9b550
Show file tree
Hide file tree
Showing 13 changed files with 2,611 additions and 3,438 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Jenkins Infra
* Copyright (c) 2023-2024 Jenkins Infra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,7 +21,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.jenkins.pluginhealth.scoring.model;

import java.time.ZonedDateTime;
Expand Down Expand Up @@ -65,8 +64,7 @@ public class Plugin {
@Type(value = JsonType.class)
private final Map<String, ProbeResult> details = new HashMap<>();

public Plugin() {
}
public Plugin() {}

public Plugin(String name, VersionNumber version, String scm, ZonedDateTime releaseTimestamp) {
this.name = name;
Expand Down Expand Up @@ -96,7 +94,6 @@ public Plugin setScm(String scm) {
this.scm = scm;
return this;
}

public ZonedDateTime getReleaseTimestamp() {
return releaseTimestamp;
}
Expand All @@ -111,11 +108,11 @@ public Map<String, ProbeResult> getDetails() {
}

public Plugin addDetails(ProbeResult newProbeResult) {
this.details.compute(newProbeResult.id(), (s, previousProbeResult) ->
newProbeResult.status() == ProbeResult.Status.ERROR ?
null :
Objects.equals(previousProbeResult, newProbeResult) ? previousProbeResult : newProbeResult
);
this.details.compute(
newProbeResult.id(),
(s, previousProbeResult) -> newProbeResult.status() == ProbeResult.Status.ERROR
? null
: Objects.equals(previousProbeResult, newProbeResult) ? previousProbeResult : newProbeResult);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,8 @@ public class Score {
@Type(JsonType.class)
private final Set<ScoreResult> details = new HashSet<>();

@Column(name = "change_score")
private Long changeScore;

public Score() {}

public long getChangeScore() {
return changeScore;
}

public void setChangeScore(long changeScore) {
this.changeScore = changeScore;
}

public Score(Plugin plugin, ZonedDateTime computedAt) {
this.plugin = plugin;
this.computedAt = computedAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Jenkins Infra
* Copyright (c) 2023-2024 Jenkins Infra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,14 +21,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.jenkins.pluginhealth.scoring.model.updatecenter;

import java.util.List;

import io.jenkins.pluginhealth.scoring.model.Resolution;

public record SecurityWarning(
String id,
String name,
List<SecurityWarningVersion> versions
) {
}
String id, String name, String url, Resolution resolution, List<SecurityWarningVersion> versions) {}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,20 @@ public interface ScoreRepository extends JpaRepository<Score, Long> {
@Query(
value =
"""
SELECT DISTINCT ON (s.plugin_id)
s.plugin_id,
s.value,
s.computed_at,
s.details,
s.id,
s.change_score
FROM scores s
JOIN plugins p on s.plugin_id = p.id
ORDER BY s.plugin_id, s.computed_at DESC;
SELECT plugin_id, value, computed_at, details, id
FROM (
SELECT
s.plugin_id,
s.value,
s.computed_at,
s.details,
s.id,
ROW_NUMBER() OVER (PARTITION BY s.plugin_id ORDER BY s.computed_at DESC) AS row_num
FROM scores s
JOIN plugins p ON s.plugin_id = p.id
) AS ranked_scores
WHERE row_num <= 2
ORDER BY plugin_id, computed_at DESC;
""",
nativeQuery = true)
List<Score> findLatestScoreForAllPlugins();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Jenkins Infra
* Copyright (c) 2023-2024 Jenkins Infra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,10 +21,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.jenkins.pluginhealth.scoring.service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -52,17 +52,31 @@ public Score save(Score score) {

@Transactional(readOnly = true)
public Optional<Score> latestScoreFor(String pluginName) {
return pluginService.findByName(pluginName)
.flatMap(repository::findFirstByPluginOrderByComputedAtDesc);
return pluginService.findByName(pluginName).flatMap(repository::findFirstByPluginOrderByComputedAtDesc);
}

@Transactional(readOnly = true)
public List<Score> getLatestScoresSummary() {
return repository.findLatestScoreForAllPlugins();
}

@Transactional(readOnly = true)
public Map<String, Score> getLatestScoresSummaryMap() {
return repository.findLatestScoreForAllPlugins().stream()
.collect(Collectors.toMap(
score -> score.getPlugin().getName(),
score -> score
));
public Map<String, Score> getLatestScoresSummaryMap(List<Score> scores) {
return scores.stream()
.collect(Collectors.toMap(
score -> score.getPlugin().getName(),
score -> score,
(existingScore, newScore) -> existingScore));
}

@Transactional(readOnly = true)
public Map<String, Long> getPreviousScoreMap(List<Score> scores) {
return scores.stream()
.collect(Collectors.toMap(
score -> score.getPlugin().getName(), // Key mapper
Score::getValue, // Value mapper
(existingValue, newValue) -> newValue // Merge function
));
}

@Transactional(readOnly = true)
Expand All @@ -72,21 +86,19 @@ public ScoreStatistics getScoresStatistics() {
final int numberOfElement = values.length;

return new ScoreStatistics(
Math.round((float) Arrays.stream(values).sum() / numberOfElement),
values[0],
values[numberOfElement - 1],
values[(int) (numberOfElement * .25)],
values[(int) (numberOfElement * .5)],
values[(int) (numberOfElement * .75)]
);
Math.round((float) Arrays.stream(values).sum() / numberOfElement),
values[0],
values[numberOfElement - 1],
values[(int) (numberOfElement * .25)],
values[(int) (numberOfElement * .5)],
values[(int) (numberOfElement * .75)]);
}

@Transactional
public int deleteOldScores() {
return repository.deleteOldScoreFromPlugin();
}

public record ScoreStatistics(double average, int minimum, int maximum, int firstQuartile, int median,
int thirdQuartile) {
}
public record ScoreStatistics(
double average, int minimum, int maximum, int firstQuartile, int median, int thirdQuartile) {}
}
Loading

0 comments on commit 6a9b550

Please sign in to comment.