-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
343 lines (316 loc) · 8.61 KB
/
main.go
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/chzyer/readline"
"github.com/sashabaranov/go-openai"
"google.golang.org/grpc"
"github.com/geomodulus/citygraph"
"github.com/geomodulus/torontobot/bot"
uq "github.com/geomodulus/torontobot/db"
"github.com/geomodulus/torontobot/discord"
"github.com/geomodulus/torontobot/viz"
)
func main() {
// For dev use: "127.0.0.1:27615"
citygraphAddr := flag.String("citygraph-addr", "", "address string for citygraph indradb GRPC server")
dbFile := flag.String("db-file", "./db/toronto.db", "Database file for tabular city data")
discordBotToken := flag.String("discord-bot-token", "", "Token for accessing Discord API")
openaiToken := flag.String("openai-token", "", "Token for accessing OpenAI API")
headless := flag.Bool("headless", false, "Run in headless mode (no stdin, only Discord bot)")
hostname := flag.String("host", "https://torontoverse.com", "host and scheme for torontoverse server")
flag.Parse()
ctx := context.Background()
var store *citygraph.Store
if *citygraphAddr != "" {
graphConn, err := grpc.Dial(*citygraphAddr, grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer graphConn.Close()
store = &citygraph.Store{GraphClient: citygraph.NewClient(graphConn)}
}
ai := openai.NewClient(*openaiToken)
// Connect to the SQLite database
db, err := sql.Open("sqlite3", *dbFile)
if err != nil {
log.Fatal(err)
}
defer db.Close()
tb, err := bot.New(ctx, db, ai, store, *hostname)
if err != nil {
log.Fatalf("Error creating bot: %s", err)
}
if *discordBotToken != "" {
discordBotServer, err := discord.OpenBotServer(db, *discordBotToken, tb)
if err != nil {
log.Fatalf("Error opening Discord bot server: %s", err)
}
defer discordBotServer.Close()
fmt.Println("TorontoBot is now live on Discord. Press CTRL-C to exit.")
}
if *headless {
// Run in headless mode
// Listen for termination signal
term := make(chan os.Signal, 1)
signal.Notify(term, syscall.SIGINT, syscall.SIGTERM)
// Wait for termination
<-term
} else {
rl, err := readline.New(">> ")
if err != nil {
log.Fatal(err)
}
// loop to read commands and print output
for {
question, err := rl.Readline()
if err != nil {
break
}
if strings.TrimSpace(question) == "" {
continue
}
table, err := tb.SelectTable(ctx, question)
if err != nil {
fmt.Println("Error selecting table:", err)
continue
}
fmt.Printf("Selected table: %q\n", table.Name)
sqlAnalysis, err := tb.SQLAnalysis(ctx, table, question)
if err != nil {
fmt.Println("Error analyzing SQL query:", err)
continue
}
if sqlAnalysis.MissingData != "" {
fmt.Printf("%s\n", sqlAnalysis.MissingData)
continue
}
fmt.Printf(
"%s\n\n%s\n\nSQL: %q\n",
sqlAnalysis.Schema,
sqlAnalysis.Applicability,
sqlAnalysis.SQL)
resultsTable, err := tb.LoadResults(sqlAnalysis.SQL, sqlAnalysis.IsCurrency)
if err != nil {
if err == sql.ErrNoRows {
fmt.Println("No results found.")
} else {
fmt.Println("Error executing SQL query:", err)
}
continue
}
// Store query for subsequent charting and export,
_, err = uq.StoreUserQuery(
db,
&uq.UserQuery{
"",
"",
"",
question,
sqlAnalysis,
resultsTable,
time.Time{},
})
if err != nil {
log.Println("Error storing query:", err)
return
}
fmt.Printf("\nQuery result:\n```%s```\n", resultsTable)
if store != nil {
chartSelected, err := tb.SelectChart(ctx, question, resultsTable)
if err != nil {
fmt.Println("Error selecting chart:", err)
continue
}
switch strings.ToLower(chartSelected.Chart) {
case "bar":
js, err := viz.GenerateBarChartJS(
"#torontobot-chart",
chartSelected.Title,
chartSelected.Data,
chartSelected.ValueIsCurrency,
viz.WithBreakpointWidth())
if err != nil {
fmt.Println("Error generating JS:", err)
continue
}
id := citygraph.NewID().String()
chartHTML, err := viz.GenerateBarChartHTML(
chartSelected.Title,
chartSelected.Data,
chartSelected.ValueIsCurrency,
true, // yes to dark mode
viz.WithFixedWidth(800),
viz.WithFixedHeight(750),
)
if err != nil {
continue
}
featureImageURL, err := viz.GenerateAndUploadFeatureImage(
ctx,
id,
chartSelected.Title,
chartHTML,
chartSelected.Data,
chartSelected.ValueIsCurrency,
)
if err != nil {
fmt.Println("Error generating feature image:", err)
continue
}
modPath, err := tb.SaveToGraph(
ctx,
id,
question,
viz.RenderBody(question, sqlAnalysis.Schema, sqlAnalysis.Applicability, sqlAnalysis.SQL),
js,
featureImageURL,
"Local User")
if err != nil {
fmt.Println("Error saving chart to graph:", err)
continue
}
fmt.Printf("Published chart at %s\n", tb.Hostname+modPath)
case "stacked-bar":
js, err := viz.GenerateStackedBarChartJS(
"#torontobot-chart",
chartSelected.Title,
chartSelected.Data,
chartSelected.ValueIsCurrency,
viz.WithBreakpointWidth())
if err != nil {
fmt.Println("Error generating JS:", err)
continue
}
id := citygraph.NewID().String()
chartHTML, err := viz.GenerateBarChartHTML(
chartSelected.Title,
chartSelected.Data,
chartSelected.ValueIsCurrency,
true, // yes to dark mode
viz.WithFixedWidth(800),
viz.WithFixedHeight(750),
)
if err != nil {
continue
}
if err := os.WriteFile("../mainapp/static/chart.html", []byte(chartHTML), 0644); err != nil {
fmt.Println("Error writing chart HTML:", err)
continue
}
featureImageURL, err := viz.GenerateAndUploadFeatureImage(
ctx,
id,
chartSelected.Title,
chartHTML,
chartSelected.Data,
chartSelected.ValueIsCurrency,
)
if err != nil {
fmt.Println("Error generating feature image:", err)
continue
}
modPath, err := tb.SaveToGraph(
ctx,
id,
question,
viz.RenderBody(question, sqlAnalysis.Schema, sqlAnalysis.Applicability, sqlAnalysis.SQL),
js,
featureImageURL,
"Local User")
if err != nil {
fmt.Println("Error saving chart to graph:", err)
continue
}
fmt.Printf("Published chart at %s\n", tb.Hostname+modPath)
case "line":
js, err := viz.GenerateLineChartJS(
"#torontobot-chart",
chartSelected.Title,
chartSelected.Data,
chartSelected.ValueIsCurrency,
viz.WithBreakpointWidth())
if err != nil {
fmt.Println("Error generating JS:", err)
continue
}
id := citygraph.NewID().String()
modPath, err := tb.SaveToGraph(
ctx,
id,
question,
viz.RenderBody(question, sqlAnalysis.Schema, sqlAnalysis.Applicability, sqlAnalysis.SQL),
js,
"",
"Local User")
if err != nil {
fmt.Println("Error saving chart to graph:", err)
continue
}
fmt.Printf("Published chart at %s\n", tb.Hostname+modPath)
case "pie":
js, err := viz.GeneratePieChartJS(
"#torontobot-chart",
chartSelected.Title,
chartSelected.Data,
chartSelected.ValueIsCurrency,
viz.WithBreakpointWidth())
if err != nil {
fmt.Println("Error generating JS:", err)
continue
}
id := citygraph.NewID().String()
chartHTML, err := viz.GeneratePieChartHTML(
chartSelected.Title,
chartSelected.Data,
chartSelected.ValueIsCurrency,
true, // yes to dark mode
viz.WithFixedWidth(800),
viz.WithFixedHeight(750),
)
if err != nil {
continue
}
featureImageURL, err := viz.GenerateAndUploadFeatureImage(
ctx,
id,
chartSelected.Title,
chartHTML,
chartSelected.Data,
chartSelected.ValueIsCurrency,
)
if err != nil {
fmt.Println("Error generating feature image:", err)
continue
}
modPath, err := tb.SaveToGraph(
ctx,
id,
question,
viz.RenderBody(question, sqlAnalysis.Schema, sqlAnalysis.Applicability, sqlAnalysis.SQL),
js,
featureImageURL,
"Local User")
if err != nil {
fmt.Println("Error saving chart to graph:", err)
continue
}
fmt.Printf("Published chart at %s\n", tb.Hostname+modPath)
// //case "scatter plot":
//
default:
fmt.Printf("Ah you need a %s, but I can't make those yet. Soon 😈\n", chartSelected.Chart)
}
}
}
}
}