-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
189 lines (166 loc) · 5.16 KB
/
app.R
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
library(shiny)
library(shinyjs)
source("global.R")
ui <- fluidPage(
useShinyjs(),
# Headers
tags$head(
tags$title("Flipside Crypto's Dashboard Expandooor"),
tags$link(rel = "stylesheet", type = "text/css", href = "styles.css"),
tags$link(rel = "icon", href = "fliptrans.png"),
tags$script(src = "rudderstack.js"),
tags$script("rudderanalytics.page('dashboard_expandoor')")
),
# Top row with logo and title
fluidRow(class = "titlerow",
column(width = 3,
div(id = "applogo",
a(href = "https://flipsidecrypto.xyz",
img(src = "Flipside_black_logo_wordmark.svg", height = "24px"),
onclick = paste0("rudderanalytics.track('", "dashboard_expandoor", "_flipside')"),
target = "_blank"))
),
column(width = 6,
div(id = "appname", "Dashboard Expandoooor")
),
column(width = 3,
div(id = "sidelinks"))
),
hr(),
# Main app body
div(class = "appbody",
br(),
# Input area with side-by-side elements
fluidRow(
column(width = 6,
div(style = "margin: 0 20%;",
textInput("slug", "Please provide your dashboard slug",
value = "near-staking-FxmsnX")
)
),
column(width = 6,
# Queries box
div(style = "margin: 0 20%;",
div(class = "card",
uiOutput("queries_used")
)
)
)
),
# Centered Start button
fluidRow(
column(width = 12,
div(style = "display: flex; justify-content: center; margin: 20px 0;",
actionButton("start", "Start", class = "btn-primary")
)
)
),
# Content area with two columns
fluidRow(
# Left column - Key Insights
column(width = 6,
div(class = "card",
uiOutput("key_insights")
)
),
# Right column - Recommendations
column(width = 6,
div(class = "card",
uiOutput("recommendations")
)
)
)
)
)
server <- function(input, output, session) {
# Reactive values to store results
rv <- reactiveValues(
queries = NULL,
key_insights = NULL,
recommendations = NULL
)
# Step 1: Get queries
queries <- eventReactive(input$start, {
withProgress(message = 'Step 1/3', detail = "Fetching queries...", value = 0.33, {
submitSnowflake(dashslug_to_queries(input$slug), snowflake_credentials)
})
})
# Step 2: Get insights once queries are available
insights <- eventReactive(queries(), {
req(queries())
withProgress(message = 'Step 2/3', detail = "Generating insights...", value = 0.66, {
summary_content <- paste0(
get_query_summary(input$slug),
paste0(queries()$QUERY_STATEMENT, collapse = "\n### NEW QUERY BELOW\n")
)
insights_response <- ask_flipai(
api_key = flipai_secret,
slug = 'query-statement-summarizer',
content = summary_content
)
insights_response$text
})
})
# Step 3: Get recommendations when Start is clicked
recommendations <- eventReactive(input$start, {
# Wait for insights but don't block UI updates
isolate({
if(is.null(insights())) return(NULL)
withProgress(message = 'Step 3/3', detail = "Generating recommendations...", value = 1, {
recommendations_response <- ask_flipai(
api_key = flipai_secret,
slug = 'data-science-pyramid',
content = insights()
)
recommendations_response$text
})
})
})
# Update reactive values as results become available
observe({
rv$queries <- queries()
})
observe({
rv$key_insights <- insights()
})
observe({
rv$recommendations <- recommendations()
})
# Render UI Components with proper styling
output$queries_used <- renderUI({
div(class = "card-body",
div(class = "card-header", "# Queries Used"),
div(class = "pre-wrap",
verbatimTextOutput("n_queries", placeholder = TRUE))
)
})
output$key_insights <- renderUI({
div(class = "card-body",
div(class = "card-header", "Key Insights"),
div(class = "pre-wrap",
verbatimTextOutput("insights_text", placeholder = TRUE))
)
})
output$recommendations <- renderUI({
div(class = "card-body",
div(class = "card-header", "Recommendations"),
div(class = "pre-wrap",
verbatimTextOutput("recommendations_text", placeholder = TRUE))
)
})
# Render actual content within the styled divs
output$n_queries <- renderPrint({
req(rv$queries)
cat("Total Queries:", nrow(rv$queries))
})
output$insights_text <- renderPrint({
req(rv$key_insights)
cat(rv$key_insights)
})
output$recommendations_text <- renderPrint({
req(rv$recommendations)
cat(rv$recommendations)
})
}
# Run the application
shinyApp(ui = ui, server = server)