Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Remove duplicated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Aug 29, 2021
1 parent ec663ac commit 3d4f148
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 116 deletions.
20 changes: 4 additions & 16 deletions internal/collector/pgbouncer_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,43 +159,31 @@ func parsePgbouncerPoolsStats(r *model.PGResult, labelNames []string) map[string
continue
}

s := stats[poolname]

// Update stats struct
switch string(colname.Name) {
case "cl_active":
s := stats[poolname]
s.clActive = v
stats[poolname] = s
case "cl_waiting":
s := stats[poolname]
s.clWaiting = v
stats[poolname] = s
case "sv_active":
s := stats[poolname]
s.svActive = v
stats[poolname] = s
case "sv_idle":
s := stats[poolname]
s.svIdle = v
stats[poolname] = s
case "sv_used":
s := stats[poolname]
s.svUsed = v
stats[poolname] = s
case "sv_tested":
s := stats[poolname]
s.svTested = v
stats[poolname] = s
case "sv_login":
s := stats[poolname]
s.svLogin = v
stats[poolname] = s
case "maxwait":
s := stats[poolname]
s.maxWait = v
stats[poolname] = s
default:
continue
}

stats[poolname] = s
}
}

Expand Down
18 changes: 4 additions & 14 deletions internal/collector/pgbouncer_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,39 +151,29 @@ func parsePgbouncerStatsStats(r *model.PGResult, labelNames []string) map[string
continue
}

s := stats[databaseFQName]

// Run column-specific logic
switch string(colname.Name) {
case "total_xact_count":
s := stats[databaseFQName]
s.xacts = v
stats[databaseFQName] = s
case "total_query_count":
s := stats[databaseFQName]
s.queries = v
stats[databaseFQName] = s
case "total_received":
s := stats[databaseFQName]
s.received = v
stats[databaseFQName] = s
case "total_sent":
s := stats[databaseFQName]
s.sent = v
stats[databaseFQName] = s
case "total_xact_time":
s := stats[databaseFQName]
s.xacttime = v
stats[databaseFQName] = s
case "total_query_time":
s := stats[databaseFQName]
s.querytime = v
stats[databaseFQName] = s
case "total_wait_time":
s := stats[databaseFQName]
s.waittime = v
stats[databaseFQName] = s
default:
continue
}

stats[databaseFQName] = s
}
}

Expand Down
14 changes: 4 additions & 10 deletions internal/collector/postgres_conflicts.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,31 +108,25 @@ func parsePostgresConflictStats(r *model.PGResult, labelNames []string) map[stri
continue
}

s := stats[databaseFQName]

// Run column-specific logic
switch string(colname.Name) {
case "confl_tablespace":
s := stats[databaseFQName]
s.tablespace = v
stats[databaseFQName] = s
case "confl_lock":
s := stats[databaseFQName]
s.lock = v
stats[databaseFQName] = s
case "confl_snapshot":
s := stats[databaseFQName]
s.snapshot = v
stats[databaseFQName] = s
case "confl_bufferpin":
s := stats[databaseFQName]
s.bufferpin = v
stats[databaseFQName] = s
case "confl_deadlock":
s := stats[databaseFQName]
s.deadlock = v
stats[databaseFQName] = s
default:
continue
}

stats[databaseFQName] = s
}
}

Expand Down
10 changes: 4 additions & 6 deletions internal/collector/postgres_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,21 @@ func parsePostgresFunctionsStats(r *model.PGResult, labelNames []string) map[str
continue
}

s := stats[functionFQName]

// Run column-specific logic
switch string(colname.Name) {
case "calls":
s := stats[functionFQName]
s.calls = v
stats[functionFQName] = s
case "total_time":
s := stats[functionFQName]
s.totaltime = v
stats[functionFQName] = s
case "self_time":
s := stats[functionFQName]
s.selftime = v
stats[functionFQName] = s
default:
continue
}

stats[functionFQName] = s
}
}

Expand Down
16 changes: 4 additions & 12 deletions internal/collector/postgres_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,34 +187,26 @@ func parsePostgresIndexStats(r *model.PGResult, labelNames []string) map[string]
continue
}

s := stats[indexname]

switch string(colname.Name) {
case "idx_scan":
s := stats[indexname]
s.idxscan = v
stats[indexname] = s
case "idx_tup_read":
s := stats[indexname]
s.idxtupread = v
stats[indexname] = s
case "idx_tup_fetch":
s := stats[indexname]
s.idxtupfetch = v
stats[indexname] = s
case "idx_blks_read":
s := stats[indexname]
s.idxread = v
stats[indexname] = s
case "idx_blks_hit":
s := stats[indexname]
s.idxhit = v
stats[indexname] = s
case "size_bytes":
s := stats[indexname]
s.sizebytes = v
stats[indexname] = s
default:
continue
}

