-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement endpoint for getting API key spent limit information #…
…218 (#219) Co-authored-by: Aliaksandr Stsiapanay <[email protected]>
- Loading branch information
1 parent
3e1a5b2
commit b1efac4
Showing
8 changed files
with
230 additions
and
24 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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/epam/aidial/core/controller/LimitController.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,35 @@ | ||
package com.epam.aidial.core.controller; | ||
|
||
import com.epam.aidial.core.Proxy; | ||
import com.epam.aidial.core.ProxyContext; | ||
import com.epam.aidial.core.util.HttpStatus; | ||
import io.vertx.core.Future; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class LimitController { | ||
|
||
private final Proxy proxy; | ||
|
||
private final ProxyContext context; | ||
|
||
public LimitController(Proxy proxy, ProxyContext context) { | ||
this.proxy = proxy; | ||
this.context = context; | ||
} | ||
|
||
public Future<?> getLimits(String deploymentName) { | ||
proxy.getRateLimiter().getLimitStats(deploymentName, context).onSuccess(limitStats -> { | ||
if (limitStats == null) { | ||
context.respond(HttpStatus.NOT_FOUND); | ||
} else { | ||
context.respond(HttpStatus.OK, limitStats); | ||
} | ||
}).onFailure(error -> { | ||
log.error("Failed to get limit stats", error); | ||
context.respond(HttpStatus.INTERNAL_SERVER_ERROR, | ||
"Failed to get limit stats for deployment=%s".formatted(deploymentName)); | ||
}); | ||
return Future.succeededFuture(); | ||
} | ||
} |
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,9 @@ | ||
package com.epam.aidial.core.data; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class LimitStats { | ||
private TokenLimitStats minuteTokenStats; | ||
private TokenLimitStats dayTokenStats; | ||
} |
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,9 @@ | ||
package com.epam.aidial.core.data; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class TokenLimitStats { | ||
private long total; | ||
private long used; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.epam.aidial.core; | ||
|
||
import io.vertx.core.http.HttpMethod; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LimitApiTest extends ResourceBaseTest { | ||
|
||
@Test | ||
public void testGetLimitStats_Success() { | ||
Response response = send(HttpMethod.GET, "/v1/deployments/test-model-v1/limits", null, null); | ||
verifyJson(response, 200, """ | ||
{ | ||
"minuteTokenStats": { | ||
"total": %d, | ||
"used": %d | ||
}, | ||
"dayTokenStats": { | ||
"total": %d, | ||
"used": %d | ||
} | ||
} | ||
""".formatted(Long.MAX_VALUE, 0, Long.MAX_VALUE, 0)); | ||
} | ||
|
||
@Test | ||
public void testGetLimitStats_UnknownModel() { | ||
Response response = send(HttpMethod.GET, "/v1/deployments/unknown-model/limits", null, null); | ||
verify(response, 404); | ||
} | ||
} |
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