This is an automated email from the git hooks/post-receive script. New commit to branch bow-v2-go in repository bow. See https://gitlab.nuiton.org/chorem/bow.git commit 2a5de390ea5510293423a35c59f2b16cf0072a53 Author: Benjamin <poussin@codelutin.com> Date: Thu Jun 4 22:09:09 2020 +0200 correction des transactions (on utilisait pas la tx :() ajout de fonction de log --- doc/stats.md | 62 ++++++++++++++++++++++++++++++++++++ pkg/constant/const.go | 4 +++ pkg/http/bookmarkResource.go | 2 +- pkg/http/router.go | 9 ++++-- pkg/repository/bookmarkRepository.go | 7 ++-- pkg/repository/database.go | 41 ++++++++++++------------ pkg/utils/log.go | 24 ++++++++++++++ 7 files changed, 120 insertions(+), 29 deletions(-) diff --git a/doc/stats.md b/doc/stats.md new file mode 100644 index 0000000..15e9eb8 --- /dev/null +++ b/doc/stats.md @@ -0,0 +1,62 @@ +authentication: + call: 1199 + min: 1.209914ms + max: 60.005541ms + avg: 8.45436ms + StdDeviation: 8.257614ms + +rest(GET /api/v1/bookmarks) # recherche par tag, ou fulltext + call: 231 + min: 2.802321ms + max: 278.448075ms + avg: 28.181521ms + StdDeviationHumain: 27.642803ms + +rest(GET /api/v1/bookmarks/tags) # recherche des tags pour aide a la completion + call: 114 + min: 35.417473ms + max: 133.779361ms + avg: 81.789094ms + stddeviation: 18.547815ms + +rest(GET /api/v1/bookmarks/{id}) # recherche d'un bookmark pour edition + call: 11 + min: 2.570579ms + max: 17.102177ms + avg: 7.007831ms + stddeviation: 4.660664ms + +rest(GET /api/v1/bookmarks/{id}/visit) # increment du compteur de clique sur un lien et retourne le vrai lien + call: 6 + min: 7.027394ms + max: 12.335118ms + avg: 10.369097ms + stddeviation: 1.908622ms + +rest(GET /api/v1/opensearch) # saisie dans la barre du navigateur (suggesion ou action) + call: 731 + min: 68.143µs + max: 330.865903ms + avg: 41.814832ms + stddeviation: 50.15556ms + +rest(GET /api/v1/users/{id}) # recup d'un utilisateur pour modification des préférences + call: 4 + min: 4.280183ms + max: 17.300764ms + avg: 8.182765ms + stddeviation: 6.119834ms + +rest(POST /api/v1/bookmarks) # creation d'un bookmark + call: 90 + min: 6.69895ms + max: 668.551172ms + avg: 25.003866ms + stddeviation: 73.254212ms + +rest(PUT /api/v1/bookmarks/{id}) # modification d'un bookmark + call: 11 + min: 6.3507ms + max: 625.336368ms + avg: 67.524142ms + stddeviation: 185.030551ms diff --git a/pkg/constant/const.go b/pkg/constant/const.go index 31bf1b6..51ceb3d 100644 --- a/pkg/constant/const.go +++ b/pkg/constant/const.go @@ -1,9 +1,13 @@ package constant import ( + "os" + "gitlab.chorem.org/chorem/bow/pkg/model" ) +var Debug = os.Getenv("BOW_DEBUG") == "true" + type httpRequestContexttKey string /* diff --git a/pkg/http/bookmarkResource.go b/pkg/http/bookmarkResource.go index 9738e9b..d0f07ce 100644 --- a/pkg/http/bookmarkResource.go +++ b/pkg/http/bookmarkResource.go @@ -23,7 +23,7 @@ func getBookmarks(w http.ResponseWriter, r *http.Request) { tags := queryParams[constant.Tags] fulltext := queryParams.Get(constant.Fulltext) - log.Println("query: ", queryParams) + utils.LogDebug("getBookmarks query: ", queryParams) orderBy := queryParams.Get(constant.OrderBy) orderAsc, err := strconv.ParseBool(queryParams.Get(constant.OrderAsc)) if err != nil { diff --git a/pkg/http/router.go b/pkg/http/router.go index 1bccfbd..f1299cc 100644 --- a/pkg/http/router.go +++ b/pkg/http/router.go @@ -95,10 +95,15 @@ func handler404(w http.ResponseWriter, r *http.Request) { func logAll(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL.Path) + query := r.URL.Query() debug := query.Get("bow_debug") + if debug != "" { + constant.Debug = debug == "true" + } - if os.Getenv("BOW_DEBUG") == "true" || debug == "true" { + if constant.Debug { log.Println("logAll", r) } next.ServeHTTP(w, r) @@ -111,7 +116,6 @@ func cors(next http.Handler) http.Handler { w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) w.Header().Set("Access-Control-Allow-Credentials", "true") if r.Method == http.MethodOptions { - log.Println("OPTION request") w.Header().Set("Access-Control-Allow-Methods", r.Header.Get("Access-Control-Request-Method")) if r.Header.Get("Access-Control-Request-Headers") != "" { w.Header().Set("Access-Control-Allow-Headers", strings.Join(r.Header.Values("Access-Control-Request-Headers"), ",")) @@ -270,7 +274,6 @@ type spaHandler struct { // file located at the index path on the SPA handler will be served. This // is suitable behavior for serving an SPA (single page application). func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - log.Println("serve web content", r.URL.Path) // get the absolute path to prevent directory traversal path, err := filepath.Abs(r.URL.Path) if err != nil { diff --git a/pkg/repository/bookmarkRepository.go b/pkg/repository/bookmarkRepository.go index 1993a24..2d67b37 100644 --- a/pkg/repository/bookmarkRepository.go +++ b/pkg/repository/bookmarkRepository.go @@ -3,7 +3,6 @@ package repository import ( "context" "fmt" - "log" "strings" "time" @@ -41,7 +40,7 @@ func BookmarkJSON(currentUser model.BowUser, id string, uri string, tags []strin tagsJoined := strings.Join(tags, ",") - log.Printf("search bookmark id: %v, uri: %v, tags: '%v', fulltext: '%v', orderBy: %v, orderAsc: %v, first:%v", id, uri, tags, fulltext, orderBy, orderAsc, first) + utils.LogDebug("search bookmark id: %v, uri: %v, tags: '%v', fulltext: '%v', orderBy: %v, orderAsc: %v, first:%v", id, uri, tags, fulltext, orderBy, orderAsc, first) var mainQuery string if id != "" { mainQuery = `select * from bookmark where id=$4` @@ -235,7 +234,7 @@ func CreateBookmark(currentUser model.BowUser, bookmark model.Bookmark) (string, return "", utils.NewHTTPError500(err, currentUser) } - log.Printf("Creation du bookmark %s\n", bookmarkAsJSON) + utils.LogDebug("Creation du bookmark %s\n", bookmarkAsJSON) q := &query{sql: `INSERT INTO bookmark AS t SELECT * FROM json_populate_record(NULL::bookmark, $1::json);`} err = q.execOnOneRow(currentUser, string(bookmarkAsJSON)) if err != nil { @@ -254,7 +253,7 @@ func UpdateBookmark(currentUser model.BowUser, bookmark model.Bookmark) error { return utils.NewHTTPError500(err, currentUser) } - log.Println("Update bookmark", bookmarkAsJSON) + utils.LogDebug("Update bookmark", bookmarkAsJSON) q := &query{sql: ` UPDATE bookmark AS t SET (uri, description, tags, privateAlias, publicAlias, lang) = diff --git a/pkg/repository/database.go b/pkg/repository/database.go index bd92d43..4f30f0d 100644 --- a/pkg/repository/database.go +++ b/pkg/repository/database.go @@ -129,7 +129,7 @@ func migrateDatabase(databaseURL string, migrateDir string) { } migrator.OnStart = func(sequence int32, name, direction, sql string) { - log.Printf("%s executing %s %s\n%s\n\n", time.Now().Format("2006-01-02 15:04:05"), name, direction, sql) + utils.Log("%s executing %s %s\n%s\n\n", time.Now().Format("2006-01-02 15:04:05"), name, direction, sql) } var currentVersion int32 @@ -137,7 +137,7 @@ func migrateDatabase(databaseURL string, migrateDir string) { if err != nil { log.Fatalf("Unable to get current version:\n %v\n", err) } - log.Printf("Migrate database from %d to last version\n", currentVersion) + utils.Log("Migrate database from %d to last version\n", currentVersion) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -152,11 +152,11 @@ func migrateDatabase(databaseURL string, migrateDir string) { err = migrator.Migrate(ctx) if err != nil { - log.Println(err) + utils.Log("%v", err) if err, ok := err.(migrate.MigrationPgError); ok { if err.Detail != "" { - log.Println("DETAIL:", err.Detail) + utils.Log("DETAIL:", err.Detail) } if err.Position != 0 { @@ -166,10 +166,10 @@ func migrateDatabase(databaseURL string, migrateDir string) { } prefix := fmt.Sprintf("LINE %d: ", ele.LineNum) - log.Printf("%s%s\n", prefix, ele.Text) + utils.Log("%s%s\n", prefix, ele.Text) padding := strings.Repeat(" ", len(prefix)+ele.ColumnNum-1) - log.Printf("%s^\n", padding) + utils.Log("%s^\n", padding) } } os.Exit(1) @@ -216,14 +216,14 @@ func (q *query) exec(currentUser model.BowUser, minRow int64, maxRow int64, argu defer tx.Rollback(context.Background()) if q.presql != "" { - _, err = db.Exec(context.Background(), q.presql, pgx.QuerySimpleProtocol(true)) + _, err = tx.Exec(context.Background(), q.presql, pgx.QuerySimpleProtocol(true)) if err != nil { return 0, utils.NewHTTPError500(err, currentUser) } } if !q.asNobody { - _, err = db.Exec(context.Background(), fmt.Sprintf(` + _, err = tx.Exec(context.Background(), fmt.Sprintf(` SET ROLE "%[1]s"; `, currentUser.ID)) if err != nil { @@ -231,7 +231,7 @@ func (q *query) exec(currentUser model.BowUser, minRow int64, maxRow int64, argu } } - modif, err := db.Exec(context.Background(), q.sql, arguments...) + modif, err := tx.Exec(context.Background(), q.sql, arguments...) if err != nil { return 0, utils.NewHTTPError500(err, currentUser) @@ -248,7 +248,7 @@ func (q *query) exec(currentUser model.BowUser, minRow int64, maxRow int64, argu } if q.postsql != "" { - _, err = db.Exec(context.Background(), q.postsql, pgx.QuerySimpleProtocol(true)) + _, err = tx.Exec(context.Background(), q.postsql, pgx.QuerySimpleProtocol(true)) if err != nil { return 0, utils.NewHTTPError500(err, currentUser) } @@ -266,8 +266,7 @@ func (q *query) exec(currentUser model.BowUser, minRow int64, maxRow int64, argu QueryString fait une requete qui ne retourne qu'une seul ligne et retourne du string (qui peut être du json) */ func (q *query) QueryString(currentUser model.BowUser, arguments ...interface{}) (string, error) { - txid, _ := utils.GenUUID() - log.Println(txid, "[DB] begin tx") + utils.LogDebug("[DB] begin tx") tx, err := db.Begin(context.Background()) if err != nil { return "", utils.NewHTTPError500(err, currentUser) @@ -275,23 +274,23 @@ func (q *query) QueryString(currentUser model.BowUser, arguments ...interface{}) defer tx.Rollback(context.Background()) if q.presql != "" { - log.Println(txid, "[DB] exec pre query", q.presql) - _, err = db.Exec(context.Background(), q.presql, pgx.QuerySimpleProtocol(true)) + utils.LogDebug("[DB] exec pre query %v", q.presql) + _, err = tx.Exec(context.Background(), q.presql, pgx.QuerySimpleProtocol(true)) if err != nil { return "", utils.NewHTTPError500(err, currentUser) } } if !q.asNobody { - log.Println(txid, "[DB] change role to", currentUser.ID) - _, err = db.Exec(context.Background(), fmt.Sprintf(`SET ROLE "%[1]s";`, currentUser.ID)) + utils.LogDebug("[DB] change role to %v", currentUser.ID) + _, err = tx.Exec(context.Background(), fmt.Sprintf(`SET ROLE "%[1]s";`, currentUser.ID)) if err != nil { return "", utils.NewHTTPError500(err, currentUser) } } - log.Println(txid, "[DB] exec query", q.sql) - row := db.QueryRow(context.Background(), q.sql, arguments...) + utils.LogDebug("[DB] exec query %v", q.sql) + row := tx.QueryRow(context.Background(), q.sql, arguments...) var pgjson pgtype.JSON err = row.Scan(&pgjson) @@ -300,14 +299,14 @@ func (q *query) QueryString(currentUser model.BowUser, arguments ...interface{}) } if q.postsql != "" { - log.Println(txid, "[DB] exec post query", q.postsql) - _, err = db.Exec(context.Background(), q.postsql, pgx.QuerySimpleProtocol(true)) + utils.LogDebug("[DB] exec post query %v", q.postsql) + _, err = tx.Exec(context.Background(), q.postsql, pgx.QuerySimpleProtocol(true)) if err != nil { return "", utils.NewHTTPError500(err, currentUser) } } - log.Println(txid, "[DB] commit tx") + utils.LogDebug("[DB] commit tx") err = tx.Commit(context.Background()) if err != nil { return "", utils.NewHTTPError500(err, currentUser) diff --git a/pkg/utils/log.go b/pkg/utils/log.go new file mode 100644 index 0000000..42fe58e --- /dev/null +++ b/pkg/utils/log.go @@ -0,0 +1,24 @@ +package utils + +import ( + "log" + + "gitlab.chorem.org/chorem/bow/pkg/constant" +) + +/* +LogDebug log if debug level +*/ +func LogDebug(msg string, a ...interface{}) { + if constant.Debug { + log.Printf(msg, a...) + } +} + +/* +Log log all time +*/ +func Log(msg string, a ...interface{}) { + log.Printf(msg, a...) +} + -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.