Skip to content

Commit

Permalink
Merge pull request #48 from daithihearn/agg-pipeline
Browse files Browse the repository at this point in the history
feat: use aggregation pipeline for 30 day average calculation
  • Loading branch information
daithihearn authored Oct 27, 2023
2 parents b2b5772 + 8d158c1 commit 78cb427
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.16.2
1.16.3
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.springframework.boot.runApplication
info = Info(
title =
"Electricity Prices API",
version = "1.16.2",
version = "1.16.3",
description = "Returns PVPC electricity prices for a given range"
),
servers = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import ie.daithi.electricityprices.model.*
import ie.daithi.electricityprices.repos.PriceRepo
import ie.daithi.electricityprices.utils.*
import org.apache.logging.log4j.LogManager
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.aggregation.Aggregation.*
import org.springframework.data.mongodb.core.aggregation.MatchOperation
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import java.time.LocalDate
Expand All @@ -14,7 +18,8 @@ import java.time.LocalDateTime
class PriceService(
private val priceRepo: PriceRepo,
private val reeRest: WebClient,
private val esiosRest: WebClient
private val esiosRest: WebClient,
private val mongoTemplate: MongoTemplate
) {

private val logger = LogManager.getLogger(this::class.simpleName)
Expand Down Expand Up @@ -121,8 +126,22 @@ class PriceService(
fun getThirtyDayAverage(date: LocalDate): Double {
val start = date.minusDays(30).atStartOfDay().minusSeconds(1)
val end = date.plusDays(1).atStartOfDay().minusSeconds(1)
val prices = getPrices(start, end)
return calculateAverage(prices)

// Stage 1: Filter documents within the desired date range
val matchStage: MatchOperation = match(Criteria.where("dateTime").gte(start).lte(end))

// Stage 2: Calculate the average price
val groupStage = group().avg("price").`as`("averagePrice")

// Define the aggregation pipeline
val aggregation = newAggregation(matchStage, groupStage)

// Execute the aggregation
val result = mongoTemplate.aggregate(aggregation, "prices", AveragePriceResult::class.java)

// Retrieve and return the average price
return result.uniqueMappedResult?.averagePrice
?: throw DataNotAvailableYetException("30 day average data not available for $date")
}

/*
Expand Down Expand Up @@ -181,3 +200,5 @@ class PriceService(


}

data class AveragePriceResult(val averagePrice: Double)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.web.reactive.function.client.WebClient
import java.time.LocalDate
import java.time.LocalDateTime
Expand All @@ -17,8 +18,9 @@ class PriceServiceTest {
private val priceRepo = mockk<PriceRepo>(relaxed = true)
private val reeRest = mockk<WebClient>(relaxed = true)
private val esiosRest = mockk<WebClient>(relaxed = true)
private val mongoTemplate = mockk<MongoTemplate>(relaxed = true)

private val priceService = PriceService(priceRepo, reeRest, esiosRest)
private val priceService = PriceService(priceRepo, reeRest, esiosRest, mongoTemplate)

@Nested
inner class GetPricesTest {
Expand Down

0 comments on commit 78cb427

Please sign in to comment.