Skip to content

Commit

Permalink
Merge pull request #61 from MathieuSoysal/road-to-v2
Browse files Browse the repository at this point in the history
Road to v2
  • Loading branch information
MathieuSoysal authored Feb 11, 2024
2 parents b4d9bfd + 65210c4 commit cacad64
Show file tree
Hide file tree
Showing 86 changed files with 754 additions and 711 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/Integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,4 @@ jobs:
with:
ARCHIVE_MODE: DAY_SUM_UP
SPECIFIC_DAY: "2024-01-03"
LINK_TO_DATA: https://mathieusoysal.github.io/CROUS-assistant-Collector/v1/logements-crous/available

creating-archive-all-logement:
needs: unit-test
uses: ./.github/workflows/reusable-archiving.yml
with:
ARCHIVE_MODE: ALL_LOGEMENTS
SPECIFIC_DAY: "2024-01-03"
LINK_TO_DATA: https://mathieusoysal.github.io/CROUS-assistant-Collector/v1/logements-crous/available
LINK_TO_DATA: https://mathieusoysal.github.io/CROUS-assistant-Collector/v2
4 changes: 2 additions & 2 deletions .github/workflows/archiving-day.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
uses: ./.github/workflows/reusable-archiving.yml
with:
ARCHIVE_MODE: DAY_SUM_UP
LINK_TO_DATA: https://mathieusoysal.github.io/CROUS-assistant-Collector/v1/logements-crous/available
LINK_TO_DATA: https://mathieusoysal.github.io/CROUS-assistant-Collector/v2
deploy: true
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -31,7 +31,7 @@ jobs:
with:
SPECIFIC_DAY: ${{ github.event.inputs.specific-date }}
ARCHIVE_MODE: DAY_SUM_UP
LINK_TO_DATA: https://mathieusoysal.github.io/CROUS-assistant-Collector/v1/logements-crous/available
LINK_TO_DATA: https://mathieusoysal.github.io/CROUS-assistant-Collector/v2
deploy: true
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/reusable-archiving.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
description: 'The link to the data to archive'
required: false
type: string
default: 'https://mathieusoysal.github.io/CROUS-assistant-Collector/v1/logements-crous/available'
default: 'https://mathieusoysal.github.io/CROUS-assistant-Collector/v2'
SPECIFIC_DAY:
description: 'The day to archive, day is used olny if ARCHIVE_MODE is DAY'
required: false
Expand All @@ -27,7 +27,7 @@ on:
description: 'The target folder to deploy the archive to github pages'
required: false
type: string
default: 'v1/logements-crous'
default: 'v2'

secrets:
MAIL:
Expand Down
39 changes: 0 additions & 39 deletions .github/workflows/update-all-logements.yml

This file was deleted.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


<name>CROUS-Assistant-Collector</name>
<description>Program to collect available logement CROUS.</description>
<description>Program to collect available residence CROUS.</description>
<url>https://github.com/MathieuSoysal/CROUS-assistant-Collector</url>

<properties>
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/io/github/mathieusoysal/ArchiveMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import com.github.forax.beautifullogger.Logger;

import io.github.mathieusoysal.archivers.ArchiverAllLogements;
import io.github.mathieusoysal.archivers.ArchiverDay;
import io.github.mathieusoysal.archivers.ArchiverHour;
import io.github.mathieusoysal.archivers.Archiver;
import io.github.mathieusoysal.archivers.Archivers;

