1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-19 05:09:42 +02:00

Overhaul the space stats module

Closes #274

All space counters are reset after document and catergory operations.
This commit is contained in:
HarveyKandola 2019-09-24 13:39:57 +01:00
parent 6b723568d3
commit fad1de2e41
7 changed files with 1095 additions and 1142 deletions

View file

@ -105,14 +105,6 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
return
}
err = h.Store.Space.IncrementCategoryCount(ctx, cat.SpaceID)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
ctx.Transaction.Commit()
cat, err = h.Store.Category.Get(ctx, cat.RefID)
@ -122,6 +114,7 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
return
}
h.Store.Space.SetStats(ctx, cat.SpaceID)
h.Store.Audit.Record(ctx, audit.EventTypeCategoryAdd)
response.WriteJSON(w, cat)
@ -303,16 +296,9 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
return
}
err = h.Store.Space.DecrementCategoryCount(ctx, cat.SpaceID)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
ctx.Transaction.Commit()
h.Store.Space.SetStats(ctx, cat.SpaceID)
h.Store.Audit.Record(ctx, audit.EventTypeCategoryDelete)
response.WriteEmpty(w)

View file

@ -249,12 +249,6 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *store.Sto
SourceType: activity.SourceTypeDocument,
ActivityType: activity.TypeCreated})
err = store.Space.IncrementContentCount(ctx, newDocument.SpaceID)
if err != nil {
err = errors.Wrap(err, "cannot increment space content count")
return
}
err = ctx.Transaction.Commit()
if err != nil {
err = errors.Wrap(err, "cannot commit new document import")
@ -269,6 +263,7 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *store.Sto
go indexer.IndexDocument(ctx, newDocument, da)
store.Space.SetStats(ctx, newDocument.SpaceID)
store.Audit.Record(ctx, audit.EventTypeDocumentUpload)
return

View file

@ -311,6 +311,7 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
ctx.Transaction.Commit()
h.Store.Space.SetStats(ctx, d.SpaceID)
h.Store.Audit.Record(ctx, audit.EventTypeDocumentUpdate)
// Live document indexed for search.
@ -411,16 +412,9 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
ActivityType: activity.TypeDeleted})
}
err = h.Store.Space.DecrementContentCount(ctx, doc.SpaceID)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
ctx.Transaction.Commit()
h.Store.Space.SetStats(ctx, doc.SpaceID)
h.Store.Audit.Record(ctx, audit.EventTypeDocumentDelete)
go h.Indexer.DeleteDocument(ctx, documentID)

View file

@ -28,7 +28,7 @@ type Store struct {
store.SpaceStorer
}
// Add adds new folder into the store.
// Add adds new space into the store.
func (s Store) Add(ctx domain.RequestContext, sp space.Space) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`
INSERT INTO dmz_space
@ -190,54 +190,49 @@ func (s Store) Delete(ctx domain.RequestContext, id string) (rows int64, err err
return s.DeleteConstrained(ctx.Transaction, "dmz_space", ctx.OrgID, id)
}
// IncrementCategoryCount increments usage counter for space category.
func (s Store) IncrementCategoryCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_category=c_count_category+1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
// SetStats updates the number of category/documents in space.
func (s Store) SetStats(ctx domain.RequestContext, spaceID string) (err error) {
tx, err := s.Runtime.Db.Beginx()
if err != nil {
err = errors.Wrap(err, "execute increment category count")
}
s.Runtime.Log.Error("transaction", err)
return
}
// DecrementCategoryCount decrements usage counter for space category.
func (s Store) DecrementCategoryCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_category=c_count_category-1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
var docs, cats int
f := s.IsFalse()
row := s.Runtime.Db.QueryRow(s.Bind("SELECT COUNT(*) FROM dmz_doc WHERE c_orgid=? AND c_spaceid=? AND c_lifecycle=1 AND c_template="+f),
ctx.OrgID, spaceID)
err = row.Scan(&docs)
if err == sql.ErrNoRows {
docs = 0
}
if err != nil {
s.Runtime.Log.Error("SetStats", err)
docs = 0
}
row = s.Runtime.Db.QueryRow(s.Bind("SELECT COUNT(*) FROM dmz_category WHERE c_orgid=? AND c_spaceid=?"),
ctx.OrgID, spaceID)
err = row.Scan(&cats)
if err == sql.ErrNoRows {
cats = 0
}
if err != nil {
s.Runtime.Log.Error("SetStats", err)
cats = 0
}
_, err = tx.Exec(s.Bind(`UPDATE dmz_space SET
c_count_content=?, c_count_category=?, c_revised=?
WHERE c_orgid=? AND c_refid=?`),
docs, cats, time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute decrement category count")
}
return
}
// IncrementContentCount increments usage counter for space category.
func (s Store) IncrementContentCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_content=c_count_content+1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute increment content count")
}
return
}
// DecrementContentCount decrements usage counter for space category.
func (s Store) DecrementContentCount(ctx domain.RequestContext, spaceID string) (err error) {
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
c_count_content=c_count_content-1, c_revised=? WHERE c_orgid=? AND c_refid=?`),
time.Now().UTC(), ctx.OrgID, spaceID)
if err != nil {
err = errors.Wrap(err, "execute decrement category count")
s.Runtime.Log.Error("SetStats", err)
tx.Rollback()
}
tx.Commit()
return
}

View file

@ -65,10 +65,7 @@ type SpaceStorer interface {
Update(ctx domain.RequestContext, sp space.Space) (err error)
Delete(ctx domain.RequestContext, id string) (rows int64, err error)
AdminList(ctx domain.RequestContext) (sp []space.Space, err error)
IncrementCategoryCount(ctx domain.RequestContext, spaceID string) (err error)
DecrementCategoryCount(ctx domain.RequestContext, spaceID string) (err error)
IncrementContentCount(ctx domain.RequestContext, spaceID string) (err error)
DecrementContentCount(ctx domain.RequestContext, spaceID string) (err error)
SetStats(ctx domain.RequestContext, spaceID string) (err error)
}
// CategoryStorer defines required methods for category and category membership management

View file

@ -247,17 +247,10 @@ func (h *Handler) SaveAs(w http.ResponseWriter, r *http.Request) {
}
}
err = h.Store.Space.IncrementContentCount(ctx, doc.SpaceID)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
// Commit and return new document template
ctx.Transaction.Commit()
h.Store.Space.SetStats(ctx, doc.SpaceID)
h.Store.Audit.Record(ctx, audit.EventTypeTemplateAdd)
doc, err = h.Store.Document.Get(ctx, docID)
@ -434,16 +427,9 @@ func (h *Handler) Use(w http.ResponseWriter, r *http.Request) {
}
}
err = h.Store.Space.IncrementContentCount(ctx, d.SpaceID)
if err != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}
ctx.Transaction.Commit()
h.Store.Space.SetStats(ctx, d.SpaceID)
h.Store.Audit.Record(ctx, audit.EventTypeTemplateUse)
nd, err := h.Store.Document.Get(ctx, documentID)

File diff suppressed because one or more lines are too long