Skip to content

Commit 6f0e4a8

Browse files
committed
Metrics: Add auto tracked files mode
When enabled, files will be automatically added to tracked files.
1 parent fc60ebf commit 6f0e4a8

File tree

8 files changed

+366
-137
lines changed

8 files changed

+366
-137
lines changed

cli/commands.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ func (c *cli) CmdMetrics(args ...string) error {
355355
_ = cmd.String("add", "", "Add a file to the metrics route")
356356
_ = cmd.String("list", "*", "List files in metrics + Optionnal pattern to filter results")
357357
_ = cmd.String("delete", "", "Delete a file from the metrics route")
358+
_ = cmd.Bool("auto-enable", true, "Enable automatic addition of new files to tracked files")
359+
_ = cmd.Bool("auto-disable", false, "Disable automatic addition of new files to tracked files")
360+
_ = cmd.Bool("auto-status", true, "Print boolean of automatic addition of new files to tracked files")
358361

359362
// Can't use cmd.Parse(args) because it doesn't handle -command without
360363
// an argument following which is needed for list
@@ -372,6 +375,12 @@ func (c *cli) CmdMetrics(args ...string) error {
372375
c.CmdAddmetric(args[1])
373376
} else if args[0] == "-delete" && len(args) == 2 {
374377
c.CmdDelmetric(args[1])
378+
} else if args[0] == "-auto-enable" {
379+
c.CmdEnableauto()
380+
} else if args[0] == "-auto-disable" {
381+
c.CmdDisableauto()
382+
} else if args[0] == "-auto-status" {
383+
c.CmdStatusauto()
375384
} else {
376385
cmd.Usage()
377386
return nil
@@ -442,6 +451,37 @@ func (c *cli) CmdListmetrics(pattern string) error {
442451
return nil
443452
}
444453

454+
func (c *cli) CmdEnableauto() error {
455+
client := c.GetRPC()
456+
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
457+
defer cancel()
458+
client.EnableAuto(ctx, &empty.Empty{})
459+
return nil
460+
}
461+
462+
func (c *cli) CmdDisableauto() error {
463+
client := c.GetRPC()
464+
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
465+
defer cancel()
466+
client.DisableAuto(ctx, &empty.Empty{})
467+
return nil
468+
}
469+
470+
func (c *cli) CmdStatusauto() error {
471+
client := c.GetRPC()
472+
ctx, cancel := context.WithTimeout(context.Background(), defaultRPCTimeout)
473+
defer cancel()
474+
status, _ := client.GetStatusAuto(ctx, &empty.Empty{})
475+
476+
if status.Status {
477+
log.Info("Auto tracked files is enabled")
478+
} else {
479+
log.Info("Auto tracked files is disabled")
480+
}
481+
482+
return nil
483+
}
484+
445485
func (c *cli) CmdRemove(args ...string) error {
446486
cmd := SubCmd("remove", "IDENTIFIER", "Remove an existing mirror")
447487
force := cmd.Bool("f", false, "Never prompt for confirmation")

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func defaultConfig() Configuration {
6161
RPCPassword: "",
6262
MetricsEnabled: false,
6363
MetricsTopFilesRetention: 0,
64+
MetricsAutoTrackedFiles: false,
6465
}
6566
}
6667

@@ -98,6 +99,7 @@ type Configuration struct {
9899

99100
MetricsEnabled bool `yaml:"MetricsEnabled"`
100101
MetricsTopFilesRetention int `yaml:"MetricsTopFilesRetention"`
102+
MetricsAutoTrackedFiles bool `yaml:"MetricsAutoTrackedFiles"`
101103
}
102104

103105
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) {
@@ -147,6 +149,52 @@ func (r *Redis) AddCountry(country string) error {
147149
return err
148150
}
149151

152+
func (r *Redis) AddTrackedFile(file string) error {
153+
conn, err := r.Connect()
154+
if err != nil {
155+
return err
156+
}
157+
defer conn.Close()
158+
159+
exists, err := redis.Int(conn.Do("SISMEMBER", "FILES", file))
160+
if err != nil {
161+
return errors.Wrap(err, "failed to check for file presence")
162+
} else if exists == 0 {
163+
return status.Error(codes.FailedPrecondition,
164+
"file does not exist")
165+
}
166+
167+
exists, err = redis.Int(conn.Do("SADD", "TRACKED_FILES", file))
168+
if err != nil {
169+
return errors.Wrap(err, "failed to add file to metrics")
170+
} else if exists == 0 {
171+
return status.Error(codes.AlreadyExists, "file already is in metrics")
172+
}
173+
return nil
174+
}
175+
176+
func (r *Redis) DeleteTrackedFile(file string) error {
177+
conn, err := r.Connect()
178+
if err != nil {
179+
return err
180+
}
181+
defer conn.Close()
182+
183+
exists, err := redis.Int(conn.Do("SISMEMBER", "TRACKED_FILES", file))
184+
if err != nil {
185+
return errors.Wrap(err, "failed to check for file presence")
186+
} else if exists == 0 {
187+
return status.Error(codes.FailedPrecondition,
188+
"file is not part of tracked files")
189+
}
190+
191+
_, err = conn.Do("SREM", "TRACKED_FILES", file)
192+
if err != nil {
193+
return errors.Wrap(err, "failed to remove file from tracked files")
194+
}
195+
return nil
196+
}
197+
150198
type NetReadyError struct {
151199
error
152200
}

mirrorbits.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,6 @@
140140
## Keep in mind that this will have an impact on the database's RAM usage
141141
## It is not recommended to keep more than a few days of retention.
142142
## MetricsTopFilesRetention: 0
143+
144+
## Enable / Disable automatically adding a new file to tracked files
145+
## 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)