Question

I'm sorry, I couldn't come up with a better title.

I have a table that has a row inserted every time someone hits my JSON api. In essence it has 2 columns, Ipaddress (nvarchar) and date (datetimeoffset).

Part of my problem is, I'm not sure exactly how to define what data I want, but overall I'm trying to figure out if someone is playing shenanigans on my api.

I think what I'm looking for is average requests per second/minute per ip address.

Could anyone help me write a query that does that?

Was it helpful?

Solution

To see today, by IP, per minute:

DECLARE @day DATE = CURRENT_TIMESTAMP;

WITH x AS (
  SELECT Ipaddress, m = DATEADD(MINUTE, DATEDIFF(MINUTE, @day, [date]), @day)
  FROM dbo.tablename
  WHERE CONVERT(DATE, [date]) = @day
)
SELECT Ipaddress, m, c = COUNT(*)
  FROM x 
  GROUP BY Ipaddress m
  ORDER BY c DESC; -- OR ORDER BY m

You could further derive averages from that data and filter depending on what you consider to be indicative of "shenanigans"...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top