github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libpages/stats.go (about) 1 // Copyright 2018 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package libpages 6 7 import ( 8 "errors" 9 "time" 10 11 "github.com/keybase/client/go/kbfs/tlf" 12 ) 13 14 // ActivesGetter holds necessary data to generate number of active TLFs or 15 // hosts. 16 type ActivesGetter interface { 17 // GetActives returns the number of active TLFs and active hosts in the 18 // past dur. 19 GetActives(dur time.Duration) (activeTlfs int, activeHosts int, err error) 20 } 21 22 // ActivityStatsStorer defines a set of methods to record activities based on 23 // TLF ID and host names. 24 type ActivityStatsStorer interface { 25 // RecordActives records that tlfID and host has just been active. 26 RecordActives(tlfID tlf.ID, host string) 27 28 // GetActivesGetter returns a ActivesGetter from current state of the 29 // ActivityStatsStorer. 30 GetActivesGetter() (ActivesGetter, error) 31 } 32 33 // NameableDuration is a wrapper around time.Duration that allows customized 34 // String() encoding. 35 type NameableDuration struct { 36 Duration time.Duration 37 Name string 38 } 39 40 // String returns d.Name if it's not empty, or d.Duration.String(). 41 func (d NameableDuration) String() string { 42 if len(d.Name) > 0 { 43 return d.Name 44 } 45 return d.Duration.String() 46 } 47 48 // ActivityStatsEnabler describes what backend storer a StatsReporter should 49 // use for activity-based stats, and how the stats should be generated. 50 type ActivityStatsEnabler struct { 51 // Storer specifies a backend storer that a StatsReporter should use to 52 // store data necessary for generating activity-based stats. 53 Storer ActivityStatsStorer 54 // Durations specifies a slice of durations that activity-based stats 55 // should be about. For example, [1h, 1d, 1week] makes the StatsReporter 56 // should report hourly, daily, and weekly active stats. 57 Durations []NameableDuration 58 // Interval specifies how often the activity-based stats should be 59 // reported. 60 Interval time.Duration 61 } 62 63 // StatsReporter defines a collection of methods for stats reporting. 64 type StatsReporter interface { 65 // ReportServedRequest is called by libpages whenever a request comes in. 66 ReportServedRequest(r *ServedRequestInfo) 67 } 68 69 type multiStatReporter []StatsReporter 70 71 var _ StatsReporter = multiStatReporter(nil) 72 73 // NewMultiStatReporter creates a StatsReporter that reports through all passed 74 // in reporters. 75 func NewMultiStatReporter(reporters ...StatsReporter) StatsReporter { 76 return multiStatReporter(reporters) 77 } 78 79 // ReportServedRequest implements the StatsReporter interface. 80 func (m multiStatReporter) ReportServedRequest( 81 r *ServedRequestInfo) { 82 for _, reporter := range m { 83 reporter.ReportServedRequest(r) 84 } 85 } 86 87 type nullActivityStatsStorer struct{} 88 89 var _ ActivityStatsStorer = nullActivityStatsStorer{} 90 91 // RecordActives (does not) implement the ActivityStatsStorer interface. 92 func (nullActivityStatsStorer) RecordActives(tlf.ID, string) {} 93 94 // GetActiveTlfs (does not) implement the ActivityStatsStorer interface. 95 func (nullActivityStatsStorer) GetActivesGetter() (ActivesGetter, error) { 96 return nil, errors.New("not supported") 97 }