github.com/GuanceCloud/cliutils@v1.1.21/pprofparser/service/storage/disk.go (about)

     1  package storage
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"path/filepath"
     9  	"time"
    10  
    11  	"github.com/GuanceCloud/cliutils/pprofparser/cfg"
    12  	"github.com/GuanceCloud/cliutils/pprofparser/domain/parameter"
    13  )
    14  
    15  var _ Storage = &Disk{}
    16  
    17  type Disk struct {
    18  }
    19  
    20  func (d *Disk) GetProfilePathOld(workspaceUUID string, profileID string, unixTimeNS int64, ossFilename string) string {
    21  	return filepath.Join(d.GetProfileDirOld(workspaceUUID, profileID, unixTimeNS), ossFilename)
    22  }
    23  
    24  func (d *Disk) GetProfileDirOld(workspaceUUID string, profileID string, unixTimeNS int64) string {
    25  	if unixTimeNS >= parameter.MinTimestampMicro && unixTimeNS <= parameter.MaxTimestampMicro {
    26  		unixTimeNS *= 1000
    27  	}
    28  	date := time.Unix(0, unixTimeNS).In(timeZoneCST).Format("20060102")
    29  	return filepath.Join(cfg.Cfg.Storage.Disk.ProfileDir, date, workspaceUUID, profileID)
    30  }
    31  
    32  func (d *Disk) GetProfilePath(workspaceUUID string, profileID string, unixTimeNS int64, ossFilename string) string {
    33  	return d.GetProfileDir(workspaceUUID, profileID, unixTimeNS) + "/" + ossFilename
    34  }
    35  
    36  func (d *Disk) GetProfileDir(workspaceUUID string, profileID string, unixTimeNS int64) string {
    37  	if unixTimeNS >= parameter.MinTimestampMicro && unixTimeNS <= parameter.MaxTimestampMicro {
    38  		unixTimeNS *= 1000
    39  	}
    40  	date := time.Unix(0, unixTimeNS).In(timeZoneCST).Format("20060102")
    41  	return filepath.Join(cfg.Cfg.Storage.Disk.ProfileDir, date, workspaceUUID, profileID[:2], profileID)
    42  }
    43  
    44  func (d *Disk) IsFileExists(path string) (bool, error) {
    45  	stat, err := os.Stat(path)
    46  	if err != nil {
    47  		if errors.Is(err, os.ErrNotExist) {
    48  			return false, nil
    49  		}
    50  		return false, fmt.Errorf("stat file [%s] err: %w", path, err)
    51  	}
    52  	return stat.Mode().IsRegular(), nil
    53  }
    54  
    55  func (d *Disk) ReadFile(path string) (io.ReadCloser, error) {
    56  	f, err := os.Open(path)
    57  	if err != nil {
    58  		return nil, fmt.Errorf("open file [%s] fail: %w", path, err)
    59  	}
    60  	return f, nil
    61  }