github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/usageStats/compressedstats.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package usageStats
    18  
    19  import (
    20  	"encoding/csv"
    21  	"os"
    22  	"strconv"
    23  	"strings"
    24  	"sync/atomic"
    25  	"time"
    26  
    27  	log "github.com/sirupsen/logrus"
    28  )
    29  
    30  type CompressedStats struct {
    31  	CompressedBytes int64
    32  	TimeStamp       time.Time
    33  }
    34  
    35  var cstats map[uint64]*CompressedStats
    36  
    37  func init() {
    38  	cstats = make(map[uint64]*CompressedStats)
    39  	//cstats = CompressedStats{CompressedBytes: 0}
    40  }
    41  
    42  func getCompressedStatsFilename(baseDir string) string {
    43  	var sb strings.Builder
    44  
    45  	err := os.MkdirAll(baseDir, 0764)
    46  	if err != nil {
    47  		return ""
    48  	}
    49  	sb.WriteString(baseDir)
    50  	sb.WriteString("compressed_stats.csv")
    51  	return sb.String()
    52  }
    53  
    54  func UpdateCompressedStats(segFileSize int64, orgid uint64) {
    55  	if _, ok := cstats[orgid]; !ok {
    56  		cstats[orgid] = &CompressedStats{}
    57  	}
    58  	atomic.AddInt64(&cstats[orgid].CompressedBytes, segFileSize)
    59  }
    60  
    61  func flushCompressedStatsToFile(orgid uint64) error {
    62  	if _, ok := cstats[orgid]; ok {
    63  		if cstats[orgid].CompressedBytes > 0 {
    64  			cstats[orgid].TimeStamp = time.Now().UTC()
    65  			filename := getCompressedStatsFilename(GetBaseStatsDir(orgid))
    66  			fd, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    67  			if err != nil {
    68  				return err
    69  			}
    70  			defer fd.Close()
    71  			w := csv.NewWriter(fd)
    72  			var crecords [][]string
    73  			var crecord []string
    74  			compressedBytesAsString := strconv.FormatInt(cstats[orgid].CompressedBytes, 10)
    75  			crecord = append(crecord, compressedBytesAsString, cstats[orgid].TimeStamp.String())
    76  			crecords = append(crecords, crecord)
    77  			err = w.WriteAll(crecords)
    78  			if err != nil {
    79  				log.Errorf("flushCompressedStatsToFile: write failed, err=%v", err)
    80  				return err
    81  			}
    82  
    83  			log.Debugf("flushCompressedStatsToFile: flushed stats segFileSize=%v, timestamp=%v", cstats[orgid].CompressedBytes, cstats[orgid].TimeStamp)
    84  
    85  			atomic.StoreInt64(&cstats[orgid].CompressedBytes, 0)
    86  		}
    87  	}
    88  	return nil
    89  }