From 2b9be3b5212ebd8294f7f045153a98ee294b219f Mon Sep 17 00:00:00 2001
From: "S.P. Mohanty" <spmohanty91@gmail.com>
Date: Fri, 5 Jun 2020 17:06:55 +0200
Subject: [PATCH] Upload metadata csv file to S3 when applicable

---
 flatland/evaluators/aicrowd_helpers.py | 16 ++++++++++------
 flatland/evaluators/service.py         | 15 +++++++++------
 2 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/flatland/evaluators/aicrowd_helpers.py b/flatland/evaluators/aicrowd_helpers.py
index 0d46dca0..606550d5 100644
--- a/flatland/evaluators/aicrowd_helpers.py
+++ b/flatland/evaluators/aicrowd_helpers.py
@@ -3,6 +3,7 @@ import os
 import random
 import subprocess
 import uuid
+import pathlib
 
 ###############################################################
 # Expected Env Variables
@@ -11,7 +12,7 @@ import uuid
 # AICROWD_IS_GRADING : true
 # CROWDAI_IS_GRADING : true
 # S3_BUCKET : aicrowd-production
-# S3_UPLOAD_PATH_TEMPLATE : misc/flatland-rl-Media/{}.mp4
+# S3_UPLOAD_PATH_TEMPLATE : misc/flatland-rl-Media/{}
 # AWS_ACCESS_KEY_ID
 # AWS_SECRET_ACCESS_KEY
 # http_proxy
@@ -20,7 +21,7 @@ import uuid
 AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", False)
 AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", False)
 S3_BUCKET = os.getenv("S3_BUCKET", "aicrowd-production")
-S3_UPLOAD_PATH_TEMPLATE = os.getenv("S3_UPLOAD_PATH_TEMPLATE", "misc/flatland-rl-Media/{}.mp4")
+S3_UPLOAD_PATH_TEMPLATE = os.getenv("S3_UPLOAD_PATH_TEMPLATE", "misc/flatland-rl-Media/{}")
 
 
 def get_boto_client():
@@ -62,7 +63,7 @@ def upload_random_frame_to_s3(frames_folder):
     if not S3_BUCKET:
         raise Exception("S3_BUCKET not provided...")
 
-    image_target_key = S3_UPLOAD_PATH_TEMPLATE.replace(".mp4", ".png").format(str(uuid.uuid4()))
+    image_target_key = (S3_UPLOAD_PATH_TEMPLATE + ".png").format(str(uuid.uuid4()))
     s3.put_object(
         ACL="public-read",
         Bucket=S3_BUCKET,
@@ -79,14 +80,17 @@ def upload_to_s3(localpath):
     if not S3_BUCKET:
         raise Exception("S3_BUCKET not provided...")
 
-    image_target_key = S3_UPLOAD_PATH_TEMPLATE.format(str(uuid.uuid4()))
+    file_suffix = str(pathlib.Path(localpath).suffix)
+    file_target_key = (S3_UPLOAD_PATH_TEMPLATE + file_suffix).format(
+        str(uuid.uuid4())
+    )
     s3.put_object(
         ACL="public-read",
         Bucket=S3_BUCKET,
-        Key=image_target_key,
+        Key=file_target_key,
         Body=open(localpath, 'rb')
     )
-    return image_target_key
+    return file_target_key
 
 
 def make_subprocess_call(command, shell=False):
diff --git a/flatland/evaluators/service.py b/flatland/evaluators/service.py
index 782dc185..e0a352f6 100644
--- a/flatland/evaluators/service.py
+++ b/flatland/evaluators/service.py
@@ -746,12 +746,15 @@ class FlatlandRemoteEvaluationService:
         # Write Results to a file (if applicable)
         #####################################################################
         if self.result_output_path:
-            if self.evaluation_metadata_df is not None:
-                self.evaluation_metadata_df.to_csv(self.result_output_path)
-                print("Wrote output results to : {}".format(self.result_output_path))
-            else:
-                print("[WARING] Unable to write final results to the specified path"
-                      " as metadata.csv is not provided in the tests_folder")
+            self.evaluation_metadata_df.to_csv(self.result_output_path)
+            print("Wrote output results to : {}".format(self.result_output_path))
+            
+            # Upload the metadata file to S3 
+            if aicrowd_helpers.is_grading() and aicrowd_helpers.is_aws_configured():
+                metadata_s3_key = aicrowd_helpers.upload_to_s3(
+                    self.result_output_path
+                )
+                self.evaluation_state["meta"]["private_metadata_s3_key"] = metadata_s3_key
 
         _command_response = {}
         _command_response['type'] = messages.FLATLAND_RL.ENV_SUBMIT_RESPONSE
-- 
GitLab