forked from xhyuaner/OpenRepsSentiment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.go
302 lines (285 loc) · 8.58 KB
/
sentiment.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
package api
import (
"SDDS/forms"
"SDDS/global"
"SDDS/models"
"github.com/gin-gonic/gin"
"net/http"
)
//var client = openai.NewClient("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")
// HandleValidatorError 处理参数校验错误
func HandleValidatorError(c *gin.Context, err error) {
//var errs validator.ValidationErrors
//ok := errors.As(err, &errs)
errs, ok := err.(validator.ValidationErrors)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{
"msg": "参数校验失败",
})
}
c.JSON(http.StatusBadRequest, gin.H{
"msg": removeTopStruct(errs.Translate(global.Trans)),
})
return
}
//func GetGPTRes(ctx *gin.Context) {
// userQuestionForm := forms.UserQuestionForm{}
// if err := ctx.ShouldBind(&userQuestionForm); err != nil {
// HandleValidatorError(ctx, err)
// return
// }
// resp, err := client.CreateChatCompletion(
// context.Background(),
// openai.ChatCompletionRequest{
// Model: openai.GPT3Dot5Turbo,
// Messages: []openai.ChatCompletionMessage{
// {
// Role: openai.ChatMessageRoleUser,
// Content: userQuestionForm.UserQuestion,
// },
// },
// },
// )
//
// if err != nil {
// fmt.Printf("ChatCompletion error: %v\n", err)
// return
// }
// ctx.JSON(http.StatusOK, gin.H{
// "status": http.StatusOK,
// "message": "回复成功!",
// })
//
// fmt.Println(resp.Choices[0].Message.Content)
//}
// GetOverall 获取某个项目在某个周月标识情况下情感的总数,正向情感数量,负向情感数
func GetOverall(ctx *gin.Context) {
sentimentBForm := forms.SentimentBForm{}
if err := ctx.ShouldBind(&sentimentBForm); err != nil {
HandleValidatorError(ctx, err)
return
}
var sentiment []*models.Sentiment
res := global.DB.Model(&models.Sentiment{}).Select("SUM(mblog_total) as mblog_total,SUM(pos_num) as pos_num,SUM(neg_num) as neg_num").
Where("repo_name=? and time_flag=? and start_date=? and end_date=?",
sentimentBForm.RepoName, sentimentBForm.TimeFlag, sentimentBForm.StartDate, sentimentBForm.EndDate).
Find(&sentiment)
if res.RowsAffected == 0 {
ctx.JSON(http.StatusNotFound, gin.H{
"msg": "未查询到数据",
})
return
}
//var response []interface{}
//response = append(response, sentiment)
ctx.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": sentiment,
})
}
// GetTimeAxis 获取时间轴时间
func GetTimeAxis(ctx *gin.Context) {
sentimentBForm := forms.SentimentBForm{}
if err := ctx.ShouldBind(&sentimentBForm); err != nil {
HandleValidatorError(ctx, err)
return
}
var sentiments []*models.Sentiment
// SELECT DISTINCT(start_date),end_date from developsentiment WHERE repo_name='flutter' and time_flag=0;
res := global.DB.Model(&models.Sentiment{}).Select("start_date, end_date").
Where("repo_name=? and time_flag=?",
sentimentBForm.RepoName, sentimentBForm.TimeFlag).Distinct("start_date", "end_date").
Order("start_date").Find(&sentiments)
if res.RowsAffected == 0 {
ctx.JSON(http.StatusNotFound, gin.H{
"msg": "未查询到数据",
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": sentiments,
})
}
// GetDetail 获取某个项目在某个周月标识情况下情感的数量和经纬度数据
func GetDetail(ctx *gin.Context) {
sentimentBForm := forms.SentimentBForm{}
if err := ctx.ShouldBind(&sentimentBForm); err != nil {
HandleValidatorError(ctx, err)
return
}
var sentiments []*models.Sentiment
res := global.DB.Model(&models.Sentiment{}).Where("repo_name=? AND time_flag=?",
sentimentBForm.RepoName, sentimentBForm.TimeFlag).Preload("Area").Find(&sentiments)
if res.RowsAffected == 0 {
ctx.JSON(http.StatusNotFound, gin.H{
"msg": "未查询到数据",
})
return
}
// 封装返回数据格式
// 生成随机数
//rand.Seed(time.Now().UnixNano())
//fmt.Printf("随机浮点数: %f\n", randomFloat)
var response []interface{}
var features []gin.H
for _, sentiment := range sentiments {
// 生成0到100之间的随机浮点数
//randomFloat := rand.Float64() * 100.0
features = append(features, gin.H{
"type": "Feature",
"properties": gin.H{
"adcode": "United States",
"mblog_total": sentiment.MblogTotal,
"pos_num": sentiment.PosNum,
"neg_num": sentiment.NegNum,
"startdate": sentiment.StartDate,
"enddate": sentiment.EndDate,
},
"geometry": gin.H{
"type": "Point",
"coordinates": []float64{
sentiment.Area.Longitude, sentiment.Area.Latitude,
},
},
})
}
response = append(response, gin.H{
"type": "FeatureCollection",
"features": features,
})
ctx.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": response,
})
}
func GetDatas(ctx *gin.Context) {
pageForm := forms.PageForm{}
if err := ctx.ShouldBind(&pageForm); err != nil {
HandleValidatorError(ctx, err)
return
}
var sentiments []*models.Sentiment
res := global.DB.Model(&models.Sentiment{}).Preload("Area").Limit(pageForm.Nums).
Offset(pageForm.Nums * (pageForm.PageIndex - 1)).Find(&sentiments)
if res.RowsAffected == 0 {
ctx.JSON(http.StatusNotFound, gin.H{
"msg": "未查询到数据",
})
return
}
// 封装返回数据格式
// 生成随机数
//rand.Seed(time.Now().UnixNano())
//fmt.Printf("随机浮点数: %f\n", randomFloat)
var response []interface{}
var features []gin.H
for _, sentiment := range sentiments {
// 生成0到100之间的随机浮点数
//randomFloat := rand.Float64() * 100.0
features = append(features, gin.H{
"type": "Feature",
"properties": gin.H{
"adcode": "United States",
"mblog_total": sentiment.MblogTotal,
"pos_num": sentiment.PosNum,
"neg_num": sentiment.NegNum,
"startdate": sentiment.StartDate,
"enddate": sentiment.EndDate,
},
"geometry": gin.H{
"type": "Point",
"coordinates": []float64{
sentiment.Area.Longitude, sentiment.Area.Latitude,
},
},
})
}
response = append(response, gin.H{
"type": "FeatureCollection",
"features": features,
})
ctx.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": response,
})
}
//// GetSentimentByRepoName 获取指定仓库整体数据
//func GetSentimentByRepoName(ctx *gin.Context) {
//
// repoNameForm := forms.RepoNameForm{}
// if err := ctx.ShouldBind(&repoNameForm); err != nil {
// HandleValidatorError(ctx, err)
// return
// }
//
// result := forms.SentiByRepoForm{}
// res := global.DB.Model(&models.Sentiment{}).Select(
// "repo_name, sum(mblog_total) as total_comment, sum(pos_num) as total_pos, sum(neg_num) as total_neg").
// Where("repo_name=?", repoNameForm.RepoName).Group("repo_name").Find(&result)
// //global.DB.Scopes(Paginate(int(req.Pn), int(req.PSize))).Find(&users)
// if res.RowsAffected == 0 {
// ctx.JSON(http.StatusNotFound, gin.H{
// "msg": "未查询到数据",
// })
// return
// }
//
// fmt.Println("通过仓库名称查询到情感数据")
// ctx.JSON(http.StatusOK, gin.H{
// "data": result,
// })
//}
//
//// GetSentimentTime 获取指定仓库详细情感数据
//func GetSentimentTime(ctx *gin.Context) {
// repoDetail := forms.RepoDetailForm{}
// if err := ctx.ShouldBind(&repoDetail); err != nil {
// HandleValidatorError(ctx, err)
// return
// }
//
// var result []*models.Sentiment
// res := global.DB.Model(&models.Sentiment{}).Where("repo_name=? AND time_flag=? AND start_date=?",
// repoDetail.RepoName, repoDetail.TimeFlag, repoDetail.StartDate).Preload("Area").Find(&result)
// //global.DB.Scopes(Paginate(int(req.Pn), int(req.PSize))).Find(&users)
// if res.RowsAffected == 0 {
// ctx.JSON(http.StatusNotFound, gin.H{
// "msg": "未查询到数据",
// })
// return
// }
//
// fmt.Println("查询到仓库详细情感数据")
// ctx.JSON(http.StatusOK, gin.H{
// "data": result,
// })
//}
//
//// GetSentimentDefault 按照默认参数(repo_name=flutter&time_flag=0&start_date=2020-01-01)
//// 获取指定仓库详细情感数据,并写入json文件
//func GetSentimentDefault(ctx *gin.Context) {
// var result []*models.Sentiment
// global.DB.Model(&models.Sentiment{}).Where("repo_name='flutter' AND time_flag='0' AND start_date='2020-01-01'").
// Preload("Area").Find(&result)
//
// // 调用 json.MarshalIndent 将结构体序列化为 JSON 格式的字节切片
// jsonData, err := json.MarshalIndent(result, "", " ")
// if err != nil {
// fmt.Println("Error marshalling JSON:", err)
// return
// }
//
// // 将 JSON 数据写入文件
// file, err := os.Create("./developer-sentiment/data/default_sentiment_data.json")
// if err != nil {
// fmt.Println("Error creating file:", err)
// return
// }
// defer file.Close()
//
// file.Write(jsonData)
// fmt.Println("文件写入完成!!")
//
//}
//func GetDevelopTrend(ctx *gin.Context) {}