forked from syedamanali77/alerthub-feedback-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_csv.py
More file actions
167 lines (141 loc) · 5.12 KB
/
export_csv.py
File metadata and controls
167 lines (141 loc) · 5.12 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
AlertHub Feedback Data Exporter
===============================
Exports complete processed feedback data to CSV format.
To use:
python export_csv.py
"""
import csv
import os
import psycopg2
import json
from datetime import datetime
from psycopg2.extras import RealDictCursor
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# PostgreSQL credentials
PG_HOST = os.getenv("PG_HOST")
PG_PORT = os.getenv("PG_PORT")
PG_DB = os.getenv("PG_DB")
PG_USER = os.getenv("PG_USER")
PG_PASSWORD = os.getenv("PG_PASSWORD")
def export_feedback_data():
"""Export all processed feedback data to a single CSV file"""
try:
# Create exports directory if it doesn't exist
exports_dir = 'exports'
os.makedirs(exports_dir, exist_ok=True)
# Use fixed filename, will be overwritten each time
csv_path = os.path.join(exports_dir, "feedback_data.csv")
# Connect to PostgreSQL
conn = psycopg2.connect(
host=PG_HOST,
port=PG_PORT,
database=PG_DB,
user=PG_USER,
password=PG_PASSWORD
)
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
# Query to get all processed feedback data
query = """
SELECT
submission_id,
agree_to_followup,
followup_email,
feedback_text as original_feedback,
detected_language,
language_name,
translated_text,
sentiment,
primary_category,
pillar,
confidence,
subcategories,
urgency,
urgency_score,
complexity,
complexity_score,
keywords,
text_length,
word_count
FROM feedback_submissions
ORDER BY submission_id
"""
cursor.execute(query)
rows = cursor.fetchall()
if not rows:
print("No feedback data found to export")
return None
# Convert rows to dictionaries and process JSON fields
processed_rows = []
for row in rows:
row_dict = dict(row)
# Parse subcategories JSON if exists
if row_dict['subcategories']:
try:
subcategories_list = json.loads(row_dict['subcategories'])
row_dict['subcategories'] = '; '.join(subcategories_list) if subcategories_list else ''
except:
row_dict['subcategories'] = str(row_dict['subcategories'])
else:
row_dict['subcategories'] = ''
# Convert boolean fields
row_dict['agree_to_followup'] = 'Yes' if row_dict['agree_to_followup'] else 'No'
# Clean up None values
for key, value in row_dict.items():
if value is None:
row_dict[key] = ''
processed_rows.append(row_dict)
# Define the column order and headers
fieldnames = [
'submission_id',
'agree_to_followup',
'followup_email',
'original_feedback',
'detected_language',
'language_name',
'translated_text',
'sentiment',
'primary_category',
'pillar',
'confidence',
'subcategories',
'urgency',
'urgency_score',
'complexity',
'complexity_score',
'keywords',
'text_length',
'word_count'
]
# Write to CSV
with open(csv_path, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(processed_rows)
print(f"Successfully exported {len(processed_rows)} feedback records")
print(f"File location: {os.path.abspath(csv_path)}")
return csv_path
except Exception as e:
print(f"Error exporting data: {e}")
return None
finally:
if 'conn' in locals():
conn.close()
def main():
"""Main export function"""
print("AlertHub Feedback Data Exporter")
print("=" * 50)
csv_file = export_feedback_data()
if csv_file:
print(f"\nExport completed successfully!")
print(f"CSV file: {os.path.basename(csv_file)}")
# Show file size
file_size = os.path.getsize(csv_file)
file_size_mb = round(file_size / (1024 * 1024), 2)
print(f"File size: {file_size_mb} MB")
else:
print("\nExport failed!")
if __name__ == "__main__":
main()