Skip to content

Commit

Permalink
feat: show listings statistics
Browse files Browse the repository at this point in the history
This feature gives statistical information about the following
1. total listings
2. rented listings
3.  total income
4. Yearly income broken down by monthly earnins
5. Yearly listings broken down by monthly listing

This gives the agent an overview of their performance.
  • Loading branch information
Hoxtygen committed Nov 30, 2024
1 parent 33235d8 commit 01157c3
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.bson.Document;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand Down Expand Up @@ -308,4 +309,13 @@ public ResponseEntity<SuccessDataResponse<Listing>> updateTakenListing(
updatedListing.setStatus(HttpStatus.OK);
return new ResponseEntity<>(updatedListing, HttpStatus.OK);
}

@GetMapping("/listing-statistics")
public ResponseEntity<SuccessDataResponse<Document>> getListingStatistics() {
SuccessDataResponse<Document> response = new SuccessDataResponse<>();
response.setData(listingService.getAggregateListingStats());
response.setMessage("Statistics data retrieved successfully");
response.setStatus(HttpStatus.OK);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.codeplanks.home360.domain.listing;

import lombok.Data;

import java.util.List;

@Data
public class AggregateListingStatistics {
private Integer total_listings;
private Integer rented_listings;
private Integer total_income;
private List<MonthlyIncome> income;
private List<MonthlyListings> listings;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.codeplanks.home360.domain.listing;

import java.util.Map;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class MonthlyIncome {
private int year;
private Map<String, Integer> months;

@Override
public String toString() {
return "MonthlyIncome{" + "year=" + year + ", months=" + months + '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.codeplanks.home360.domain.listing;

import lombok.Getter;
import lombok.Setter;

import java.util.Map;

@Getter
@Setter
public class MonthlyListings {
private int year;
private Map<String, Integer> months;

@Override
public String toString() {
return "MonthlyListings{" +
"year=" + year +
", months=" + months +
'}';
}
}
136 changes: 135 additions & 1 deletion src/main/java/com/codeplanks/home360/service/ListingServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
import com.codeplanks.home360.exception.UnAuthorizedException;
import com.codeplanks.home360.repository.ListingRepository;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.bson.BsonNull;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;

Expand All @@ -27,6 +32,7 @@
public class ListingServiceImpl implements ListingService {
private final ListingRepository listingRepository;
private final UserServiceImpl userService;
@Autowired MongoTemplate mongoTemplate;

Logger logger = LoggerFactory.getLogger(ListingServiceImpl.class);

Expand All @@ -37,7 +43,6 @@ public Listing createListing(ListingDTO request) {
throw new UnAuthorizedException(
"You are not authorized to create a listing." + " Please confirm your email to proceed.");
}
;

Listing listing =
Listing.builder()
Expand Down Expand Up @@ -153,4 +158,133 @@ public Listing updateRentedListing(RentUpdate rentUpdate) {

return savedListing;
}

public Document getAggregateListingStats() {
Integer userId = userService.extractUserId();
List<Document> pipeline = Arrays.asList(new Document("$match",
new Document("agent_id", userId)),
new Document("$facet",
new Document("totalListings", Arrays.asList(new Document("$count", "total_count")))
.append("rentedListings", Arrays.asList(new Document("$match",
new Document("rented", true)),
new Document("$count", "total_rented")))
.append("totalIncome", Arrays.asList(new Document("$match",
new Document("rented", true)),
new Document("$group",
new Document("_id",
new BsonNull())
.append("total_income",
new Document("$sum",
new Document("$add", Arrays.asList("$cost.annual_rent", "$cost.agent_fee", "$cost.caution_fee", "$cost.agreement_fee")))))))
.append("income", Arrays.asList(new Document("$match",
new Document("rented", true)),
new Document("$addFields",
new Document("total_income",
new Document("$add", Arrays.asList("$cost.annual_rent", "$cost.agent_fee", "$cost.caution_fee", "$cost.agreement_fee")))),
new Document("$group",
new Document("_id",
new Document("year",
new Document("$year", "$created_at"))
.append("month",
new Document("$month", "$created_at")))
.append("total_income",
new Document("$sum", "$total_income"))),
new Document("$group",
new Document("_id", "$_id.year")
.append("months",
new Document("$push",
new Document("k",
new Document("$arrayElemAt", Arrays.asList(Arrays.asList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"),
new Document("$subtract", Arrays.asList("$_id.month", 1L)))))
.append("v", "$total_income")))),
new Document("$addFields",
new Document("months",
new Document("$mergeObjects", Arrays.asList(new Document("$arrayToObject",
new Document("$map",
new Document("input",
new Document("$range", Arrays.asList(0L,
new Document("$cond", Arrays.asList(new Document("$eq", Arrays.asList("$_id",
new Document("$year",
new java.util.Date()))),
new Document("$month",
new java.util.Date()), 12L)))))
.append("as", "i")
.append("in",
new Document("k",
new Document("$arrayElemAt", Arrays.asList(Arrays.asList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"), "$$i")))
.append("v", 0L)))),
new Document("$arrayToObject", "$months"))))),
new Document("$addFields",
new Document("months",
new Document("$map",
new Document("input",
new Document("$objectToArray", "$months"))
.append("as", "m")
.append("in",
new Document("name", "$$m.k")
.append(
"amount", "$$m.v"))))),
new Document("$project",
new Document("_id", 0L)
.append("year", "$_id")
.append("months", 1L))))
.append("listings", Arrays.asList(new Document("$group",
new Document("_id",
new Document("year",
new Document("$year", "$created_at"))
.append("month",
new Document("$month", "$created_at")))
.append("count",
new Document("$sum", 1L))),
new Document("$group",
new Document("_id", "$_id.year")
.append("months",
new Document("$push",
new Document("k",
new Document("$arrayElemAt", Arrays.asList(Arrays.asList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"),
new Document("$subtract", Arrays.asList("$_id.month", 1L)))))
.append("v", "$count")))),
new Document("$addFields",
new Document("months",
new Document("$mergeObjects", Arrays.asList(new Document("$arrayToObject",
new Document("$map",
new Document("input",
new Document("$range", Arrays.asList(0L,
new Document("$cond", Arrays.asList(new Document("$eq", Arrays.asList("$_id",
new Document("$year",
new java.util.Date()))),
new Document("$month",
new java.util.Date()), 12L)))))
.append("as", "i")
.append("in",
new Document("k",
new Document("$arrayElemAt", Arrays.asList(Arrays.asList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"), "$$i")))
.append("v", 0L)))),
new Document("$arrayToObject", "$months"))))),
new Document("$addFields",
new Document("months",
new Document("$map",
new Document("input",
new Document("$objectToArray", "$months"))
.append("as", "m")
.append("in",
new Document("name", "$$m.k")
.append(
"amount", "$$m.v"))))),
new Document("$project",
new Document("_id", 0L)
.append("year", "$_id")
.append("months", 1L))))),
new Document("$project",
new Document("total_listings",
new Document("$arrayElemAt", Arrays.asList("$totalListings.total_count", 0L)))
.append("rented_listings",
new Document("$arrayElemAt", Arrays.asList("$rentedListings.total_rented", 0L)))
.append("total_income",
new Document("$arrayElemAt", Arrays.asList("$totalIncome.total_income", 0L)))
.append("income", "$income")
.append("listings", "$listings")));

return mongoTemplate.getDb().getCollection("listings").aggregate(pipeline).first();
}
}

0 comments on commit 01157c3

Please sign in to comment.