-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
220 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
interweb-server/src/main/java/de/l3s/interweb/server/features/api/ApiRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package de.l3s.interweb.server.features.api; | ||
|
||
import java.time.Instant; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
||
import de.l3s.interweb.server.features.user.User; | ||
|
||
@Entity | ||
@Cacheable | ||
@Table(name = "api_request") | ||
public class ApiRequest extends PanacheEntityBase { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
public Long id; | ||
|
||
@JsonIgnore | ||
@ManyToOne(optional = false, fetch = FetchType.EAGER) | ||
public User user; | ||
|
||
@JsonIgnore | ||
@ManyToOne(optional = false, fetch = FetchType.LAZY) | ||
public ApiKey apikey; | ||
|
||
@NotNull | ||
public String action; | ||
|
||
@CreationTimestamp | ||
public Instant created; | ||
|
||
public static ApiRequest of(String action, ApiKey apikey) { | ||
ApiRequest request = new ApiRequest(); | ||
request.user = apikey.user; | ||
request.apikey = apikey; | ||
request.action = action; | ||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
interweb-server/src/main/java/de/l3s/interweb/server/features/api/ApiRequestSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package de.l3s.interweb.server.features.api; | ||
|
||
import java.time.Instant; | ||
|
||
import de.l3s.interweb.core.search.ContentType; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
|
||
import de.l3s.interweb.core.search.SearchQuery; | ||
|
||
@Entity | ||
@Cacheable | ||
@Table(name = "api_request_search") | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class ApiRequestSearch extends ApiRequest { | ||
|
||
@Transient | ||
public String action = "search"; | ||
|
||
@NotNull | ||
public String engine; | ||
|
||
@NotNull | ||
public String contentType; | ||
|
||
@NotNull | ||
public String query; | ||
|
||
@NotNull | ||
@Column(name = "est_cost") | ||
public Double estimatedCost = 0d; | ||
|
||
@CreationTimestamp | ||
public Instant created; | ||
|
||
public static ApiRequestSearch of(String engine, ContentType contentType, String query, Double estimatedCost, ApiKey apikey) { | ||
ApiRequestSearch request = new ApiRequestSearch(); | ||
request.user = apikey.user; | ||
request.apikey = apikey; | ||
request.engine = engine; | ||
request.contentType = contentType.name(); | ||
request.query = query; | ||
request.estimatedCost = estimatedCost; | ||
return request; | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
interweb-server/src/main/java/de/l3s/interweb/server/features/api/RequestsService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
interweb-server/src/main/java/de/l3s/interweb/server/features/api/UsageSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.l3s.interweb.server.features.api; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import de.l3s.interweb.server.features.user.User; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@RegisterForReflection | ||
public class UsageSearch { | ||
@JsonProperty("estimated_cost") | ||
public Double estimatedCost; | ||
@JsonProperty("total_requests") | ||
public Long totalRequests; | ||
|
||
public UsageSearch(Long inputTokens, Long outputTokens, Double estimatedCost, Long totalRequests) { | ||
this.estimatedCost = estimatedCost; | ||
this.totalRequests = totalRequests; | ||
} | ||
|
||
public static Uni<UsageSearch> findByApikey(ApiKey apikey) { | ||
return ApiRequestChat.find(""" | ||
select sum(estimatedCost) as estimatedCost, count(*) as totalRequests | ||
from ApiRequestSearch | ||
where apikey.id = ?1 | ||
""", apikey.id) | ||
.project(UsageSearch.class) | ||
.singleResult(); | ||
} | ||
|
||
public static Uni<UsageSearch> findByUser(User user) { | ||
return ApiRequestChat.find(""" | ||
select sum(estimatedCost) as estimatedCost, count(*) as totalRequests | ||
from ApiRequestSearch | ||
where user.id = ?1 | ||
""", user.id) | ||
.project(UsageSearch.class) | ||
.singleResult(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
interweb-server/src/main/java/de/l3s/interweb/server/features/api/UsageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package de.l3s.interweb.server.features.api; | ||
|
||
import de.l3s.interweb.server.features.user.User; | ||
|
||
import io.quarkus.cache.Cache; | ||
import io.quarkus.cache.CacheName; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import io.quarkus.hibernate.reactive.panache.common.WithSession; | ||
import io.quarkus.vertx.ConsumeEvent; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
@ApplicationScoped | ||
public class UsageService { | ||
|
||
@Inject | ||
@CacheName("my-cache") | ||
Cache cache; | ||
|
||
public Uni<String> getNonBlockingExpensiveValue(Object key) { | ||
return cache.get(key, k -> { | ||
/* | ||
* Put an expensive call here. | ||
* It will be executed only if the key is not already associated with a value in the cache. | ||
*/ | ||
}); | ||
} | ||
|
||
public boolean isExceeded(User user) { | ||
return false; | ||
} | ||
|
||
@WithSession | ||
@ConsumeEvent("api-request") | ||
public Uni<Void> consumeRequest(ApiRequest request) { | ||
return request.persistAndFlush().replaceWithVoid(); | ||
} | ||
|
||
@WithSession | ||
@ConsumeEvent("api-request-chat") | ||
public Uni<Void> consumeChatRequest(ApiRequestChat request) { | ||
return request.persistAndFlush().replaceWithVoid(); | ||
} | ||
|
||
@WithSession | ||
@ConsumeEvent("api-request-search") | ||
public Uni<Void> consumeSearchRequest(ApiRequestSearch request) { | ||
return request.persistAndFlush().replaceWithVoid(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters