-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
analytics feature #14
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
663fc3c
feat(wip): implement analytics
86555b5
chore: merge upstream
9bbc596
refactor: cleaner code with domain-esque approach
9ca488c
Update pr.yml
38e93ad
docs: create documentation for cmd package
6cf846a
feat: working analytics
19e662f
test: added test for utils
9a0b023
test: add analytics pkg tests
c526e51
fix: removing length for userCmd map
6f391a7
test: add complete analytics pkg tests
faf572b
test: fix test cases
3182ca0
chore: securing required environment variables
88a3b52
test: missing flush all users id
b0249a0
chore: cert url for database
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
ENVIRONMENT= | ||
BOT_TOKEN= | ||
SENTRY_DSN= | ||
REDIS_URL= | ||
DATABASE_URL= | ||
TZ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package analytics | ||
|
||
// On this package we have 2 main keys on redis: | ||
// analytics:hour and analytics:counter | ||
|
||
import ( | ||
"github.com/allegro/bigcache/v3" | ||
"github.com/bsm/redislock" | ||
"github.com/getsentry/sentry-go" | ||
"github.com/go-redis/redis/v8" | ||
"github.com/jmoiron/sqlx" | ||
tb "gopkg.in/tucnak/telebot.v2" | ||
) | ||
|
||
type Dependency struct { | ||
Memory *bigcache.BigCache | ||
Redis *redis.Client | ||
Locker *redislock.Client | ||
Bot *tb.Bot | ||
Logger *sentry.Client | ||
DB *sqlx.DB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package analytics_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"log" | ||
"os" | ||
"teknologi-umum-bot/analytics" | ||
"testing" | ||
"time" | ||
|
||
"github.com/allegro/bigcache/v3" | ||
"github.com/go-redis/redis/v8" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/lib/pq" | ||
) | ||
|
||
var db *sqlx.DB | ||
var cache *redis.Client | ||
var memory *bigcache.BigCache | ||
|
||
func TestMain(m *testing.M) { | ||
Setup() | ||
|
||
defer Teardown() | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func Cleanup() { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
c, err := db.Connx(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer c.Close() | ||
|
||
tx, err := c.BeginTxx(ctx, &sql.TxOptions{}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
_, err = tx.ExecContext(ctx, "TRUNCATE TABLE analytics") | ||
if err != nil { | ||
tx.Rollback() | ||
log.Fatal(err) | ||
} | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
tx.Rollback() | ||
log.Fatal(err) | ||
} | ||
|
||
err = cache.FlushAll(ctx).Err() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = memory.Reset() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func Setup() { | ||
dbURL, err := pq.ParseURL(os.Getenv("DATABASE_URL")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
db, err = sqlx.Open("postgres", dbURL) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
redisURL, err := redis.ParseURL(os.Getenv("REDIS_URL")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
cache = redis.NewClient(redisURL) | ||
|
||
memory, err = bigcache.NewBigCache(bigcache.DefaultConfig(time.Hour * 1)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = analytics.MustMigrate(db) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func Teardown() { | ||
memory.Close() | ||
cache.Close() | ||
db.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package analytics | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/aldy505/decrr" | ||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
func (d *Dependency) IncrementUsrDB(ctx context.Context, users []UserMap) error { | ||
c, err := d.DB.Connx(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer c.Close() | ||
|
||
t, err := c.BeginTxx(ctx, &sql.TxOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, user := range users { | ||
now := time.Now() | ||
|
||
_, err = t.ExecContext( | ||
ctx, | ||
`INSERT INTO analytics | ||
(user_id, username, display_name, counter, created_at, joined_at, updated_at) | ||
VALUES | ||
($1, $2, $3, $4, $5, $6, $7) | ||
ON CONFLICT (user_id) | ||
DO UPDATE | ||
SET counter = (SELECT counter FROM analytics WHERE user_id = $1)+$4, | ||
username = $2, | ||
display_name = $3, | ||
updated_at = $7`, | ||
user.UserID, | ||
user.Username, | ||
user.DisplayName, | ||
user.Counter, | ||
now, | ||
now, | ||
now, | ||
) | ||
if err != nil { | ||
t.Rollback() | ||
return err | ||
} | ||
} | ||
|
||
err = t.Commit() | ||
if err != nil { | ||
t.Rollback() | ||
return decrr.Wrap(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *Dependency) IncrementUsrRedis(ctx context.Context, user UserMap) error { | ||
p := d.Redis.TxPipeline() | ||
defer p.Close() | ||
|
||
usrID := strconv.FormatInt(user.UserID, 10) | ||
|
||
exists, err := d.Redis.HExists(ctx, "analytics:"+usrID, "count").Result() | ||
if err != nil && !errors.Is(err, redis.Nil) { | ||
return decrr.Wrap(err) | ||
} | ||
|
||
if !exists { | ||
p.HSet( | ||
ctx, | ||
"analytics:"+usrID, | ||
"counter", | ||
0, | ||
"username", | ||
user.Username, | ||
"display_name", | ||
user.DisplayName, | ||
) | ||
} | ||
|
||
// Per Redis' documentation, INCR will create a new key | ||
// if the named key does not exists in the first place. | ||
p.HIncrBy(ctx, "analytics:"+usrID, "counter", 1) | ||
|
||
// Add the user ID into the Sets of users | ||
p.SAdd(ctx, "analytics:users", usrID) | ||
|
||
_, err = p.Exec(ctx) | ||
if err != nil { | ||
return decrr.Wrap(err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kenapa Usr bukan User?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well bcs I'm this knd of prsn