Skip to content

Commit 8c6c383

Browse files
committed
Metrics: Add auto tracked files mode
When enabled, files will be automatically added to tracked files.
1 parent 0f460f3 commit 8c6c383

File tree

8 files changed

+393
-138
lines changed

8 files changed

+393
-138
lines changed

cli/commands.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ func (c *cli) CmdHelp() error {
101101
{"add", "Add a new mirror"},
102102
{"addMetric", "Add a tracked file to the metrics route"},
103103
{"disable", "Disable a mirror"},
104+
{"disableAuto", "Disable automatic file tracking"},
104105
{"delMetric", "Delete a tracked file from the metrics route"},
105106
{"edit", "Edit a mirror"},
106107
{"enable", "Enable a mirror"},
108+
{"enableAuto", "Enable automatic file tracking"},
107109
{"export", "Export the mirror database"},
108110
{"list", "List all mirrors"},
109111
{"listMetrics", "List all tracked files from the metrics route"},
@@ -114,6 +116,7 @@ func (c *cli) CmdHelp() error {
114116
{"scan", "(Re-)Scan a mirror"},
115117
{"show", "Print a mirror configuration"},
116118
{"stats", "Show download stats"},
119+
{"statusAuto", "Show automatic file tracking boolean"},
117120
{"upgrade", "Seamless binary upgrade"},
118121
{"version", "Print version information"},
119122
} {
@@ -444,6 +447,69 @@ func (c *cli) CmdListmetrics(args ...string) error {
444447
return nil
445448
}
446449

450+
func (c *cli) CmdEnableauto(args ...string) error {
451+
cmd := SubCmd("enableAuto", "", "Enable automatic addition of new files to tracked files")
452+
453+
if err := cmd.Parse(args); err != nil {
454+
return nil
455+
}
456+
if cmd.NArg() != 0 {
457+
cmd.Usage()
458+
return nil
459+
}
460+
461+
client := c.GetRPC()
462+
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
463+
defer cancel()
464+
client.EnableAuto(ctx, &empty.Empty{})
465+
466+
return nil
467+
}
468+
469+
func (c *cli) CmdDisableauto(args ...string) error {
470+
cmd := SubCmd("disableAuto", "", "Disable automatic addition of new files to tracked files")
471+
472+
if err := cmd.Parse(args); err != nil {
473+
return nil
474+
}
475+
if cmd.NArg() != 0 {
476+
cmd.Usage()
477+
return nil
478+
}
479+
480+
client := c.GetRPC()
481+
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
482+
defer cancel()
483+
client.DisableAuto(ctx, &empty.Empty{})
484+
485+
return nil
486+
}
487+
488+
func (c *cli) CmdStatusauto(args ...string) error {
489+
cmd := SubCmd("statusAuto", "", "Print boolean of automatic addition of new files to tracked files")
490+
491+
if err := cmd.Parse(args); err != nil {
492+
return nil
493+
}
494+
if cmd.NArg() != 0 {
495+
cmd.Usage()
496+
return nil
497+
}
498+
499+
client := c.GetRPC()
500+
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
501+
defer cancel()
502+
status, _ := client.GetStatusAuto(ctx, &empty.Empty{})
503+
504+
if status.Status {
505+
log.Info("Auto tracked files is enabled")
506+
} else {
507+
log.Info("Auto tracked files is disabled")
508+
}
509+
510+
return nil
511+
}
512+
447513
func (c *cli) CmdRemove(args ...string) error {
448514
cmd := SubCmd("remove", "IDENTIFIER", "Remove an existing mirror")
449515
force := cmd.Bool("f", false, "Never prompt for confirmation")

config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func defaultConfig() Configuration {
6060
RPCListenAddress: "localhost:3390",
6161
RPCPassword: "",
6262
MetricsTopFilesRetention: 1,
63+
MetricsAutoTrackedFiles: false,
6364
}
6465
}
6566

@@ -95,7 +96,8 @@ type Configuration struct {
9596
RPCListenAddress string `yaml:"RPCListenAddress"`
9697
RPCPassword string `yaml:"RPCPassword"`
9798

98-
MetricsTopFilesRetention int `yaml:"MetricsTopFilesRetention"`
99+
MetricsTopFilesRetention int `yaml:"MetricsTopFilesRetention"`
100+
MetricsAutoTrackedFiles bool `yaml:"MetricsAutoTrackedFiles"`
99101
}
100102

101103
type fallback struct {

database/utils.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package database
55

66
import (
7-
"errors"
87
"strconv"
98

109
"github.com/gomodule/redigo/redis"
10+
"github.com/pkg/errors"
11+
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/status"
1113
)
1214

1315
func (r *Redis) GetListOfMirrors() (map[int]string, error) {
@@ -160,6 +162,52 @@ func (r *Redis) AddCountry(country string) error {
160162
return nil
161163
}
162164

165+
func (r *Redis) AddTrackedFile(file string) error {
166+
conn, err := r.Connect()
167+
if err != nil {
168+
return err
169+
}
170+
defer conn.Close()
171+
172+
exists, err := redis.Int(conn.Do("SISMEMBER", "FILES", file))
173+
if err != nil {
174+
return errors.Wrap(err, "failed to check for file presence")
175+
} else if exists == 0 {
176+
return status.Error(codes.FailedPrecondition,
177+
"file does not exist")
178+
}
179+
180+
exists, err = redis.Int(conn.Do("SADD", "TRACKED_FILES", file))
181+
if err != nil {
182+
return errors.Wrap(err, "failed to add file to metrics")
183+
} else if exists == 0 {
184+
return status.Error(codes.AlreadyExists, "file already is in metrics")
185+
}
186+
return nil
187+
}
188+
189+
func (r *Redis) DeleteTrackedFile(file string) error {
190+
conn, err := r.Connect()
191+
if err != nil {
192+
return err
193+
}
194+
defer conn.Close()
195+
196+
exists, err := redis.Int(conn.Do("SISMEMBER", "TRACKED_FILES", file))
197+
if err != nil {
198+
return errors.Wrap(err, "failed to check for file presence")
199+
} else if exists == 0 {
200+
return status.Error(codes.FailedPrecondition,
201+
"file is not part of tracked files")
202+
}
203+
204+
_, err = conn.Do("SREM", "TRACKED_FILES", file)
205+
if err != nil {
206+
return errors.Wrap(err, "failed to remove file from tracked files")
207+
}
208+
return nil
209+
}
210+
163211
type NetReadyError struct {
164212
error
165213
}

mirrorbits.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,6 @@
137137
## Keep in mind that this will have an impact on the database's RAM usage
138138
## It is not recommended to keep more than a few days of retention.
139139
## MetricsTopFilesRetention: 1
140+
141+
## Enable / Disable automatically adding a new file to tracked files
142+
## MetricsAutoTrackedFiles: false

rpc/rpc.go

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -730,52 +730,11 @@ func (c *CLI) GetMirrorLogs(ctx context.Context, in *GetMirrorLogsRequest) (*Get
730730
}
731731

732732
func (c *CLI) AddMetric(ctx context.Context, in *Metric) (*empty.Empty, error) {
733-
conn, err := c.redis.Connect()
734-
if err != nil {
735-
return nil, err
736-
}
737-
defer conn.Close()
738-
739-
exists, err := redis.Int(conn.Do("SISMEMBER", "FILES", in.Filename))
740-
if err != nil {
741-
return nil, errors.Wrap(err, "failed to check for file presence")
742-
} else if exists == 0 {
743-
return nil, status.Error(codes.FailedPrecondition,
744-
"file does not exist")
745-
}
746-
747-
exists, err = redis.Int(conn.Do("SADD", "TRACKED_FILES", in.Filename))
748-
if err != nil {
749-
return nil, errors.Wrap(err, "failed to add file to metrics")
750-
} else if exists == 0 {
751-
return nil, status.Error(codes.AlreadyExists,
752-
"file already is in metrics")
753-
}
754-
755-
return &empty.Empty{}, nil
733+
return &empty.Empty{}, c.redis.AddTrackedFile(in.Filename)
756734
}
757735

758736
func (c *CLI) DelMetric(ctx context.Context, in *Metric) (*empty.Empty, error) {
759-
conn, err := c.redis.Connect()
760-
if err != nil {
761-
return nil, err
762-
}
763-
defer conn.Close()
764-
765-
exists, err := redis.Int(conn.Do("SISMEMBER", "TRACKED_FILES", in.Filename))
766-
if err != nil {
767-
return nil, errors.Wrap(err, "failed to check for file presence")
768-
} else if exists == 0 {
769-
return nil, status.Error(codes.FailedPrecondition,
770-
"file is not part of tracked files")
771-
}
772-
773-
_, err = conn.Do("SREM", "TRACKED_FILES", in.Filename)
774-
if err != nil {
775-
return nil, errors.Wrap(err, "failed to remove file from tracked files")
776-
}
777-
778-
return &empty.Empty{}, nil
737+
return &empty.Empty{}, c.redis.DeleteTrackedFile(in.Filename)
779738
}
780739

781740
func (c *CLI) ListMetrics(ctx context.Context, in *Metric) (*MetricsList, error) {
@@ -799,3 +758,17 @@ func (c *CLI) ListMetrics(ctx context.Context, in *Metric) (*MetricsList, error)
799758

800759
return &MetricsList{Filename: files}, nil
801760
}
761+
762+
func (c *CLI) EnableAuto(context.Context, *empty.Empty) (*empty.Empty, error) {
763+
GetConfig().MetricsAutoTrackedFiles = true
764+
return &empty.Empty{}, nil
765+
}
766+
767+
func (c *CLI) DisableAuto(context.Context, *empty.Empty) (*empty.Empty, error) {
768+
GetConfig().MetricsAutoTrackedFiles = false
769+
return &empty.Empty{}, nil
770+
}
771+
772+
func (c *CLI) GetStatusAuto(context.Context, *empty.Empty) (*StatusAuto, error) {
773+
return &StatusAuto{Status: GetConfig().MetricsAutoTrackedFiles}, nil
774+
}

0 commit comments

Comments
 (0)