Skip to content

Commit

Permalink
Add hour to ai analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharsanan1 committed Oct 3, 2024
1 parent fd4654a commit 1ddb49c
Showing 1 changed file with 13 additions and 1 deletion.
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

0 comments on commit 1ddb49c

Please sign in to comment.