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 #51 from tiki/release/1.1.2
Browse files Browse the repository at this point in the history
Release/1.1.2
  • Loading branch information
mike-audi authored Apr 30, 2023
2 parents 34644e3 + c20bbe6 commit cd6b093
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 43 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.1
version: 1.1.2
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.1</version>
<version>1.1.2</version>
<packaging>jar</packaging>

<name>L0 Registry</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public void delete(String appId, String id){
addressService.deleteById(found.get());
signService.deleteAllById(found.get());
repository.deleteByCid(found.get().getCid());
usageService.decrement(appId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@
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);
Optional<UsageDO> getFirstByConfigAppIdOrderByCreatedDesc(String appId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.stripe.model.UsageRecord;
import com.stripe.param.SubscriptionListParams;
import com.stripe.param.UsageRecordCreateOnSubscriptionItemParams;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
Expand Down Expand Up @@ -46,16 +47,23 @@ public UsageService(
this.nuPriceId = nuPriceId;
}

@Transactional
public void increment(String appId){
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime start = now.truncatedTo(ChronoUnit.DAYS);
List<UsageDO> found = repository.getAllByConfigAppIdAndCreatedBetween(appId, start, start.plusDays(1));
long total;
UsageDO update;
if(found.isEmpty()){
Optional<UsageDO> latest = repository.getFirstByConfigAppIdOrderByCreatedDesc(appId);
update = new UsageDO();
update.setConfig(configService.getCreate(appId));
total = 1L;
if(latest.isPresent()){
total = latest.get().getTotal() + 1;
update.setConfig(latest.get().getConfig());
}else{
update.setConfig(configService.getCreate(appId));
total = 1L;
}
update.setCreated(now);
}else{
update = found.get(0);
Expand All @@ -64,7 +72,35 @@ public void increment(String appId){
update.setTotal(total);
update.setModified(now);
repository.save(update);
report(appId);
report(appId, total, 1L);
}

@Transactional
public void decrement(String appId){
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime start = now.truncatedTo(ChronoUnit.DAYS);
List<UsageDO> found = repository.getAllByConfigAppIdAndCreatedBetween(appId, start, start.plusDays(1));
long total;
UsageDO update;
if(found.isEmpty()){
Optional<UsageDO> latest = repository.getFirstByConfigAppIdOrderByCreatedDesc(appId);
update = new UsageDO();
if(latest.isPresent()){
total = latest.get().getTotal() - 1;
update.setConfig(latest.get().getConfig());
}else{
update.setConfig(configService.getCreate(appId));
total = 0L;
}
update.setCreated(now);
}else{
update = found.get(0);
total = update.getTotal() - 1;
}
update.setTotal(total);
update.setModified(now);
repository.save(update);
report(appId, total, 0L);
}

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

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

ConfigDO config = configService.getBilling(appId);
if (config.getBillingId() == null) {
Expand Down Expand Up @@ -146,13 +180,13 @@ void report(String appId) {
}

UsageRecordCreateOnSubscriptionItemParams mauParams = new UsageRecordCreateOnSubscriptionItemParams.Builder()
.setQuantity(monthlyTotal)
.setQuantity(activeUsers)
.build();
UsageRecordCreateOnSubscriptionItemParams nuParams = new UsageRecordCreateOnSubscriptionItemParams.Builder()
.setQuantity(1L)
.setQuantity(newUsers)
.build();
UsageRecord.createOnSubscriptionItem(mauItem, mauParams, null);
UsageRecord.createOnSubscriptionItem(nuItem, nuParams, null);
if(activeUsers > 0) UsageRecord.createOnSubscriptionItem(mauItem, mauParams, null);
if(newUsers > 0) UsageRecord.createOnSubscriptionItem(nuItem, nuParams, null);
}catch (StripeException ex){
logger.error(ex.getMessage(), ex);
}
Expand Down
111 changes: 86 additions & 25 deletions src/test/java/com/mytiki/l0_registry/UsageTest.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,25 @@
package com.mytiki.l0_registry;

import com.mytiki.l0_registry.features.latest.config.ConfigDO;
import com.mytiki.l0_registry.features.latest.config.ConfigService;
import com.mytiki.l0_registry.features.latest.id.IdDO;
import com.mytiki.l0_registry.features.latest.sign.SignRepository;
import com.mytiki.l0_registry.features.latest.usage.UsageAO;
import com.mytiki.l0_registry.features.latest.usage.UsageDO;
import com.mytiki.l0_registry.features.latest.usage.UsageRepository;
import com.mytiki.l0_registry.features.latest.usage.UsageService;
import com.mytiki.l0_registry.l0.auth.L0AuthService;
import com.mytiki.l0_registry.main.App;
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Subscription;
import com.stripe.model.SubscriptionCollection;
import com.stripe.model.SubscriptionItem;
import com.stripe.model.UsageRecord;
import com.stripe.param.SubscriptionListParams;
import com.stripe.param.UsageRecordCreateOnSubscriptionItemParams;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.client.MockRestServiceServer;

import java.net.URI;
import java.net.URISyntaxException;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
Expand All @@ -55,6 +36,9 @@ public class UsageTest {
@Autowired
private UsageRepository repository;

@Autowired
private ConfigService configService;

@Test
public void Test_IncrementNone_Success(){
ZonedDateTime now = ZonedDateTime.now();
Expand Down Expand Up @@ -95,4 +79,81 @@ public void Test_IncrementExists_Success(){
assertNotNull(usage.get(0).getModified());
assertNotNull(usage.get(0).getCreated());
}

@Test
public void Test_IncrementMultipleDays_Success(){
ZonedDateTime now = ZonedDateTime.now();

String appId = UUID.randomUUID().toString();
UsageDO usage = new UsageDO();

usage.setConfig(configService.getCreate(appId));
usage.setTotal(1L);
usage.setCreated(now.minusDays(3));
usage.setModified(now.minusDays(3));
repository.save(usage);

service.increment(appId);

ZonedDateTime start = now.truncatedTo(ChronoUnit.DAYS);
List<UsageDO> today =
repository.getAllByConfigAppIdAndCreatedBetween(appId, start, start.plusDays(1));

assertEquals(1, today.size());
assertEquals(appId, today.get(0).getConfig().getAppId());
assertEquals(2, today.get(0).getTotal());
assertNotNull(today.get(0).getUsageId());
assertNotNull(today.get(0).getModified());
assertNotNull(today.get(0).getCreated());
}

@Test
public void Test_DecrementMultipleDays_Success(){
ZonedDateTime now = ZonedDateTime.now();

String appId = UUID.randomUUID().toString();
UsageDO usage = new UsageDO();

usage.setConfig(configService.getCreate(appId));
usage.setTotal(1L);
usage.setCreated(now.minusDays(3));
usage.setModified(now.minusDays(3));
repository.save(usage);

service.decrement(appId);

ZonedDateTime start = now.truncatedTo(ChronoUnit.DAYS);
List<UsageDO> today =
repository.getAllByConfigAppIdAndCreatedBetween(appId, start, start.plusDays(1));

assertEquals(1, today.size());
assertEquals(appId, today.get(0).getConfig().getAppId());
assertEquals(0, today.get(0).getTotal());
assertNotNull(today.get(0).getUsageId());
assertNotNull(today.get(0).getModified());
assertNotNull(today.get(0).getCreated());
}

@Test
public void Test_DecrementExists_Success(){
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime start = now.truncatedTo(ChronoUnit.DAYS);

String appId = UUID.randomUUID().toString();
int iterations = 10;
for(int i=0; i<iterations; i++) {
service.increment(appId);
}
service.decrement(appId);

List<UsageDO> usage =
repository.getAllByConfigAppIdAndCreatedBetween(appId, start, start.plusDays(1));

assertEquals(1, usage.size());
assertEquals(appId, usage.get(0).getConfig().getAppId());
assertEquals(iterations-1, usage.get(0).getTotal());
assertNotNull(usage.get(0).getUsageId());
assertNotNull(usage.get(0).getModified());
assertNotNull(usage.get(0).getCreated());
}
}

0 comments on commit cd6b093

Please sign in to comment.