Updating /api/v1/trending
This commit is contained in:
parent
5a41756d48
commit
6a9ebf917e
@ -12,27 +12,33 @@ import (
|
|||||||
"github.com/jackc/pgx/v4"
|
"github.com/jackc/pgx/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
var trendingexport string
|
var trendsText string
|
||||||
var trendingHashTags string
|
|
||||||
var trendingwords string
|
|
||||||
|
|
||||||
var hashtagtotal map[string]interface{}
|
|
||||||
var wordstotal map[string]interface{}
|
|
||||||
//hashtagmeta := make(map[string]interface{});
|
|
||||||
|
|
||||||
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 getTrendingHashtags() {
|
func runMetrics() {
|
||||||
sql := "SELECT UNNEST(activities.hashtags) as hashtags, count(actors.id) " +
|
hashtagtotal := runTrendingMetrics()
|
||||||
"FROM activities " +
|
totalJson := make(map[string]interface{})
|
||||||
"LEFT JOIN actors ON activities.document->>'attributedTo'=actors.document->>'id' " +
|
totalJson["hashtags"] = hashtagtotal
|
||||||
"WHERE actors.bot=TRUE " +
|
|
||||||
//"AND activities.identifiedat > LOCALTIMESTAMP - INTERVAL '30 MINUTES' " +
|
data, err := json.Marshal(totalJson)
|
||||||
"GROUP BY hashtags ORDER BY count DESC LIMIT 20"
|
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;`
|
||||||
|
|
||||||
for {
|
|
||||||
rows, err := pool.Query(context.Background(), sql)
|
rows, err := pool.Query(context.Background(), sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -57,86 +63,21 @@ func getTrendingHashtags() {
|
|||||||
}
|
}
|
||||||
rows.Close()
|
rows.Close()
|
||||||
|
|
||||||
hashtagtotal = make(map[string]interface{});
|
hashtagtotal := make(map[string]interface{});
|
||||||
hashtagtotal["count"] = hashcount
|
hashtagtotal["count"] = hashcount
|
||||||
hashtagtotal["datetime"] = time.Now().UTC()
|
hashtagtotal["datetime"] = time.Now().UTC()
|
||||||
hashtagtotal["items"] = hashtagitems
|
hashtagtotal["items"] = hashtagitems
|
||||||
|
|
||||||
totalJson := make(map[string]interface{})
|
return hashtagtotal
|
||||||
totalJson["hashtags"] = hashtagtotal
|
|
||||||
|
|
||||||
data, err := json.Marshal(totalJson)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("error marshaling combined activity: %v\n", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trendingHashTags = string(data)
|
// GET handlers
|
||||||
time.Sleep(time.Minute * 30)
|
func getTrending(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func gettrends() {
|
|
||||||
for {
|
|
||||||
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 {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
trenditems := make([]interface{}, 0)
|
|
||||||
|
|
||||||
for rows.Next() {
|
|
||||||
var word string
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
rows.Close()
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func gettrending(w http.ResponseWriter, r *http.Request) {
|
|
||||||
enableCors(&w)
|
enableCors(&w)
|
||||||
// fmt.Fprintf(w, "%s", trendingexport)
|
fmt.Fprintf(w, "%s", trendsText)
|
||||||
}
|
}
|
||||||
|
|
||||||
func gettrendinghashtags(w http.ResponseWriter, r *http.Request) {
|
func getSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
enableCors(&w)
|
|
||||||
fmt.Fprintf(w, "%s", trendingHashTags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func gettrendingwords(w http.ResponseWriter, r *http.Request) {
|
|
||||||
enableCors(&w)
|
|
||||||
fmt.Fprintf(w, "%s", trendingexport)
|
|
||||||
}
|
|
||||||
|
|
||||||
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"]
|
||||||
@ -218,13 +159,15 @@ func getsearch(w http.ResponseWriter, r *http.Request) {
|
|||||||
func main() {
|
func main() {
|
||||||
pool = getDbPool()
|
pool = getDbPool()
|
||||||
|
|
||||||
go gettrends()
|
go func() {
|
||||||
go getTrendingHashtags()
|
for {
|
||||||
|
runMetrics()
|
||||||
|
time.Sleep(30 * time.Second)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
http.HandleFunc("/api/v1/search", getsearch)
|
http.HandleFunc("/api/v1/search", getSearch)
|
||||||
http.HandleFunc("/api/v1/trending", gettrending)
|
http.HandleFunc("/api/v1/trending", getTrending)
|
||||||
http.HandleFunc("/api/v1/trending/hashtags", gettrendinghashtags)
|
|
||||||
http.HandleFunc("/api/v1/trending/words", gettrendingwords)
|
|
||||||
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