aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2020-03-05 11:45:23 +0000
committerFelix Hanley <felix@userspace.com.au>2020-03-05 11:45:23 +0000
commita1d9e95decc1fc51c5aaf56b7675f233eadaac7d (patch)
tree2fada4344765f8c1284f0a15dbeac96e84d4cd4c
parent5e4cb1854e7dbdc0da2e892a609b7461fe7100fe (diff)
downloadsws-a1d9e95decc1fc51c5aaf56b7675f233eadaac7d.tar.gz
sws-a1d9e95decc1fc51c5aaf56b7675f233eadaac7d.tar.bz2
Fix referrer from self
-rw-r--r--referrer.go101
1 files changed, 101 insertions, 0 deletions
diff --git a/referrer.go b/referrer.go
new file mode 100644
index 0000000..d531cd8
--- /dev/null
+++ b/referrer.go
@@ -0,0 +1,101 @@
+package sws
+
+import (
+ "net/url"
+ "sort"
+ "strings"
+ "time"
+)
+
+type Referrer struct {
+ Name string `json:"name"`
+ LastSeenAt time.Time `json:"last_seen_at" db:"last_seen_at"`
+ hitSet *HitSet
+}
+
+type ReferrerSet []*Referrer
+
+func NewReferrerSet(hs *HitSet) ReferrerSet {
+ tmp := make(map[string]*Referrer)
+ for _, h := range hs.Hits() {
+ if h.Referrer == nil {
+ continue
+ }
+
+ u, err := url.Parse(*h.Referrer)
+ if err != nil || h.Host == u.Host {
+ continue
+ }
+ r := &Referrer{
+ Name: u.Host,
+ LastSeenAt: h.CreatedAt,
+ hitSet: hs.Filter(func(t *Hit) bool {
+ if t.Referrer == nil {
+ return false
+ }
+ return strings.Contains(*t.Referrer, u.Host)
+ }),
+ }
+ // if b.LastSeenAt.Before(h.CreatedAt) {
+ // b.LastSeenAt = h.CreatedAt
+ // }
+ //b.hitSet.Add(h)
+ tmp[u.Host] = r
+ }
+ if len(tmp) < 1 {
+ return nil
+ }
+ out := make([]*Referrer, len(tmp))
+ i := 0
+ for _, b := range tmp {
+ out[i] = b
+ i++
+ }
+ return ReferrerSet(out)
+}
+
+func (rs *ReferrerSet) SortByName() {
+ sort.Slice(*rs, func(i, j int) bool {
+ return (*rs)[i].Name < (*rs)[j].Name
+ })
+}
+
+func (rs *ReferrerSet) SortByHits() {
+ sort.Slice(*rs, func(i, j int) bool {
+ return (*rs)[i].hitSet.Count() > (*rs)[j].hitSet.Count()
+ })
+}
+
+func (rs ReferrerSet) GetReferrer(s string) *Referrer {
+ for _, r := range rs {
+ if r.Name == s {
+ return r
+ }
+ }
+ return nil
+}
+
+func (r Referrer) Label() string {
+ return r.Name
+}
+
+func (r Referrer) Count() int {
+ return r.hitSet.Count()
+}
+
+func (r Referrer) YValue() int {
+ return r.hitSet.Count()
+}
+
+func (rs ReferrerSet) YMax() int {
+ max := 0
+ for _, r := range rs {
+ if r.hitSet.Count() > max {
+ max = r.hitSet.Count()
+ }
+ }
+ return max
+}
+func (rs ReferrerSet) XSeries() []*Referrer {
+ return rs
+}