Commit 75b5177b authored by zauberstuhl's avatar zauberstuhl

Merge branch 'ap_improvements' into 'master'

AP improvements

See merge request !72
parents 1e484d85 38585a06
Pipeline #455 passed with stages
in 21 minutes and 1 second
......@@ -8,11 +8,6 @@ variables:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ""
UPDATE_CHANNEL: alpha
cache:
key: "ganggo"
paths:
- vendor
- node_modules
stages:
- test
- build
......@@ -20,17 +15,27 @@ stages:
before_script:
- export VERSION=$(git tag -l --sort=-v:refname |head -n1)
- export PROJECT_PATH=$GOPATH/src/git.feneas.org/ganggo
- mkdir -p $PROJECT_PATH
- mkdir -p $PROJECT_PATH bin
- rm -r $GOPATH/bin
- ln -s $(pwd) $PROJECT_PATH/ganggo
- ln -sf $(pwd)/bin $GOPATH/bin
- cd $PROJECT_PATH/ganggo
- make install
run unit tests:
linux:test:
artifacts:
expire_in: "1 hour"
paths:
- vendor/
- node_modules/
- bin/
stage: test
script:
- make install
- make test
except:
- master
linux:amd64:
artifacts:
expire_in: "1 day"
expire_in: "1 hour"
paths:
- updater.*.bin
stage: build
......@@ -38,9 +43,11 @@ linux:amd64:
- export GOARCH=amd64
- export GOOS=linux
- make release
dependencies:
- linux:test
windows:amd64:
artifacts:
expire_in: "1 day"
expire_in: "1 hour"
paths:
- updater.*.bin
stage: build
......@@ -51,11 +58,13 @@ windows:amd64:
- export GOARCH=amd64
- export GOOS=windows
- make release
dependencies:
- linux:test
only:
- /^v[\d\.]+-\w.*$/
windows:386:
artifacts:
expire_in: "1 day"
expire_in: "1 hour"
paths:
- updater.*.bin
stage: build
......@@ -66,11 +75,13 @@ windows:386:
- export GOARCH=386
- export GOOS=windows
- make release
dependencies:
- linux:test
only:
- /^v[\d\.]+-\w.*$/
linux:arm:7:
artifacts:
expire_in: "1 day"
expire_in: "1 hour"
paths:
- updater.*.bin
stage: build
......@@ -81,6 +92,8 @@ linux:arm:7:
- export GOOS=linux
- export GOARM=7
- make release
dependencies:
- linux:test
only:
- /^v[\d\.]+-\w.*$/
deploy artifacts to bintray:
......@@ -89,10 +102,10 @@ deploy artifacts to bintray:
stage: deploy
script:
- bash ci/scripts/bintray.sh
only:
- /^v[\d\.]+-\w.*$/
dependencies:
- linux:amd64
- windows:amd64
- windows:386
- linux:arm:7
only:
- /^v[\d\.]+-\w.*$/
......@@ -118,9 +118,9 @@ func generateSchema(version string) SchemaJson {
}
var (
user models.User
comment models.Comment
post models.Post
users models.Users
comments models.Comments
posts models.Posts
)
return SchemaJson{
......@@ -137,12 +137,12 @@ func generateSchema(version string) SchemaJson {
OpenRegistrations: true,
Usage: SchemaUsageJson{
Users: SchemaUsersJson{
Total: user.Count(),
ActiveHalfyear: user.ActiveHalfyear(),
ActiveMonth: user.ActiveMonth(),
Total: users.Count(),
ActiveHalfyear: users.ActiveHalfyear(),
ActiveMonth: users.ActiveMonth(),
},
LocalPosts: post.Count(),
LocalComments: comment.Count(),
LocalPosts: posts.CountLocal(),
LocalComments: comments.CountLocal(),
},
MetaData: SchemaMetaDataJson{
NodeName: appName,
......
......@@ -93,14 +93,11 @@ func (dispatcher *Dispatcher) findRecipients(parentPost *models.Post, parentUser
return persons, nil
}
} else if parentPost != nil {
if parentPost.Public {
// everyone we are sharing with
return dispatcher.findPublicEndpoints()
} else {
// it is not local just send it to
// the remote server it should handle the rest
// it is not local just send it to
// the remote server it should handle the rest
var persons = []models.Person{parentPost.Person}
if !parentPost.Public {
// in case of AP we will fetch known visibilties as well
var persons = []models.Person{parentPost.Person}
var visibilities models.Visibilities
err := visibilities.FindByPost(*parentPost)
if err == nil {
......@@ -110,8 +107,8 @@ func (dispatcher *Dispatcher) findRecipients(parentPost *models.Post, parentUser
} else {
revel.AppLog.Error("Dispatcher findRecipients", err.Error(), err)
}
return persons, nil
}
return persons, nil
}
return []models.Person{}, nil
}
......@@ -129,7 +126,7 @@ func (dispatcher *Dispatcher) findPublicEndpoints() (persons []models.Person, er
var person models.Person
err = person.FindFirstByPodID(pod.ID)
if err != nil {
return
continue
}
persons = append(persons, person)
}
......
......@@ -31,9 +31,6 @@ func (r *Receiver) Retraction(entity federation.MessageRetract) {
}
defer db.Close()
// relay retraction before deleting database entries!
(&Dispatcher{Message: entity}).Run()
var dbModel interface{}
switch entity.ParentType() {
case federation.Reshare:
......@@ -83,6 +80,9 @@ func (r *Receiver) Retraction(entity federation.MessageRetract) {
}
if dbModel != nil {
// relay retraction before deleting database entries!
(&Dispatcher{Message: entity}).Run()
err = db.Delete(dbModel).Error
if err != nil {
revel.AppLog.Error(err.Error())
......
......@@ -22,6 +22,8 @@ import (
"github.com/revel/revel"
"git.feneas.org/ganggo/gorm"
"git.feneas.org/ganggo/federation"
"github.com/patrickmn/go-cache"
"fmt"
)
type Comment struct {
......@@ -97,7 +99,15 @@ func (signature *CommentSignature) AfterFind(db *gorm.DB) error {
return db.Model(signature).Related(&signature.SignatureOrder).Error
}
func (c *Comment) Count() (count int) {
func (c *Comments) CountLocal() (count int) {
return c.count(true)
}
func (c *Comments) CountNonLocal() (count int) {
return c.count(false)
}
func (c *Comments) count(local bool) (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -105,9 +115,25 @@ func (c *Comment) Count() (count int) {
}
defer db.Close()
operator := "="
if local {
operator = ">"
}
countInt, found := databaseCache.Get(
fmt.Sprintf("comment.count.%s", operator))
if found {
// return cached value
return countInt.(int)
}
db.Table("comments").Joins(
`left join people on comments.person_id = people.id`,
).Where("people.user_id > 0").Count(&count)
).Where(fmt.Sprintf("people.user_id %s 0", operator)).Count(&count)
// set caching
databaseCache.Set(fmt.Sprintf("comment.count.%s", operator), count,
cache.DefaultExpiration)
return
}
......
......@@ -22,6 +22,7 @@ import (
"errors"
"github.com/revel/revel"
"git.feneas.org/ganggo/ganggo/app/helpers"
"github.com/patrickmn/go-cache"
"git.feneas.org/ganggo/gorm"
_ "git.feneas.org/ganggo/gorm/dialects/postgres"
_ "git.feneas.org/ganggo/gorm/dialects/mssql"
......@@ -62,6 +63,8 @@ const (
var DB Database
var databaseCache = cache.New(5*time.Minute, 10*time.Minute)
func OpenDatabase() (*gorm.DB, error) {
db, err := gorm.Open(DB.Driver, DB.Url)
if err != nil {
......
......@@ -22,6 +22,8 @@ import (
"git.feneas.org/ganggo/gorm"
"github.com/revel/revel"
"git.feneas.org/ganggo/federation"
"github.com/patrickmn/go-cache"
"fmt"
)
type Post struct {
......@@ -161,7 +163,21 @@ func (p *Post) AfterDelete(db *gorm.DB) (err error) {
return
}
func (p *Post) Count() (count int) {
func (p *Posts) CountLocal() (count int) {
return p.count(true, 0)
}
func (p *Posts) CountNonLocal() (count int) {
return p.count(false, 0)
}
func (p *Posts) CountByUserID(id uint) (count int) {
// NOTE false is not intuitive here but
// we need the equal-operator
return p.count(false, id)
}
func (p *Posts) count(local bool, id uint) (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -169,9 +185,24 @@ func (p *Post) Count() (count int) {
}
defer db.Close()
operator := "="
if local {
operator = ">"
}
key := fmt.Sprintf("post.count.%s.%d", operator, id)
countInt, found := databaseCache.Get(key)
if found {
// return cached value
return countInt.(int)
}
db.Table("posts").Joins(
`left join people on posts.person_id = people.id`,
).Where("people.user_id > 0").Count(&count)
).Where(fmt.Sprintf("people.user_id %s %d", operator, id)).Count(&count)
// set caching
databaseCache.Set(key, count, cache.DefaultExpiration)
return
}
......@@ -230,6 +261,20 @@ func (posts *Posts) FindAllPublic(offset uint) (err error) {
return query.Find(posts).Error
}
func (posts *Posts) FindAllNonLocalPublic(offset uint) (err error) {
db, err := OpenDatabase()
if err != nil {
return err
}
defer db.Close()
query := db.Offset(offset).Limit(10).
Joins(`left join people on posts.person_id = people.id`).
Where(`people.user_id = ? and public = ?`, 0, true).Order(`created_at desc`)
return query.Find(posts).Error
}
func (posts *Posts) FindAllPublicByPerson(person Person, offset uint) (err error) {
db, err := OpenDatabase()
if err != nil {
......
......@@ -30,6 +30,7 @@ import (
"encoding/pem"
"golang.org/x/crypto/bcrypt"
"git.feneas.org/ganggo/ganggo/app/helpers"
"github.com/patrickmn/go-cache"
)
type User struct {
......@@ -50,6 +51,8 @@ type User struct {
Aspects []Aspect `gorm:"AssociationForeignKey:UserID"`
}
type Users []User
type UserStream struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
......@@ -151,7 +154,7 @@ func (user *User) FindByUsername(name string) (err error) {
return db.Where("username = ?", name).Find(user).Error
}
func (user *User) Count() (count int) {
func (users *Users) Count() (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -159,10 +162,16 @@ func (user *User) Count() (count int) {
}
defer db.Close()
if countInt, found := databaseCache.Get("users.count.all"); found {
return countInt.(int)
}
db.Table("users").Count(&count)
databaseCache.Set("users.count.all", count, cache.DefaultExpiration)
return
}
func (user *User) ActiveHalfyear() (count int) {
func (users *Users) ActiveHalfyear() (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -170,12 +179,17 @@ func (user *User) ActiveHalfyear() (count int) {
}
defer db.Close()
if countInt, found := databaseCache.Get("users.count.halfyear"); found {
return countInt.(int)
}
halfYear := time.Now().AddDate(0, -6, 0)
db.Table("users").Where("last_seen >= ?", halfYear).Count(&count)
databaseCache.Set("users.count.halfyear", count, cache.DefaultExpiration)
return
}
func (user *User) ActiveMonth() (count int) {
func (users *Users) ActiveMonth() (count int) {
db, err := OpenDatabase()
if err != nil {
revel.AppLog.Error(err.Error())
......@@ -183,8 +197,13 @@ func (user *User) ActiveMonth() (count int) {
}
defer db.Close()
if countInt, found := databaseCache.Get("users.count.month"); found {
return countInt.(int)
}
month := time.Now().AddDate(0, -1, 0)
db.Table("users").Where("last_seen >= ?", month).Count(&count)
databaseCache.Set("users.count.month", count, cache.DefaultExpiration)
return
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment