Skip to content

Commit

Permalink
fix access stats, adjust 301 to 308
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyale committed Sep 4, 2021
1 parent 26da4f6 commit 6bac7d1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
8 changes: 8 additions & 0 deletions backend/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down
13 changes: 12 additions & 1 deletion data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
}
37 changes: 22 additions & 15 deletions data/gateway_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion release_batch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6bac7d1

Please sign in to comment.