[WIP] S3 Upload of JSON log

This commit is contained in:
Shaun Hannah 2018-02-27 05:17:20 -05:00
parent 7f94c724af
commit 7c7f88ab3c
4 changed files with 70 additions and 2 deletions

View file

@ -15,6 +15,7 @@
<name>Mage Client</name> <name>Mage Client</name>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.mage</groupId> <groupId>org.mage</groupId>
<artifactId>mage</artifactId> <artifactId>mage</artifactId>
@ -68,6 +69,11 @@
<artifactId>jetlang</artifactId> <artifactId>jetlang</artifactId>
<version>0.2.9</version> <version>0.2.9</version>
</dependency> </dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.286</version>
</dependency>
<dependency> <dependency>
<groupId>com.jgoodies</groupId> <groupId>com.jgoodies</groupId>
<artifactId>forms</artifactId> <artifactId>forms</artifactId>

View file

@ -35,6 +35,7 @@ import javax.swing.*;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import mage.cards.decks.Deck; import mage.cards.decks.Deck;
import mage.client.remote.S3Uploader;
import mage.client.MageFrame; import mage.client.MageFrame;
import mage.client.SessionHandler; import mage.client.SessionHandler;
import mage.client.chat.ChatPanelBasic; import mage.client.chat.ChatPanelBasic;
@ -194,9 +195,16 @@ public class CallbackClientImpl implements CallbackClient {
break; break;
} }
case GAME_OVER: { case GAME_OVER: {
GamePanel panel = MageFrame.getGame(callback.getObjectId()); GamePanel panel = MageFrame.getGame(callback.getObjectId());
if (panel != null) { if (panel != null) {
appendJsonEvent("GAME_OVER", callback.getObjectId(), callback.getData()); appendJsonEvent("GAME_OVER", callback.getObjectId(), callback.getData());
ActionData actionData = appendJsonEvent("GAME_OVER", callback.getObjectId(), callback.getData());
String logFileName = "game-" + actionData.gameId + ".json";
S3Uploader.upload(logFileName, actionData.gameId.toString());
panel.endMessage((String) callback.getData(), callback.getMessageId()); panel.endMessage((String) callback.getData(), callback.getMessageId());
} }
break; break;
@ -304,8 +312,8 @@ public class CallbackClientImpl implements CallbackClient {
break; break;
} }
case END_GAME_INFO: case END_GAME_INFO:
appendJsonEvent("GAME_OVER", callback.getObjectId(), callback.getData());
MageFrame.getInstance().showGameEndDialog((GameEndView) callback.getData()); MageFrame.getInstance().showGameEndDialog((GameEndView) callback.getData());
break; break;
case SHOW_USERMESSAGE: case SHOW_USERMESSAGE:
List<String> messageData = (List<String>) callback.getData(); List<String> messageData = (List<String>) callback.getData();
@ -402,11 +410,12 @@ public class CallbackClientImpl implements CallbackClient {
} }
}); });
} }
private void appendJsonEvent(String name, UUID gameId, Object value) { private ActionData appendJsonEvent(String name, UUID gameId, Object value) {
Session session = SessionHandler.getSession(); Session session = SessionHandler.getSession();
ActionData actionData = new ActionData(name, gameId); ActionData actionData = new ActionData(name, gameId);
actionData.value = value; actionData.value = value;
session.appendJsonLog(actionData); session.appendJsonLog(actionData);
return actionData;
} }
private void createChatStartMessage(ChatPanelBasic chatPanel) { private void createChatStartMessage(ChatPanelBasic chatPanel) {
chatPanel.setStartMessageDone(true); chatPanel.setStartMessageDone(true);

View file

@ -0,0 +1,47 @@
package mage.client.remote;
import java.io.File;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import org.apache.log4j.Logger;
import javax.xml.crypto.Data;
public class S3Uploader {
private static final Logger logger = Logger.getLogger(S3Uploader.class);
public static Boolean upload(String filePath, String keyName) throws Exception {
String existingBucketName = System.getenv("S3_BUCKET") != null ? System.getenv("S3_BUCKET")
: "xmage-game-logs-dev";
String accessKeyId = System.getenv("AWS_ACCESS_ID");
String secretKeyId = System.getenv("AWS_SECRET_KEY");
if(accessKeyId == "" || secretKeyId == "" || existingBucketName == "") {
logger.info("Aborting json log sync.");
return false;
}
String path = new File("./" + filePath).getCanonicalPath();
logger.info("Syncing " + path + " to bucket: " + existingBucketName + " with AWS Access Id: " + accessKeyId);
BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKeyId, secretKeyId);
TransferManager tm = new TransferManager(awsCreds);
Upload upload = tm.upload(existingBucketName, "/game/" + keyName + ".json", new File(path));
try {
upload.waitForUploadResult();
logger.info("Sync Complete For " + path + " to bucket: " + existingBucketName + " with AWS Access Id: " + accessKeyId);
new File(path);
return true;
} catch (AmazonClientException amazonClientException) {
System.out.println("Unable to upload file, upload was aborted.");
amazonClientException.printStackTrace();
return false;
}
}
}

View file

@ -76,6 +76,12 @@
<version>${project.version}</version> <version>${project.version}</version>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.16.1</version>
</dependency>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
<artifactId>mage-game-commanderfreeforall</artifactId> <artifactId>mage-game-commanderfreeforall</artifactId>