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
|
|
|
|
2021-05-08 10:52:06 +00:00
|
|
|
//go:generate -command counterfeiter go run github.com/maxbrunsfeld/counterfeiter/v6
|
2021-03-03 07:53:50 +00:00
|
|
|
//go:generate counterfeiter -o mocks/folderSummaryService.go --fake-name FolderSummaryService . FolderSummaryService
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
package model
|
2015-03-26 22:26:51 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-21 07:41:15 +00:00
|
|
|
"context"
|
2019-03-26 19:53:58 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-03-26 22:26:51 +00:00
|
|
|
"time"
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
"github.com/thejerf/suture/v4"
|
2019-07-09 09:40:30 +00:00
|
|
|
|
2019-02-26 08:09:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2020-02-12 10:59:12 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
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"
|
2020-12-22 19:17:14 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/svcutil"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2015-03-26 22:26:51 +00:00
|
|
|
)
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
type FolderSummaryService interface {
|
|
|
|
suture.Service
|
2022-04-05 19:32:06 +00:00
|
|
|
Summary(folder string) (*FolderSummary, error)
|
2019-03-26 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
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
|
2019-08-15 14:29:37 +00:00
|
|
|
evLogger events.Logger
|
2015-03-29 08:25:41 +00:00
|
|
|
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{}
|
|
|
|
}
|
|
|
|
|
2019-08-15 14:29:37 +00:00
|
|
|
func NewFolderSummaryService(cfg config.Wrapper, m Model, id protocol.DeviceID, evLogger events.Logger) FolderSummaryService {
|
2015-12-23 15:31:12 +00:00
|
|
|
service := &folderSummaryService{
|
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 07:24:39 +00:00
|
|
|
Supervisor: suture.New("folderSummaryService", svcutil.SpecWithDebugLogger(l)),
|
|
|
|
cfg: cfg,
|
|
|
|
model: m,
|
|
|
|
id: id,
|
|
|
|
evLogger: evLogger,
|
|
|
|
immediate: make(chan string),
|
|
|
|
folders: make(map[string]struct{}),
|
|
|
|
foldersMut: sync.NewMutex(),
|
2015-04-28 21:12:19 +00:00
|
|
|
}
|
2015-03-26 22:26:51 +00:00
|
|
|
|
2020-12-22 19:17:14 +00:00
|
|
|
service.Add(svcutil.AsService(service.listenForUpdates, fmt.Sprintf("%s/listenForUpdates", service)))
|
|
|
|
service.Add(svcutil.AsService(service.calculateSummaries, fmt.Sprintf("%s/calculateSummaries", service)))
|
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
|
|
|
}
|
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
func (c *folderSummaryService) String() string {
|
|
|
|
return fmt.Sprintf("FolderSummaryService@%p", c)
|
|
|
|
}
|
|
|
|
|
2022-04-05 19:32:06 +00:00
|
|
|
// FolderSummary replaces the previously used map[string]interface{}, and needs
|
|
|
|
// to keep the structure/naming for api backwards compatibility
|
|
|
|
type FolderSummary struct {
|
|
|
|
Errors int `json:"errors"`
|
|
|
|
PullErrors int `json:"pullErrors"` // deprecated
|
|
|
|
|
|
|
|
Invalid string `json:"invalid"` // deprecated
|
|
|
|
|
|
|
|
GlobalFiles int `json:"globalFiles"`
|
|
|
|
GlobalDirectories int `json:"globalDirectories"`
|
|
|
|
GlobalSymlinks int `json:"globalSymlinks"`
|
|
|
|
GlobalDeleted int `json:"globalDeleted"`
|
|
|
|
GlobalBytes int64 `json:"globalBytes"`
|
|
|
|
GlobalTotalItems int `json:"globalTotalItems"`
|
|
|
|
|
|
|
|
LocalFiles int `json:"localFiles"`
|
|
|
|
LocalDirectories int `json:"localDirectories"`
|
|
|
|
LocalSymlinks int `json:"localSymlinks"`
|
|
|
|
LocalDeleted int `json:"localDeleted"`
|
|
|
|
LocalBytes int64 `json:"localBytes"`
|
|
|
|
LocalTotalItems int `json:"localTotalItems"`
|
|
|
|
|
|
|
|
NeedFiles int `json:"needFiles"`
|
|
|
|
NeedDirectories int `json:"needDirectories"`
|
|
|
|
NeedSymlinks int `json:"needSymlinks"`
|
|
|
|
NeedDeletes int `json:"needDeletes"`
|
|
|
|
NeedBytes int64 `json:"needBytes"`
|
|
|
|
NeedTotalItems int `json:"needTotalItems"`
|
|
|
|
|
|
|
|
ReceiveOnlyChangedFiles int `json:"receiveOnlyChangedFiles"`
|
|
|
|
ReceiveOnlyChangedDirectories int `json:"receiveOnlyChangedDirectories"`
|
|
|
|
ReceiveOnlyChangedSymlinks int `json:"receiveOnlyChangedSymlinks"`
|
|
|
|
ReceiveOnlyChangedDeletes int `json:"receiveOnlyChangedDeletes"`
|
|
|
|
ReceiveOnlyChangedBytes int64 `json:"receiveOnlyChangedBytes"`
|
|
|
|
ReceiveOnlyTotalItems int `json:"receiveOnlyTotalItems"`
|
|
|
|
|
|
|
|
InSyncFiles int `json:"inSyncFiles"`
|
|
|
|
InSyncBytes int64 `json:"inSyncBytes"`
|
|
|
|
|
|
|
|
State string `json:"state"`
|
|
|
|
StateChanged time.Time `json:"stateChanged"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
|
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 07:24:39 +00:00
|
|
|
Version int64 `json:"version"` // deprecated
|
|
|
|
Sequence int64 `json:"sequence"`
|
|
|
|
RemoteSequence map[protocol.DeviceID]int64 `json:"remoteSequence"`
|
2022-04-05 19:32:06 +00:00
|
|
|
|
|
|
|
IgnorePatterns bool `json:"ignorePatterns"`
|
|
|
|
WatchError string `json:"watchError"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *folderSummaryService) Summary(folder string) (*FolderSummary, error) {
|
|
|
|
res := new(FolderSummary)
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2020-02-12 10:59:12 +00:00
|
|
|
var local, global, need, ro db.Counts
|
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 07:24:39 +00:00
|
|
|
var ourSeq int64
|
|
|
|
var remoteSeq map[protocol.DeviceID]int64
|
2019-03-26 19:53:58 +00:00
|
|
|
errors, err := c.model.FolderErrors(folder)
|
2020-02-12 10:59:12 +00:00
|
|
|
if err == nil {
|
|
|
|
var snap *db.Snapshot
|
|
|
|
if snap, err = c.model.DBSnapshot(folder); err == nil {
|
|
|
|
global = snap.GlobalSize()
|
|
|
|
local = snap.LocalSize()
|
2020-05-11 13:07:06 +00:00
|
|
|
need = snap.NeedSize(protocol.LocalDeviceID)
|
2020-02-12 10:59:12 +00:00
|
|
|
ro = snap.ReceiveOnlyChangedSize()
|
|
|
|
ourSeq = snap.Sequence(protocol.LocalDeviceID)
|
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 07:24:39 +00:00
|
|
|
remoteSeq = snap.RemoteSequences()
|
2020-02-12 10:59:12 +00:00
|
|
|
snap.Release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// For API backwards compatibility (SyncTrayzor needs it) an empty folder
|
|
|
|
// summary is returned for not running folders, an error might actually be
|
|
|
|
// more appropriate
|
2021-03-07 12:43:22 +00:00
|
|
|
if err != nil && err != ErrFolderPaused && err != ErrFolderNotRunning {
|
2019-03-26 19:53:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-12 10:59:12 +00:00
|
|
|
|
2022-04-05 19:32:06 +00:00
|
|
|
res.Errors = len(errors)
|
|
|
|
res.PullErrors = len(errors) // deprecated
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2022-04-05 19:32:06 +00:00
|
|
|
res.Invalid = "" // Deprecated, retains external API for now
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2022-04-05 19:32:06 +00:00
|
|
|
res.GlobalFiles, res.GlobalDirectories, res.GlobalSymlinks, res.GlobalDeleted, res.GlobalBytes, res.GlobalTotalItems = global.Files, global.Directories, global.Symlinks, global.Deleted, global.Bytes, global.TotalItems()
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2022-04-05 19:32:06 +00:00
|
|
|
res.LocalFiles, res.LocalDirectories, res.LocalSymlinks, res.LocalDeleted, res.LocalBytes, res.LocalTotalItems = local.Files, local.Directories, local.Symlinks, local.Deleted, local.Bytes, local.TotalItems()
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2020-03-22 10:46:42 +00:00
|
|
|
fcfg, haveFcfg := c.cfg.Folder(folder)
|
|
|
|
|
|
|
|
if haveFcfg && fcfg.IgnoreDelete {
|
|
|
|
need.Deleted = 0
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:23:08 +00:00
|
|
|
need.Bytes -= c.model.FolderProgressBytesCompleted(folder)
|
|
|
|
// This may happen if we are in progress of pulling files that were
|
|
|
|
// deleted globally after the pull started.
|
|
|
|
if need.Bytes < 0 {
|
|
|
|
need.Bytes = 0
|
|
|
|
}
|
2022-04-05 19:32:06 +00:00
|
|
|
res.NeedFiles, res.NeedDirectories, res.NeedSymlinks, res.NeedDeletes, res.NeedBytes, res.NeedTotalItems = need.Files, need.Directories, need.Symlinks, need.Deleted, need.Bytes, need.TotalItems()
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
if haveFcfg && (fcfg.Type == config.FolderTypeReceiveOnly || fcfg.Type == config.FolderTypeReceiveEncrypted) {
|
2019-03-26 19:53:58 +00:00
|
|
|
// Add statistics for things that have changed locally in a receive
|
2020-11-09 14:33:32 +00:00
|
|
|
// only or receive encrypted folder.
|
2022-04-05 19:32:06 +00:00
|
|
|
res.ReceiveOnlyChangedFiles = ro.Files
|
|
|
|
res.ReceiveOnlyChangedDirectories = ro.Directories
|
|
|
|
res.ReceiveOnlyChangedSymlinks = ro.Symlinks
|
|
|
|
res.ReceiveOnlyChangedDeletes = ro.Deleted
|
|
|
|
res.ReceiveOnlyChangedBytes = ro.Bytes
|
|
|
|
res.ReceiveOnlyTotalItems = ro.TotalItems()
|
2019-03-26 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 19:32:06 +00:00
|
|
|
res.InSyncFiles, res.InSyncBytes = global.Files-need.Files, global.Bytes-need.Bytes
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2022-04-05 19:32:06 +00:00
|
|
|
res.State, res.StateChanged, err = c.model.State(folder)
|
2019-03-26 19:53:58 +00:00
|
|
|
if err != nil {
|
2022-04-05 19:32:06 +00:00
|
|
|
res.Error = err.Error()
|
2019-03-26 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
lib/api: Improve folder summary event, verbose service (#9370)
This makes a couple of small improvements to the folder summary
mechanism:
- The folder summary includes the local and remote sequence numbers in
clear text, rather than some odd sum that I'm not sure what it was
intended to represent.
- The folder summary event is generated when appropriate, regardless of
whether there is an event listener. We did this before because
generating it was expensive, and we wanted to avoid doing it
unnecessarily. Nowadays, however, it's mostly just reading out
pre-calculated metadata, and anyway, it's nice if it shows up reliably
when running with -verbose.
The point of all this is to make it easier to use these events to judge
when devices are, in fact, in sync. As-is, if I'm looking at two
devices, it's very difficult to reliably determine if they are in sync
or not. The reason is that while we can ask device A if it thinks it's
in sync, we can't see if the answer is "yes" because it has processed
all changes from B, or if it just doesn't know about the changes from B
yet. With proper sequence numbers in the event we can compare the two
and determine the truth. This makes testing a lot easier.
2024-01-31 07:24:39 +00:00
|
|
|
res.Version = ourSeq // legacy
|
|
|
|
res.Sequence = ourSeq
|
|
|
|
res.RemoteSequence = remoteSeq
|
2019-03-26 19:53:58 +00:00
|
|
|
|
2021-01-12 15:25:21 +00:00
|
|
|
ignorePatterns, _, _ := c.model.CurrentIgnores(folder)
|
2022-04-05 19:32:06 +00:00
|
|
|
res.IgnorePatterns = false
|
2019-03-26 19:53:58 +00:00
|
|
|
for _, line := range ignorePatterns {
|
|
|
|
if len(line) > 0 && !strings.HasPrefix(line, "//") {
|
2022-04-05 19:32:06 +00:00
|
|
|
res.IgnorePatterns = true
|
2019-03-26 19:53:58 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.model.WatchError(folder)
|
|
|
|
if err != nil {
|
2022-04-05 19:32:06 +00:00
|
|
|
res.WatchError = err.Error()
|
2019-03-26 19:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:26:51 +00:00
|
|
|
// listenForUpdates subscribes to the event bus and makes note of folders that
|
|
|
|
// need their data recalculated.
|
2020-11-17 12:19:04 +00:00
|
|
|
func (c *folderSummaryService) listenForUpdates(ctx context.Context) error {
|
2022-04-22 06:42:20 +00:00
|
|
|
sub := c.evLogger.Subscribe(events.LocalIndexUpdated | events.RemoteIndexUpdated | events.StateChanged | events.RemoteDownloadProgress | events.DeviceConnected | events.ClusterConfigReceived | events.FolderWatchStateChanged | events.DownloadProgress)
|
2019-08-15 14:29:37 +00:00
|
|
|
defer sub.Unsubscribe()
|
2015-03-26 22:26:51 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
// This loop needs to be fast so we don't miss too many events.
|
|
|
|
|
|
|
|
select {
|
2021-12-22 19:16:21 +00:00
|
|
|
case ev, ok := <-sub.C():
|
|
|
|
if !ok {
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2019-06-30 07:23:47 +00:00
|
|
|
c.processUpdate(ev)
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-ctx.Done():
|
2020-11-17 12:19:04 +00:00
|
|
|
return ctx.Err()
|
2019-06-30 07:23:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-04-22 06:42:20 +00:00
|
|
|
case events.DeviceConnected, events.ClusterConfigReceived:
|
2019-06-30 07:23:47 +00:00
|
|
|
// When a device connects we schedule a refresh of all
|
|
|
|
// folders shared with that device.
|
2015-04-14 11:57:42 +00:00
|
|
|
|
2022-04-22 06:42:20 +00:00
|
|
|
var deviceID protocol.DeviceID
|
|
|
|
if ev.Type == events.DeviceConnected {
|
|
|
|
data := ev.Data.(map[string]string)
|
|
|
|
deviceID, _ = protocol.DeviceIDFromString(data["id"])
|
|
|
|
} else {
|
|
|
|
data := ev.Data.(ClusterConfigReceivedEventData)
|
|
|
|
deviceID = data.Device
|
|
|
|
}
|
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:
|
2024-07-31 05:31:14 +00:00
|
|
|
data := ev.Data.(map[string]map[string]*PullerProgress)
|
2019-06-30 07:23:47 +00:00
|
|
|
c.foldersMut.Lock()
|
|
|
|
for folder := range data {
|
|
|
|
c.folders[folder] = struct{}{}
|
|
|
|
}
|
|
|
|
c.foldersMut.Unlock()
|
|
|
|
return
|
|
|
|
|
|
|
|
case events.StateChanged:
|
|
|
|
data := ev.Data.(map[string]interface{})
|
2019-11-29 07:30:17 +00:00
|
|
|
if data["to"].(string) != "idle" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if from := data["from"].(string); from != "syncing" && from != "sync-preparing" {
|
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.
|
2020-11-17 12:19:04 +00:00
|
|
|
func (c *folderSummaryService) calculateSummaries(ctx context.Context) error {
|
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() {
|
2020-02-14 07:11:54 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-11-17 12:19:04 +00:00
|
|
|
return ctx.Err()
|
2020-02-14 07:11:54 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
c.sendSummary(ctx, 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:
|
2020-02-14 07:11:54 +00:00
|
|
|
c.sendSummary(ctx, folder)
|
2015-03-29 08:25:41 +00:00
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-ctx.Done():
|
2020-11-17 12:19:04 +00:00
|
|
|
return ctx.Err()
|
2015-03-26 22:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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
|
|
|
|
}
|
|
|
|
|
2022-04-05 19:32:06 +00:00
|
|
|
type FolderSummaryEventData struct {
|
|
|
|
Folder string `json:"folder"`
|
|
|
|
Summary *FolderSummary `json:"summary"`
|
|
|
|
}
|
|
|
|
|
2015-03-29 08:25:41 +00:00
|
|
|
// sendSummary send the summary events for a single folder
|
2020-02-14 07:11:54 +00:00
|
|
|
func (c *folderSummaryService) sendSummary(ctx context.Context, 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
|
|
|
|
}
|
2022-04-05 19:32:06 +00:00
|
|
|
c.evLogger.Log(events.FolderSummary, FolderSummaryEventData{
|
|
|
|
Folder: folder,
|
|
|
|
Summary: data,
|
2015-03-29 08:25:41 +00:00
|
|
|
})
|
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeGlobal, metricTypeFiles).Set(float64(data.GlobalFiles))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeGlobal, metricTypeDirectories).Set(float64(data.GlobalDirectories))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeGlobal, metricTypeSymlinks).Set(float64(data.GlobalSymlinks))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeGlobal, metricTypeDeleted).Set(float64(data.GlobalDeleted))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeGlobal, metricTypeBytes).Set(float64(data.GlobalBytes))
|
|
|
|
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeLocal, metricTypeFiles).Set(float64(data.LocalFiles))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeLocal, metricTypeDirectories).Set(float64(data.LocalDirectories))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeLocal, metricTypeSymlinks).Set(float64(data.LocalSymlinks))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeLocal, metricTypeDeleted).Set(float64(data.LocalDeleted))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeLocal, metricTypeBytes).Set(float64(data.LocalBytes))
|
|
|
|
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeNeed, metricTypeFiles).Set(float64(data.NeedFiles))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeNeed, metricTypeDirectories).Set(float64(data.NeedDirectories))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeNeed, metricTypeSymlinks).Set(float64(data.NeedSymlinks))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeNeed, metricTypeDeleted).Set(float64(data.NeedDeletes))
|
|
|
|
metricFolderSummary.WithLabelValues(folder, metricScopeNeed, metricTypeBytes).Set(float64(data.NeedBytes))
|
|
|
|
|
2015-09-29 18:05:22 +00:00
|
|
|
for _, devCfg := range c.cfg.Folders()[folder].Devices {
|
2020-02-14 07:11:54 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get completion percentage of this folder for the
|
|
|
|
// remote device.
|
2021-03-07 12:43:22 +00:00
|
|
|
comp, err := c.model.Completion(devCfg.DeviceID, folder)
|
|
|
|
if err != nil {
|
|
|
|
l.Debugf("Error getting completion for folder %v, device %v: %v", folder, devCfg.DeviceID, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ev := comp.Map()
|
|
|
|
ev["folder"] = folder
|
|
|
|
ev["device"] = devCfg.DeviceID.String()
|
|
|
|
c.evLogger.Log(events.FolderCompletion, ev)
|
2015-03-29 08:25:41 +00:00
|
|
|
}
|
|
|
|
}
|