-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateGraph.go
318 lines (282 loc) · 8.76 KB
/
createGraph.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os/exec"
"regexp"
"slices"
"strings"
"time"
"unicode"
"github.com/bwmarrin/discordgo"
)
type DataElemment struct {
Time time.Time
Items map[string]string
}
type DataSet []*DataElemment
func NewDataSet() DataSet {
return DataSet{}
}
func (ds *DataSet) AddDataElemment(time time.Time, items *map[string]string) {
*ds = append(*ds, &DataElemment{
time,
*items,
})
}
func (ds *DataSet) extractItemKeys() (keys []string) {
for _, de := range *ds {
for key := range (*de).Items {
if slices.Contains(keys, key) {
continue
}
keys = append(keys, key)
}
}
return keys
}
func (ds DataSet) String() string {
var b strings.Builder
for _, de := range ds {
fmt.Fprintf(&b, "time: %v, items: %v\n", de.Time, de.Items)
}
return b.String()
}
var yMin, yMax map[string]string = map[string]string{
"湿度": "0",
// "土壌水分": "0",
"バッテリー": "0",
}, map[string]string{
"湿度": "100",
// "土壌水分": "100",
"バッテリー": "2",
}
func determineIfMessageIsData() func(string) bool {
re, _ := regexp.Compile(`^[^:]+:\s*-?\d`)
return func(message string) bool {
return re.MatchString(message)
}
}
func ResisterAtMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if !determineIfMessageIsData()(m.Content) {
return
}
log.Println("get data format message")
items := formatMessageIntoMap(m.Content)
json, err := json.Marshal(items)
if err != nil {
log.Println("error marshal json:", err)
}
AddJSONDataToItemData(m.Message.Timestamp, string(json), m.ChannelID)
}
func GraphHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
if len(options) == 0 {
createGraph(s, i, 24)
return
}
switch options[0].Name {
case "hour":
xrange := options[0].IntValue()
createGraph(s, i, xrange)
}
}
func register(s *discordgo.Session, i *discordgo.InteractionCreate) {
days := 90
// すべての処理が終わってメッセージを送っても遅いので最初に送っておく
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Now, resistering messages to DB, about %d days.\nPlease, wait few minutes.", days),
},
})
if err != nil {
log.Println("error respond message :", err)
return
}
// ここからが処理の本番
log.Println("(register) getting messages...")
messages, err := getNumOfTargetMessages(s, i, 96*days)
if err != nil {
log.Println("(register) error getting messages:", err)
return
}
if 0 == len(messages) {
log.Println("(register) got 0 messages:", err)
return
}
log.Println("(register) filter and shape messages...")
dataSet := formatMessagesIntoDataSet(messages, time.Hour*time.Duration(24*days))
log.Println("(register) resister data in DB...")
for _, dataEle := range dataSet {
json, err := json.Marshal(dataEle.Items)
if err != nil {
log.Println("error marshal json:", err)
}
AddJSONDataToItemData(dataEle.Time, string(json), i.ChannelID)
}
}
func createGraph(s *discordgo.Session, i *discordgo.InteractionCreate, xrange int64) {
latestTime, err := GetLatestTimeFromItemData(i.ChannelID)
if err != nil {
errorlogAndRespondToDiscord(s, i, "error get latest message time:", err)
return
}
log.Println("get data from DB...")
dataSet, err := GetDataSetFromItemData(i.ChannelID, *latestTime, (*latestTime).Add(-time.Hour*time.Duration(xrange)))
if err != nil {
errorlogAndRespondToDiscord(s, i, "error get data from DB:", err)
return
}
log.Printf(" dataSet:\n%v", dataSet)
log.Println("creating graph...")
keys := dataSet.extractItemKeys()
pngs := make([]io.Reader, 0, len(keys))
for _, key := range keys {
png, err := createPngGraph(dataSet, key, yMin[key], yMax[key])
if err != nil {
errorlogAndRespondToDiscord(s, i, "error create graph.", err)
return
}
pngs = append(pngs, png)
}
log.Println("respond messge...")
err = respondPngsToDiscord(s, i, "Create graph!", pngs...)
if err != nil {
log.Println("error respond message :", err)
return
}
}
func respondPngsToDiscord(s *discordgo.Session, i *discordgo.InteractionCreate, content string, pngs ...io.Reader) error {
var respondFiles []*discordgo.File
for i, png := range pngs {
respondFiles = append(respondFiles, &discordgo.File{
ContentType: "image/png",
Name: fmt.Sprintf("%s-%d.png", time.Now().String(), i),
Reader: png,
})
}
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
Files: respondFiles,
},
})
if err != nil {
_, err := s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
Content: content,
Files: respondFiles,
})
log.Println(err)
}
return err
}
func errorlogAndRespondToDiscord(s *discordgo.Session, i *discordgo.InteractionCreate, text string, err error) {
log.Println(text, ":", err)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Sorry, " + text,
},
})
}
func createPngGraph(dataSet DataSet, key, y_min, y_max string) (io.Reader, error) {
gnuplotText := fmt.Sprintln("set timefmt '%Y/%m/%d-%H:%M:%S';")
gnuplotText += fmt.Sprintf("set title '%s';", key)
gnuplotText += fmt.Sprintln("set xdata time;")
gnuplotText += fmt.Sprintln("set format x '%H:%M';")
gnuplotText += fmt.Sprintln("set xlabel '時間';")
gnuplotText += fmt.Sprintln("set xtics 60*60;")
gnuplotText += fmt.Sprintln("set xtics rotate by 90 right;")
gnuplotText += fmt.Sprintln("set ytics nomirror;")
gnuplotText += fmt.Sprintf("set yrange [%s:%s];", y_min, y_max)
gnuplotText += fmt.Sprintln("set terminal pngcairo;")
gnuplotText += fmt.Sprintf("plot '< cat -' using 1:2 axis x1y1 with line title '%s'", key)
log.Println(" gnuplotText:\n" + gnuplotText)
var dataText string
for _, dataEle := range dataSet {
dataText += fmt.Sprintln(dataEle.Time.Local().Format("2006/01/02-15:04:05"), dataEle.Items[key])
}
cmd := exec.Command("gnuplot", "-e", gnuplotText)
cmd.Stdin = strings.NewReader(dataText)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error exec gnuplot: %w \n command stdout and stderr is\n%s", err, string(output))
}
return bytes.NewReader(output), nil
}
func formatMessagesIntoDataSet(messages []*discordgo.Message, period time.Duration) (dataSet DataSet) {
limitTime := messages[0].Timestamp.Add(-period)
for _, m := range messages {
if m.Timestamp.Before(limitTime) {
break
}
items := formatMessageIntoMap(m.Content)
dataSet.AddDataElemment(m.Timestamp, &items)
}
log.Printf(" formatted data:\n%#v", dataSet)
return
}
func formatMessageIntoMap(message string) map[string]string {
items := make(map[string]string)
itemStrs := strings.Split(message, ",")
for _, itemStr := range itemStrs {
pair := strings.Split(itemStr, ":")
key := strings.TrimSpace(pair[0])
value := strings.TrimFunc(pair[1], isNotNum)
items[key] = value
}
return items
}
func isNotNum(c rune) bool {
return c != '.' && !unicode.IsNumber(c)
}
func getNumOfTargetMessages(s *discordgo.Session, i *discordgo.InteractionCreate, num int) ([]*discordgo.Message, error) {
messages := make([]*discordgo.Message, 0, 2*num)
var beforeID string
isData := determineIfMessageIsData()
// 目的のメッセージを指定個数用意するループ
// このループは5回程度に止めないと時間がかかりすぎるのかbotが応答しなかった扱いになる
for j := 0; j < num/100+1; j++ {
// メッセージを100件単位で取ってくる
buffer, err := s.ChannelMessages(i.ChannelID, 100, beforeID, "", "")
if err != nil {
return nil, fmt.Errorf("error get messages: %w", err)
}
if len(buffer) == 0 {
// ここでループから抜ける場合、messagesがnum未満の可能性がある。
break
}
messages = appendFilteredMessages(messages, buffer, isData)
if len(messages) >= num {
messages = messages[0:num]
break
}
beforeID = messages[len(messages)-1].ID
}
logMessages(messages)
return messages, nil
}
func appendFilteredMessages(beAppended, beFiltered []*discordgo.Message, isData func(string) bool) []*discordgo.Message {
for _, b := range beFiltered {
if !isData(b.Content) {
continue
}
beAppended = append(beAppended, b)
}
return beAppended
}
func logMessages(messages []*discordgo.Message) {
var messageContents string
for _, b := range messages {
messageContents += fmt.Sprintln(b.Content)
}
log.Printf(" get message(num=%d):\n%s\n", len(messages), messageContents)
}