Skip to content
Merged
28 changes: 28 additions & 0 deletions ai-data-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.6.3</version>
</dependency>

<!-- Testing dependencies -->
<dependency>
<groupId>org.springframework.batch</groupId>
Expand Down Expand Up @@ -233,6 +239,28 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.6.3</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amapstruct.defaultComponentModel=spring</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
Expand All @@ -43,14 +42,6 @@ public static class RateLimiting {
private int maxConcurrentCalls;
}

@Data
public static class RetryPolicy {
private int maxAttempts;
private int minBackoffDuration;

private TimeUnit minBackoffTimeUnit;
}

@Data
public static class EndpointConfig {
private String path;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 <Sapient Corporation>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the
* License.
*/

package com.publicissapient.kpidashboard.client.customapi.config;

import lombok.Data;

import java.util.concurrent.TimeUnit;

@Data
public class RetryPolicy {
private int maxAttempts;
private int minBackoffDuration;
private TimeUnit minBackoffTimeUnit;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2024 <Sapient Corporation>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the
* License.
*/

package com.publicissapient.kpidashboard.client.shareddataservice;

import com.publicissapient.kpidashboard.client.shareddataservice.config.SharedDataServiceConfig;
import com.publicissapient.kpidashboard.job.aiusagestatisticscollector.dto.PagedAIUsagePerOrgLevel;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.util.retry.Retry;

import java.time.Duration;

@Component
public class SharedDataServiceClient {
private final WebClient webClient;
private final SharedDataServiceConfig sharedDataServiceConfig;

private static final String LEVEL_NAME_PARAM = "levelName";
private static final String API_KEY_HEADER = "x-api-key";

public SharedDataServiceClient(SharedDataServiceConfig sharedDataServiceConfig) {
this.sharedDataServiceConfig = sharedDataServiceConfig;

String baseUrl = sharedDataServiceConfig.getBaseUrl();
String apiKeyValue = sharedDataServiceConfig.getApiKey();

this.webClient = WebClient.builder()
.baseUrl(baseUrl)
.defaultHeader(API_KEY_HEADER, apiKeyValue)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
}

public PagedAIUsagePerOrgLevel getAIUsageStatsAsync(String levelName) {
int maxAttempts = sharedDataServiceConfig.getRetryPolicy().getMaxAttempts();
int minBackoffDuration = sharedDataServiceConfig.getRetryPolicy().getMinBackoffDuration();
String path = sharedDataServiceConfig.getAiUsageStatisticsEndpoint().getPath();

return webClient.get()
.uri(uriBuilder -> uriBuilder
.path(path)
.queryParam(LEVEL_NAME_PARAM, levelName)
.build())
.retrieve()
.bodyToMono(PagedAIUsagePerOrgLevel.class)
.retryWhen(Retry.backoff(maxAttempts, Duration.ofSeconds(minBackoffDuration)).jitter(0.5))
.block();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 <Sapient Corporation>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the
* License.
*/

package com.publicissapient.kpidashboard.client.shareddataservice.config;

import com.publicissapient.kpidashboard.client.customapi.config.RetryPolicy;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import lombok.Data;

@Data
@Configuration
@ConfigurationProperties(prefix = "shared-data-service-api-config")
public class SharedDataServiceConfig {
private String baseUrl;
private String apiKey;
private final AIUsageStatisticsEndpoint aiUsageStatisticsEndpoint = new AIUsageStatisticsEndpoint();
private final RetryPolicy retryPolicy = new RetryPolicy();

@Data
public static class AIUsageStatisticsEndpoint {
private String path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 <Sapient Corporation>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the
* License.
*/

package com.publicissapient.kpidashboard.job.aiusagestatisticscollector.config;

import com.publicissapient.kpidashboard.job.config.base.BatchConfig;
import com.publicissapient.kpidashboard.job.config.base.SchedulingConfig;
import com.publicissapient.kpidashboard.job.config.validator.ConfigValidator;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.util.StringUtils;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@Data
@Configuration
@ConfigurationProperties(prefix = "jobs.ai-usage-statistics-collector")
public class AIUsageStatisticsCollectorJobConfig implements ConfigValidator {
private String name;
private SchedulingConfig scheduling;

private BatchConfig batching;

private Set<String> configValidationErrors = new HashSet<>();

@PostConstruct
private void retrieveJobConfigValidationErrors() {
this.validateConfiguration();

this.batching.validateConfiguration();
this.scheduling.validateConfiguration();

this.configValidationErrors = new HashSet<>();
this.configValidationErrors.addAll(this.batching.getConfigValidationErrors());
this.configValidationErrors.addAll(this.scheduling.getConfigValidationErrors());
}

@Override
public void validateConfiguration() {
if(StringUtils.isEmpty(this.name)) {
configValidationErrors.add("The job 'name' parameter is required");
}
}

@Override
public Set<String> getConfigValidationErrors() {
return Collections.unmodifiableSet(this.configValidationErrors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 <Sapient Corporation>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the
* License.
*/

package com.publicissapient.kpidashboard.job.aiusagestatisticscollector.dto;

import java.time.Instant;
import java.util.List;

public record AIUsagePerUser(String email, List<MetricsPerUser> metrics) {
public record MetricsPerUser(String key, Long value, Long valueLast30Days, Long valueYTD, Long valueTotal, Instant timestamp) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 <Sapient Corporation>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the
* License.
*/

package com.publicissapient.kpidashboard.job.aiusagestatisticscollector.dto;

import com.publicissapient.kpidashboard.job.aiusagestatisticscollector.enums.AIUsageAggregationType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AIUsageSummary {
private Long totalLocGenerated;
private Long totalPrompts;
private Long userCount;
private Long otherMetrics;
private AIUsageAggregationType aggregationType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 <Sapient Corporation>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the
* License.
*/

package com.publicissapient.kpidashboard.job.aiusagestatisticscollector.dto;

import java.time.Instant;

public record MetricsPerUser(String key, Long value, Long valueLast30Days, Long valueYTD, Long valueTotal, Instant timestamp) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2024 <Sapient Corporation>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the
* License.
*/

package com.publicissapient.kpidashboard.job.aiusagestatisticscollector.dto;

import java.time.Instant;
import java.util.List;

public record PagedAIUsagePerOrgLevel(String levelType,
String levelName,
Instant statsDate,
AIUsageSummary usageSummary,
List<AIUsagePerUser> users,
int currentPage,
int totalPages,
long totalElements,
int pageSize) {
}
Loading
Loading