Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
monitor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dorian Koch
monitor
Commits
c0dcdd10
Verified
Commit
c0dcdd10
authored
8 months ago
by
Dorian Koch
Browse files
Options
Downloads
Patches
Plain Diff
Add export
parent
1b036148
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
export.py
+74
-0
74 additions, 0 deletions
export.py
with
74 additions
and
0 deletions
export.py
0 → 100644
+
74
−
0
View file @
c0dcdd10
import
os
import
sys
import
csv
import
time
import
psycopg2
def
export_response_times_to_csv
():
# Get environment variables
db_host
=
os
.
getenv
(
'
DB_HOST
'
)
db_name
=
os
.
getenv
(
'
DB_NAME
'
)
db_user
=
os
.
getenv
(
'
DB_USER
'
)
db_password
=
os
.
getenv
(
'
DB_PASSWORD
'
)
try
:
# Log start of export
print
(
"
Starting database export...
"
,
file
=
sys
.
stderr
)
start_time
=
time
.
time
()
# Connect to PostgreSQL database
print
(
f
"
Connecting to database
{
db_name
}
on host
{
db_host
}
...
"
,
file
=
sys
.
stderr
)
conn
=
psycopg2
.
connect
(
host
=
db_host
,
database
=
db_name
,
user
=
db_user
,
password
=
db_password
)
cur
=
conn
.
cursor
()
# Count total rows
cur
.
execute
(
"
SELECT COUNT(*) FROM response_times
"
)
total_rows
=
cur
.
fetchone
()[
0
]
print
(
f
"
Total rows to export:
{
total_rows
}
"
,
file
=
sys
.
stderr
)
# Execute query to fetch all rows from response_times table
print
(
"
Fetching data...
"
,
file
=
sys
.
stderr
)
cur
.
execute
(
"
SELECT * FROM response_times
"
)
# Get column names
column_names
=
[
desc
[
0
]
for
desc
in
cur
.
description
]
# Create a CSV writer that writes to stdout
writer
=
csv
.
writer
(
sys
.
stdout
)
# Write headers
writer
.
writerow
(
column_names
)
# Write data rows with progress tracking
rows_exported
=
0
for
row
in
cur
:
writer
.
writerow
(
row
)
rows_exported
+=
1
# Periodic progress update
if
rows_exported
%
1000
==
0
:
print
(
f
"
Exported
{
rows_exported
}
/
{
total_rows
}
rows...
"
,
file
=
sys
.
stderr
)
# Log completion
end_time
=
time
.
time
()
export_duration
=
end_time
-
start_time
print
(
f
"
\n
Export completed in
{
export_duration
:
.
2
f
}
seconds.
"
,
file
=
sys
.
stderr
)
print
(
f
"
Exported
{
rows_exported
}
rows in total.
"
,
file
=
sys
.
stderr
)
# Close cursor and connection
cur
.
close
()
conn
.
close
()
except
Exception
as
e
:
print
(
f
"
An error occurred:
{
e
}
"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
if
__name__
==
"
__main__
"
:
export_response_times_to_csv
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment