activity_score.go 1010 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package page_writer
  2. import "time"
  3. type ActivityScore struct {
  4. lastActiveTsNs int64
  5. decayedActivenessScore int64
  6. }
  7. func NewActivityScore() *ActivityScore {
  8. return &ActivityScore{}
  9. }
  10. func (as ActivityScore) MarkRead() {
  11. now := time.Now().UnixNano()
  12. deltaTime := (now - as.lastActiveTsNs) >> 30 // about number of seconds
  13. as.lastActiveTsNs = now
  14. as.decayedActivenessScore = as.decayedActivenessScore>>deltaTime + 256
  15. if as.decayedActivenessScore < 0 {
  16. as.decayedActivenessScore = 0
  17. }
  18. }
  19. func (as ActivityScore) MarkWrite() {
  20. now := time.Now().UnixNano()
  21. deltaTime := (now - as.lastActiveTsNs) >> 30 // about number of seconds
  22. as.lastActiveTsNs = now
  23. as.decayedActivenessScore = as.decayedActivenessScore>>deltaTime + 1024
  24. if as.decayedActivenessScore < 0 {
  25. as.decayedActivenessScore = 0
  26. }
  27. }
  28. func (as ActivityScore) ActivityScore() int64 {
  29. deltaTime := (time.Now().UnixNano() - as.lastActiveTsNs) >> 30 // about number of seconds
  30. return as.decayedActivenessScore >> deltaTime
  31. }