diff --git a/lib/ur/contract/contract.go b/lib/ur/contract/contract.go index e6f1b55e4..d4a962388 100644 --- a/lib/ur/contract/contract.go +++ b/lib/ur/contract/contract.go @@ -7,39 +7,15 @@ package contract import ( - "database/sql/driver" - "encoding/json" "errors" "reflect" "sort" "strconv" "time" - - "github.com/lib/pq" ) type IntMap map[string]int -func (p IntMap) Value() (driver.Value, error) { - return json.Marshal(p) -} - -func (p *IntMap) Scan(src interface{}) error { - source, ok := src.([]byte) - if !ok { - return errors.New("Type assertion .([]byte) failed.") - } - - var i map[string]int - err := json.Unmarshal(source, &i) - if err != nil { - return err - } - - *p = i - return nil -} - type Report struct { // Generated Received time.Time `json:"-"` // Only from DB @@ -109,8 +85,8 @@ type Report struct { UpgradeAllowedAuto bool `json:"upgradeAllowedAuto,omitempty" since:"2"` // V2.5 fields (fields that were in v2 but never added to the database - UpgradeAllowedPre bool `json:"upgradeAllowedPre,omitempty" since:"2"` - RescanIntvs pq.Int64Array `json:"rescanIntvs,omitempty" since:"2"` + UpgradeAllowedPre bool `json:"upgradeAllowedPre,omitempty" since:"2"` + RescanIntvs Int64Array `json:"rescanIntvs,omitempty" since:"2"` // v3 fields @@ -132,18 +108,18 @@ type Report struct { CustomStunServers bool `json:"customStunServers,omitempty" since:"3"` FolderUsesV3 struct { - ScanProgressDisabled int `json:"scanProgressDisabled,omitempty" since:"3"` - ConflictsDisabled int `json:"conflictsDisabled,omitempty" since:"3"` - ConflictsUnlimited int `json:"conflictsUnlimited,omitempty" since:"3"` - ConflictsOther int `json:"conflictsOther,omitempty" since:"3"` - DisableSparseFiles int `json:"disableSparseFiles,omitempty" since:"3"` - DisableTempIndexes int `json:"disableTempIndexes,omitempty" since:"3"` - AlwaysWeakHash int `json:"alwaysWeakHash,omitempty" since:"3"` - CustomWeakHashThreshold int `json:"customWeakHashThreshold,omitempty" since:"3"` - FsWatcherEnabled int `json:"fsWatcherEnabled,omitempty" since:"3"` - PullOrder IntMap `json:"pullOrder,omitempty" since:"3"` - FilesystemType IntMap `json:"filesystemType,omitempty" since:"3"` - FsWatcherDelays pq.Int64Array `json:"fsWatcherDelays,omitempty" since:"3"` + ScanProgressDisabled int `json:"scanProgressDisabled,omitempty" since:"3"` + ConflictsDisabled int `json:"conflictsDisabled,omitempty" since:"3"` + ConflictsUnlimited int `json:"conflictsUnlimited,omitempty" since:"3"` + ConflictsOther int `json:"conflictsOther,omitempty" since:"3"` + DisableSparseFiles int `json:"disableSparseFiles,omitempty" since:"3"` + DisableTempIndexes int `json:"disableTempIndexes,omitempty" since:"3"` + AlwaysWeakHash int `json:"alwaysWeakHash,omitempty" since:"3"` + CustomWeakHashThreshold int `json:"customWeakHashThreshold,omitempty" since:"3"` + FsWatcherEnabled int `json:"fsWatcherEnabled,omitempty" since:"3"` + PullOrder IntMap `json:"pullOrder,omitempty" since:"3"` + FilesystemType IntMap `json:"filesystemType,omitempty" since:"3"` + FsWatcherDelays Int64Array `json:"fsWatcherDelays,omitempty" since:"3"` } `json:"folderUsesV3,omitempty" since:"3"` GUIStats struct { @@ -193,8 +169,8 @@ func New() *Report { r.FolderUsesV3.FilesystemType = make(IntMap) r.GUIStats.Theme = make(IntMap) r.TransportStats = make(IntMap) - r.RescanIntvs = make(pq.Int64Array, 0) - r.FolderUsesV3.FsWatcherDelays = make(pq.Int64Array, 0) + r.RescanIntvs = make(Int64Array, 0) + r.FolderUsesV3.FsWatcherDelays = make(Int64Array, 0) return r } @@ -409,21 +385,6 @@ func (r *Report) FieldNames() []string { } } -func (r Report) Value() (driver.Value, error) { - // This needs to be string, yet we read back bytes.. - bs, err := json.Marshal(r) - return string(bs), err -} - -func (r *Report) Scan(value interface{}) error { - b, ok := value.([]byte) - if !ok { - return errors.New("type assertion to []byte failed") - } - - return json.Unmarshal(b, &r) -} - func clear(v interface{}, since int) error { s := reflect.ValueOf(v).Elem() t := s.Type() @@ -461,8 +422,8 @@ func clear(v interface{}, since int) error { return nil } -func SortPqInt64Array(slice pq.Int64Array) { - sort.Slice(slice, func(a, b int) bool { - return slice[a] < slice[b] +func (arr Int64Array) Sort() { + sort.Slice(arr, func(a, b int) bool { + return arr[a] < arr[b] }) } diff --git a/lib/ur/contract/contract_other.go b/lib/ur/contract/contract_other.go new file mode 100644 index 000000000..59c84e940 --- /dev/null +++ b/lib/ur/contract/contract_other.go @@ -0,0 +1,11 @@ +// Copyright (C) 2020 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, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +// +build !ursrv + +package contract + +type Int64Array []int64 diff --git a/lib/ur/contract/contract_ursrv.go b/lib/ur/contract/contract_ursrv.go new file mode 100644 index 000000000..016a7be8f --- /dev/null +++ b/lib/ur/contract/contract_ursrv.go @@ -0,0 +1,54 @@ +// Copyright (C) 2020 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, +// You can obtain one at https://mozilla.org/MPL/2.0/. + +// +build ursrv + +package contract + +import ( + "database/sql/driver" + "encoding/json" + "errors" + + "github.com/lib/pq" +) + +type Int64Array pq.Int64Array + +func (p IntMap) Value() (driver.Value, error) { + return json.Marshal(p) +} + +func (p *IntMap) Scan(src interface{}) error { + source, ok := src.([]byte) + if !ok { + return errors.New("Type assertion .([]byte) failed.") + } + + var i map[string]int + err := json.Unmarshal(source, &i) + if err != nil { + return err + } + + *p = i + return nil +} + +func (r Report) Value() (driver.Value, error) { + // This needs to be string, yet we read back bytes.. + bs, err := json.Marshal(r) + return string(bs), err +} + +func (r *Report) Scan(value interface{}) error { + b, ok := value.([]byte) + if !ok { + return errors.New("type assertion to []byte failed") + } + + return json.Unmarshal(b, &r) +} diff --git a/lib/ur/usage_report.go b/lib/ur/usage_report.go index 9c7b87dd8..da1227a64 100644 --- a/lib/ur/usage_report.go +++ b/lib/ur/usage_report.go @@ -153,7 +153,7 @@ func (s *Service) reportData(ctx context.Context, urVersion int, preview bool) ( l.Warnf("Unhandled versioning type for usage reports: %s", cfg.Versioning.Type) } } - contract.SortPqInt64Array(report.RescanIntvs) + report.RescanIntvs.Sort() for _, cfg := range s.cfg.Devices() { if cfg.Introducer { @@ -256,7 +256,7 @@ func (s *Service) reportData(ctx context.Context, urVersion int, preview bool) ( report.FolderUsesV3.FilesystemType[cfg.FilesystemType.String()]++ report.FolderUsesV3.FsWatcherDelays = append(report.FolderUsesV3.FsWatcherDelays, int64(cfg.FSWatcherDelayS)) } - contract.SortPqInt64Array(report.FolderUsesV3.FsWatcherDelays) + report.FolderUsesV3.FsWatcherDelays.Sort() guiCfg := s.cfg.GUI() // Anticipate multiple GUI configs in the future, hence store counts.