Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #50 from tiki/release/1.1.1
Browse files Browse the repository at this point in the history
fix: sum app totals over last month
  • Loading branch information
mike-audi authored Apr 30, 2023
2 parents 60757f8 + 3c2c8b7 commit 34644e3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ info:
license:
name: MIT
url: https://github.com/tiki/l0-registry/blob/main/LICENSE
version: 1.1.0
version: 1.1.1
servers:
- url: https://registry.l0.mytiki.com
paths:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.mytiki</groupId>
<artifactId>l0_registry</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<packaging>jar</packaging>

<name>L0 Registry</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.mytiki.l0_registry.features.latest.usage;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;

public interface UsageRepository extends JpaRepository<UsageDO, Long> {
List<UsageDO> getAllByConfigAppIdAndCreatedBetween(String appId, ZonedDateTime start, ZonedDateTime end);

@Query("SELECT SUM(u.total) " +
"FROM UsageDO u " +
"WHERE u.config.appId = :appId " +
"AND u.created >= :start AND u.created < :end")
Long getTotalByConfigAppIdAndCreatedBetween(String appId, ZonedDateTime start, ZonedDateTime end);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void increment(String appId){
update.setTotal(total);
update.setModified(now);
repository.save(update);
report(appId, total);
report(appId);
}

public List<UsageAO> get(String userId, Integer month, Integer year){
Expand Down Expand Up @@ -109,9 +109,12 @@ private Set<String> getApps(String userId){
}

@Async
void report(String appId, long total) {
void report(String appId) {
try {
if (total <= minUsers) return;
ZonedDateTime end = ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).plusDays(1);
Long monthlyTotal = repository.getTotalByConfigAppIdAndCreatedBetween(appId, end.minusMonths(1), end);
if (monthlyTotal == null || monthlyTotal <= minUsers) return;

ConfigDO config = configService.getBilling(appId);
if (config.getBillingId() == null) {
logger.error("No billing id for appId: " + appId);
Expand Down Expand Up @@ -143,7 +146,7 @@ void report(String appId, long total) {
}

UsageRecordCreateOnSubscriptionItemParams mauParams = new UsageRecordCreateOnSubscriptionItemParams.Builder()
.setQuantity(total)
.setQuantity(monthlyTotal)
.build();
UsageRecordCreateOnSubscriptionItemParams nuParams = new UsageRecordCreateOnSubscriptionItemParams.Builder()
.setQuantity(1L)
Expand Down

0 comments on commit 34644e3

Please sign in to comment.