stats[indexname] = s
}
}

Expand Down
22 changes: 4 additions & 18 deletions internal/collector/postgres_replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,47 +217,33 @@ func parsePostgresReplicationStats(r *model.PGResult, labelNames []string) map[s
continue
}

s := stats[pid]

// Run column-specific logic
switch string(colname.Name) {
case "pending_lag_bytes":
s := stats[pid]
s.values["pending_lag_bytes"] = v
stats[pid] = s
case "write_lag_bytes":
s := stats[pid]
s.values["write_lag_bytes"] = v
stats[pid] = s
case "flush_lag_bytes":
s := stats[pid]
s.values["flush_lag_bytes"] = v
stats[pid] = s
case "replay_lag_bytes":
s := stats[pid]
s.values["replay_lag_bytes"] = v
stats[pid] = s
case "write_lag_seconds":
s := stats[pid]
s.values["write_lag_seconds"] = v
stats[pid] = s
case "flush_lag_seconds":
s := stats[pid]
s.values["flush_lag_seconds"] = v
stats[pid] = s
case "replay_lag_seconds":
s := stats[pid]
s.values["replay_lag_seconds"] = v
stats[pid] = s
case "total_lag_bytes":
s := stats[pid]
s.values["total_lag_bytes"] = v
stats[pid] = s
case "total_lag_seconds":
s := stats[pid]
s.values["total_lag_seconds"] = v
stats[pid] = s
default:
continue
}

stats[pid] = s
}
}

Expand Down
6 changes: 4 additions & 2 deletions internal/collector/postgres_replication_slots.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,17 @@ func parsePostgresReplicationSlotStats(r *model.PGResult, labelNames []string) m
continue
}

s := stats[slotFQName]

// Run column-specific logic
switch string(colname.Name) {
case "since_restart_bytes":
s := stats[slotFQName]
s.retainedBytes = v
stats[slotFQName] = s
default:
continue
}

stats[slotFQName] = s
}
}

Expand Down
42 changes: 4 additions & 38 deletions internal/collector/postgres_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,87 +362,53 @@ func parsePostgresStatementsStats(r *model.PGResult, labelNames []string) map[st
continue
}

s := stats[statement]

// Run column-specific logic
switch string(colname.Name) {
case "calls":
s := stats[statement]
s.calls += v
stats[statement] = s
case "rows":
s := stats[statement]
s.rows += v
stats[statement] = s
case "total_time", "total_exec_time":
s := stats[statement]
s.totalExecTime += v
stats[statement] = s
case "total_plan_time":
s := stats[statement]
s.totalPlanTime += v
stats[statement] = s
case "blk_read_time":
s := stats[statement]
s.blkReadTime += v
stats[statement] = s
case "blk_write_time":
s := stats[statement]
s.blkWriteTime += v
stats[statement] = s
case "shared_blks_hit":
s := stats[statement]
s.sharedBlksHit += v
stats[statement] = s
case "shared_blks_read":
s := stats[statement]
s.sharedBlksRead += v
stats[statement] = s
case "shared_blks_dirtied":
s := stats[statement]
s.sharedBlksDirtied += v
stats[statement] = s
case "shared_blks_written":
s := stats[statement]
s.sharedBlksWritten += v
stats[statement] = s
case "local_blks_hit":
s := stats[statement]
s.localBlksHit += v
stats[statement] = s
case "local_blks_read":
s := stats[statement]
s.localBlksRead += v
stats[statement] = s
case "local_blks_dirtied":
s := stats[statement]
s.localBlksDirtied += v
stats[statement] = s
case "local_blks_written":
s := stats[statement]
s.localBlksWritten += v
stats[statement] = s
case "temp_blks_read":
s := stats[statement]
s.tempBlksRead += v
stats[statement] = s
case "temp_blks_written":
s := stats[statement]
s.tempBlksWritten += v
stats[statement] = s
case "wal_records":
s := stats[statement]
s.walRecords += v
stats[statement] = s
case "wal_fpi":
s := stats[statement]
s.walFPI += v
stats[statement] = s
case "wal_bytes":
s := stats[statement]
s.walBytes += v
stats[statement] = s
default:
continue
}

stats[statement] = s
}
}

Expand Down

0 comments on commit 3d4f148

Please sign in to comment.