-
Notifications
You must be signed in to change notification settings - Fork 0
xAPI client integration #17
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3858dd0
very basic client integration
cliffcaseyyet 2317224
Merge branch 'main' into basic_lrs
cliffcaseyyet 9ca3e7d
basic buffer with timed clear
cliffcaseyyet 4e77933
Merge branch 'main' into basic_lrs
cliffcaseyyet df7b063
Merge branch 'main' into basic_lrs
cliffcaseyyet 0aed1e4
error handling for xapi client, and tests for client. updated pom for…
cliffcaseyyet 0f576b6
(possibly temporary) add lib before verify
cliffcaseyyet 8f34b03
added lib to ci step
cliffcaseyyet eea5444
readme comment clarity
cliffcaseyyet 749c513
forgot max retries
cliffcaseyyet 2de5844
remove step, switch to make
cliffcaseyyet e51abc6
comment addtl TODO
cliffcaseyyet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -22,4 +22,4 @@ jobs: | |
| cache: maven | ||
|
|
||
| - name: Verify | ||
| run: mvn --batch-mode verify | ||
| run: make verify | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,98 @@ | ||
| package com.yetanalytics.hlaxapi; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.springframework.scheduling.annotation.Scheduled; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonMappingException; | ||
| import com.yetanalytics.hlaxapi.config.XapiConfig; | ||
|
|
||
| import com.yetanalytics.xapi.client.StatementClient; | ||
| import com.yetanalytics.xapi.exception.StatementClientException; | ||
| import com.yetanalytics.xapi.model.Statement; | ||
| import com.yetanalytics.xapi.client.LRS; | ||
|
|
||
| import com.yetanalytics.xapi.util.Mapper; | ||
|
|
||
| @Component | ||
| public class XapiClient { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(XapiClient.class); | ||
|
|
||
| private StatementClient client; | ||
|
|
||
| private List<Statement> buffer; | ||
|
|
||
| private Integer batchSize; | ||
|
|
||
| private Integer retryCount = 0; | ||
|
|
||
| private Integer maxRetries; | ||
|
|
||
| public XapiClient(XapiConfig xapiConfig) { | ||
| LRS lrs = new LRS( | ||
| xapiConfig.lrsConfig.host, | ||
| xapiConfig.lrsConfig.key, | ||
| xapiConfig.lrsConfig.secret, | ||
| xapiConfig.lrsConfig.batch | ||
| ); | ||
| client = new StatementClient(lrs); | ||
| buffer = new ArrayList<Statement>(); | ||
| batchSize = xapiConfig.lrsConfig.batch; | ||
| maxRetries = xapiConfig.lrsConfig.maxRetries; | ||
| } | ||
|
|
||
| private StatementClient getClient() { | ||
| return client; | ||
| } | ||
|
|
||
| public void sendStatementFromString(String s) throws JsonMappingException, JsonProcessingException{ | ||
| Statement stmt = Mapper.getMapper().readValue(s, Statement.class); | ||
| addToBuffer(stmt); | ||
| } | ||
|
|
||
| /** Synchronized buffer methods */ | ||
|
|
||
| private synchronized void addToBuffer(Statement stmt){ | ||
| buffer.add(stmt); | ||
| } | ||
|
|
||
| // Check and post buffer to LRS every 10 seconds (or ENV) if contains statements | ||
| @Scheduled(fixedRateString = "${xapi.buffer.clear-rate:10000}") | ||
| private synchronized void clearBuffer() { | ||
| logger.info("Buffer Size: {}", buffer.size()); | ||
| if (buffer.size() > 0){ | ||
| try { | ||
| List<UUID> results = client.postStatements(buffer); | ||
| logger.info("Stored statements: {}", results); | ||
| buffer = new ArrayList<Statement>(); | ||
| logger.info("Cleared Buffer"); | ||
| retryCount = 0; | ||
| } catch (StatementClientException e) { | ||
| logger.error("Error sending statements to LRS:", e); | ||
| if (retryCount < maxRetries) { | ||
| retryCount++; | ||
| logger.info("Retrying to send statements to LRS, attempt {}/{}", retryCount, maxRetries); | ||
| } else { | ||
| // TODO: For durability, we should write the buffer to a file or database or DLQ for retrying later | ||
| // might be a case for a light queueing system like RabbitMQ. Also failures will be reduced if we | ||
| // pre-validate statements before sending to the LRS. Also we may want to reduce the accrual of | ||
| // statements in the buffer if we have a DLQ strategy, that way less innocent statements are lost. | ||
| // For now, we will just clear the buffer after max retries. | ||
| // TODO: Additionally we should consider a cap on the buffer size, and if it exceeds that cap, | ||
| // we should start dropping statements or writing to a DLQ. | ||
| buffer = new ArrayList<Statement>(); | ||
| retryCount = 0; | ||
| logger.info("Cleared Buffer after max retries, statement data lost!"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.