-
Notifications
You must be signed in to change notification settings - Fork 54
/
buffercache.go
84 lines (68 loc) · 2.34 KB
/
buffercache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package reports
import (
"context"
"database/sql"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/pganalyze/collector/input/postgres"
"github.com/pganalyze/collector/output/pganalyze_collector"
"github.com/pganalyze/collector/state"
"github.com/pganalyze/collector/util"
)
// BuffercacheReport - Report on the Postgres buffer cache
type BuffercacheReport struct {
ReportRunID string
CollectedAt time.Time
Data state.PostgresBuffercache
}
// RunID - Returns the ID of this report run
func (report BuffercacheReport) RunID() string {
return report.ReportRunID
}
// ReportType - Returns the type of the report as a string
func (report BuffercacheReport) ReportType() string {
return "buffercache"
}
// Run the report
func (report *BuffercacheReport) Run(ctx context.Context, server *state.Server, logger *util.Logger, connection *sql.DB) (err error) {
systemType := server.Config.SystemType
report.Data, err = postgres.GetBuffercache(ctx, logger, connection, systemType, server.Config.IgnoreSchemaRegexp)
if err != nil {
return
}
return
}
// Result of the report
func (report *BuffercacheReport) Result() *pganalyze_collector.Report {
var r pganalyze_collector.Report
var data pganalyze_collector.BuffercacheReportData
var exists bool
r.ReportRunId = report.ReportRunID
r.ReportType = "buffercache"
r.CollectedAt = timestamppb.New(report.CollectedAt)
data.FreeBytes = report.Data.FreeBytes
data.TotalBytes = report.Data.TotalBytes
databaseNameToIdx := make(map[string]int32)
for _, entry := range report.Data.Entries {
e := pganalyze_collector.BuffercacheEntry{Bytes: entry.Bytes, Toast: entry.Toast}
e.DatabaseIdx, exists = databaseNameToIdx[entry.DatabaseName]
if !exists {
ref := pganalyze_collector.DatabaseReference{Name: entry.DatabaseName}
e.DatabaseIdx = int32(len(data.DatabaseReferences))
data.DatabaseReferences = append(data.DatabaseReferences, &ref)
databaseNameToIdx[entry.DatabaseName] = e.DatabaseIdx
}
if entry.SchemaName != nil {
e.SchemaName = *entry.SchemaName
}
if entry.ObjectName != nil {
e.ObjectName = *entry.ObjectName
}
if entry.ObjectKind != nil {
e.ObjectKind = *entry.ObjectKind
}
data.BuffercacheEntries = append(data.BuffercacheEntries, &e)
}
r.Data = &pganalyze_collector.Report_BuffercacheReportData{BuffercacheReportData: &data}
return &r
}