Skip to content

Commit

Permalink
feat: Add a helper func to measure elapsed time
Browse files Browse the repository at this point in the history
* Profile important functions and log elapsed times in debug mode

* Remove old DB migration files

Signed-off-by: Mahendra Paipuri <[email protected]>
  • Loading branch information
mahendrapaipuri committed Jul 3, 2024
1 parent 6bae211 commit b0c1678
Show file tree
Hide file tree
Showing 26 changed files with 61 additions and 329 deletions.
8 changes: 8 additions & 0 deletions internal/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import (
"net/url"
"os"
"strings"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/google/uuid"
"github.com/mahendrapaipuri/ceems/pkg/grafana"
"github.com/zeebo/xxh3"
"gopkg.in/yaml.v3"
)

// TimeTrack tracks execution time of each function
func TimeTrack(start time.Time, name string, logger log.Logger) {
elapsed := time.Since(start)
level.Debug(logger).Log("msg", name, "elapsed_time", elapsed)
}

// SanitizeFloat replaces +/-Inf and NaN with zero
func SanitizeFloat(v float64) float64 {
if math.IsInf(v, 0) || math.IsNaN(v) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ updatetime:
setup:
// Setup manager struct that retrieves unit data
manager, err := c.ResourceManager(c.Logger)
fmt.Printf("%#v\n", manager)
if err != nil {
level.Error(c.Logger).Log("msg", "Resource manager setup failed", "err", err)
return nil, err
Expand Down Expand Up @@ -450,6 +451,9 @@ func (s *statsDB) execStatements(
clusterUsers []models.ClusterUsers,
clusterProjects []models.ClusterProjects,
) {
// Measure elapsed time
defer common.TimeTrack(time.Now(), "DB insertions", s.logger)

var ignore = 0
var err error
for _, cluster := range clusterUnits {
Expand Down Expand Up @@ -651,6 +655,9 @@ func (s *statsDB) vacuum() error {

// Creates backup of DB after vacuuming DB
func (s *statsDB) createBackup() error {
// Measure elapsed time
defer common.TimeTrack(time.Now(), "DB backup", s.logger)

// First vacuum DB to reduce size
if err := s.vacuum(); err != nil {
level.Warn(s.logger).Log("msg", "Failed to vacuum DB", "err", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/api/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ var mockUsersOne = []models.ClusterUsers{

func newMockManager(logger log.Logger) (*resource.Manager, error) {
return &resource.Manager{
Logger: logger,
Fetchers: []resource.Fetcher{
&mockFetcherOne{logger: logger},
&mockFetcherTwo{logger: logger},
Expand Down
4 changes: 0 additions & 4 deletions pkg/api/db/migrations-copy/000001_create_units_table.down.sql

This file was deleted.

41 changes: 0 additions & 41 deletions pkg/api/db/migrations-copy/000001_create_units_table.up.sql

This file was deleted.

2 changes: 0 additions & 2 deletions pkg/api/db/migrations-copy/000002_create_usage_table.down.sql

This file was deleted.

24 changes: 0 additions & 24 deletions pkg/api/db/migrations-copy/000002_create_usage_table.up.sql

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/api/db/migrations-copy/000003_alter_units_table.down.sql

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/api/db/migrations-copy/000003_alter_units_table.up.sql

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions pkg/api/db/migrations-copy/000006_alter_unit_usage_tables.up.sql

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

67 changes: 0 additions & 67 deletions pkg/api/db/migrations-copy/000010_alter_metric_types.down.sql

This file was deleted.

Loading

0 comments on commit b0c1678

Please sign in to comment.