Showing posts with label ssrs. Show all posts
Showing posts with label ssrs. Show all posts

Wednesday, January 22, 2014

Data-Driven Subscriptions in SQL Server

I recently had a project that used data-driven subscriptions for bulk processing of SSRS reports. Subscriptions is a very powerful tool, but it lacks centralized monitoring tools, so I had to dig deep into the content database where SSRS service maintains all processing data. By default, this database is called ReportServer.

List of reports published to SSRS service is stored in Catalog table where each report is assigned an ItemID. Report can be associated with multiple subscriptions. Information about each individual subscription is stored in Subscriptions table. Here is a query that retrieves a list of reports with their corresponding subscriptions:

Select
       c.ItemID,
       c.Name,
       s.*
From
       [dbo].[Catalog] c
       Inner Join [dbo].[Subscriptions] s On s.Report_OID = c.ItemID

Subscription has many parameters defined by the user when subscription is created and stored in various text columns of Subscription table in the form of XML.

For example, ExtensionsSettings column stores information about rendering format, destination and file name settings, etc. It can be accessed by converting XML data into a record set and running XML query functions on it such as:

;WITH x AS (Select SubscriptionID, CAST(ExtensionSettings as xml) as ExtensionSettingsXML From [dbo].[Subscriptions])
Select
       s.SubscriptionID
       ,ExtensionSettingsXML
       ,ExtensionSettingsXML.v.value ('Name[1]', 'varchar(100)') as ParamValue
       ,ExtensionSettingsXML.v.value ('Field[1]', 'varchar(100)') as ParamValue
       ,ExtensionSettingsXML.v.value ('Value[1]', 'varchar(100)') as ParamValue
From
       [dbo].[Subscriptions] s
       Inner Join x On x.SubscriptionID = s.SubscriptionID
       CROSS APPLY ExtensionSettingsXML.nodes ('/ParameterValues/ParameterValue') as ExtensionSettingsXML(v)

When Subscription is initiated, it creates a record in ActiveSubscriptions table for each executing instance. Once processed, this record is removed.

Select * From [dbo].[ActiveSubscriptions]

Data-Driven Subscription works by generating a dataset (by executing a query) that provides values for report input, mapping those values to report parameters and queuing report execution. You can also use input data set attributes to specify file name for report rendering, control location where files are saved, and other parameters.
Table Events serves a role of execution queue where a record is created for each actual instance of the report with set parameters. For example, if your input data set contains 5000 records, Events table will have 5000 records one for each instance of report execution. Records are deleted from Events table as soon as report execution is complete.

Select * From [dbo].[Event]

With all that information in mind, you can create a SSRS report that queries ReportServer database and reports status of all active subscriptions. For example, you can use the following query to get this information:

Select
       s.SubscriptionID,
       'ReportName' = c.Name,
       'ReportPath' = c.Path,
       'SubscriptionDesc' = s.Description,
       'SubscriptionOwner' = us.UserName,
       'LatestStatus' = s.LastStatus,
       'LastRun' = s.LastRunTime,
       asub.ActiveID,
       asub.TotalNotifications,
       asub.TotalSuccesses,
       asub.TotalFailures,
       n.RemaininReports
From
       Subscriptions s
       join Catalog c on c.ItemID = s.Report_OID
       join ReportSchedule rs on rs.SubscriptionID = s.SubscriptionID
       join Users uc on uc.UserID = c.ModifiedByID
       join Users us on us.UserID = s.OwnerId
       Left Join ActiveSubscriptions asub On asub.SubscriptionID = s.SubscriptionID
       Left Join
              (
                     Select
                           SubscriptionID,
                           ActivationID,
                           count(*) as RemaininReports
                     From
                           Notifications n
                     Group by
                           SubscriptionID, ActivationID
              ) n On asub.ActiveID = n.ActivationID

Base on this users will be able to monitor existing subscriptions and see their last status as well as monitor active subscriptions and their progress.

Tuesday, December 18, 2012

Vertical and horizontal lines or strips on Line Chart in SSRS


How do you show a vertical or horizontal line on a line chart in SSRS? After hours or research it seems that approach involving StripLines works the best.
Horizontal or vertical likes can be used on the chart to indicate baselines or highlight particular sections as on the sample below:


So, how do you build a chart like that? Start with the data set.
My sample dataset contains a simple query that returns: month (as date) for X axis, value for Y axis, baseline value for Y axis, baseline value for X axis (date). In order to properly plot the vertical baseline the date used as a baseline value for X axis must be converted to its integer representation. 

Here is the query:

Declare @table Table (id int identity(1,1), event_date datetime, event_value int)

Insert @table 
Select 'Jan 1, 2010', 200
Union Select 'Feb 1, 2010', 250
Union Select 'Mar 1, 2010', 300
Union Select 'Apr 1, 2010', 200
Union Select 'May 1, 2010', 150
Union Select 'Jun 1, 2010', 50
Union Select 'Jul 1, 2010', 300
Union Select 'Aug 1, 2010', 400
Union Select 'Sep 1, 2010', 200
Union Select 'Oct 1, 2010', 150
Union Select 'Nov 1, 2010', 100
Union Select 'Dec 1, 2010', 100

Declare @baseline_value int,
@baseline_date datetime

Set @baseline_value  = 250
Set @baseline_date = 'June 1, 2010'

Select 
event_date,
event_value,
@baseline_value as baseline_value,
@baseline_date as baseline_date,
CAST(@baseline_date as int) as baseline_date_int
From 
@table

The result of the query looks like this:


Create a new report and use above query as a data source. Add new Line Chart object, associate it with the data source. Select “event_value” column to be used for Values and “event_date” column will be used for Category Groups.

Modify Horizontal Axis properties as highlighted below:


Format axis label as "MMM yyyy".

Click on X axis and in the properties window find "StipLines" attribute. Click on "Collection" to open collection editor.

Modify attributes as highlighted below:


IntervalOffset value is set using formula "=CInt(Max(Fields!baseline_date_int.Value))".

Do the same steps for Y axis. IntervalOffset value is set using formula "=CInt(Max(Fields!baseline_value.Value))"

Preview the report and enjoy the result!

You can use the same technique to highlight areas on X or Y axis as below. Click here to download sample "Vertical and Horizontal Lines on line graph.rdl" for more details.