github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/config/stats.go (about)

     1  package config
     2  
     3  import (
     4  	"github.com/qri-io/jsonschema"
     5  )
     6  
     7  // Stats configures qri statistical metadata calculation
     8  type Stats struct {
     9  	Cache cache `json:"cache"`
    10  	// For later addition:
    11  	// StopFreqCountThreshold int
    12  }
    13  
    14  // SetArbitrary is an interface implementation of base/fill/struct in order to safely
    15  // consume config files that have definitions beyond those specified in the struct.
    16  // This simply ignores all additional fields at read time.
    17  func (cfg *Stats) SetArbitrary(key string, val interface{}) error {
    18  	return nil
    19  }
    20  
    21  // cache configures the cached storage of stats
    22  type cache struct {
    23  	Type    string `json:"type"`
    24  	MaxSize uint64 `json:"maxsize"`
    25  	Path    string `json:"path,omitempty"`
    26  }
    27  
    28  // DefaultStats creates & returns a new default stats configuration
    29  func DefaultStats() *Stats {
    30  	return &Stats{
    31  		Cache: cache{
    32  			Type: "fs",
    33  			// Default to 25MiB
    34  			MaxSize: 1024 * 25,
    35  		},
    36  	}
    37  }
    38  
    39  // Validate validates all the fields of stats returning all errors found.
    40  func (cfg Stats) Validate() error {
    41  	schema := jsonschema.Must(`{
    42      "$schema": "http://json-schema.org/draft-06/schema#",
    43      "title": "Stats",
    44      "description": "Config for the qri stats cache",
    45      "type": "object",
    46      "required": [
    47        "cache"
    48      ],
    49      "properties": {
    50        "cache": {
    51          "description": "The configuration for the cache that stores recent calculated stats.",
    52          "type": "object",
    53          "required": [
    54            "type",
    55            "maxsize"
    56          ],
    57          "properties": {
    58            "maxsize": {
    59              "description": "The maximum size, in bytes, that the cache should hold",
    60              "type": "number"
    61            },
    62            "path": {
    63              "description": "The path to the cache. Default is empty. If empty, Qri will save the cache in the Qri Path",
    64              "type": "string"
    65            },
    66            "type": {
    67              "description": "Type of cache",
    68              "type": "string",
    69              "enum": [
    70                "fs",
    71                "mem",
    72                "postgres"
    73              ]
    74            }
    75          }
    76        }
    77      }
    78    }`)
    79  	return validate(schema, &cfg)
    80  }
    81  
    82  // Copy returns a deep copy of the Stats struct
    83  func (cfg *Stats) Copy() *Stats {
    84  	return &Stats{
    85  		Cache: cache{
    86  			Type:    cfg.Cache.Type,
    87  			MaxSize: cfg.Cache.MaxSize,
    88  			Path:    cfg.Cache.Path,
    89  		},
    90  	}
    91  }