Skip to content

Commit

Permalink
feat: add user's usage request
Browse files Browse the repository at this point in the history
  • Loading branch information
astappiev committed Nov 10, 2024
1 parent c825a5e commit 9f7c6fc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import io.quarkus.hibernate.reactive.panache.PanacheEntityBase;
import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
import io.quarkus.runtime.annotations.RegisterForReflection;
import io.quarkus.security.Authenticated;
import io.quarkus.security.identity.SecurityIdentity;
import io.smallrye.mutiny.Uni;
Expand Down Expand Up @@ -68,16 +67,12 @@ public Uni<Void> delete(@QueryParam("id") Long id, @QueryParam("apikey") String
@GET
@Path("/usage")
@RolesAllowed({Roles.APPLICATION})
public Uni<Usage> chat() {
public Uni<UsageSummary> chat() {
ApiKey apikey = securityIdentity.getCredential(ApiKey.class);

return ChatUsage.findByApikey(apikey).map(Usage::new);
return ChatUsage.findByApikey(apikey).map(UsageSummary::new);
}

public record CreateToken(@NotNull @NotEmpty @Size(max = 255) String name, @Size(max = 512) String url, @Size(max = 1024) String description) {
}

@RegisterForReflection
public record Usage(ChatUsage chat) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import de.l3s.interweb.server.features.user.User;

@RegisterForReflection
public class ChatUsage {
@JsonProperty("input_tokens")
Expand All @@ -24,6 +26,22 @@ public ChatUsage(Long inputTokens, Long outputTokens, Double estimatedCost, Long
}

public static Uni<ChatUsage> findByApikey(ApiKey apikey) {
return ApiChatRequest.find("select sum(inputTokens) as inputTokens, sum(outputTokens) as outputTokens, sum(estimatedCost) as estimatedCost, count(*) as totalRequests from ApiChatRequest where apikey.id = ?1", apikey.id).project(ChatUsage.class).singleResult();
return ApiChatRequest.find("""
select sum(inputTokens) as inputTokens, sum(outputTokens) as outputTokens, sum(estimatedCost) as estimatedCost, count(*) as totalRequests
from ApiChatRequest
where apikey.id = ?1
""", apikey.id)
.project(ChatUsage.class)
.singleResult();
}

public static Uni<ChatUsage> findByUser(User user) {
return ApiChatRequest.find("""
select sum(inputTokens) as inputTokens, sum(outputTokens) as outputTokens, sum(estimatedCost) as estimatedCost, count(*) as totalRequests
from ApiChatRequest
where user.id = ?1
""", user.id)
.project(ChatUsage.class)
.singleResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.l3s.interweb.server.features.api;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public record UsageSummary(ChatUsage chat) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.jboss.logging.Logger;

import de.l3s.interweb.server.Roles;
import de.l3s.interweb.server.features.api.ChatUsage;
import de.l3s.interweb.server.features.api.UsageSummary;

@Tag(name = "Auth & Identity", description = "User management")
@Path("/")
Expand Down Expand Up @@ -134,6 +136,15 @@ public User me() {
return (User) securityIdentity.getPrincipal();
}

@GET
@Path("/users/usage")
@Authenticated
@Operation(summary = "Return the usage of the current user")
public Uni<UsageSummary> chat() {
User user = (User) securityIdentity.getPrincipal();
return ChatUsage.findByUser(user).map(UsageSummary::new);
}

public record LoginBody(@NotNull @NotEmpty @Email @Size(max = 255) String email) {
}
}

0 comments on commit 9f7c6fc

Please sign in to comment.