-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
12 changed files
with
190 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/org/panda_lang/reposilite/stats/StatsCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.panda_lang.reposilite.stats; | ||
|
||
import org.panda_lang.reposilite.Reposilite; | ||
import org.panda_lang.reposilite.console.NanoCommand; | ||
import org.panda_lang.utilities.commons.console.Effect; | ||
|
||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
public final class StatsCommand implements NanoCommand { | ||
|
||
private final int limiter; | ||
|
||
public StatsCommand(int limiter) { | ||
this.limiter = limiter; | ||
} | ||
|
||
@Override | ||
public boolean call(Reposilite reposilite) { | ||
StatsService statsService = reposilite.getStatsService(); | ||
Map<String, Integer> stats = statsService.fetchStats(entry -> entry.getValue() >= limiter); | ||
|
||
Reposilite.getLogger().info(""); | ||
Reposilite.getLogger().info("Statistics: "); | ||
Reposilite.getLogger().info(" Requests count: " + statsService.countRecords() + " (sum: " + statsService.sumRecords() + ")"); | ||
Reposilite.getLogger().info(" Recorded: " + (stats.isEmpty() ? "[] " : "") + "(list filtered by at least " + Effect.BLACK_BOLD + limiter + Effect.RESET + " recorded requests)"); | ||
int order = 0; | ||
|
||
for (Entry<String, Integer> entry : stats.entrySet()) { | ||
Reposilite.getLogger().info(" " + (++order) + ". (" + entry.getValue() + ") " + entry.getKey()); | ||
} | ||
|
||
Reposilite.getLogger().info(""); | ||
return true; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/panda_lang/reposilite/stats/StatsEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.panda_lang.reposilite.stats; | ||
|
||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class StatsEntity implements Serializable { | ||
|
||
private final Map<String, Integer> records = new HashMap<>(128); | ||
|
||
public void setRecords(Map<String, Integer> records) { | ||
this.records.putAll(records); | ||
} | ||
|
||
public Map<String, Integer> getRecords() { | ||
return records; | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/panda_lang/reposilite/stats/StatsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.panda_lang.reposilite.stats; | ||
|
||
import java.io.IOException; | ||
import java.util.Comparator; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
public final class StatsService { | ||
|
||
private final StatsEntity entity; | ||
private final StatsStorage statsStorage; | ||
|
||
public StatsService() { | ||
this.entity = new StatsEntity(); | ||
this.statsStorage = new StatsStorage(); | ||
} | ||
|
||
public void save() throws IOException { | ||
statsStorage.saveStats(entity); | ||
} | ||
|
||
public void load() throws IOException { | ||
StatsEntity storedEntity = statsStorage.loadStats(); | ||
entity.setRecords(storedEntity.getRecords()); | ||
} | ||
|
||
public void record(String uri) { | ||
entity.getRecords().compute(uri, (key, count) -> (count == null) ? 1 : count + 1); | ||
} | ||
|
||
public Map<String, Integer> fetchStats(Predicate<Entry<String, Integer>> filter) { | ||
return entity.getRecords().entrySet().stream() | ||
.filter(filter) | ||
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(oldValue, newValue) -> oldValue, LinkedHashMap::new)); | ||
} | ||
|
||
public int sumRecords() { | ||
return entity.getRecords().values().stream() | ||
.mapToInt(Integer::intValue) | ||
.sum(); | ||
} | ||
|
||
public int countRecords() { | ||
return entity.getRecords().size(); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/org/panda_lang/reposilite/stats/StatsStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2020 Dzikoysk | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.panda_lang.reposilite.stats; | ||
|
||
import org.panda_lang.reposilite.Reposilite; | ||
import org.panda_lang.reposilite.ReposiliteConstants; | ||
import org.panda_lang.reposilite.utils.FilesUtils; | ||
import org.panda_lang.reposilite.utils.YamlUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public final class StatsStorage { | ||
|
||
private static final File STATS_FILE = new File(ReposiliteConstants.STATS_FILE_NAME); | ||
|
||
public StatsEntity loadStats() throws IOException { | ||
if (!STATS_FILE.exists()) { | ||
Reposilite.getLogger().info("Generating stats data file..."); | ||
FilesUtils.copyResource("/stats.yml", ReposiliteConstants.STATS_FILE_NAME); | ||
Reposilite.getLogger().info("Empty stats file has been generated"); | ||
} | ||
else { | ||
Reposilite.getLogger().info("Using an existing stats data file"); | ||
} | ||
|
||
StatsEntity statsEntity = YamlUtils.load(STATS_FILE, StatsEntity.class); | ||
Reposilite.getLogger().info("Records: " + statsEntity.getRecords().size()); | ||
|
||
return statsEntity; | ||
} | ||
|
||
public void saveStats(StatsEntity entity) throws IOException { | ||
YamlUtils.save(STATS_FILE, entity); | ||
Reposilite.getLogger().info("Stored records: " + entity.getRecords().size()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
records: [] |