...
 
Commits (2)
FROM golang:1.10
EXPOSE 8080
WORKDIR /go/src/gitlab.com/shereland/graphql
COPY . .
......
......@@ -78,6 +78,7 @@ type photo struct {
// GetPopularPosts get most popular posts from last day
func GetPopularPosts() []map[string]string {
postsArray := make([]map[string]string, 0, 5)
client := database.DBRedisCon
yesterday := time.Now().Local().AddDate(0, 0, -1)
keyRedis := "post_views:" + yesterday.Format("20060102")
......@@ -87,6 +88,11 @@ func GetPopularPosts() []map[string]string {
member := posts.Val()[key].Member
slugs = append(slugs, "\""+member.(string)+"\"")
}
if len(slugs) == 0 {
return postsArray
}
inClause := "(" + strings.Join(slugs, ", ") + ")"
db := database.DBCon
......@@ -107,7 +113,6 @@ func GetPopularPosts() []map[string]string {
}
defer rows.Close()
postsArray := make([]map[string]string, 0, 5)
for rows.Next() {
var p post
if err := rows.Scan(&p.title, &p.slug, &p.categorySlug); err != nil {
......
......@@ -11,7 +11,10 @@ import (
"os"
"github.com/go-redis/redis"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gitlab.com/shereland/graphql/database"
"gitlab.com/shereland/graphql/metrics"
)
func main() {
......@@ -26,12 +29,14 @@ func main() {
Password: "", // no password set
DB: 0, // use default DB
})
prometheus.MustRegister(metrics.ResolverDurations)
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(page)
}))
http.Handle("/graphql", authenticationMiddleware(&Handler{Schema: schema}))
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":8080", nil))
}
......
package metrics
import "github.com/prometheus/client_golang/prometheus"
var (
ResolverDurations = prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "resolvers_duration_milliseconds",
Help: "Time (ms) for each resolver.",
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
},
[]string{"resolvers"},
)
)
......@@ -2,20 +2,26 @@ package resolvers
import (
"context"
"time"
"gitlab.com/shereland/graphql/generic"
"gitlab.com/shereland/graphql/metrics"
)
type Resolver struct{}
// LatestTweets return the last two tweets from _shereland
func (r *Resolver) LatestTweets() *[]*Tweet {
elapsed := time.Since(time.Now())
tweetsArray := make([]*Tweet, 0, 2)
tweets := generic.GetLatestTweets()
for _, tweet := range tweets {
t := Tweet{tweet}
tweetsArray = append(tweetsArray, &t)
}
metrics.ResolverDurations.WithLabelValues("LatestTweets").Observe(float64(elapsed.Nanoseconds()) / 1000)
return &tweetsArray
}
......@@ -27,6 +33,7 @@ func (r *Resolver) Notifications() *NotificationResolver {
// Post return a single post by slug
func (r *Resolver) Post(args struct{ Slug string }) *Post {
elapsed := time.Since(time.Now())
p := generic.GetPost(args.Slug)
comments := make([]Comment, 0, 2)
if p["comments"] != nil {
......@@ -88,6 +95,8 @@ func (r *Resolver) Post(args struct{ Slug string }) *Post {
relatedBooks: relatedBooks,
relatedPosts: relatedPosts,
album: album}
metrics.ResolverDurations.WithLabelValues("Post").Observe(float64(elapsed.Nanoseconds()) / 1000)
return &post
}
......@@ -97,6 +106,7 @@ func (r *Resolver) Posts(args struct {
Category *string
Page *int32
}) *Posts {
elapsed := time.Since(time.Now())
posts := make([]*Post, 0, 4)
var page int32
......@@ -179,17 +189,20 @@ func (r *Resolver) Posts(args struct {
numberOfPages := <-numberOfPagesMessage
metrics.ResolverDurations.WithLabelValues("Posts").Observe(float64(elapsed.Nanoseconds()) / 1000)
return &Posts{currentPage: page, lastPage: numberOfPages, data: &posts}
}
// PopularPosts Return array of posts (max 5) ordered by the most popular first
func (r *Resolver) PopularPosts() *[]*Post {
elapsed := time.Since(time.Now())
postsArray := make([]*Post, 0, 5)
posts := generic.GetPopularPosts()
for _, post := range posts {
p := Post{title: post["title"], url: post["url"]}
postsArray = append(postsArray, &p)
}
metrics.ResolverDurations.WithLabelValues("PopularPosts").Observe(float64(elapsed.Nanoseconds()) / 1000)
return &postsArray
}
......@@ -205,6 +218,7 @@ type NotificationResolver struct{}
// FriendshipRequests get all friendship requests to user
func (n NotificationResolver) FriendshipRequests(ctx context.Context) *[]*friendshipRequestsResolver {
elapsed := time.Since(time.Now())
sessionid := ctx.Value("sessionid").(string)
notifications := generic.FriendshipNotifications(sessionid)
notificationsArray := make([]*friendshipRequestsResolver, 0, 2)
......@@ -212,6 +226,7 @@ func (n NotificationResolver) FriendshipRequests(ctx context.Context) *[]*friend
f := friendshipRequestsResolver{slug: notification["slug"], name: notification["name"]}
notificationsArray = append(notificationsArray, &f)
}
metrics.ResolverDurations.WithLabelValues("FriendshipRequests").Observe(float64(elapsed.Nanoseconds()) / 1000)
return &notificationsArray
}
......