-- SDA Metrics by 'SDA Spatial Query','SDA Tabular Query','SDA User Interface'
use baflogrpt;
GO
SET STATISTICS IO ON
GO
DROP TABLE IF EXISTS #sdametrics_day
DROP TABLE IF EXISTS #sdametrics_hour
DROP TABLE IF EXISTS #sdametrics2
declare @startLogDateTime datetime
set @startLogDateTime = '2018-06-01T00:00:00'
declare @endLogDateTime datetime
set @endLogDateTime = '2019-06-01T00:00:00'
--Creates table
declare @t table(dt datetime, d varchar(50), h int, y int, m int , SDA_Log_Message varchar(50), SDA_Application_Name varchar(50), logCount int, app int );
insert into @t
--Grabs the log ids for Soil Data Access usage
select
FORMAT (Log.LogDateTime, 'd', 'en-US') AS dt,
FORMAT(Log.LogDateTime, 'dddd') AS d,
DATEPART(hour,Log.LogDateTime) AS h,
datepart(year, Log.LogDateTime) y,
datepart(month, Log.LogDateTime) m,
log.LogMessage AS SDA_Log_Message,
[Application].ApplicationName AS SDA_Application_Name,
log.LogID AS logCount,
1 AS app
from dbo.log AS Log with(nolock)
left outer join dbo.application AS Application with(nolock) on Application.ApplicationID = Log.ApplicationID
where Log.LogDateTime >= @startLogDateTime
and Log.LogDateTime <= @endLogDateTime
and Application.ApplicationName in ('SDA Spatial Query','SDA Tabular Query','SDA User Interface')
and Log.LogType = 'METRIC'
and Log.LogSubtype = 'SDA AUDIT'
;
--Aggregates by hour (24 hour format)
select dt, 1 AS id, d, h , y , m ,
count(DISTINCT logCount) as [hour_count]
INTO #sdametrics_hour
from @t
group by y, m, d, dt, h
order by y, m, d, dt, h
--Hour Metrics
SELECT TOP 1
percentile_cont (0.05) within group (order by hour_count)
over(partition by id) as hour_05_percentile,
percentile_cont (0.25) within group (order by hour_count)
over(partition by id) as hour_25_percentile,
percentile_cont (0.50) within group (order by hour_count)
over(partition by id) as hour_50_percentile,
percentile_cont (0.75) within group (order by hour_count)
over(partition by id) as hour_75_percentile,
percentile_cont (0.95) within group (order by hour_count)
over(partition by id) as hour_95_percentile,
MIN(hour_count) over(partition by id) as min_hour_count,
MAX(hour_count) over(partition by id) as max_hour_count,
AVG(hour_count) over(partition by id) as avg_hour_count
FROM #sdametrics_hour
--Aggregates and sums the log count by Date
SELECT app, dt,
count(DISTINCT logCount) AS sum_day_count,
d
INTO #sdametrics_day
FROM @t
GROUP BY app, dt, d
;
--Aggregates and sums the log count by Day of the week
SELECT DISTINCT
percentile_cont (0.05) within group (order by sum_day_count)
over(partition by d) as day_05_percentile,
percentile_cont (0.25) within group (order by sum_day_count)
over(partition by d) as day_25_percentile,
percentile_cont (0.50) within group (order by sum_day_count)
over(partition by d) as day_50_percentile,
percentile_cont (0.75) within group (order by sum_day_count)
over(partition by d) as day_75_percentile,
percentile_cont (0.95) within group (order by sum_day_count)
over(partition by d) as day_95_percentile,
MIN(sum_day_count) over(partition by d) as min_day_count,
MAX(sum_day_count) over(partition by d) as max_day_count,
AVG(sum_day_count) over(partition by d) as avg_day_count,
d AS [day]
FROM #sdametrics_day
;
--Aggregates and sums the log count for the weekday excluding Saturday and Sunda
SELECT DISTINCT
percentile_cont (0.05) within group (order by sum_day_count)
over(partition by app) as day_05_percentile,
percentile_cont (0.25) within group (order by sum_day_count)
over(partition by app) as day_25_percentile,
percentile_cont (0.50) within group (order by sum_day_count)
over(partition by app) as day_50_percentile,
percentile_cont (0.75) within group (order by sum_day_count)
over(partition by app) as day_75_percentile,
percentile_cont (0.95) within group (order by sum_day_count)
over(partition by app) as day_95_percentile,
MIN(sum_day_count) over(partition by app) as min_day_count,
MAX(sum_day_count) over(partition by app) as max_day_count,
AVG(sum_day_count) over(partition by app) as avg_day_count
FROM #sdametrics_day WHERE d NOT IN ('Saturday', 'Sunday')
;
DROP TABLE IF EXISTS #sdametrics_day
DROP TABLE IF EXISTS #sdametrics_hour
DROP TABLE IF EXISTS #sdametrics2
GO
SET STATISTICS IO OFF
GO