Advertisement
Guest User

SQL to Google Calendar

a guest
Jul 29th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 6.65 KB | None | 0 0
  1. --****************************************************************************************
  2. -- This script returns a (graphical) timeline for data specified in the Job Runtime creation table
  3. --****************************************************************************************
  4. SET nocount ON
  5.  
  6. DECLARE @DT datetime
  7. DECLARE @StartDT datetime
  8. DECLARE @EndDT datetime
  9. DECLARE @MinRuntimeInSec INT
  10. DECLARE @SendMail INT
  11. DECLARE @ReturnRecocordset INT
  12. DECLARE @Emailprofilename VARCHAR(50)
  13. DECLARE @EmailRecipients VARCHAR(50)
  14.  
  15. --***************************************************************************************
  16. -- Set variables
  17. --***************************************************************************************
  18. SET @StartDT = getdate() - 1
  19. SET @EndDT = getdate()
  20. SET @MinRuntimeInSec = 60 --Ignore jobs with runtime smaller then this
  21.  
  22. SET @ReturnRecocordset = 0
  23. SET @SendMail = 1
  24. SET @Emailprofilename = 'YourEmailProfile'
  25. SET @EmailRecipients = 'YourEmail'
  26.  
  27. --***************************************************************************************
  28. -- Pre-run cleanup (just in case)
  29. --***************************************************************************************
  30. IF OBJECT_ID('tempdb..#JobRuntime') IS NOT NULL DROP TABLE #JobRuntime;
  31. IF OBJECT_ID('tempdb..##GoogleGraph') IS NOT NULL DROP TABLE ##GoogleGraph;
  32.  
  33. --***************************************************************************************
  34. -- Create a table for HTML assembly
  35. --***************************************************************************************
  36. CREATE TABLE ##GoogleGraph ([ID] [INT] IDENTITY(1,1) NOT NULL,
  37.                             [HTML] [VARCHAR](8000) NULL);
  38.  
  39. --***************************************************************************************
  40. -- Create the Job Runtime information table
  41. --***************************************************************************************
  42.  
  43.  
  44. -- PUT A SELECT STATEMENT HERE THAT RETURNS TWO COLUMNS
  45. -- COLUMN [dt] (Datetime) and COLUMN [ItemCount] (Integer)
  46. -- SELECT exampleDate AS 'dt'
  47.     , SUM(*) AS 'ItemCount'
  48. INTO #JobRuntime
  49. -- FROM exampleTable
  50. IF NOT EXISTS (SELECT 1 FROM #JobRuntime)
  51.     GOTO NothingToDo
  52.  
  53. --***************************************************************************************
  54. -- Format for google graph - Header
  55. -- (Split into multiple inserts because the default text result setting is 256 chars)
  56. --***************************************************************************************
  57. INSERT INTO ##GoogleGraph (HTML)
  58. SELECT '<html>
  59.     <head>
  60.     <!--<META HTTP-EQUIV="refresh" CONTENT="3">-->
  61.     <script type="text/javascript" src="https://www.google.com/jsapi?autoload={''modules'':[{''name'':''visualization'', ''version'':''1'',''packages'':[''calendar'']}]}"></script>'
  62. INSERT INTO ##GoogleGraph (HTML)
  63. SELECT '    <script type="text/javascript">
  64.     google.setOnLoadCallback(drawChart);
  65.     function drawChart() {'
  66. INSERT INTO ##GoogleGraph (HTML)
  67. SELECT '    var container = document.getElementById(''JobTimeline'');
  68.     var chart = new google.visualization.Calendar(container);
  69.     var dataTable = new google.visualization.DataTable();'
  70. INSERT INTO ##GoogleGraph (HTML)
  71. SELECT '    dataTable.addColumn({ type: ''date'', id: ''Date'' });
  72.       dataTable.addColumn({ type: ''number'', id: ''Number of Tickets'' });
  73.       dataTable.addRows(['
  74.  
  75. --***************************************************************************************
  76. -- Format for google graph - Data
  77. --***************************************************************************************
  78. INSERT INTO ##GoogleGraph (HTML)
  79. SELECT  '       [ '
  80.         +'new Date('
  81.         +     CAST(DATEPART(YEAR,   dt) AS VARCHAR(4))
  82.         +', '+CAST(DATEPART(MONTH,  dt) -1 AS VARCHAR(4)) --Java months count from 0
  83.         +', '+CAST(DATEPART(DAY,    dt) AS VARCHAR(4))
  84.         + ') , '
  85.         + ItemCount
  86.         +  ' ] ,'
  87. FROM    #JobRuntime
  88.  
  89. SELECT *
  90. FROM #JobRuntime
  91. --***************************************************************************************
  92. -- Format for google graph - Footer
  93. --***************************************************************************************
  94. INSERT INTO ##GoogleGraph (HTML)
  95. SELECT '    ]);
  96.  
  97.     var options =
  98.     {
  99.         title: "Items by Day",
  100.         height: 900,
  101.         calendar: { cellSize: 30 }, --MODIFY THIS NUMBER TO SIZE YOUR CALENDAR
  102.     };
  103.  
  104.     chart.draw(dataTable, options);
  105.  
  106. }'
  107. INSERT INTO ##GoogleGraph (HTML)
  108. SELECT '
  109.     </script>
  110.     </head>
  111.     <body>'
  112.     +'<font face="Helvetica" size="3" >'
  113.     +'Job timeline on: '+@@servername
  114.     +' from '+CONVERT(VARCHAR(20), @StartDT, 120)
  115.     +' until '+CONVERT(VARCHAR(20), @EndDT, 120)
  116.     +CASE WHEN @MinRuntimeInSec = 0 THEN '' ELSE ' (hiding jobs with runtime < '+CAST(@MinRuntimeInSec AS VARCHAR(10))+' seconds)' END
  117.     +'</font>
  118.         <div id="JobTimeline" style="width: 1870px; height: 900px;"></div>
  119.     </body>
  120. </html>'
  121.  
  122. --***************************************************************************************
  123. -- Output HTML page - copy output & paste to a .HTML file and open with google chrome
  124. --***************************************************************************************
  125. IF @ReturnRecocordset = 1
  126.     SELECT html FROM ##GoogleGraph ORDER BY ID
  127.  
  128. --***************************************************************************************
  129. -- Send Email -
  130. --***************************************************************************************
  131. IF @SendMail = 1
  132.     EXECUTE msdb.dbo.sp_send_dbmail
  133.          @profile_name = @Emailprofilename
  134.         ,@recipients = @EmailRecipients
  135.         ,@subject = 'JobTimeline'
  136.         ,@body = 'See attachment for JobTimeline, open with Google Chrome!'
  137.         ,@body_format = 'HTML' -- or TEXT
  138.         ,@importance = 'Normal' --Low Normal High
  139.         ,@sensitivity = 'Normal' --Normal Personal Private Confidential
  140.         ,@execute_query_database = 'master'
  141.         ,@query_result_header = 1
  142.         ,@query = 'set nocount on; SELECT HTML FROM ##GoogleGraph'
  143.         ,@query_result_no_padding = 1  -- prevent SQL adding padding spaces in the result
  144.         --,@query_no_truncate= 1       -- mutually exclusive with @query_result_no_padding
  145.         ,@attach_query_result_as_file = 1
  146.         ,@query_attachment_filename= 'JobTimeline.HTML'
  147.  
  148.  
  149. GOTO Cleanup
  150.  
  151. --***************************************************************************************
  152. -- Just in case....
  153. --***************************************************************************************
  154. NothingToDo:
  155.  
  156. print 'No job runtime info found....'
  157.  
  158. --***************************************************************************************
  159. -- Cleanup
  160. --***************************************************************************************
  161. Cleanup:
  162. IF OBJECT_ID('tempdb..#JobRuntime') IS NOT NULL DROP TABLE #JobRuntime;
  163. IF OBJECT_ID('tempdb..##GoogleGraph') IS NOT NULL DROP TABLE ##GoogleGraph;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement