From 6bac7d1290ebab2c32f56f2de7304a1d76188803 Mon Sep 17 00:00:00 2001 From: zhyale Date: Sat, 4 Sep 2021 21:55:12 +0800 Subject: [PATCH] fix access stats, adjust 301 to 308 --- backend/init.go | 8 ++++++++ data/data.go | 13 ++++++++++++- data/gateway_stat.go | 37 ++++++++++++++++++++++--------------- release_batch.sh | 2 +- 4 files changed, 43 insertions(+), 17 deletions(-) diff --git a/backend/init.go b/backend/init.go index 3219156..522d674 100644 --- a/backend/init.go +++ b/backend/init.go @@ -130,6 +130,14 @@ func InitDatabase() { utils.DebugPrintln("InitDatabase ALTER TABLE applications add shield_enabled", err) } } + + // v1.2.4 add constraint to access_stats + if !dal.ExistConstraint("access_stats", "stat_id") { + err = dal.ExecSQL(`ALTER TABLE "access_stats" ADD CONSTRAINT "stat_id" unique ("app_id","url_path","stat_date")`) + if err != nil { + //utils.DebugPrintln("InitDatabase ALTER TABLE access_stats add constraint", err) + } + } } // LoadAppConfiguration ... diff --git a/data/data.go b/data/data.go index 003dbe0..e295d62 100644 --- a/data/data.go +++ b/data/data.go @@ -33,7 +33,7 @@ var ( // IsPrimary i.e. Is Primary Node IsPrimary bool // Version of JANUSEC - Version = "1.2.3" + Version = "1.2.4" // NodeKey share with all nodes NodeKey []byte ) @@ -97,3 +97,14 @@ func (dal *MyDAL) ExistColumnInTable(tableName string, columnName string) bool { } return count > 0 } + +// ExistConstraint ... +func (dal *MyDAL) ExistConstraint(tableName string, constraintName string) bool { + var count int64 + const sql = `SELECT count(1) FROM information_schema.constraint_column_usage WHERE table_name=$1 and constraint_name=$2` + err := dal.db.QueryRow(sql, tableName, constraintName).Scan(&count) + if err != nil { + utils.DebugPrintln("ExistConstraint QueryRow", err) + } + return count > 0 +} \ No newline at end of file diff --git a/data/gateway_stat.go b/data/gateway_stat.go index 4f45095..0fdce62 100644 --- a/data/gateway_stat.go +++ b/data/gateway_stat.go @@ -14,33 +14,40 @@ import ( // CreateTableIfNotExistsAccessStats create statistics table func (dal *MyDAL) CreateTableIfNotExistsAccessStats() error { - const sqlCreateTableIfNotExistsStats = `CREATE TABLE IF NOT EXISTS "access_stats"("id" bigserial PRIMARY KEY, "app_id" bigint, "url_path" VARCHAR(256) NOT NULL, "stat_date" VARCHAR(16) NOT NULL, "amount" bigint, "update_time" bigint)` + const sqlCreateTableIfNotExistsStats = `CREATE TABLE IF NOT EXISTS "access_stats"("id" bigserial PRIMARY KEY, "app_id" bigint, "url_path" VARCHAR(256) NOT NULL, "stat_date" VARCHAR(16) NOT NULL, "amount" bigint, "update_time" bigint, CONSTRAINT "stat_id" unique ("app_id","url_path","stat_date"))` _, err := dal.db.Exec(sqlCreateTableIfNotExistsStats) return err } // IncAmount update access statistics func (dal *MyDAL) IncAmount(appID int64, urlPath string, statDate string, delta int64, updateTime int64) error { - var id, amount int64 + //var id, amount int64 if len(urlPath) > 255 { urlPath = urlPath[0:255] } - const sql = `select "id","amount" from "access_stats" where "app_id"=$1 and "url_path"=$2 and "stat_date"=$3 LIMIT 1` - err := dal.db.QueryRow(sql, appID, urlPath, statDate).Scan(&id, &amount) + const sql = `INSERT INTO "access_stats"("app_id","url_path","stat_date","amount","update_time") VALUES($1,$2,$3,$4,$5) ON CONFLICT ("app_id","url_path","stat_date") DO UPDATE SET "amount"="access_stats"."amount"+$4,"update_time"=$5` + _, err := dal.db.Exec(sql, appID, urlPath, statDate, delta, updateTime) if err != nil { - // Not existed before - const sqlInsert = `INSERT INTO "access_stats"("app_id","url_path","stat_date","amount","update_time") VALUES($1,$2,$3,$4,$5)` - _, err = dal.db.Exec(sqlInsert, appID, urlPath, statDate, delta, updateTime) + utils.DebugPrintln("IncAmount insert", err) + } + /* + const sql = `select "id","amount" from "access_stats" where "app_id"=$1 and "url_path"=$2 and "stat_date"=$3 LIMIT 1` + err := dal.db.QueryRow(sql, appID, urlPath, statDate).Scan(&id, &amount) if err != nil { - utils.DebugPrintln("IncAmount insert", err) + // Not existed before + const sqlInsert = `INSERT INTO "access_stats"("app_id","url_path","stat_date","amount","update_time") VALUES($1,$2,$3,$4,$5)` + _, err = dal.db.Exec(sqlInsert, appID, urlPath, statDate, delta, updateTime) + if err != nil { + utils.DebugPrintln("IncAmount insert", err) + } + return err } - return err - } - const sqlUpdate = `UPDATE "access_stats" SET "amount"=$1,"update_time"=$2 WHERE "id"=$3` - _, err = dal.db.Exec(sqlUpdate, amount+delta, updateTime, id) - if err != nil { - utils.DebugPrintln("IncAmount update", err) - } + const sqlUpdate = `UPDATE "access_stats" SET "amount"=$1,"update_time"=$2 WHERE "id"=$3` + _, err = dal.db.Exec(sqlUpdate, amount+delta, updateTime, id) + if err != nil { + utils.DebugPrintln("IncAmount update", err) + } + */ return err } diff --git a/release_batch.sh b/release_batch.sh index 36a90cd..0a34b95 100755 --- a/release_batch.sh +++ b/release_batch.sh @@ -2,7 +2,7 @@ printf "Creating installation package\n" printf "Checklist:\n" printf "* Angular Admin Version Check. \n" printf "* Janusec Version Check. \n" -version="1.2.3" +version="1.2.4" printf "Version: ${version} \n" read -r -p "Are You Sure? [Y/n] " option