Skip to content

Commit

Permalink
Fixes bug 733187 - Added a script to export UUIDs from a PostgreSQL d…
Browse files Browse the repository at this point in the history
…atabase to a CSV file.
  • Loading branch information
adngdb committed Mar 21, 2012
1 parent ce3bfd3 commit c1cf4ee
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions scripts/movecrashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

import csv
import datetime
import json
import os
Expand All @@ -40,6 +41,57 @@
)


def export_uuids(path, numberofdays):
"""Export crash report uuids from a PostgreSQL database to a CSV file
path - Directory where the csv file will be created.
numberofdays - Number of days of crash reports to retrieve, before the most
recent crash date.
"""
database = db.Database(config)
connection = database.connection()
cur = connection.cursor()

# steps
# 1. pull all distinct dates
sql = """
SELECT DISTINCT to_char(date_processed, 'YYYY-MM-DD') as day
FROM reports
ORDER BY day DESC
"""
if numberofdays:
sql = "%s LIMIT %s" % (sql, numberofdays)

print 'Calculating dates... '
days = db.execute(cur, sql)

days_list = []
for day in days:
days_list.append(day[0])

store_filename = 'uuids.csv'
store_filename = os.path.normpath('%s/%s' % (path, store_filename))
store_file = open(store_filename, 'w')
store = csv.writer(store_file, delimiter=',', quotechar='"')
print 'Store file created: %s' % store_filename

for day in days_list:
date_from = dtu.datetimeFromISOdateString(day)
date_to = date_from + datetime.timedelta(1)

sql = "SELECT uuid FROM reports WHERE date_processed BETWEEN %s AND %s"

print 'Getting crash reports for day %s' % date_from.date()
crashes_list = db.execute(cur, sql, (date_from, date_to))
for crash in crashes_list:
store.writerow(crash)

store_file.close()
connection.close()
return store_filename


def export(path, numberofdays=0):
"""Export crash reports from a PostgreSQL database.
Expand Down Expand Up @@ -301,6 +353,19 @@ def usage():
cfile = export(path, numberofdays)
print 'Generated crash file: %s' % cfile

if cmd == 'export_uuids':
# default values
path = '.'
numberofdays = 0

if len(args) >= 1:
path = args[0]
if len(args) >= 2:
numberofdays = args[1]

cfile = export_uuids(path, numberofdays)
print 'Generated uuids file: %s' % cfile

elif cmd == 'import':
if len(args) != 2:
usage()
Expand Down

0 comments on commit c1cf4ee

Please sign in to comment.