public enum ArchiveMode {
DAY_SUM_UP(new ArchiverDay()),
ALL_LOGEMENTS(new ArchiverAllLogements()),
HOUR(new ArchiverHour());
DAY_SUM_UP(Archivers.ARCHIVER_DAY),
HOUR(Archivers.ARCHIVER_HOUR);

private static final Logger LOGGER = Logger.getLogger();
private final Archiver archiver;
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/io/github/mathieusoysal/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ public String getName() {
}

public boolean isPresent() {
return System.getenv(name) != null;
return getValueWithProperties() != null;
}

public String getValue() {
LOGGER.info(() -> "Getting " + name + " from environment variables");
String propertie = System.getenv(name);
String propertie = getValueWithProperties();
if (propertie == null) {
LOGGER.error(() -> name + " not found in environment variables");
throw new PropertiesNotFoundRuntimeException(name);
}
return propertie;
}

private String getValueWithProperties() {
String result = System.getenv(name);
if (result == null)
return System.getProperty(name);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.mathieusoysal.archivers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import io.github.mathieusoysal.data.managment.collectors.DataCollectorFromArchive;
import io.github.mathieusoysal.residence.Residence;

class ArchivedResidences {

private Map<Integer, Residence> residences;

public ArchivedResidences(Set<Residence> residences) {
this.residences = residences
.stream()
.collect(Collectors.toMap(Residence::getId, residence -> residence));
}

public ArchivedResidences() {
this.residences = new HashMap<>();
}

public static ArchivedResidences generateArchivedResidencesFromLinkArchive(final String linkToArchive) {
var dataCollector = new DataCollectorFromArchive(linkToArchive);
var residences = new ArchivedResidences();
residences.addResidences(dataCollector.getAllResidences());
return residences;
}

public void addResidence(Residence residence) {
if (!residences.containsKey(residence.getId()))
residences.put(residence.getId(), residence);
else {
Residence existingResidence = residences.get(residence.getId());
existingResidence.addResidence(residence);
}
}

public void addResidences(Collection<Residence> residences) {
residences.forEach(this::addResidence);
}

public void addResidences(Residence[][] residences) {
for (Residence[] residences2 : residences)
for (Residence residence : residences2)
addResidence(residence);
}

public List<Residence> getResidences() {
return new ArrayList<>(residences.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@FunctionalInterface
public interface Archiver {
public static final String DEFAULT_LINK_TO_ARCHIVE = "https://mathieusoysal.github.io/CROUS-assistant-Collector/v1/logements-crous/available";
static final String DEFAULT_LINK_TO_ARCHIVE = "https://mathieusoysal.github.io/CROUS-assistant-Collector/v2";
static final ArchiveSaver ARCHIVE_SAVER = ArchiveSaver.startPath();

static String getLinkToArchive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,37 @@
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import com.github.forax.beautifullogger.Logger;

import io.github.mathieusoysal.data.managment.collectors.DataCollectorFromArchive;
import io.github.mathieusoysal.data.managment.savers.ArchiveName;
import io.github.mathieusoysal.logement.LogementsClassifier;
import io.github.mathieusoysal.residence.Residence;

public class ArchiverAllLogements implements Archiver {
class ArchiverAllResidences {

private static final Logger LOGGER = Logger.getLogger();

@Override
public void archive() {
var archivedFile = archiveAllLogements();
updateHashOfAllLogement(archivedFile);
static void updateArchiveOfAllResidences(List<Residence> collectedResidences) {
var archivedFile = updateArchivedFile(collectedResidences);
updateHashOfAllResidence(archivedFile);
}

private void updateHashOfAllLogement(File archivedFile) {
var hash = getHashOfArchivedFile(archivedFile);
ARCHIVE_SAVER
.endPathAndSaveData(ArchiveName.HASH_ALL_LOGEMENTS, hash);
private static File updateArchivedFile(List<Residence> collectedResidences) {
var archivedResidences = ArchivedResidences.generateArchivedResidencesFromLinkArchive(Archiver.getLinkToArchive());
archivedResidences.addResidences(collectedResidences);
var archivedFile = Archiver.ARCHIVE_SAVER
.endPathAndSaveData(ArchiveName.ALL_LOGEMENTS, archivedResidences.getResidences());
return archivedFile;
}

private File archiveAllLogements() {
var dataCollector = new DataCollectorFromArchive(Archiver.getLinkToArchive());
var logements = new LogementsClassifier();
logements.addLogements(dataCollector.getAllLogements());
logements.addLogements(dataCollector.getConvertedSumUpOfDay(Archiver.getDayToArchive()));
return ARCHIVE_SAVER
.endPathAndSaveData(ArchiveName.ALL_LOGEMENTS, logements.getLogements());
private static void updateHashOfAllResidence(File archivedFile) {
var hash = getHashOfArchivedFile(archivedFile);
Archiver.ARCHIVE_SAVER
.endPathAndSaveData(ArchiveName.HASH_ALL_LOGEMENTS, hash);
}

String getHashOfArchivedFile(File archivedFile) {
static String getHashOfArchivedFile(File archivedFile) {
String result = "";
try {
byte[] digest = getHashedBytesFrom(archivedFile);
Expand All @@ -52,17 +50,16 @@ String getHashOfArchivedFile(File archivedFile) {
return result;
}

private String convertBytesToString(byte[] digest) {
private static String convertBytesToString(byte[] digest) {
StringBuilder sb = new StringBuilder();
for (byte b : digest)
sb.append(String.format("%02x", b));
return sb.toString();
}

private byte[] getHashedBytesFrom(File archivedFile) throws NoSuchAlgorithmException, IOException {
private static byte[] getHashedBytesFrom(File archivedFile) throws NoSuchAlgorithmException, IOException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(Files.readAllBytes(archivedFile.toPath()));
return md.digest();
}

}
10 changes: 6 additions & 4 deletions src/main/java/io/github/mathieusoysal/archivers/ArchiverDay.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package io.github.mathieusoysal.archivers;

import io.github.mathieusoysal.data.managment.collectors.DataCollectorFromArchive;
import io.github.mathieusoysal.data.managment.convertors.Convertor;
import io.github.mathieusoysal.data.managment.savers.ArchiveName;

public class ArchiverDay implements Archiver {
class ArchiverDay implements Archiver {

@Override
public void archive() {
var dataCollector = new DataCollectorFromArchive(Archiver.getLinkToArchive());
var sumUpOfTheDay = dataCollector.getSumUpOfDay(Archiver.getDayToArchive());
var sumUpOfTheDay = dataCollector.getConvertedSumUpOfDay(Archiver.getDayToArchive());
String sumUpOfTheDayAsString = Convertor.convertIdMatrixToJson(sumUpOfTheDay);
ARCHIVE_SAVER
.addPath("available")
.addPath("available-residences-id")
.addPath(Archiver.getDayToArchive())
.endPathAndSaveData(ArchiveName.DAY_SUM_UP, sumUpOfTheDay);
.endPathAndSaveData(ArchiveName.DAY_SUM_UP, sumUpOfTheDayAsString);
}

}
17 changes: 13 additions & 4 deletions src/main/java/io/github/mathieusoysal/archivers/ArchiverHour.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package io.github.mathieusoysal.archivers;

import java.time.LocalDate;
import java.util.List;

import io.github.mathieusoysal.Properties;
import io.github.mathieusoysal.data.managment.collectors.DataCollectorFromCrous;
import io.github.mathieusoysal.data.managment.savers.ArchiveName;
import io.github.mathieusoysal.residence.Residence;

public class ArchiverHour implements Archiver {
class ArchiverHour implements Archiver {

@Override
public void archive() {
var logements = DataCollectorFromCrous.getAvailableLogementsWithConnection(Properties.MAIL.getValue(),
var residences = archiveHour();
ArchiverAllResidences.updateArchiveOfAllResidences(residences);
}

private List<Residence> archiveHour() {
var residences = DataCollectorFromCrous.getAvailableResidencesWithConnection(Properties.MAIL.getValue(),
Properties.PASSWORD.getValue());
var ids = residences.stream().map(Residence::getId).toList();
ARCHIVE_SAVER
.addPath("available")
.addPath("available-residences-id")
.addPath(LocalDate.now())
.endPathAndSaveData(ArchiveName.HOUR, logements);
.endPathAndSaveData(ArchiveName.HOUR, ids);
return residences;
}

}
18 changes: 18 additions & 0 deletions src/main/java/io/github/mathieusoysal/archivers/Archivers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.mathieusoysal.archivers;

public enum Archivers implements Archiver {
ARCHIVER_DAY(new ArchiverDay()),
ARCHIVER_HOUR(new ArchiverHour());

private Archiver archiver;

Archivers(Archiver archiver) {
this.archiver = archiver;
}

@Override
public void archive() {
archiver.archive();
}

}
Loading

0 comments on commit cacad64

Please sign in to comment.