-
Notifications
You must be signed in to change notification settings - Fork 100
Draft: Statistics monitoring #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Skantes
wants to merge
8
commits into
etix:master
Choose a base branch
from
Skantes:monitoring
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4809227
Stats: add per country download counter
Skantes f081f2f
Metrics: add prometheus monitoring metrics route
Skantes ff0098b
Metrics: add detailed metrics for selected files
Skantes 44053a4
Metrics: add daily top 10
Skantes 09cbf02
Metrics: Add auto tracked files mode
Skantes de9c84b
Reload: reload metrics config
Skantes 10b589b
Cli: add metrics status
Skantes 2d20184
Metrics: keep route when disabled
Skantes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,12 @@ | |
| package database | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strconv" | ||
|
|
||
| "github.com/gomodule/redigo/redis" | ||
| "github.com/pkg/errors" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| func (r *Redis) GetListOfMirrors() (map[int]string, error) { | ||
|
|
@@ -41,6 +43,158 @@ func (r *Redis) GetListOfMirrors() (map[int]string, error) { | |
| return mirrors, nil | ||
| } | ||
|
|
||
| func (r *Redis) GetListOfFiles() ([]string, error) { | ||
| conn, err := r.Connect() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| values, err := redis.Values(conn.Do("SMEMBERS", "FILES")) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| files := make([]string, len(values)) | ||
| for i, v := range values { | ||
| value, okValue := v.([]byte) | ||
| if !okValue { | ||
| return nil, errors.New("invalid type for file") | ||
| } | ||
| files[i] = string(value) | ||
| } | ||
|
|
||
| return files, nil | ||
| } | ||
|
|
||
| func (r *Redis) GetListOfTrackedFiles() ([]string, error) { | ||
| conn, err := r.Connect() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| values, err := redis.Values(conn.Do("SMEMBERS", "TRACKED_FILES")) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| files := make([]string, len(values)) | ||
| for i, v := range values { | ||
| value, okValue := v.([]byte) | ||
| if !okValue { | ||
| return nil, errors.New("invalid type for file") | ||
| } | ||
| files[i] = string(value) | ||
| } | ||
|
|
||
| return files, nil | ||
| } | ||
|
|
||
| func (r *Redis) GetListOfCountries() ([]string, error) { | ||
| conn, err := r.Connect() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| values, err := redis.Values(conn.Do("SMEMBERS", "COUNTRIES")) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| countries := make([]string, len(values)) | ||
| for i, v := range values { | ||
| value, okValue := v.([]byte) | ||
| if !okValue { | ||
| return nil, errors.New("invalid type for countries") | ||
| } | ||
| countries[i] = string(value) | ||
| } | ||
|
|
||
| return countries, nil | ||
| } | ||
|
|
||
| func (r *Redis) GetListOfTrackFilesFields(file string) ([]string, error) { | ||
| conn, err := r.Connect() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| values, err := redis.Values(conn.Do("HKEYS", "STATS_TRACKED_"+file)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| files := make([]string, len(values)) | ||
| for i, v := range values { | ||
| value, okValue := v.([]byte) | ||
| if !okValue { | ||
| return nil, errors.New("invalid type for file") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not the error you want to return. |
||
| } | ||
| files[i] = string(value) | ||
| } | ||
|
|
||
| return files, nil | ||
| } | ||
|
|
||
| func (r *Redis) AddCountry(country string) error { | ||
| conn, err := r.Connect() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer conn.Close() | ||
| _, err = conn.Do("SADD", "COUNTRIES", country) | ||
| return err | ||
| } | ||
|
|
||
| func (r *Redis) AddTrackedFile(file string) error { | ||
| conn, err := r.Connect() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| exists, err := redis.Int(conn.Do("SISMEMBER", "FILES", file)) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to check for file presence") | ||
| } else if exists == 0 { | ||
| return status.Error(codes.FailedPrecondition, | ||
| "file does not exist") | ||
| } | ||
|
|
||
| exists, err = redis.Int(conn.Do("SADD", "TRACKED_FILES", file)) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to add file to metrics") | ||
| } else if exists == 0 { | ||
| return status.Error(codes.AlreadyExists, "file already is in metrics") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *Redis) DeleteTrackedFile(file string) error { | ||
| conn, err := r.Connect() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| exists, err := redis.Int(conn.Do("SISMEMBER", "TRACKED_FILES", file)) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to check for file presence") | ||
| } else if exists == 0 { | ||
| return status.Error(codes.FailedPrecondition, | ||
| "file is not part of tracked files") | ||
| } | ||
|
|
||
| _, err = conn.Do("SREM", "TRACKED_FILES", file) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to remove file from tracked files") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type NetReadyError struct { | ||
| error | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want
countriesmore thanfiles.