Open platform to collect and prioritize feedback
better home page performance (#259)
Guilherme Oenningb0f04f072ecc
diff --git a/app/pkg/dbx/dbx.go b/app/pkg/dbx/dbx.goindex 2c232b422..7955228d1 100644--- a/app/pkg/dbx/dbx.go+++ b/app/pkg/dbx/dbx.go@@ -107,8 +107,8 @@ type Trx struct {var formatter = strings.NewReplacer("\t", "", "\n", " ")-// QueryRow the database with given SQL command and returns only one row-func (trx Trx) QueryRow(command string, args ...interface{}) *sql.Row {+// Execute given SQL command+func (trx Trx) Execute(command string, args ...interface{}) error {if trx.logger.IsEnabled(log.DEBUG) {command = formatter.Replace(command)start := time.Now()@@ -116,23 +116,13 @@ func (trx Trx) QueryRow(command string, args ...interface{}) *sql.Row {trx.logger.Debugf("%s %v in %s", log.Yellow(command), log.Blue(args), log.Magenta(time.Since(start).String()))}()}- return trx.tx.QueryRow(command, args...)-}-// Query the database with given SQL command-func (trx Trx) Query(command string, args ...interface{}) (*sql.Rows, error) {- if trx.logger.IsEnabled(log.DEBUG) {- command = formatter.Replace(command)- start := time.Now()- defer func() {- trx.logger.Debugf("%s %v in %s", log.Yellow(command), log.Blue(args), log.Magenta(time.Since(start).String()))- }()- }- return trx.tx.Query(command, args...)+ _, err := trx.tx.Exec(command, args...)+ return err}-// Execute given SQL command-func (trx Trx) Execute(command string, args ...interface{}) error {+// Scalar returns first row and first column+func (trx Trx) Scalar(data interface{}, command string, args ...interface{}) error {if trx.logger.IsEnabled(log.DEBUG) {command = formatter.Replace(command)start := time.Now()@@ -140,13 +130,8 @@ func (trx Trx) Execute(command string, args ...interface{}) error {trx.logger.Debugf("%s %v in %s", log.Yellow(command), log.Blue(args), log.Magenta(time.Since(start).String()))}()}- _, err := trx.tx.Exec(command, args...)- return err-}-// Scalar returns first row and first column-func (trx Trx) Scalar(data interface{}, command string, args ...interface{}) error {- row := trx.QueryRow(command, args...)+ row := trx.tx.QueryRow(command, args...)err := row.Scan(data)if err == sql.ErrNoRows {return app.ErrNotFound@@ -156,7 +141,15 @@ func (trx Trx) Scalar(data interface{}, command string, args ...interface{}) err// Get first row and bind to given datafunc (trx Trx) Get(data interface{}, command string, args ...interface{}) error {- rows, err := trx.Query(command, args...)+ if trx.logger.IsEnabled(log.DEBUG) {+ command = formatter.Replace(command)+ start := time.Now()+ defer func() {+ trx.logger.Debugf("%s %v in %s", log.Yellow(command), log.Blue(args), log.Magenta(time.Since(start).String()))+ }()+ }++ rows, err := trx.tx.Query(command, args...)if err != nil {return err}@@ -172,10 +165,18 @@ func (trx Trx) Get(data interface{}, command string, args ...interface{}) error// QueryIntArray executes given SQL command and return first column as intfunc (trx Trx) QueryIntArray(command string, args ...interface{}) ([]int, error) {+ if trx.logger.IsEnabled(log.DEBUG) {+ command = formatter.Replace(command)+ start := time.Now()+ defer func() {+ trx.logger.Debugf("%s %v in %s", log.Yellow(command), log.Blue(args), log.Magenta(time.Since(start).String()))+ }()+ }+values := make([]int, 0)var value int- rows, err := trx.Query(command, args...)+ rows, err := trx.tx.Query(command, args...)if err != nil {return nil, err}@@ -195,7 +196,15 @@ func (trx Trx) QueryIntArray(command string, args ...interface{}) ([]int, error)// Exists returns true if at least one record is foundfunc (trx Trx) Exists(command string, args ...interface{}) (bool, error) {- rows, err := trx.Query(command, args...)+ if trx.logger.IsEnabled(log.DEBUG) {+ command = formatter.Replace(command)+ start := time.Now()+ defer func() {+ trx.logger.Debugf("%s %v in %s", log.Yellow(command), log.Blue(args), log.Magenta(time.Since(start).String()))+ }()+ }++ rows, err := trx.tx.Query(command, args...)if rows != nil {defer rows.Close()return rows.Next(), nil@@ -205,7 +214,15 @@ func (trx Trx) Exists(command string, args ...interface{}) (bool, error) {// Count returns number of rowsfunc (trx Trx) Count(command string, args ...interface{}) (int, error) {- rows, err := trx.Query(command, args...)+ if trx.logger.IsEnabled(log.DEBUG) {+ command = formatter.Replace(command)+ start := time.Now()+ defer func() {+ trx.logger.Debugf("%s %v in %s", log.Yellow(command), log.Blue(args), log.Magenta(time.Since(start).String()))+ }()+ }++ rows, err := trx.tx.Query(command, args...)defer rows.Close()count := 0for rows != nil && rows.Next() {@@ -216,7 +233,15 @@ func (trx Trx) Count(command string, args ...interface{}) (int, error) {//Select all matched rows bind to given datafunc (trx Trx) Select(data interface{}, command string, args ...interface{}) error {- rows, err := trx.Query(command, args...)+ if trx.logger.IsEnabled(log.DEBUG) {+ command = formatter.Replace(command)+ start := time.Now()+ defer func() {+ trx.logger.Debugf("%s %v in %s", log.Yellow(command), log.Blue(args), log.Magenta(time.Since(start).String()))+ }()+ }++ rows, err := trx.tx.Query(command, args...)if err != nil {return err}diff --git a/app/pkg/dbx/mapping.go b/app/pkg/dbx/mapping.goindex 29a4a4272..c475f6961 100644--- a/app/pkg/dbx/mapping.go+++ b/app/pkg/dbx/mapping.go@@ -76,6 +76,14 @@ type TypeMapper struct {//NewTypeMapper creates a new instance of TypeMapper for given reflect.Typefunc NewTypeMapper(t reflect.Type) TypeMapper {all := make(map[string]FieldInfo, 0)++ if t.Kind() != reflect.Struct {+ return TypeMapper{+ Type: t,+ Fields: all,+ }+ }+for i := 0; i < t.NumField(); i++ {field := t.Field(i)columnName := field.Tag.Get("db")diff --git a/app/pkg/dbx/mapping_test.go b/app/pkg/dbx/mapping_test.goindex 558680ed5..d784e4f2d 100644--- a/app/pkg/dbx/mapping_test.go+++ b/app/pkg/dbx/mapping_test.go@@ -17,6 +17,13 @@ func createScanner(values ...interface{}) func(dest ...interface{}) error {}}+func TestTypeMapper_NonStruct(t *testing.T) {+ RegisterTestingT(t)+ var id int+ mapping := dbx.NewTypeMapper(reflect.TypeOf(id))+ Expect(len(mapping.Fields)).To(Equal(0))+}+func TestTypeMapper_SimpleStruct(t *testing.T) {RegisterTestingT(t)u := user{}@@ -58,6 +65,14 @@ func TestTypeMapper_DeepNestedStruct(t *testing.T) {Expect(mapping.Fields["user_tenant_name"].FieldName).To(Equal([]string{"User", "Tenant", "Name"}))}+func TestRowMapper_NonStruct(t *testing.T) {+ RegisterTestingT(t)+ mapper := dbx.NewRowMapper()+ var id int+ mapper.Map(&id, []string{"id"}, createScanner(5))+ Expect(id).To(Equal(5))+}+func TestRowMapper_SimpleStruct(t *testing.T) {RegisterTestingT(t)mapper := dbx.NewRowMapper()diff --git a/app/storage/postgres/idea.go b/app/storage/postgres/idea.goindex 8f190a07f..37f2f5abd 100644--- a/app/storage/postgres/idea.go+++ b/app/storage/postgres/idea.go@@ -129,16 +129,38 @@ func (s *IdeaStorage) SetCurrentUser(user *models.User) {}var (- sqlSelectIdeasWhere = `SELECT i.id,+ sqlSelectIdeasWhere = ` WITH agg_comments AS (+ SELECT+ idea_id,+ COUNT(CASE WHEN comments.created_on > CURRENT_DATE - INTERVAL '30 days' THEN 1 END) as recent,+ COUNT(*) as all+ FROM comments+ INNER JOIN ideas+ ON ideas.id = comments.idea_id+ WHERE ideas.tenant_id = $1+ GROUP BY idea_id+ ),+ agg_supporters AS (+ SELECT+ idea_id,+ COUNT(*) as recent+ FROM idea_supporters+ INNER JOIN ideas+ ON ideas.id = idea_supporters.idea_id+ WHERE ideas.tenant_id = $1+ AND idea_supporters.created_on > CURRENT_DATE - INTERVAL '30 days'+ GROUP BY idea_id+ )+ SELECT i.id,i.number,i.title,i.slug,i.description,i.created_on,i.supporters,- (SELECT COUNT(*) FROM comments WHERE idea_id = i.id) as comments,- (SELECT COUNT(*) FROM idea_supporters WHERE idea_id = i.id AND created_on > CURRENT_DATE - INTERVAL '30 days') AS recent_supporters,- (SELECT COUNT(*) FROM comments WHERE idea_id = i.id AND created_on > CURRENT_DATE - INTERVAL '30 days') AS recent_comments,+ COALESCE(agg_c.all, 0) as comments,+ COALESCE(agg_s.recent, 0) AS recent_supporters,+ COALESCE(agg_c.recent, 0) AS recent_comments,i.status,u.id AS user_id,u.name AS user_name,@@ -168,8 +190,12 @@ var (LEFT JOIN tags tON t.id = it.tag_id%s+ LEFT JOIN agg_comments agg_c+ ON agg_c.idea_id = i.id+ LEFT JOIN agg_supporters agg_s+ ON agg_s.idea_id = i.idWHERE %s- GROUP BY i.id, u.id, r.id, d.id`+ GROUP BY i.id, u.id, r.id, d.id, agg_c.all, agg_c.recent, agg_s.recent`)func (s *IdeaStorage) getIdeaQuery(filter string) string {@@ -285,10 +311,11 @@ func (s *IdeaStorage) Update(number int, title, description string) (*models.Ide// Add a new idea in the databasefunc (s *IdeaStorage) Add(title, description string, userID int) (*models.Idea, error) {var id int- row := s.trx.QueryRow(`INSERT INTO ideas (title, slug, number, description, tenant_id, user_id, created_on, supporters, status)- VALUES ($1, $2, (SELECT COALESCE(MAX(number), 0) + 1 FROM ideas i WHERE i.tenant_id = $4), $3, $4, $5, $6, 0, 0)- RETURNING id`, title, slug.Make(title), description, s.tenant.ID, userID, time.Now())- if err := row.Scan(&id); err != nil {+ err := s.trx.Get(&id,+ `INSERT INTO ideas (title, slug, number, description, tenant_id, user_id, created_on, supporters, status)+ VALUES ($1, $2, (SELECT COALESCE(MAX(number), 0) + 1 FROM ideas i WHERE i.tenant_id = $4), $3, $4, $5, $6, 0, 0)+ RETURNING id`, title, slug.Make(title), description, s.tenant.ID, userID, time.Now())+ if err != nil {return nil, err}@@ -303,7 +330,9 @@ func (s *IdeaStorage) AddComment(number int, content string, userID int) (int, e}var id int- if err := s.trx.QueryRow("INSERT INTO comments (idea_id, content, user_id, created_on) VALUES ($1, $2, $3, $4) RETURNING id", idea.ID, content, userID, time.Now()).Scan(&id); err != nil {+ if err := s.trx.Get(&id,+ "INSERT INTO comments (idea_id, content, user_id, created_on) VALUES ($1, $2, $3, $4) RETURNING id",+ idea.ID, content, userID, time.Now()); err != nil {return 0, err}diff --git a/app/storage/postgres/tag.go b/app/storage/postgres/tag.goindex dccaba23d..08cbb95d8 100644--- a/app/storage/postgres/tag.go+++ b/app/storage/postgres/tag.go@@ -56,11 +56,11 @@ func (s *TagStorage) Add(name, color string, isPublic bool) (*models.Tag, error)tagSlug := slug.Make(name)var id int- row := s.trx.QueryRow(`+ err := s.trx.Get(&id, `INSERT INTO tags (name, slug, color, is_public, created_on, tenant_id)VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`, name, tagSlug, color, isPublic, time.Now(), s.tenant.ID)- if err := row.Scan(&id); err != nil {+ if err != nil {return nil, err}diff --git a/app/storage/postgres/tenant.go b/app/storage/postgres/tenant.goindex 8692e2c55..ec435046b 100644--- a/app/storage/postgres/tenant.go+++ b/app/storage/postgres/tenant.go@@ -94,10 +94,11 @@ func (s *TenantStorage) SetCurrentUser(user *models.User) {// Add given tenant to tenant listfunc (s *TenantStorage) Add(name string, subdomain string, status int) (*models.Tenant, error) {var id int- row := s.trx.QueryRow(`INSERT INTO tenants (name, subdomain, created_on, cname, invitation, welcome_message, status)- VALUES ($1, $2, $3, '', '', '', $4)- RETURNING id`, name, subdomain, time.Now(), status)- if err := row.Scan(&id); err != nil {+ err := s.trx.Get(&id,+ `INSERT INTO tenants (name, subdomain, created_on, cname, invitation, welcome_message, status)+ VALUES ($1, $2, $3, '', '', '', $4)+ RETURNING id`, name, subdomain, time.Now(), status)+ if err != nil {return nil, err}diff --git a/app/storage/postgres/user.go b/app/storage/postgres/user.goindex fdaa9d37d..9a6a7fdf0 100644--- a/app/storage/postgres/user.go+++ b/app/storage/postgres/user.go@@ -94,7 +94,9 @@ func (s *UserStorage) GetByProvider(tenantID int, provider string, uid string) (func (s *UserStorage) Register(user *models.User) error {now := time.Now()user.Email = strings.TrimSpace(user.Email)- if err := s.trx.QueryRow("INSERT INTO users (name, email, created_on, tenant_id, role) VALUES ($1, $2, $3, $4, $5) RETURNING id", user.Name, user.Email, now, user.Tenant.ID, user.Role).Scan(&user.ID); err != nil {+ if err := s.trx.Get(&user.ID,+ "INSERT INTO users (name, email, created_on, tenant_id, role) VALUES ($1, $2, $3, $4, $5) RETURNING id",+ user.Name, user.Email, now, user.Tenant.ID, user.Role); err != nil {return err}diff --git a/app/storage/postgres/user_test.go b/app/storage/postgres/user_test.goindex fcd401456..983faad3f 100644--- a/app/storage/postgres/user_test.go+++ b/app/storage/postgres/user_test.go@@ -137,11 +137,11 @@ func TestUserStorage_Register_MultipleProviders(t *testing.T) {defer TeardownDatabaseTest()var tenantID int- trx.QueryRow(`+ trx.Get(&tenantID, `INSERT INTO tenants (name, subdomain, created_on, status)VALUES ('My Domain Inc.','mydomain', now(), 1)RETURNING id- `).Scan(&tenantID)+ `)user := &models.User{Name: "Jon Snow",