2015-03-26 22:26:51 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-03-26 22:26:51 +00:00
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
package model
|
2015-03-26 22:26:51 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-26 19:53:58 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-03-26 22:26:51 +00:00
|
|
|
"time"
|
|
|
|
|
2019-02-26 08:09:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2016-07-25 10:42:17 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2018-09-08 09:56:56 +00:00
|
|
|
"github.com/thejerf/suture"
|
2015-03-26 22:26:51 +00:00
|
|
|
)
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
const minSummaryInterval = time.Minute
|
|
|
|
|
|
|
|
type FolderSummaryService interface {
|
|
|
|
suture.Service
|
|
|
|
Summary(folder string) (map[string]interface{}, error)
|
|
|
|
OnEventRequest()
|
|
|
|
}
|
|
|
|
|
2015-12-23 15:31:12 +00:00
|
|
|
// The folderSummaryService adds summary information events (FolderSummary and
|
2015-03-26 22:26:51 +00:00
|
|
|
// FolderCompletion) into the event stream at certain intervals.
|
2015-12-23 15:31:12 +00:00
|
|
|
type folderSummaryService struct {
|
2015-04-28 21:12:19 +00:00
|
|
|
*suture.Supervisor
|
|
|
|
|
2019-02-26 08:09:25 +00:00
|
|
|
cfg config.Wrapper
|
2019-03-26 19:53:58 +00:00
|
|
|
model Model
|
|
|
|
id protocol.DeviceID
|
2015-03-29 08:25:41 +00:00
|
|
|
stop chan struct{}
|
|
|
|
immediate chan string
|
2015-03-26 22:26:51 +00:00
|
|
|
|
|
|
|
// For keeping track of folders to recalculate for
|
|
|
|
foldersMut sync.Mutex
|
|
|
|
folders map[string]struct{}
|
2015-04-28 21:12:19 +00:00
|
|
|
|
|
|
|
// For keeping track of when the last event request on the API was
|
|
|
|
lastEventReq time.Time
|
|
|
|
lastEventReqMut sync.Mutex
|
2015-03-26 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
func NewFolderSummaryService(cfg config.Wrapper, m Model, id protocol.DeviceID) FolderSummaryService {
|
2015-12-23 15:31:12 +00:00
|
|
|
service := &folderSummaryService{
|
2018-08-13 18:39:08 +00:00
|
|
|
Supervisor: suture.New("folderSummaryService", suture.Spec{
|
2018-09-08 09:56:56 +00:00
|
|
|
PassThroughPanics: true,
|
2018-08-13 18:39:08 +00:00
|
|
|
}),
|
2015-09-29 18:05:22 +00:00
|
|
|
cfg: cfg,
|
2015-04-28 21:12:19 +00:00
|
|
|
model: m,
|
2019-03-26 19:53:58 +00:00
|
|
|
id: id,
|
2015-04-28 21:12:19 +00:00
|
|
|
stop: make(chan struct{}),
|
|
|
|
immediate: make(chan string),
|
|
|
|
folders: make(map[string]struct{}),
|
|
|
|
foldersMut: sync.NewMutex(),
|
|
|
|
lastEventReqMut: sync.NewMutex(),
|
|
|
|
}
|
2015-03-26 22:26:51 +00:00
|
|
|
|
2015-12-23 15:31:12 +00:00
|
|
|
service.Add(serviceFunc(service.listenForUpdates))
|
|
|
|
service.Add(serviceFunc(service.calculateSummaries))
|
2015-03-26 22:26:51 +00:00
|
|
|
|
2015-12-23 15:31:12 +00:00
|
|
|
return service
|
2015-03-26 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 15:31:12 +00:00
|
|
|
func (c *folderSummaryService) Stop() {
|
2015-04-28 21:12:19 +00:00
|
|
|
c.Supervisor.Stop()
|
2015-03-26 22:26:51 +00:00
|
|
|
close(c.stop)
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
func (c *folderSummaryService) String() string {
|
|
|
|
return fmt.Sprintf("FolderSummaryService@%p", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *folderSummaryService) Summary(folder string) (map[string]interface{}, error) {
|
|
|
|
var res = make(map[string]interface{})
|
|
|
|
|
|
|
|
errors, err := c.model.FolderErrors(folder)
|
|
|
|
if err != nil && err != ErrFolderPaused {
|
|
|
|
// Stats from the db can still be obtained if the folder is just paused
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res["errors"] = len(errors)
|
|
|
|
res["pullErrors"] = len(errors) // deprecated
|
|
|
|
|
|
|
|
res["invalid"] = "" // Deprecated, retains external API for now
|
|
|
|
|
|
|
|
global := c.model.GlobalSize(folder)
|
|
|
|
res["globalFiles"], res["globalDirectories"], res["globalSymlinks"], res["globalDeleted"], res["globalBytes"], res["globalTotalItems"] = global.Files, global.Directories, global.Symlinks, global.Deleted, global.Bytes, global.TotalItems()
|
|
|
|
|
|
|
|
local := c.model.LocalSize(folder)
|
|
|
|
res["localFiles"], res["localDirectories"], res["localSymlinks"], res["localDeleted"], res["localBytes"], res["localTotalItems"] = local.Files, local.Directories, local.Symlinks, local.Deleted, local.Bytes, local.TotalItems()
|
|
|
|
|
|
|
|
need := c.model.NeedSize(folder)
|
|
|
|
res["needFiles"], res["needDirectories"], res["needSymlinks"], res["needDeletes"], res["needBytes"], res["needTotalItems"] = need.Files, need.Directories, need.Symlinks, need.Deleted, need.Bytes, need.TotalItems()
|
|
|
|
|
|
|
|
if c.cfg.Folders()[folder].Type == config.FolderTypeReceiveOnly {
|
|
|
|
// Add statistics for things that have changed locally in a receive
|
|
|
|
// only folder.
|
|
|
|
ro := c.model.ReceiveOnlyChangedSize(folder)
|
|
|
|
res["receiveOnlyChangedFiles"] = ro.Files
|
|
|
|
res["receiveOnlyChangedDirectories"] = ro.Directories
|
|
|
|
res["receiveOnlyChangedSymlinks"] = ro.Symlinks
|
|
|
|
res["receiveOnlyChangedDeletes"] = ro.Deleted
|
|
|
|
res["receiveOnlyChangedBytes"] = ro.Bytes
|
|
|
|
res["receiveOnlyTotalItems"] = ro.TotalItems()
|
|
|
|
}
|
|
|
|
|
|
|
|
res["inSyncFiles"], res["inSyncBytes"] = global.Files-need.Files, global.Bytes-need.Bytes
|
|
|
|
|
|
|
|
res["state"], res["stateChanged"], err = c.model.State(folder)
|
|
|
|
if err != nil {
|
|
|
|
res["error"] = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
ourSeq, _ := c.model.CurrentSequence(folder)
|
|
|
|
remoteSeq, _ := c.model.RemoteSequence(folder)
|
|
|
|
|
|
|
|
res["version"] = ourSeq + remoteSeq // legacy
|
|
|
|
res["sequence"] = ourSeq + remoteSeq // new name
|
|
|
|
|
|
|
|
ignorePatterns, _, _ := c.model.GetIgnores(folder)
|
|
|
|
res["ignorePatterns"] = false
|
|
|
|
for _, line := range ignorePatterns {
|
|
|
|
if len(line) > 0 && !strings.HasPrefix(line, "//") {
|
|
|
|
res["ignorePatterns"] = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.model.WatchError(folder)
|
|
|
|
if err != nil {
|
|
|
|
res["watchError"] = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *folderSummaryService) OnEventRequest() {
|
|
|
|
c.lastEventReqMut.Lock()
|
|
|
|
c.lastEventReq = time.Now()
|
|
|
|
c.lastEventReqMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:26:51 +00:00
|
|
|
// listenForUpdates subscribes to the event bus and makes note of folders that
|
|
|
|
// need their data recalculated.
|
2015-12-23 15:31:12 +00:00
|
|
|
func (c *folderSummaryService) listenForUpdates() {
|
2019-06-30 07:23:47 +00:00
|
|
|
sub := events.Default.Subscribe(events.LocalIndexUpdated | events.RemoteIndexUpdated | events.StateChanged | events.RemoteDownloadProgress | events.DeviceConnected | events.FolderWatchStateChanged | events.DownloadProgress)
|
2015-03-26 22:26:51 +00:00
|
|
|
defer events.Default.Unsubscribe(sub)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// This loop needs to be fast so we don't miss too many events.
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ev := <-sub.C():
|
2019-06-30 07:23:47 +00:00
|
|
|
c.processUpdate(ev)
|
|
|
|
case <-c.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 10:42:17 +00:00
|
|
|
|
2019-06-30 07:23:47 +00:00
|
|
|
func (c *folderSummaryService) processUpdate(ev events.Event) {
|
|
|
|
var folder string
|
2016-07-25 10:42:17 +00:00
|
|
|
|
2019-06-30 07:23:47 +00:00
|
|
|
switch ev.Type {
|
|
|
|
case events.DeviceConnected:
|
|
|
|
// When a device connects we schedule a refresh of all
|
|
|
|
// folders shared with that device.
|
2015-04-14 11:57:42 +00:00
|
|
|
|
2019-06-30 07:23:47 +00:00
|
|
|
data := ev.Data.(map[string]string)
|
|
|
|
deviceID, _ := protocol.DeviceIDFromString(data["id"])
|
2015-03-29 08:25:41 +00:00
|
|
|
|
2019-06-30 07:23:47 +00:00
|
|
|
c.foldersMut.Lock()
|
|
|
|
nextFolder:
|
|
|
|
for _, folder := range c.cfg.Folders() {
|
|
|
|
for _, dev := range folder.Devices {
|
|
|
|
if dev.DeviceID == deviceID {
|
|
|
|
c.folders[folder.ID] = struct{}{}
|
|
|
|
continue nextFolder
|
|
|
|
}
|
2015-03-29 08:25:41 +00:00
|
|
|
}
|
2019-06-30 07:23:47 +00:00
|
|
|
}
|
|
|
|
c.foldersMut.Unlock()
|
2015-03-26 22:26:51 +00:00
|
|
|
|
2019-06-30 07:23:47 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case events.DownloadProgress:
|
|
|
|
data := ev.Data.(map[string]map[string]*pullerProgress)
|
|
|
|
c.foldersMut.Lock()
|
|
|
|
for folder := range data {
|
|
|
|
c.folders[folder] = struct{}{}
|
|
|
|
}
|
|
|
|
c.foldersMut.Unlock()
|
|
|
|
return
|
|
|
|
|
|
|
|
case events.StateChanged:
|
|
|
|
data := ev.Data.(map[string]interface{})
|
|
|
|
if !(data["to"].(string) == "idle" && data["from"].(string) == "syncing") {
|
2015-03-26 22:26:51 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-30 07:23:47 +00:00
|
|
|
|
|
|
|
// The folder changed to idle from syncing. We should do an
|
|
|
|
// immediate refresh to update the GUI. The send to
|
|
|
|
// c.immediate must be nonblocking so that we can continue
|
|
|
|
// handling events.
|
|
|
|
|
|
|
|
folder = data["folder"].(string)
|
|
|
|
select {
|
|
|
|
case c.immediate <- folder:
|
|
|
|
c.foldersMut.Lock()
|
|
|
|
delete(c.folders, folder)
|
|
|
|
c.foldersMut.Unlock()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Refresh whenever we do the next summary.
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// The other events all have a "folder" attribute that they
|
|
|
|
// affect. Whenever the local or remote index is updated for a
|
|
|
|
// given folder we make a note of it.
|
|
|
|
// This folder needs to be refreshed whenever we do the next
|
|
|
|
// refresh.
|
|
|
|
|
|
|
|
folder = ev.Data.(map[string]interface{})["folder"].(string)
|
2015-03-26 22:26:51 +00:00
|
|
|
}
|
2019-06-30 07:23:47 +00:00
|
|
|
|
|
|
|
c.foldersMut.Lock()
|
|
|
|
c.folders[folder] = struct{}{}
|
|
|
|
c.foldersMut.Unlock()
|
2015-03-26 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculateSummaries periodically recalculates folder summaries and
|
|
|
|
// completion percentage, and sends the results on the event bus.
|
2015-12-23 15:31:12 +00:00
|
|
|
func (c *folderSummaryService) calculateSummaries() {
|
2015-03-26 22:26:51 +00:00
|
|
|
const pumpInterval = 2 * time.Second
|
|
|
|
pump := time.NewTimer(pumpInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pump.C:
|
|
|
|
t0 := time.Now()
|
2015-03-29 08:25:41 +00:00
|
|
|
for _, folder := range c.foldersToHandle() {
|
|
|
|
c.sendSummary(folder)
|
2015-03-26 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't want to spend all our time calculating summaries. Lets
|
|
|
|
// set an arbitrary limit at not spending more than about 30% of
|
|
|
|
// our time here...
|
|
|
|
wait := 2*time.Since(t0) + pumpInterval
|
|
|
|
pump.Reset(wait)
|
|
|
|
|
2015-03-29 08:25:41 +00:00
|
|
|
case folder := <-c.immediate:
|
|
|
|
c.sendSummary(folder)
|
|
|
|
|
2015-03-26 22:26:51 +00:00
|
|
|
case <-c.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// foldersToHandle returns the list of folders needing a summary update, and
|
|
|
|
// clears the list.
|
2015-12-23 15:31:12 +00:00
|
|
|
func (c *folderSummaryService) foldersToHandle() []string {
|
2015-04-28 15:34:55 +00:00
|
|
|
// We only recalculate summaries if someone is listening to events
|
2015-03-29 08:25:41 +00:00
|
|
|
// (a request to /rest/events has been made within the last
|
|
|
|
// pingEventInterval).
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
c.lastEventReqMut.Lock()
|
|
|
|
last := c.lastEventReq
|
|
|
|
c.lastEventReqMut.Unlock()
|
2019-03-26 19:53:58 +00:00
|
|
|
if time.Since(last) > minSummaryInterval {
|
2015-03-29 08:25:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:26:51 +00:00
|
|
|
c.foldersMut.Lock()
|
|
|
|
res := make([]string, 0, len(c.folders))
|
|
|
|
for folder := range c.folders {
|
|
|
|
res = append(res, folder)
|
|
|
|
delete(c.folders, folder)
|
|
|
|
}
|
|
|
|
c.foldersMut.Unlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-03-29 08:25:41 +00:00
|
|
|
// sendSummary send the summary events for a single folder
|
2015-12-23 15:31:12 +00:00
|
|
|
func (c *folderSummaryService) sendSummary(folder string) {
|
2015-03-29 08:25:41 +00:00
|
|
|
// The folder summary contains how many bytes, files etc
|
|
|
|
// are in the folder and how in sync we are.
|
2019-03-26 19:53:58 +00:00
|
|
|
data, err := c.Summary(folder)
|
2018-01-14 17:01:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-29 08:25:41 +00:00
|
|
|
events.Default.Log(events.FolderSummary, map[string]interface{}{
|
|
|
|
"folder": folder,
|
|
|
|
"summary": data,
|
|
|
|
})
|
|
|
|
|
2015-09-29 18:05:22 +00:00
|
|
|
for _, devCfg := range c.cfg.Folders()[folder].Devices {
|
2019-03-26 19:53:58 +00:00
|
|
|
if devCfg.DeviceID.Equals(c.id) {
|
2015-03-29 08:25:41 +00:00
|
|
|
// We already know about ourselves.
|
|
|
|
continue
|
|
|
|
}
|
2017-11-21 07:25:38 +00:00
|
|
|
if _, ok := c.model.Connection(devCfg.DeviceID); !ok {
|
2015-03-29 08:25:41 +00:00
|
|
|
// We're not interested in disconnected devices.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get completion percentage of this folder for the
|
|
|
|
// remote device.
|
2019-03-26 19:53:58 +00:00
|
|
|
comp := c.model.Completion(devCfg.DeviceID, folder).Map()
|
2018-01-14 12:08:40 +00:00
|
|
|
comp["folder"] = folder
|
|
|
|
comp["device"] = devCfg.DeviceID.String()
|
|
|
|
events.Default.Log(events.FolderCompletion, comp)
|
2015-03-29 08:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:26:51 +00:00
|
|
|
// serviceFunc wraps a function to create a suture.Service without stop
|
|
|
|
// functionality.
|
|
|
|
type serviceFunc func()
|
|
|
|
|
|
|
|
func (f serviceFunc) Serve() { f() }
|
|
|
|
func (f serviceFunc) Stop() {}
|