Adding trending words to restapi

This commit is contained in:
Farhan Khan 2021-12-19 21:36:10 -05:00
parent 560c90ed8e
commit dcf6ee9a10

View File

@ -12,26 +12,30 @@ import (
"github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4"
) )
var trendsText string var metricsText 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 runMetrics() { func runMetrics() {
hashtagtotal := runTrendingMetrics() hashtagtotal := runTrendingHashtags()
wordstotal := runTrendingWords()
totalJson := make(map[string]interface{}) totalJson := make(map[string]interface{})
totalJson["hashtags"] = hashtagtotal totalJson["hashtags"] = hashtagtotal
totalJson["words"] = wordstotal
totalJson["datetime"] = time.Now().UTC()
data, err := json.Marshal(totalJson) data, err := json.Marshal(totalJson)
if err != nil { if err != nil {
log.Fatalf("error marshaling combined activity: %v\n", err) log.Fatalf("error marshaling combined activity: %v\n", err)
} }
trendsText = string(data) metricsText = string(data)
} }
func runTrendingMetrics() map[string]interface{} { func runTrendingHashtags() map[string]interface{} {
sql := `SELECT UNNEST(activities.hashtags) as hashtags, count(actors.id) sql := `SELECT UNNEST(activities.hashtags) as hashtags, count(actors.id)
from activities from activities
LEFT JOIN actors ON activities.document->>'attributedTo'=actors.document->>'id' LEFT JOIN actors ON activities.document->>'attributedTo'=actors.document->>'id'
@ -65,16 +69,70 @@ GROUP BY hashtags ORDER BY count DESC LIMIT 20;`
hashtagtotal := make(map[string]interface{}); hashtagtotal := make(map[string]interface{});
hashtagtotal["count"] = hashcount hashtagtotal["count"] = hashcount
hashtagtotal["datetime"] = time.Now().UTC()
hashtagtotal["items"] = hashtagitems hashtagtotal["items"] = hashtagitems
return hashtagtotal return hashtagtotal
} }
func runTrendingWords() map[string]interface{} {
sql := `WITH popular_words AS (
select word FROM ts_stat(
'
SELECT to_tsvector(''simple'', normalized) FROM activities
LEFT JOIN actors ON activities.document->>''attributedTo''=actors.document->>''id''
WHERE activities.identifiedat > current_timestamp - interval ''60 minutes''
AND actors.bot=false
'
)
WHERE length(word) > 3
AND NOT word in (SELECT word FROM stopwords)
ORDER BY ndoc DESC LIMIT 100)
SELECT concat_ws(' ', a1.word, a2.word) phrase, count(*)
FROM popular_words AS a1
CROSS JOIN popular_words AS a2
CROSS JOIN activities
WHERE normalized ilike format('%%%s %s%%', a1.word, a2.word)
AND identifiedat > current_timestamp - interval '60 minutes'
GROUP BY 1
HAVING count(*) > 1
ORDER BY 2 DESC LIMIT 20;
`
rows, err := pool.Query(context.Background(), sql)
if err != nil {
panic(err)
}
trendingitems := make([]interface{}, 0);
trendingcount := 0
for rows.Next() {
var trendingword string
var count int
err = rows.Scan(&trendingword, &count)
if err != nil {
panic(err)
}
trendingitem := make(map[string]interface{})
trendingitem["trending"] = trendingword
trendingitem["count"] = count
trendingitems = append(trendingitems, trendingitem)
trendingcount = trendingcount + 1
}
rows.Close()
trendingwordtotal := make(map[string]interface{});
trendingwordtotal["count"] = trendingcount
trendingwordtotal["items"] = trendingitems
return trendingwordtotal
}
// GET handlers // GET handlers
func getTrending(w http.ResponseWriter, r *http.Request) { func getTrending(w http.ResponseWriter, r *http.Request) {
enableCors(&w) enableCors(&w)
fmt.Fprintf(w, "%s", trendsText) fmt.Fprintf(w, "%s", metricsText)
} }
func getSearch(w http.ResponseWriter, r *http.Request) { func getSearch(w http.ResponseWriter, r *http.Request) {
@ -159,10 +217,12 @@ func getSearch(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
pool = getDbPool() pool = getDbPool()
metricsText = "[]"
go func() { go func() {
for { for {
runMetrics() runMetrics()
time.Sleep(30 * time.Second) time.Sleep(10 * time.Minute)
} }
}() }()