Merge branch 'trendingwords' into 'master'
Trendingwords See merge request khanzf/fedilogue!17
This commit is contained in:
commit
560c90ed8e
@ -28,7 +28,7 @@ type PublicKeyType struct {
|
|||||||
|
|
||||||
type ActorJson struct {
|
type ActorJson struct {
|
||||||
id int
|
id int
|
||||||
uri string `json:"id"`
|
Uri string `json:"id"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Inbox string `json:"inbox"`
|
Inbox string `json:"inbox"`
|
||||||
Outbox string `json:"outbox"`
|
Outbox string `json:"outbox"`
|
||||||
@ -53,7 +53,7 @@ type TagType struct {
|
|||||||
|
|
||||||
type PostJson struct {
|
type PostJson struct {
|
||||||
id int
|
id int
|
||||||
uri string `json:"id"`
|
Uri string `json:"id"`
|
||||||
InReplyTo string `json:"inReplyTo"`
|
InReplyTo string `json:"inReplyTo"`
|
||||||
|
|
||||||
normalized string
|
normalized string
|
||||||
|
@ -12,61 +12,72 @@ import (
|
|||||||
"github.com/jackc/pgx/v4"
|
"github.com/jackc/pgx/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
var trendingexport string
|
var trendsText string
|
||||||
|
|
||||||
func enableCors(w *http.ResponseWriter) {
|
func enableCors(w *http.ResponseWriter) {
|
||||||
(*w).Header().Set("Access-Control-Allow-Origin", "*")
|
(*w).Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
}
|
}
|
||||||
|
|
||||||
func gettrends() {
|
func runMetrics() {
|
||||||
var err error
|
hashtagtotal := runTrendingMetrics()
|
||||||
var rows pgx.Rows
|
totalJson := make(map[string]interface{})
|
||||||
|
totalJson["hashtags"] = hashtagtotal
|
||||||
|
|
||||||
for {
|
data, err := json.Marshal(totalJson)
|
||||||
rows, err = pool.Query(context.Background(), "SELECT word, ndoc FROM ts_stat($$ SELECT normalized_tsvector FROM activities WHERE activities.identifiedat > current_timestamp - interval '60 minutes' $$) ORDER BY ndoc DESC LIMIT 10")
|
if err != nil {
|
||||||
|
log.Fatalf("error marshaling combined activity: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
trendsText = string(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runTrendingMetrics() map[string]interface{} {
|
||||||
|
sql := `SELECT UNNEST(activities.hashtags) as hashtags, count(actors.id)
|
||||||
|
from activities
|
||||||
|
LEFT JOIN actors ON activities.document->>'attributedTo'=actors.document->>'id'
|
||||||
|
WHERE actors.bot=false
|
||||||
|
AND activities.identifiedat > LOCALTIMESTAMP - INTERVAL '30 MINUTES'
|
||||||
|
GROUP BY hashtags ORDER BY count DESC LIMIT 20;`
|
||||||
|
|
||||||
|
rows, err := pool.Query(context.Background(), sql)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
hashtagitems := make([]interface{}, 0);
|
||||||
|
hashcount := 0
|
||||||
|
for rows.Next() {
|
||||||
|
var hashtag string
|
||||||
|
var count int
|
||||||
|
|
||||||
|
err = rows.Scan(&hashtag, &count)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
trenditems := make([]interface{}, 0)
|
hashtagitem := make(map[string]interface{})
|
||||||
//fmt.Println(trenditems)
|
hashtagitem["hashtag"] = hashtag
|
||||||
|
hashtagitem["count"] = count
|
||||||
for rows.Next() {
|
hashtagitems = append(hashtagitems, hashtagitem)
|
||||||
var word string
|
hashcount = hashcount + 1
|
||||||
var ndoc int
|
|
||||||
|
|
||||||
err = rows.Scan(&word, &ndoc)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
trenditem := make(map[string]interface{})
|
|
||||||
trenditem["ndoc"] = ndoc
|
|
||||||
trenditem["word"] = word
|
|
||||||
trenditems = append(trenditems, trenditem)
|
|
||||||
}
|
|
||||||
|
|
||||||
totalJson := make(map[string]interface{})
|
|
||||||
totalJson["trends"] = trenditems
|
|
||||||
|
|
||||||
data, err := json.Marshal(totalJson)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("error marshaling combined activity: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
trendingexport = string(data)
|
|
||||||
|
|
||||||
time.Sleep(time.Second * 60)
|
|
||||||
}
|
}
|
||||||
|
rows.Close()
|
||||||
|
|
||||||
|
hashtagtotal := make(map[string]interface{});
|
||||||
|
hashtagtotal["count"] = hashcount
|
||||||
|
hashtagtotal["datetime"] = time.Now().UTC()
|
||||||
|
hashtagtotal["items"] = hashtagitems
|
||||||
|
|
||||||
|
return hashtagtotal
|
||||||
}
|
}
|
||||||
|
|
||||||
func trending(w http.ResponseWriter, r *http.Request) {
|
// GET handlers
|
||||||
|
func getTrending(w http.ResponseWriter, r *http.Request) {
|
||||||
enableCors(&w)
|
enableCors(&w)
|
||||||
fmt.Fprintf(w, "%s", trendingexport)
|
fmt.Fprintf(w, "%s", trendsText)
|
||||||
}
|
}
|
||||||
|
|
||||||
func search(w http.ResponseWriter, r *http.Request) {
|
func getSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
enableCors(&w)
|
enableCors(&w)
|
||||||
searchkeys, exists_search := r.URL.Query()["s"]
|
searchkeys, exists_search := r.URL.Query()["s"]
|
||||||
offsetkeys, exists_offset := r.URL.Query()["o"]
|
offsetkeys, exists_offset := r.URL.Query()["o"]
|
||||||
@ -148,10 +159,15 @@ func search(w http.ResponseWriter, r *http.Request) {
|
|||||||
func main() {
|
func main() {
|
||||||
pool = getDbPool()
|
pool = getDbPool()
|
||||||
|
|
||||||
go gettrends()
|
go func() {
|
||||||
|
for {
|
||||||
|
runMetrics()
|
||||||
|
time.Sleep(30 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
http.HandleFunc("/api/v1/search", search)
|
http.HandleFunc("/api/v1/search", getSearch)
|
||||||
http.HandleFunc("/api/v1/trending", trending)
|
http.HandleFunc("/api/v1/trending", getTrending)
|
||||||
log.Print("Starting HTTP inbox on port http://0.0.0.0:6431")
|
log.Print("Starting HTTP inbox on port http://0.0.0.0:6431")
|
||||||
log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil))
|
log.Fatal(http.ListenAndServe("0.0.0.0:6431", nil))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user