forked from exadel-inc/CompreFace
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRY-410: add possibility to initialize caches by api-keys on app star…
…t up (#6)
- Loading branch information
1 parent
e71d829
commit e15c749
Showing
15 changed files
with
248 additions
and
106 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
43 changes: 43 additions & 0 deletions
43
...c/main/java/com/exadel/frs/core/trainservice/cache/EmbeddingCacheProviderInitializer.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,43 @@ | ||
package com.exadel.frs.core.trainservice.cache; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class EmbeddingCacheProviderInitializer implements ApplicationRunner { | ||
private final EmbeddingCacheProvider embeddingCacheProvider; | ||
@Value("${embeddings.cache.initialization.api-keys}") | ||
private String initializationApiKeys; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) { | ||
var apiKeys = Optional.ofNullable(initializationApiKeys) | ||
.map(keys -> keys.split(",")) | ||
.stream() | ||
.flatMap(Arrays::stream) | ||
.filter(s -> !s.isBlank()) | ||
.filter(EmbeddingCacheProviderInitializer::isUuid) | ||
.collect(Collectors.toSet()); | ||
embeddingCacheProvider.fillInCache(apiKeys); | ||
} | ||
|
||
private static boolean isUuid(String s) { | ||
try { | ||
UUID.fromString(s); | ||
return true; | ||
} catch (IllegalArgumentException e) { | ||
log.warn("Invalid UUID: {}", s, e); | ||
return false; | ||
} | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
java/api/src/main/java/com/exadel/frs/core/trainservice/util/MaskUtils.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,21 @@ | ||
package com.exadel.frs.core.trainservice.util; | ||
|
||
import org.springframework.lang.NonNull; | ||
|
||
public final class MaskUtils { | ||
private MaskUtils() { | ||
// NOOP | ||
} | ||
|
||
public static String maskApiKey(@NonNull String apiKey) { | ||
var maskedApiKey = new StringBuilder(); | ||
for (int i = 0; i < apiKey.length(); i++) { | ||
if (i < 4 || apiKey.charAt(i) == '-') { | ||
maskedApiKey.append(apiKey.charAt(i)); | ||
} else { | ||
maskedApiKey.append('*'); | ||
} | ||
} | ||
return maskedApiKey.toString(); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
java/api/src/test/java/com/exadel/frs/core/trainservice/TestLiquibase.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 com.exadel.frs.core.trainservice; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.sql.DataSource; | ||
import liquibase.Contexts; | ||
import liquibase.LabelExpression; | ||
import liquibase.Liquibase; | ||
import liquibase.database.DatabaseFactory; | ||
import liquibase.database.jvm.JdbcConnection; | ||
import liquibase.integration.spring.SpringResourceAccessor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.io.ResourceLoader; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class TestLiquibase { | ||
@Autowired | ||
ResourceLoader resourceLoader; | ||
|
||
@Autowired | ||
private Environment env; | ||
|
||
@Autowired | ||
DataSource dataSource; | ||
|
||
@PostConstruct | ||
public void initDatabase() { | ||
try { | ||
Liquibase liquibase = new Liquibase( | ||
"db/changelog/db.changelog-master.yaml", | ||
new SpringResourceAccessor(resourceLoader), | ||
DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(dataSource.getConnection())) | ||
); | ||
setLiquibaseChangeLogParams(liquibase); | ||
liquibase.update(new Contexts(), new LabelExpression()); | ||
} catch (Exception e) { | ||
//manage exception | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void setLiquibaseChangeLogParams(final Liquibase liquibase) { | ||
String clientId = env.getProperty("spring.liquibase.parameters.common-client.client-id", "CommonClientId"); | ||
String accessTokenValidity = env.getProperty("spring.liquibase.parameters.common-client.access-token-validity", "2400"); | ||
String refreshTokenValidity = env.getProperty("spring.liquibase.parameters.common-client.refresh-token-validity", "1209600"); | ||
String authorizedGrantTypes = env.getProperty("spring.liquibase.parameters.common-client.authorized-grant-types", "password,refresh_token"); | ||
|
||
liquibase.setChangeLogParameter("common-client.client-id", clientId); | ||
liquibase.setChangeLogParameter("common-client.access-token-validity", accessTokenValidity); | ||
liquibase.setChangeLogParameter("common-client.refresh-token-validity", refreshTokenValidity); | ||
liquibase.setChangeLogParameter("common-client.authorized-grant-types", authorizedGrantTypes); | ||
} | ||
} |
Oops, something went wrong.