Skip to content
Snippets Groups Projects
Commit 757aafb1 authored by Someshwar, Rudra (PG/T - Comp Sci & Elec Eng)'s avatar Someshwar, Rudra (PG/T - Comp Sci & Elec Eng)
Browse files

Update file ec2_function.py

parent 956c9419
No related branches found
No related tags found
No related merge requests found
from flask import Flask, request, jsonify
import boto3
import json
import random
import math
app = Flask(__name__)
s3 = boto3.client('s3')
@app.route('/')
def hello_world():
return 'Hello from Flask!'
@app.route('/work', methods=['POST'])
def run_simulation():
# Parse the JSON input data
input_json = request.json
def main(input_key, output_key):
# Read the input JSON data from the S3 bucket
input_bucket = 'ec2-function-input'
output_bucket = 'instance-results'
# input_key = event['key']
obj = s3.get_object(Bucket=input_bucket, Key=input_key)
input_json = json.loads(obj['Body'].read().decode('utf-8'))
# Extract the required parameters from the input data
minhistory = int(input_json['history'])
......@@ -23,11 +22,6 @@ def run_simulation():
buy_signals = input_json['buy_signals']
sell_signals = input_json['sell_signals']
# print(minhistory, shots, signaltype, P)
# print(buy_signals)
# print(sell_signals)
# create empty lists to store the results
risk95_values = []
risk99_values = []
......@@ -71,15 +65,37 @@ def run_simulation():
risk95_values.append(var95)
risk99_values.append(var99)
# Create a dictionary to store the simulation results
simulation_results = {
'risk95_values': risk95_values,
'risk99_values': risk99_values
}
return (risk95_values, risk99_values)
# # Write the simulation results to a JSON file
# with open('simulation_results.json', 'w') as f:
# json.dump(simulation_results, f)
# # Upload the output to S3 bucket
# s3.upload_file('simulation_results.json', bucket_name, output_key)
output_data_json = json.dumps(simulation_results)
s3.put_object(Bucket=output_bucket,Key=output_key, Body=output_data_json)
# # Return the results as a JSON response
# response_json = {
# 'result': result
# # Write the output to a new file in the S3 bucket
# output_bucket = event['output_bucket']
# output_key = key
# output_data = {
# 'risk95_values': risk95_values,
# 'risk99_values': risk99_values
# }
# return jsonify(response_json)
# s3.put_object(Bucket=output_bucket, Key=output_key, Body=json.dumps(output_data))
# # Return a success response
# response = {
# 'statusCode': 200,
# 'body': json.dumps('Simulation completed successfully!')
# }
# return response
return (risk95_values, risk99_values)
# if __name__ == '__main__':
# app.run()
\ No newline at end of file
if __name__ == "__main__":
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment