Advertisement
Guest User

Untitled

a guest
Jan 17th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import psycopg2
  4. import psycopg2.extras
  5. from datetime import datetime, date, timedelta
  6.  
  7. hostname = 'localhost'
  8. username = 'julie'
  9. password = 'julie'
  10. database = 'vagrant'
  11. myConnection = psycopg2.connect(host=hostname, user=username, password=password, dbname=database)
  12. myConnection.autocommit = True
  13.  
  14. def create_table(conn):
  15. cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  16. cur.execute(
  17. """
  18. CREATE TABLE transactions_transformed (
  19. email text,
  20. today boolean,
  21. yesterday boolean,
  22. this_week boolean,
  23. medium text,
  24. campaign text,
  25. source text,
  26. content text,
  27. term text,
  28. mailing_id bigint,
  29. amount_dollars double precision,
  30. first_contrib boolean,
  31. source_group text
  32. );
  33. """)
  34.  
  35. def retrieve(conn):
  36. cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  37. cur.execute("""
  38. SELECT k.transaction_email, k.transaction_timestamp, k.utm -> 'medium' as medium,
  39. k.utm -> 'campaign' as campaign,
  40. k.utm -> 'source' as source,
  41. k.utm -> 'content' as content,
  42. k.utm -> 'term' as term,k.is_group1,k.is_group2,
  43. k.amount,
  44. t.earliest
  45. FROM public.transactions k
  46. join (select transaction_email, min(transaction_timestamp) as earliest
  47. from public.transactions
  48. group by transaction_email) t on t.transaction_email = k.transaction_email;
  49. """)
  50. return cur.fetchall()
  51.  
  52. create_table(myConnection)
  53. results = retrieve(myConnection)
  54.  
  55. for row in results:
  56. email = row['transaction_email']
  57. email = email.lower()
  58. transaction_time = row['transaction_timestamp']
  59. if transaction_time.date() == datetime.today().date():
  60. today = True
  61. else:
  62. today = False
  63. if transaction_time.date() == date.today() - timedelta(1):
  64. yesterday = True
  65. else:
  66. yesterday = False
  67. if transaction_time.date() > date.today() - timedelta(7):
  68. past_week = True
  69. else:
  70. past_week = False
  71. medium = row['medium']
  72. source = row['source']
  73. campaign = row['campaign']
  74. content = row['content']
  75. if medium == 'email':
  76. mailing_id = content.split('-')[0]
  77. else:
  78. mailing_id = None
  79. term = row['term']
  80. group1 = row['is_group1']
  81. group2 = row['is_group2']
  82. if group1:
  83. group = 'group1'
  84. elif group2:
  85. group = 'group2'
  86. else:
  87. group = None
  88. amount = row['amount']
  89. earliest_transaction = row['earliest']
  90. if transaction_time == earliest_transaction:
  91. first_transaction = True
  92. else:
  93. first_transaction = False
  94. print email
  95. print today
  96. print yesterday
  97. cur = myConnection.cursor(cursor_factory=psycopg2.extras.DictCursor)
  98. cur.execute("""
  99. insert into public.transactions_transformed
  100. (email,today,yesterday,this_week,medium,campaign,source,content,term,
  101. mailing_id,amount_dollars,first_contrib,source_group)
  102. VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
  103. """,(email,
  104. today,
  105. yesterday,
  106. past_week,
  107. medium,
  108. campaign,
  109. source,
  110. content,
  111. term,
  112. mailing_id,
  113. amount,
  114. first_transaction,
  115. group))
  116.  
  117.  
  118. myConnection.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement