Calculate sum overlapping minutes in tableau - tableau-api

I have two excel extracts, one containing the schedules of employees and the other containing the actual times the employees worked. There are multiple schedule blocks in a day and employees can have multiple activity start and stop times in or out of scheduled time.
Schedule:
Actvtivity:
I need to use this data to calculate the sum of actual minutes worked only during the scheduled time with Tableau. In the above images the first schedule event would have 115 minutes worked and the second line would have 92 totaling in 207.
I tried to start easy by only calculating activity entries that fall within both the start and stop of a scheduled block, but tableau either return nothing or the "Many to 1" *
attempt 1:
case attr([#Fields:Date]) when ATTR([ScheduleStopStart].[#fields:excDate])
then
if attr([Start]) >= Min([ScheduleStopStart].[Start])
and attr([Stop]) <= max([ScheduleStopStart].[Stop])
then datediff("minute", attr([Start]), attr([Stop]))
END
END
Attempt 2:
IF ATTR([#Fields:Date]) = ATTR([ScheduleStopStart].[#fields:excDate]) then (
if attr([Start]) >= attr([ScheduleStopStart].[Start])
and attr([Stop]) <= attr([ScheduleStopStart].[Stop])
then DATEDIFF("minute", attr([Start]),attr([Stop]))
else 0
end
)
else
0
END

Related

Grafana 2 queries in same panel but set query interval difference

I have 2 queries in a grafana panel.
I want to run Query A every 5 min
I want to run Query B every 10 min, so I can check the value difference between each query using transform.
How can I set the query interval, I know I can change scrape interval but my goal here is to check pending messages and if it doesnt change in 10 min trigger an alert. I am trying to get a count at 1st minute and get count again at 10th minute. check the difference using transform and trigger an alert if no change (messages are not getting processed )
using grafana 7
Thanks !

How to count no of agents who have spent X days in the system in Anylogic

I am building invoice processing simulation model. invoice creation date is the source generation date . Every Invoice will have due date which varies. 31 days, 60 days, 90 days.
Any agent generated at resource I can add these days to the source generation date and get due date. Next step is at point I should know how many invoices are past due date .
e.g 10 invoices are between 1- 5 days, 15 invoices are between 6 to 20 days etc. I have created three variable in the agent population. Start time, end time and Due time
double DueTime= (time()-dateToTime (due_date));
This is calculating DueTime correctly.
Then in the main I am trying to compute the count of agent
int x_1_5= count( invoices, p -> p.DueTime >1 && p.DueTime<=5 );
age_1_5=String.valueOf(x_1_5);
This is giving me 0 as output.
Is there a way to do do this ?
You are only calculating your DueDate variable once at the start of the agent creation.
You need to turn it into a function getDueTime() that returns the double value when called. Just paste return (time()-dateToTime (due_date)); into the function and then in your counter call
int x_1_5= count( invoices, p -> p.getDueTime() >1 && p.getDueTime()<=5 );
Variables do not act like functions and vice versa.
Tried this approach. Created function in Invoice agent and then calling it on the main inside another function. But it is throwing error .

Repeat each date specific number of times

I have gotten so much help form this forum from times to times, but this is the first time I am posting a question.
What I need is:
I have an excel file that is extracted from a machine, which records temperatures every 15 minutes per hour, 24 hours per day, every day per month... (96 rows per day)
In Column A we have the dates
01/09/2017 (x96 times)
02/09/2017 (x96 times)
...
...
...
30/09/2017 (x96 times)
Sometimes this machine gets stuck and stops recording, so until it gets back to work and start recording again, I have already missed some records, which I have to enter by hand.
So, I need a macro, to check if each date of the month has 96 rows in column A and if not then add the missing rows... It happens some dates to have 70 rows, so in that case I need the macro to insert another 26 rows of the same date...
I don't know if this is easy, but it would be very helpful if someone could give me a solution.

Daily counts with TSQL?

I have a site where I record client metrics in a SQL Server 2008 db on every link clicked. I have already written the query to get the daily total clicks, however I want to find out how many times the user clicked within a given timespan (ie. within 5 seconds).
The idea here is to lock out incoming IP addresses that are trying to scrape content. It would be assumed that if more than 5 "clicks" is detected within 5 seconds or the number of daily clicks from a given IP address exceeds some value, that this is a scraping attempt.
I have tried a few variations of the following:
-- when a user clicked more than 5 times in 5 seconds
SELECT DATEADD(SECOND, DATEDIFF(SECOND, 0, ClickTimeStamp), 0) as ClickTimeStamp, COUNT(UserClickID) as [Count]
FROM UserClicks
WHERE DATEDIFF(SECOND, 0, ClickTimeStamp) = 5
GROUP BY IPAddress, ClickTimeStamp
This one in particular returns the following error:
Msg 535, Level 16, State 0, Line 3 The datediff function resulted in
an overflow. The number of dateparts separating two date/time
instances is too large. Try to use datediff with a less precise
datepart.
So once again, I want to use the seconds datepart, which I believe I'm on the right track, but not quite getting it.
Help appreciated. Thanks.
-- UPDATE --
Great suggestions and helped me think that the approach is wrong. The check is going to be made on every click. What I should do is for a given timestamp, check to see if in the last 5 seconds 5 clicks have been recorded from the same IP address. So it would be something like, count the number of clicks for > GetDate() - 5 seconds
Trying the following still isn't giving me an accurate figure.
SELECT COUNT(*)
FROM UserClicks
WHERE ClickTimeStamp >= GetDate() - DATEADD(SECOND, -5, GetDate())
Hoping my syntax is good, I only have oracle to test this on. I'm going to assume you have an ID column called user_id that is unique to that user (is it user_click_id? helpful to include table create statements in these questions when you can)
You'll have to preform a self join on this one. Logic will be take the userclick and join onto userclick on userId = userId and difference on clicktimestamp is between 0-5 seconds. Then it's counting from the subselect.
select u1.user_id, u1.clicktimestamp, u2.clicktimestamp
from userclicks uc1
left join user_clicks uc2
on u2.userk_id = u1.user_id
and datediff(second,u1.ClickTimeStamp,u2.ClickTimeStamp) <= 5
and datediff(second,u1.ClickTimeStamp,u2.ClickTimeStamp) > 0
This select statement should give you the user_id/clicktimestampe and 1 row for every record that is between 0 and 5 seconds apart from that clicktimestamp from the same user. Now it's just a matter of counting all user_id,u1.clicktimestamp combinations and highlighting the ones with 5 or more. Take the above query and turn it into a subselect and pull counts from it:
select u1.user_id, u1.clicktimestamp, count(1)
from
(select u1.user_id, u1.clicktimestamp
from userclicks uc1
left join user_clicks uc2
on u2.userk_id = u1.user_id
and datediff(second,u1.ClickTimeStamp,u2.ClickTimeStamp) <= 5
and datediff(second,u1.ClickTimeStamp,u2.ClickTimeStamp) > 0) a
group by u1.user_id, u1.clicktimestamp
having count(1) >= 5
Wish I could verify my syntax on a MS machine....there might be some typo's in there, but the logic should be good.
An answer for your UPDATE: the problem is in the third line of
SELECT COUNT(*)
FROM UserClicks
WHERE ClickTimeStamp >= GetDate() - DATEADD(SECOND, -5, GetDate())
GetDate() - DATEADD(SECOND, -5, GetDate()) is saying "take the current date time and subtract (the current date time minus five seconds)". I'm not entirely sure what kind of value this produces, but it won't be the one you want.
You still want some kind of time-period, perahps like so:
SELECT count(*)
from UserClicks
where IPAddress = #IPAddress
and ClickTimeStamp between getdate() and dateadd(second, -5, getdate())
I'm a bit uncomfortable using getdate() there--if you have a specific datetime value (accurate to the second), you should probably use it.
Assuming log entries are only entered for current activity -- that is, whenever a new row is inserted, the logged time is for that point in time and never for any prior point in time -- then you should only need to review data for a set period of time, and not have to review "all data" as you are doing now.
Next question is: how frequently do you make this check? If you are concerned with clicks per second, then something between "once per hour" and "once every 24 hours" seems reasonable.
Next up: define your interval. "All clicks per IPAddress within 5 seconds" could go two ways: set window (00-04, 05-09, 10-14, etc), or sliding window(00-04, 01-05, 02-06, etc.) Probably irrelevant with a 5 second window, but perhaps more relevant for longer periods (clicks per "day").
With that, the general approach I'd take is:
Start with earliest point in time you care about (1 hour ago, 24 hours ago)
Set up "buckets", means by which time windows can be identified (00:00:00 - 00:00:04, 00:00:05 - 00:00:09, etc.). This could be done as a temp table.
For all events, calculate number of elapsed seconds since your earliest point
For each bucket, count number of events that hit that bucket, grouped by IPAddress (inner join on the temp table on seconds between lowValue and highValue)
Identify those that exceed your threshold (having count(*) > X), and defenestrate them.

How can I schedule bi-weekly jobs?

My app requires users to schedule recurring events that can recur daily, weekly, monthly, or bi-weekly.
By bi-weekly, I mean every fortnight (14 days) starting from an arbitrary date value provided at the time of creation.
My jobs table has two columns to support this: job_frequency_id and job_frequency_value. I'm able to schedule all types except for bi-weekly.
The first col is an FK to the job_frequencies table; it contains daily, weekly, monthy, bi-weekly values. The job_frequency_value contains the value corresponding to the frequency.
For example: If a job has a job_frquency_id == 3 and job_frequency_value == 10, it will run every 10th day of the month.
How do I add bi-weekly support without tampering with my db structure? I will use the job_frequency_value col to store the start date of the 14 day period, but I'm unsure of the calculation going forward.
Say your starting date is stored as a variable named 'createdDate'.
nextFortnight = DateAdd("ww", job_frequency_value*2, createdDate);
can you wrap your scheduled task in a and set it to run every week?
Something like
<cfif DateDiff('ww',CreateDate(2011,01,01),Today'sDate) MOD 2 EQ 1>
That way if the weeks are odd your scheduled task runs completely and if it's an odd week then it runs the scheduled task, but ignore all your code.