Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import json
  2. import psycopg2
  3. import sys
  4. import time
  5.  
  6.  
  7.  
  8. # Source database & query which monitors job queue
  9. connStringSource = "host=<hostname> port=8060 dbname=workgroup user=<tableau or read-only> password=<password>"
  10. sqlSource= "SELECT job_name, \
  11. Case \
  12. WHEN progress = -1 then 'Pending'\
  13. WHEN progress < 100 then 'In Process'\
  14. WHEN finish_code = 0 then 'Success'\
  15. ELSE 'Error'\
  16. END as Status,\
  17. COUNT(job_name),\
  18. now() \
  19. FROM background_jobs \
  20. WHERE job_name IN ('Refresh Extracts', 'Increment Extracts', 'Subscription Notifications') \
  21. GROUP BY\
  22. job_name, Case \
  23. WHEN progress = -1 then 'Pending'\
  24. WHEN progress < 100 then 'In Process'\
  25. WHEN finish_code = 0 then 'Success'\
  26. ELSE 'Error'\
  27. END, now()"
  28.  
  29. # Target database to host results
  30. connStringTarget = "host=foo.bar.rds.amazonaws.com port=5432 dbname=<some_database> user=<user> password=<pw>"
  31. sqlTarget = "INSERT INTO backgrounder_activity (job_name, status, status_count, measurement_time) VALUES (%s,%s, %s, %s)"
  32.  
  33. while 1:
  34. sourceConnection = psycopg2.connect(connStringSource)
  35. sourceCursor = sourceConnection.cursor()
  36. sourceCursor.execute(sqlSource)
  37. # Get job queue
  38. rows = sourceCursor.fetchall()
  39. sourceCursor.close()
  40. sourceConnection.close()
  41.  
  42. targetConnection = psycopg2.connect(connStringTarget)
  43. targetCursor = targetConnection.cursor()
  44.  
  45. for row in rows:
  46. targetCursor.execute(sqlTarget, (row[0], row[1], row[2],row[3] ))
  47. targetConnection.commit()
  48. #print (cursor.mogrify(sql, (1477, 'someMachine', timeslice['from'], metric['name'], timeslice['values']['average_value'] )))
  49.  
  50. targetCursor.close()
  51. targetConnection.close()
  52. print('Data Recorded')
  53. time.sleep(60)
  54.  
  55. #-- CREATE TABLE STATEMENTS:
  56. # CREATE TABLE public.backgrounder_activity
  57. #(
  58. # id integer NOT NULL DEFAULT nextval('backgrounder_activity_id_seq'::regclass),
  59. # job_name character varying(50) COLLATE pg_catalog."default",
  60. # status character varying(50) COLLATE pg_catalog."default",
  61. # status_count integer,
  62. # measurement_time timestamp without time zone,
  63. # CONSTRAINT pk_id PRIMARY KEY (id)
  64. #)
  65. #WITH (
  66. # OIDS = FALSE
  67. #)
  68. #TABLESPACE pg_default;
  69. #
  70. #ALTER TABLE public.backgrounder_activity
  71. # OWNER to postgres;
  72. #
  73. #-- Index: idx_id
  74. #
  75. #-- DROP INDEX public.idx_id;
  76. #
  77. #CREATE UNIQUE INDEX idx_id
  78. # ON public.backgrounder_activity USING btree
  79. # (id)
  80. # TABLESPACE pg_default;
  81. #
  82. #-- Index: idx_job_status
  83. #
  84. #-- DROP INDEX public.idx_job_status;
  85. #
  86. #CREATE INDEX idx_job_status
  87. # ON public.backgrounder_activity USING btree
  88. # (job_name COLLATE pg_catalog."default", status COLLATE pg_catalog."default")
  89. # TABLESPACE pg_default;
  90. #
  91. #ALTER TABLE public.backgrounder_activity
  92. # CLUSTER ON idx_job_status;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement