Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hour detail to ai analytics #2529

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.apk.enforcer.analytics;

import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
import com.google.protobuf.Value;
import io.envoyproxy.envoy.data.accesslog.v3.AccessLogCommon;
import io.envoyproxy.envoy.data.accesslog.v3.HTTPAccessLogEntry;
Expand All @@ -43,6 +44,9 @@
import org.wso2.apk.enforcer.constants.AnalyticsConstants;
import org.wso2.apk.enforcer.constants.MetadataConstants;

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -268,7 +272,9 @@ public Map<String, Object> getProperties() {
if (totalTokenCount != null) aiTokenUsage.setTotalTokens(totalTokenCount);
if (promptTokenCount != null) aiTokenUsage.setPromptTokens(promptTokenCount);
if (completionTokenCount != null) aiTokenUsage.setCompletionTokens(completionTokenCount);

if (logEntry != null && logEntry.getCommonProperties() != null && logEntry.getCommonProperties().getStartTime() != null) {
aiTokenUsage.setHour(getUtcHour(logEntry.getCommonProperties().getStartTime()));
}
if (aiTokenUsage.getTotalTokens() != null || aiTokenUsage.getPromptTokens() != null || aiTokenUsage.getCompletionTokens() != null) {
map.put("aiTokenUsage", aiTokenUsage);
}
Expand Down Expand Up @@ -336,6 +342,12 @@ private void addMetadata(Struct.Builder structBuilder, String key, double value)
structBuilder.putFields(key, Value.newBuilder().setNumberValue(value).build());
}

public static int getUtcHour(Timestamp timestamp) {
Instant instant = Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
ZonedDateTime utcDateTime = instant.atZone(ZoneOffset.UTC);
return utcDateTime.getHour();
}

private void setCustomPropertiesMap(HTTPAccessLogEntry logEntry, Map<String, Object> customProperties) {

if (logEntry.getRequest().getRequestHeadersMap() != null) {
Expand Down
Loading