Skip to content

Commit

Permalink
Merge branch 'release/0.0.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
David committed Dec 11, 2021
2 parents 06e1fe4 + 676480b commit 36701d5
Show file tree
Hide file tree
Showing 13 changed files with 225,309 additions and 99 deletions.
70 changes: 39 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
<!-- PROJECT LOGO -->
<br />
<p align="center">

<h3 align="center">TourGuide</h3>

<p align="center">
Tour Guide microservices
<br />
<br />
<a href="#summary"><strong>↓ Explore the docs ↓</strong></a>
<br />
<br />
<a href="https://github.com/s2680854/openclassrooms_8th-project_tourguide-microservices/issues">Report Bug</a>
·
<a href="https://github.com/s2680854/openclassrooms_8th-project_tourguide-microservices/issues">Report Feature</a>
·
<a href="mailto:[email protected]">Contact Me</a>
</p>
</p>


<div align="center">
<img src="src/main/resources/static/logo.png" alt="logo" width="116" height="120" />
<h3 align="center">TourGuide API<i></i></h3>
<p align="center">
Changez votre manière de voyager !
<br />
<br />
<a href="#summary"><strong>↓ Explore the docs ↓</strong></a>
<br />
<br />
<a href="https://github.com/s2680854/openclassrooms_8th-project_tourguide-microservice/issues">Report Bug</a>
·
<a href="https://github.com/s2680854/openclassrooms_8th-project_tourguide-microservice/issues">Report Feature</a>
·
<a href="mailto:[email protected]">Contact Me</a>
</p>
</div>

> # 🛈 For versioning details, please visite '[Releases](https://github.com/s2680854/openclassrooms_8th-project_tourguide-microservice/releases)' tab.
>
<!-- TABLE OF CONTENTS -->
<details open="open">
<summary id="summary">Table of Contents</summary>
<ol>
<li><a href="#acknowledgements">Acknowledgements</a></li>
<li><a href="#contact">Contact</a></li>
</ol>
<summary id="summary"><b>Table of Contents</b></summary>
<ol>
<li><a href="#contact">Introduction</a></li>
<li><a href="#acknowledgements">Acknowledgements</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#project">Project Link</a></li>
</ol>
</details>

<!-- ACKNOWLEDGEMENTS -->
<!-- INTRODUCTION -->
## 📝 Introduction
TourGuide is a Spring Boot application that has been a centerpiece in TripMasters app portfolio. It allows users to see tourist attractions and to get package deals on hotel stays and admission to various attractions.
<p id="acknowledgements"></p>

## Acknowledgements
<div align="center">
<img src="src/main/resources/static/header.png" alt="header" />
</div>

<!-- ACKNOWLEDGEMENTS -->
## 📝 Acknowledgements
<p id="acknowledgements"></p>

