-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
126 lines (94 loc) · 3.37 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import streamsync as ss
import os
import s3fs
from metadata import prism_metadata
import json
import pandas as pd
import numpy as np
import boto3
# AWS/API setup
API_URL = 'https://api.clue.io/api/'
API_KEY = os.environ['API_KEY']
BUILDS_URL = API_URL + 'data_build_types/prism-builds'
# get list of builds
builds = prism_metadata.get_data_from_db(
endpoint_url=BUILDS_URL,
user_key=API_KEY,
fields=['name']
)
fs = s3fs.S3FileSystem(anon=False)
builds_list = []
for i in builds:
builds_list.append(list(i.values())[0])
builds_dict = {build: build for build in builds_list}
json_builds_dict = json.dumps(builds_dict)
# print(f"Available builds are {json_builds_dict}")
# This is a placeholder to get you started or refresh your memory.
# Delete it or adapt it as necessary.
# Documentation is available at https://streamsync.cloud
# Its name starts with _, so this function won't be exposed
def _update_message(state):
is_even = state["counter"] % 2 == 0
message = ("+Even" if is_even else "-Odd")
state["message"] = message
def decrement(state):
state["counter"] -= 1
_update_message(state)
def increment(state):
state["counter"] += 1
# Shows in the log when the event handler is run
print(f"The counter has been incremented.")
_update_message(state)
def set_build(state, payload):
# Set the state variable "build" to the selected option
state["build"] = payload
print(f"Selected build is {state['build']}")
# Create dictionary of available build files
s3 = boto3.client('s3')
bucket = 'cup.clue.io'
prefix = state["build"]
print(f"Selected build is {prefix}")
response = s3.list_objects_v2(Bucket=bucket, Prefix=prefix)
if 'Contents' in response:
objects = response['Contents']
existing_plots = [obj['Key'].split("/")[1] for obj in objects]
print(f"Found {len(existing_plots)} objects with prefix '{prefix}' in bucket '{bucket}'")
state["files"] = existing_plots
_update_message(state)
def get_plots(state):
s3 = boto3.client('s3')
bucket = 'cup.clue.io'
prefix = state["build"]
files = state["files"]
# Initialize an empty dictionary to store the plotly plots
plots = {}
# Iterate through the list of filenames
for filename in files:
# Subset to only json files
if filename.endswith(".json"):
# Create the file path by combining the prefix and filename
file_key = f"{prefix}/{filename}"
# Download the file from the S3 bucket and read its contents
file_obj = s3.get_object(Bucket=bucket, Key=file_key)
file_content = file_obj['Body'].read().decode('utf-8')
plot_name = filename.split(".")[0]
# Parse the contents as JSON and add to the plots dictionary with the filename as the key
plot_json = json.loads(file_content)
state[plot_name] = plot_json
# Set the state variable "plots" to the dictionary of plots
_update_message(state)
# Initialise the state
# "_my_private_element" won't be serialised or sent to the frontend,
# because it starts with an underscore
initial_state = ss.init_state({
"my_app": {
"title": "My App"
},
"_my_private_element": 1337,
"message": None,
"counter": 26,
"build": None,
"builds_dict": builds_dict,
"files": None,
})
_update_message(initial_state)