2

Hoping that someone can offer some assistance, I am currently working on a piece of code which seems to be having issues. Unfortunately I cant post the underlying data but will try and explain.

The code currently cycles through a selection of kpi's and calculates if something needs to be run or not based on the current time and whether the individual kpi's need to be run. At the moment if I hard code a number of kpi's the code works fine but if I let it cycle through all the kpi's, ones that should not run are being told to run, also in the select that defines things like @Period, when I let it run through all KPI's its setting the @Period incorrectly when it shouldn't run but when its meant to run it set it fine.

Overall I have 2 issues which I think are related KPI's run when they are not meant to and when they are running when they are not meant to they pick up the wrong Period either way which is odd. I think what is happening is when I let it run all the way through the KPI's are getting mixed up but I cant see how this is happening, any help would be great before I go mad.

As you can see from my code there are 2 @current_timestamps, when its set to 23.45.18.873 it should not run some of the kpi's but it runs them all anyway and the 21.45.18.873 means that they can run.

declare @job_current_time_minute float,
        @job_current_date_time_minute as datetime,
        @current_timestamp as datetime



SET @job_current_time_minute = 52--cast(datepart(minute, getdate()) as float)
SET @job_current_date_time_minute =  getdate()
--SET @current_timestamp = cast('2017-04-02 23:45:18.873' as datetime)
SET @current_timestamp = cast('2017-04-02 21:45:18.873' as datetime)
--Run: 88 6 0 52 5 5 5
--Run: 88 1 10 52 5 5 5

declare 
    @kpi_id int,
    @kpi_parent_id int,  
    @sql nvarchar(max), 
    @kpi_last_result nvarchar(100), 
    @kpi_last_runtime datetime,
    @kpi_params nvarchar(max),
    @kpi_current_time_minute float,
    @schedulue_minute float,
    @reoccurrence float,
    @result float,
    @kpi2 int,

    -- Email Variables
    @kpi_name nvarchar(150),
    @kpi_desc nvarchar(150),
    @kpi_report_link nvarchar(150),
    @kpi_email_subject nvarchar(150),
    @kpi_email_body nvarchar(300),
    @kpi_email_query nvarchar(max),
    @kpi_sms_msg nvarchar(300);


SET @kpi_params = N'@retvalOUT varchar(max) OUTPUT';

select @kpi_id = min(kpi_id) from KPI where KPI_Active = 1; --and kpi_id in (86, 88)

    while @kpi_id is not null

        begin

            select
            @kpi_parent_id = kpi_parent_id, 
            @sql = kpi_Script, 
            @schedulue_minute = datepart(minute,s.Schedule_Start), 
            @reoccurrence = s.Reoccurrence,
            @result = cast((@job_current_time_minute - datepart(minute,s.Schedule_Start)) / s.Reoccurrence as decimal(18,2)),
            @kpi_name = kpi_name,
            @kpi_desc = kpi_desc,
            @kpi_report_link = kpi_report_link,
            @kpi_email_subject = kpi_email_subject,
            @kpi_email_body = kpi_email_body,
            @kpi_email_query = kpi_email_query,
            @kpi_sms_msg = kpi_sms_msg,
            @kpi_current_time_minute = 60*DATEPART(HOUR, GETDATE())+DATEPART(MINUTE,GETDATE()),
            @kpi2 = KPI_ID
            from EWI..KPI
            inner join EWI..Schedule s on KPI.Schedule_ID = s.Schedule_ID where kpi_id = @kpi_id--order by kpi_id asc

            --set @sql = 'select @retvalOUT= (' + @sql + ')'

if floor(@result) <> ceiling(@result)

        begin

            PRINT 'Not Time to Run: ' + ' ' + cast(@kpi_id as varchar(11)) + ' ' + cast(@result as varchar(11))

        end

else

        begin

                    declare 
                    @ThresholdID int,
                    @PeriodID int,
                    @Threshold_value int,
                    @Threshold_PosNeg nvarchar(5)

            select @ThresholdID = Threshold_ID, @PeriodID = Period_ID, @Threshold_value = Threshold_value, @Threshold_PosNeg = Threshold_PosNeg from (
            --  select * from (
                select * from (
                        select KPI_ID, t.Threshold_ID, p.Period_ID, t.Threshold_Value, t.Threshold_PosNeg, p.DayStartTime, p.DayEndTime, Period_desc, [Day] 
                        from EWI..Threshold t join 
                                    EWI..Period p on t.Period_ID = p.Period_ID where KPI_ID = @kpi_id
                ) x where (case when [Day] = datename(dw,getdate()) then Period_id
                    when [Day] = choose(datepart(dw, getdate()), 'WEEKEND','Weekday','Weekday','Weekday','Weekday','Weekday','WEEKEND') then Period_ID
                    when [Day] = 'All' then Period_ID end) is not null
            ) y where case when datediff(second,DayStartTime,DayEndTime) > 0 then 
            case when convert(time(0), @current_timestamp) >= DayStartTime and convert(time(0), @current_timestamp) < DayEndTime then 1 else 0 end
            else case when convert(time(0), @current_timestamp) >= DayStartTime or convert(time(0), @current_timestamp) < DayEndTime then 1 else 0 end
            end = 1


select 'Run: ' + cast(@kpi_id as varchar(11)) + ' ' + cast(@PeriodID as varchar(11)) + ' ' + 
cast(@Threshold_value as varchar(11)) + ' ' + cast(@job_current_time_minute as varchar(11))  + ' ' + 
cast(@result as varchar(11))  + ' ' + 
cast(ceiling(@result) as varchar(11))+ ' ' +cast(floor(@result) as varchar(11))

    end


    select @kpi_id = min(kpi_id) from KPI where KPI_Active = 1 and kpi_id > @kpi_id;


end
7
  • I hate to play this role, but ... imagine you read the question and you want to help; would you understand anything? You say you cannot post data; not even a subset of them, which would suffice to reproduce the problem? Commented Apr 2, 2017 at 18:47
  • Hi Giorgos, I totally agree but trying to put together a subset of this data to post is almost impossible but I will try. I was hoping that the issue was something to do with the logic of my code or the way I am doing something. As I said if I hard code kpi's in to it, it works fine but its when I let the while loop cycle through that I have issues. Commented Apr 2, 2017 at 18:56
  • I don't think it has something to do with the KPIs, I am setting up a demo trying to "reproduce" the problem Commented Apr 2, 2017 at 18:58
  • Thanks Giorgos, I agree as the KPI's work fine when set individually but I think its an issue with my while loop. Commented Apr 2, 2017 at 19:01
  • I set up a demo (see my "answer", hopefully it will develop to be a proper one), with no while loop. Still the problem exists, I think. Commented Apr 2, 2017 at 19:02

1 Answer 1

1

Take a look at this rextester demo. Do you agree that this model reproduces the problem? It seems that comparing ceiling(@result) to floor(@result) will indeed give everywhere true.

After our discussion it seems to me that the test should be: if (@result - datepart(minute, Schedule_Start)) % @reoccurrence = 0 or something like that. But this assumes @reoccurence to be an integer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.