gosora/common/counters/systems.go

58 lines
1.4 KiB
Go
Raw Permalink Normal View History

package counters
import (
"database/sql"
2020-02-25 08:12:54 +00:00
"sync/atomic"
2022-02-21 03:53:13 +00:00
c "git.tuxpa.in/a/gosora/common"
qgen "git.tuxpa.in/a/gosora/query_gen"
"github.com/pkg/errors"
)
var OSViewCounter *DefaultOSViewCounter
type DefaultOSViewCounter struct {
2020-02-25 08:12:54 +00:00
buckets []int64 //[OSID]count
Added support for phrases in templates. The language of the end-user is now tracked and presented in the Analytics Manager. Profile owners now get alerts when someone posts on their profiles. The login page is now transpiled, estimated to be sixty times faster. The registration page is now transpiled, estimated to be sixty times faster. The IP Search page is now transpiled, estimated to be sixty times faster. The error pages are now transpiled, estimated to be sixty times faster. The login page now uses phrases. The registration page now uses phrases. IP Search now uses phrases. Renamed the ip-search template to ip_search. Alerts are now held in an alertbox container div. Added ids for the main container divs for the account manager sections. Added an id to the main container for the topic list template. Added an id to the main container for the forum list template. Added an id to the main container for the forum template. Added an avatar box CSS class for the avatar box in the account manager's avatar page. Did a bit of renaming for a future refactor in the routes counter. Did a bit of renaming for a future refactor in the operating system counter. A notice is shown to the user now when their account is inactive. The account activation status is now fetched by the user store. We now track Slackbot. You can now prepend strings to the start of router.DumpRequest request dumps to avoid tearing these bits of contextual data away from the bodies. .action file extensions are now seen as suspicious by the router. Moved routeWebsockets to common.RouteWebsockets for now. Moved routeCreateReplySubmit to routes.CreateReplySubmit. Moved alert.go into common. Moved the WebSockets logic into common. Escape strings a little earlier in the analytics routes and use integers instead of strings where possible. We now show a success notification when you update a user via the User Manager. Split the configuration properties off from CTemplateSet into CTemplateConfig. Renamed some of the properties of CTemplateSet to make them easier to understand. Removed some obsolete properties from CTemplateSet. Did a bit of spring cleaning in the template transpiler to cut down on unneccessary lines and to reduce duplication. Fixed a double else bug in ranges over maps in the template transpiler. Split the minifiers off the main template transpilation file into their own file. Refactored some of the routes which rely on alerts to use shared functions rather than having unique implementations in the routes themselves. All Themes Except Cosora: Refactored the opt nodes to make it easier to roll out bulk moderation. Shadow: Improved the notice CSS. Tweaked the sticky border colour. Cosora: The theme JS file now uses strict mode. Notices are shunted under rowhead with JS now, although this change might be reverted soon. Added CSS for notices. Fixed the padding under the avatar box in the account manager avatar page. Schema: Added the viewchunks_langs table.
2018-03-08 03:59:47 +00:00
insert *sql.Stmt
}
func NewDefaultOSViewCounter(acc *qgen.Accumulator) (*DefaultOSViewCounter, error) {
co := &DefaultOSViewCounter{
2020-02-25 08:12:54 +00:00
buckets: make([]int64, len(osMapEnum)),
insert: acc.Insert("viewchunks_systems").Columns("count,createdAt,system").Fields("?,UTC_TIMESTAMP(),?").Prepare(),
}
c.Tasks.FifteenMin.Add(co.Tick)
//c.Tasks.Sec.Add(co.Tick)
c.Tasks.Shutdown.Add(co.Tick)
return co, acc.FirstError()
}
func (co *DefaultOSViewCounter) Tick() error {
2020-02-25 08:12:54 +00:00
for id, _ := range co.buckets {
count := atomic.SwapInt64(&co.buckets[id], 0)
if e := co.insertChunk(count, id); e != nil { // TODO: Bulk insert for speed?
return errors.Wrap(errors.WithStack(e), "system counter")
}
}
return nil
}
2020-02-25 08:12:54 +00:00
func (co *DefaultOSViewCounter) insertChunk(count int64, os int) error {
if count == 0 {
return nil
}
osName := reverseOSMapEnum[os]
c.DebugLogf("Inserting a vchunk with a count of %d for OS %s (%d)", count, osName, os)
_, err := co.insert.Exec(count, osName)
return err
}
func (co *DefaultOSViewCounter) Bump(id int) {
// TODO: Test this check
c.DebugDetail("bucket ", id, ": ", co.buckets[id])
if len(co.buckets) <= id || id < 0 {
return
}
2020-02-25 08:12:54 +00:00
atomic.AddInt64(&co.buckets[id], 1)
}