* [Project Assignment](https://openclassrooms.com/fr/paths/191/projects/742/assignment)

<!-- CONTACT -->

## Contact

## ✉️ Contact
<p id="contact"></p>

David - [Mail me here!](mailto:[email protected])

[» Project Link](https://github.com/s2680854/openclassrooms_8th-project_tourguide-microservices)
## 🔗 [» Project Link](https://github.com/s2680854/openclassrooms_8th-project_tourguide-microservice)
<p id="project"></p>
Binary file added TourGuide/Snap.20211210.142540.16468.0004.trc
Binary file not shown.
Empty file.
Binary file not shown.
225,175 changes: 225,175 additions & 0 deletions TourGuide/javacore.20211210.142540.16468.0003.txt

Large diffs are not rendered by default.

26 changes: 20 additions & 6 deletions TourGuide/src/main/java/tourGuide/model/Tracker.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tourGuide.model;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

import org.apache.commons.lang3.time.StopWatch;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -40,21 +38,37 @@ public void stopTracking() {
*/
@Override
public void run() {

//=== SERVICES ===
StopWatch stopWatch = new StopWatch();

while(true) {
if(Thread.currentThread().isInterrupted() || stop) {
logger.debug("Tracker stopping");
break;
}


// Retrieves the users
List<User> users = tourGuideService.getAllUsers();
logger.debug("Begin Tracker. Tracking " + users.size() + " users.");

//=== TIMER START ===
stopWatch.start();
users.forEach(u -> tourGuideService.trackUserLocation(u));

// Tracks asynchronously each user's location
CompletableFuture<?>[] trackFutureUserLocations = users.stream()
.map(tourGuideService::trackUserLocation)
.toArray(CompletableFuture[]::new);
CompletableFuture.allOf(trackFutureUserLocations).join();

stopWatch.stop();
logger.debug("Tracker Time Elapsed: " + TimeUnit.MILLISECONDS.toSeconds(stopWatch.getTime()) + " seconds.");
//=== TIMER END ===

// Reset the timer and print the time result
logger.debug("Tracker Time Elapsed: " + TimeUnit.MILLISECONDS.toSeconds(stopWatch.getTime()) + " seconds.");
stopWatch.reset();

// Sleeps 5 seconds
try {
logger.debug("Tracker sleeping");
TimeUnit.SECONDS.sleep(trackingPollingInterval);
Expand Down
10 changes: 7 additions & 3 deletions TourGuide/src/main/java/tourGuide/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import gpsUtil.location.VisitedLocation;
import tripPricer.Provider;
Expand Down Expand Up @@ -57,8 +60,9 @@ public Date getLatestLocationTimestamp() {
return latestLocationTimestamp;
}

public void addToVisitedLocations(VisitedLocation visitedLocation) {
visitedLocations.add(visitedLocation);
public void addToVisitedLocation(VisitedLocation visitedLocation) {

visitedLocations.add(visitedLocation);
}

public List<VisitedLocation> getVisitedLocations() {
Expand All @@ -75,7 +79,7 @@ public void clearVisitedLocations() {

public void addUserReward(UserReward userReward) {

if(userRewards.stream().filter(r -> !r.attraction.attractionName.equals(userReward.attraction)).count() == 0) {
if(userRewards.stream().filter(r -> r.attraction.attractionName.equals(userReward.attraction)).count() == 0) {
userRewards.add(userReward);
}
}
Expand Down
44 changes: 18 additions & 26 deletions TourGuide/src/main/java/tourGuide/service/RewardsService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package tourGuide.service;

import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -48,34 +49,24 @@ public void setDefaultProximityBuffer() {
/** Add the user a reward if an attraction is visited.
* @param user User object.
*/
public void calculateRewards(User user) {
public CompletableFuture<?> calculateRewards(User user) {

Queue<Attraction> attractionsList = new ConcurrentLinkedQueue<Attraction>();
Queue<UserReward> userRewardsList = new ConcurrentLinkedQueue<UserReward>();
Queue<VisitedLocation> userVisitedLocations = new ConcurrentLinkedQueue<VisitedLocation>();
Queue<Attraction> attractionsList = new ConcurrentLinkedQueue<>();
attractionsList.addAll(gpsUtil.getAttractions());
Queue<VisitedLocation> userVisitedLocations = new ConcurrentLinkedQueue<>();
userVisitedLocations.addAll(user.getVisitedLocations());
userRewardsList.addAll(user.getUserRewards());

for (VisitedLocation visitedLocation : userVisitedLocations) {
for (Attraction attraction : attractionsList) {
int attractionsCount = 0;
for (UserReward userReward : userRewardsList) {
if (userReward.attraction.attractionName.equals(attraction.attractionName)) {
attractionsCount++;
}
}
if(attractionsCount == 0) {
if(nearAttraction(visitedLocation, attraction)) {
/*logger.debug("Test: UserReward(" + visitedLocation.userId + ", "
+ attraction.attractionName + ", " + getRewardPoints(attraction, user) + ")");*/
user.addUserReward(new UserReward(visitedLocation,
attraction,
getRewardPoints(attraction, user)));
}
}
}
}

return CompletableFuture.runAsync(() -> {
userVisitedLocations.stream().forEach(visitedLocation -> {
attractionsList.stream()
.filter(attraction -> nearAttraction(visitedLocation, attraction))
.forEach(attraction -> {
user.addUserReward(new UserReward(visitedLocation,
attraction,
getRewardPoints(attraction, user)));
});
});
});
}

/** Tell if the an attraction is close.
Expand Down Expand Up @@ -120,6 +111,7 @@ public double getDistance(Location loc1, Location loc2) {

double nauticalMiles = 60 * Math.toDegrees(angle);
double statuteMiles = STATUTE_MILES_PER_NAUTICAL_MILE * nauticalMiles;

return statuteMiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -131,7 +129,7 @@ public List<Provider> getTripDeals(User user) {
public VisitedLocation trackUserLocation(User user) {

VisitedLocation visitedLocation = gpsUtil.getUserLocation(user.getUserId());
user.addToVisitedLocations(visitedLocation);
user.addToVisitedLocation(visitedLocation);
rewardsService.calculateRewards(user);

return visitedLocation;
Expand Down Expand Up @@ -236,7 +234,7 @@ private void initializeInternalUsers() {
String phone = "000";
String email = userName + "@tourGuide.com";
User user = new User(UUID.randomUUID(), userName, phone, email);
logger.debug("Create User: " + user.getUserName());
//logger.debug("Create User: " + user.getUserName());
generateUserLocationHistory(user);

internalUserMap.put(userName, user);
Expand All @@ -250,7 +248,7 @@ private void initializeInternalUsers() {
private void generateUserLocationHistory(User user) {

IntStream.range(0, 3).forEach(i-> {
user.addToVisitedLocations(new VisitedLocation(user.getUserId(), new Location(generateRandomLatitude(), generateRandomLongitude()), getRandomTime()));
user.addToVisitedLocation(new VisitedLocation(user.getUserId(), new Location(generateRandomLatitude(), generateRandomLongitude()), getRandomTime()));
});
}

Expand Down
Binary file added TourGuide/src/main/resources/static/header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added TourGuide/src/main/resources/static/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 46 additions & 26 deletions TourGuide/src/test/java/tourGuide/TestPerformance.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.time.StopWatch;
import org.junit.Ignore;
import org.junit.Test;

import gpsUtil.GpsUtil;
Expand Down Expand Up @@ -41,58 +42,77 @@ public class TestPerformance {
* highVolumeGetRewards: 100,000 users within 20 minutes:
* assertTrue(TimeUnit.MINUTES.toSeconds(20) >= TimeUnit.MILLISECONDS.toSeconds(stopWatch.getTime()));
*/

@Ignore

/**
* Users should be incremented up to 100,000, and test finishes within 15 minutes
*/
@Test
public void highVolumeTrackLocation() {

//=== SERVICES ===
GpsUtil gpsUtil = new GpsUtil();
RewardsService rewardsService = new RewardsService(gpsUtil, new RewardCentral());
// Users should be incremented up to 100,000, and test finishes within 15 minutes
InternalTestHelper.setInternalUserNumber(100);
TourGuideService tourGuideService = new TourGuideService(gpsUtil, rewardsService);
StopWatch stopWatch = new StopWatch();

//=== TEST SUBJECTS ===
List<User> allUsers = new ArrayList<>();
allUsers = tourGuideService.getAllUsers();

StopWatch stopWatch = new StopWatch();

//=== TIMER START===

stopWatch.start();
for(User user : allUsers) {
tourGuideService.trackUserLocation(user);
}
allUsers.forEach(user -> tourGuideService.trackUserLocation(user));

stopWatch.stop();
tourGuideService.tracker.stopTracking();
//=== TIMER END ===

// Print the time result
System.out.println("highVolumeTrackLocation: Time Elapsed: " + TimeUnit.MILLISECONDS.toSeconds(stopWatch.getTime()) + " seconds.");

assertTrue(TimeUnit.MINUTES.toSeconds(15) >= TimeUnit.MILLISECONDS.toSeconds(stopWatch.getTime()));
}

@Ignore

/**
* Users should be incremented up to 100 000, and test finishes within 20 minutes
*/
@Test
public void highVolumeGetRewards() {

//=== SERVICES ===
GpsUtil gpsUtil = new GpsUtil();
RewardsService rewardsService = new RewardsService(gpsUtil, new RewardCentral());

// Users should be incremented up to 100,000, and test finishes within 20 minutes
InternalTestHelper.setInternalUserNumber(100);
StopWatch stopWatch = new StopWatch();

//=== TIMER START===
stopWatch.start();

//=== TEST SUBJECTS ===
TourGuideService tourGuideService = new TourGuideService(gpsUtil, rewardsService);

Attraction attraction = gpsUtil.getAttractions().get(0);
List<User> allUsers = new ArrayList<>();
allUsers = tourGuideService.getAllUsers();
allUsers.forEach(u -> u.addToVisitedLocations(new VisitedLocation(u.getUserId(), attraction, new Date())));

allUsers.forEach(u -> rewardsService.calculateRewards(u));

for(User user : allUsers) {
assertTrue(user.getUserRewards().size() > 0);
}
List<User> allUsers = tourGuideService.getAllUsers();
// Add a visited location for each user
allUsers.forEach(u -> u.addToVisitedLocation(new VisitedLocation(u.getUserId(), attraction, new Date())));

// Add asynchronously rewards to the users for their visited locations
CompletableFuture<?>[] calculateFutureRewards =
allUsers.stream()
.map(rewardsService::calculateRewards)
.toArray(CompletableFuture[]::new);
CompletableFuture.allOf(calculateFutureRewards).join();

// Test if each user received the rewards
allUsers.forEach(user -> assertTrue(user.getUserRewards().size() > 0));

stopWatch.stop();
tourGuideService.tracker.stopTracking();
//=== TIMER END ===

// Print the time result
System.out.println("highVolumeGetRewards: Time Elapsed: " + TimeUnit.MILLISECONDS.toSeconds(stopWatch.getTime()) + " seconds.");

assertTrue(TimeUnit.MINUTES.toSeconds(20) >= TimeUnit.MILLISECONDS.toSeconds(stopWatch.getTime()));
}

}
Loading

0 comments on commit 36701d5

Please sign in to comment.