Skip to content

Commit

Permalink
feat: add /chats/stats to display usage estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
astappiev committed Aug 12, 2024
1 parent 6fd2c93 commit 68147bd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
securitySchemes = {
@SecurityScheme(
securitySchemeName = "JWT",
description = "Use /users/login to obtain a JWT token.",
description = "Use /login to obtain a JWT token.",
type = SecuritySchemeType.HTTP,
scheme = "bearer",
bearerFormat = "JWT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public class Chat extends PanacheEntityBase {
public String title;

@NotNull
@ColumnDefault("0")
@Column(name = "used_tokens")
public Integer usedTokens = 0;

@NotNull
@ColumnDefault("0")
@Column(name = "estimated_cost")
public Double estimatedCost = 0d;

@CreationTimestamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@ public Uni<Void> delete(@PathParam("uuid") UUID id) {

return Chat.findById(apikey, id).call(PanacheEntityBase::delete).replaceWithVoid();
}

@GET
@Path("/stats")
public Uni<ChatsStats> chat() {
ApiKey apikey = securityIdentity.getCredential(ApiKey.class);

return ChatsStats.findByApikey(apikey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.l3s.interweb.server.features.chat;

import io.quarkus.runtime.annotations.RegisterForReflection;
import io.smallrye.mutiny.Uni;

import com.fasterxml.jackson.annotation.JsonProperty;

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

@RegisterForReflection
public class ChatsStats {
@JsonProperty("used_tokens")
public Long usedTokens;
@JsonProperty("estimated_cost")
public Double estimatedCost;
@JsonProperty("total_chats")
public Long totalChats;

public ChatsStats(Long usedTokens, Double estimatedCost, Long totalChats) {
this.usedTokens = usedTokens;
this.estimatedCost = estimatedCost;
this.totalChats = totalChats;
}

public static Uni<ChatsStats> findByApikey(ApiKey apikey) {
return Chat.find("select sum(usedTokens) as usedTokens, sum(estimatedCost) as estimatedCost, count(*) as totalChats from Chat where apikey.id = ?1", apikey.id).project(ChatsStats.class).singleResult();
}
}

0 comments on commit 68147bd

Please sign in to comment.