github.com/cs3org/reva/v2@v2.27.7/pkg/metrics/driver/json/json.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package json 20 21 import ( 22 "encoding/json" 23 "errors" 24 "os" 25 26 "github.com/cs3org/reva/v2/pkg/metrics/driver/registry" 27 28 "github.com/cs3org/reva/v2/pkg/logger" 29 "github.com/cs3org/reva/v2/pkg/metrics/config" 30 "github.com/rs/zerolog" 31 ) 32 33 var log zerolog.Logger 34 35 func init() { 36 log = logger.New().With().Int("pid", os.Getpid()).Logger() 37 driver := &MetricsJSONDriver{} 38 registry.Register(driverName(), driver) 39 } 40 41 func driverName() string { 42 return "json" 43 } 44 45 // readJSON always returns a data object but logs the error in case reading the json fails. 46 func readJSON(driver *MetricsJSONDriver) *data { 47 data := &data{} 48 49 file, err := os.ReadFile(driver.metricsDataLocation) 50 if err != nil { 51 log.Error().Err(err).Str("location", driver.metricsDataLocation).Msg("Unable to read json file from location.") 52 } 53 err = json.Unmarshal(file, data) 54 if err != nil { 55 log.Error().Err(err).Msg("Unable to unmarshall json file.") 56 } 57 58 return data 59 } 60 61 type data struct { 62 NumUsers int64 `json:"cs3_org_sciencemesh_site_total_num_users"` 63 NumGroups int64 `json:"cs3_org_sciencemesh_site_total_num_groups"` 64 AmountStorage int64 `json:"cs3_org_sciencemesh_site_total_amount_storage"` 65 } 66 67 // MetricsJSONDriver the JsonDriver struct 68 type MetricsJSONDriver struct { 69 metricsDataLocation string 70 } 71 72 // Configure configures this driver 73 func (d *MetricsJSONDriver) Configure(c *config.Config) error { 74 if c.MetricsDataLocation == "" { 75 err := errors.New("Unable to initialize a metrics data driver, has the data location (metrics_data_location) been configured?") 76 return err 77 } 78 79 d.metricsDataLocation = c.MetricsDataLocation 80 81 return nil 82 } 83 84 // GetNumUsers returns the number of site users 85 func (d *MetricsJSONDriver) GetNumUsers() int64 { 86 return readJSON(d).NumUsers 87 } 88 89 // GetNumGroups returns the number of site groups 90 func (d *MetricsJSONDriver) GetNumGroups() int64 { 91 return readJSON(d).NumGroups 92 } 93 94 // GetAmountStorage returns the amount of site storage used 95 func (d *MetricsJSONDriver) GetAmountStorage() int64 { 96 return readJSON(d).AmountStorage 97 }