Skip to content
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

HSW-92 #14

Merged
merged 9 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions basedirs/basedirs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestBaseDirs(t *testing.T) {
mainTable, errr := bdr.GroupUsage(db.DGUTAgeAll)
fixUsageTimes(mainTable)

expectedUsageTable := []*basedirs.Usage{
expectedUsageTable := []*basedirs.Usage{ //nolint:dupl
{
Name: "group1", GID: 1, UIDs: []uint32{101}, Owner: "Alan", BaseDir: projectA,
UsageSize: halfGig + twoGig, QuotaSize: 4000000000, UsageInodes: 2,
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestBaseDirs(t *testing.T) {
mainTable, errr = bdr.GroupUsage(db.DGUTAgeA3Y)
fixUsageTimes(mainTable)

expectedUsageTable = []*basedirs.Usage{
expectedUsageTable = []*basedirs.Usage{ //nolint:dupl
{
Name: "group1", GID: 1, UIDs: []uint32{101}, Owner: "Alan", BaseDir: projectA,
UsageSize: halfGig + twoGig, QuotaSize: 4000000000, UsageInodes: 2,
Expand Down Expand Up @@ -284,7 +284,7 @@ func TestBaseDirs(t *testing.T) {
mainTable, errr = bdr.GroupUsage(db.DGUTAgeA7Y)
fixUsageTimes(mainTable)

expectedUsageTable = []*basedirs.Usage{
expectedUsageTable = []*basedirs.Usage{ //nolint:dupl
{
Name: "group1", GID: 1, UIDs: []uint32{101}, Owner: "Alan", BaseDir: projectA,
UsageSize: halfGig + twoGig, QuotaSize: 4000000000, UsageInodes: 2,
Expand Down Expand Up @@ -325,7 +325,7 @@ func TestBaseDirs(t *testing.T) {
mainTable, errr = bdr.UserUsage(db.DGUTAgeAll)
fixUsageTimes(mainTable)

expectedMainTable := []*basedirs.Usage{
expectedMainTable := []*basedirs.Usage{ //nolint:dupl
{
Name: "88888", UID: 88888, GIDs: []uint32{2}, BaseDir: projectC1, UsageSize: 40,
UsageInodes: 1, Mtime: expectedMtime,
Expand Down Expand Up @@ -629,7 +629,7 @@ func TestBaseDirs(t *testing.T) {
},
}

Convey("getting subdir information for a group-basedir", func() {
Convey("getting subdir information for a group-basedir", func() { //nolint:dupl
unknownProject, errr := bdr.GroupSubDirs(1, "unknown", db.DGUTAgeAll)
So(errr, ShouldBeNil)
So(unknownProject, ShouldBeNil)
Expand Down Expand Up @@ -1225,14 +1225,16 @@ func fixSubDirTimes(sds []*basedirs.SubDir) {
}
}

func sortByDatabaseKeyOrder(usageTable []*basedirs.Usage) {
func sortByDatabaseKeyOrder(usageTable []*basedirs.Usage) []*basedirs.Usage {
if usageTable[0].UID != 0 {
sortByUID(usageTable)

return
return usageTable
}

sortByGID(usageTable)

return usageTable
}

func idToByteSlice(id uint32) []byte {
Expand Down
171 changes: 171 additions & 0 deletions basedirs/multi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*******************************************************************************
* Copyright (c) 2025 Genome Research Ltd.
*
* Authors: Michael Woolnough <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/

package basedirs

import (
"github.com/hashicorp/go-multierror"
"github.com/ugorji/go/codec"
"github.com/wtsi-hgi/wrstat-ui/db"
)

type MultiReader []*BaseDirReader

// OpenMulti opens a BaseDirReader for each path specified.
func OpenMulti(ownersPath string, paths ...string) (MultiReader, error) { //nolint:funlen
mp, err := getMountPoints()
if err != nil {
return nil, err
}

owners, err := parseOwners(ownersPath)
if err != nil {
return nil, err
}

mr := make(MultiReader, len(paths))
ch := new(codec.BincHandle)
groupCache, userCache := NewGroupCache(), NewUserCache()

for n, path := range paths {
db, err := OpenDBRO(path)
if err != nil {
return nil, err
}

mr[n] = &BaseDirReader{
db: db,
ch: ch,
mountPoints: mp,
groupCache: groupCache,
userCache: userCache,
owners: owners,
}
}

return mr, nil
}

// Close closes each database.
func (m MultiReader) Close() (err error) {
for _, r := range m {
if errr := r.Close(); err != nil {
err = multierror.Append(err, errr)
}
}

return err
}

// GroupUsage returns the usage for every GID-BaseDir combination in the
// databases.
func (m MultiReader) GroupUsage(age db.DirGUTAge) ([]*Usage, error) {
return m.usage(GroupUsageBucket, age)
}

func (m MultiReader) usage(bucket string, age db.DirGUTAge) ([]*Usage, error) {
var usage []*Usage

for _, r := range m {
u, err := r.usage(bucket, age)
if err != nil {
return nil, err
}

usage = append(usage, u...)
}

return usage, nil
}

// UserUsage returns the usage for every UID-BaseDir combination in the
// databases.
func (m MultiReader) UserUsage(age db.DirGUTAge) ([]*Usage, error) {
return m.usage(UserUsageBucket, age)
}

// GroupSubDirs returns a slice of SubDir, one for each subdirectory of the
// given basedir, owned by the given group. If basedir directly contains files,
// one of the SubDirs will be for ".".
func (m MultiReader) GroupSubDirs(gid uint32, basedir string, age db.DirGUTAge) ([]*SubDir, error) {
return m.subDirs(GroupSubDirsBucket, gid, basedir, age)
}

func (m MultiReader) subDirs(bucket string, id uint32, basedir string, age db.DirGUTAge) ([]*SubDir, error) {
for _, r := range m {
s, err := r.subDirs(bucket, id, basedir, age)
if err != nil {
return nil, err
} else if s != nil {
return s, nil
}
}

return nil, nil
}

// UserSubDirs returns a slice of SubDir, one for each subdirectory of the
// given basedir, owned by the given user. If basedir directly contains files,
// one of the SubDirs will be for ".".
func (m MultiReader) UserSubDirs(uid uint32, basedir string, age db.DirGUTAge) ([]*SubDir, error) {
return m.subDirs(UserSubDirsBucket, uid, basedir, age)
}

// SetMountPoints can be used to manually set your mountpoints, if the automatic
// discovery of mountpoints on your system doesn't work.
func (m MultiReader) SetMountPoints(mountpoints []string) {
for _, r := range m {
r.mountPoints = mountpoints
}
}

// History returns a slice of History values for the given gid and path, one
// value per Date the information was calculated.
func (m MultiReader) History(gid uint32, path string) ([]History, error) {
for _, r := range m {
h, err := r.History(gid, path)
if err != nil {
return nil, err
} else if h != nil {
return h, nil
}
}

return nil, nil
}

// SetCachedGroup sets the name of a specified GID.
func (m MultiReader) SetCachedGroup(gid uint32, name string) {
if len(m) > 0 {
m[0].SetCachedGroup(gid, name)
}
}

// SetCachedUser sets the name of a specified UID.
func (m MultiReader) SetCachedUser(uid uint32, name string) {
if len(m) > 0 {
m[0].SetCachedUser(uid, name)
}
}
Loading
Loading