Jump to content

Best Course of Action


Recommended Posts

Okay, so I need to make a script and it's kind of confusing explaining exactly what I want it to do.

There is a Number that varies from 100% to 0% and what I want to do is say:

If the number drops more than 50% in less than 3 seconds then send a msgbox

--

Another way I was thinking (Ive spent hours on this!) was trying to use the TimerInit and TimerDiff functions and say start timer at 100% and if it reaches 30% check timerdiff and then decide, however, this wont work because the number may sit at 90% then drop, or 80%, you get the idea..

Can anyone lend me a hand here?

Link to comment
Share on other sites

Something like this?

#include <Timers.au3>
$LastValue = GetCurrentValue()
$LastTime = _Timer_Init()
While 1
    Sleep(250) ; Check 4 times a second
    $Currentvalue = GetCurrentValue()
    If $Currentvalue <> $LastValue Then
        $Difference = Calculate the drop here
        If $Difference > 50 Then
            If _Timer_Diff($LastTime) > 30000 Then
                Msgbox
                $LastValue = $CurrentValue
                $LastTime = _Timer_Init()
            EndIf
        EndIf
    EndIf
WEnd

Func GetCurrentValue()
    $Value = Get the current value here
Return $Value
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You need to store old values for as long as 3 seconds. If your data is updated automatically on a periodic basis, then you need 3s/<period in s> entries in an array to store them.

If updating is asynchronous, you need a 2-D array to be able to store the timestamp as well.

After that, you need to check your 50% condition at every receipt of a new value, purge older than 3s entries and store the new value.

Is that clear?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Something like this?

#include <Timers.au3>
$LastValue = GetCurrentValue()
$LastTime = _Timer_Init()
While 1
    Sleep(250) ; Check 4 times a second
    $Currentvalue = GetCurrentValue()
    If $Currentvalue <> $LastValue Then
        $Difference = Calculate the drop here
        If $Difference > 50 Then
            If _Timer_Diff($LastTime) > 30000 Then
                Msgbox
                $LastValue = $CurrentValue
                $LastTime = _Timer_Init()
            EndIf
        EndIf
    EndIf
WEnd

Func GetCurrentValue()
    $Value = Get the current value here
Return $Value

I was thinking of something like this:

HotKeySet("{ESC}","_Exit")

OnAutoItExitRegister ("CloseLog") ;Is called on any type of exit to preserve our logs

; These constants are fore readability
Global Const $qDATA = 0
Global Const $qTIME = 1
Global Const $qSIZE = 50

; This affects how our script works
Global Const $pollingFrequency = .5 * 1000; Poll every 1/2 second
Global Const $timeDiff = 5 * 1000
; The threashold for warning about spikes
Global Const $dataDiff = 30

; I hate global data structures, but it avoids tedious passing of
; values between functions
Global $qFront = 0 ;Holds the index of the most current time
Global $qBack = 0 ;Holds the index of the oldest time
Global $qSmall = 0 ;Holds the index of the smallest time between $qFront and $qBack
Global $Queue [$qSIZE][2] ;The array that will hold our queue

Global $logHandle = FileOpen ("SomeLogFilePath.txt", 1) ;Open the log file

; Start the timer
Local $timer = TimerInit()

; Zero out the first entry
$Queue [0][$qDATA] = 0
$Queue [0][$qTIME] = 0

While 1
    Sleep($pollingFrequency)
    ; Record the current usage and time
    Local $nowDATA = _GetNetworkUsage()
    Local $nowTIME = TimerDiff ($timer)
    
    ; Add it to the queue
    _AddToQueue ($nowDATA, $nowTIME)
    
    ; Check if it is smaller than the oldest low point
    If $nowDATA < $Queue[$qSmall][$qDATA] Then 
        $qSmall = $qFront
    ElseIf $nowDATA - $Queue[$qSmall][$qDATA]  > $dataDiff Then
        _AlertMe ()
        $qSmall = $qFront
    EndIf
WEnd

Func _AlertMe ()
EndFunc

Func _AddToQueue ($data, $time)
    If $time - $Queue[$qBack][$qTIME] > $timeDiff Then ;If the last item in the queue has timed out
        ;Increment $qBack
        $qBack = Mod ($qBack + 1, $qSIZE)
        
        If $qBack - 1 = $qSmall Then ;Find the next smallest
            $qSmall = $qBack
            ;Custom loop to deal with the Queue, which is treated as a circle, with end and beginning connected
            Local $temp = $qBack
            While $temp <> $qFront
                If $Queue[$temp][$qDATA] < $Queue[$qSmall][$qDATA] Then $qSmall = $temp
                ;Increment $temp, which will wrap around at the end of the array
                $temp = Mod($temp + 1, $qSIZE)
            WEnd
        EndIf
    EndIf
    
    ;Move $qFront forward
    $qFront = Mod($qFront + 1, $qSIZE) ;Wraps around if we pass $qSIZE
    
    ;Increment $qBack if we are out of space in the queue
    If $qFront > $qBack AND $qFront - $qBack > $qSIZE Then 
        $qBack = Mod ($qBack + 1, $qSIZE)
        FileWriteLine ($logHandle, @YEAR & @MON & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " Size of Queue exeeded, please resize $qSIZE")
    ElseIf $qFront < $qBack AND ($qFront + 50) - $qBack > $qSIZE Then 
        $qBack = Mod ($qBack + 1, $qSIZE)
        FileWriteLine ($logHandle, @YEAR & @MON & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " Size of Queue exeeded, please resize $qSIZE")
    EndIf
    
    ;Insert our data in the new first slot
    $Queue[$qFront][$qDATA] = $data
    $Queue[$qFront][$qTIME] = $time
EndFunc

Func _GetNetworkUsage()
    Return  
EndFunc

Func CloseLog ()
    FileClose ($logHandle) ;Close so we don't loose our logfile
EndFunc

Func _Exit()
    Exit
EndFunc

But for some reason, this seems to only work when the percentage goes up by 30% and not down :/

Link to comment
Share on other sites

Change line

ElseIf $nowDATA - $Queue[$qSmall][$qDATA] > $dataDiff Then
to
ElseIf Abs($nowDATA - $Queue[$qSmall][$qDATA]) > $dataDiff Then
and it should work for down as well.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Make a shift register with _ArrayPush

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

+----------+-------------------------+-----------------------------------------+

| $nowdata | $Queue[$qSmall][$qDATA] | Abs($nowDATA - $Queue[$qSmall][$qDATA]) |

+----------+-------------------------+-----------------------------------------+

| 101 ____ | 70 ____________________ | 31 > 30 => Alert ______________________ |

| 39 _____ | 70 ____________________ | 31 > 30 => Alert ______________________ |

+----------+-------------------------+-----------------------------------------+

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

+----------+-------------------------+-----------------------------------------+

| $nowdata | $Queue[$qSmall][$qDATA] | Abs($nowDATA - $Queue[$qSmall][$qDATA]) |

+----------+-------------------------+-----------------------------------------+

| 101 | 70 | 31 > 30 => Alert |

| 39 | 70 | 31 > 30 => Alert |

+----------+-------------------------+-----------------------------------------+

I'm sorry I really appreciate your help, but could you explain what that means?

I am not that advanced yet :/

Fulano helped me write that queue a while back.

Edit:

I was trying to follow through and find out what these two:

[$qSmall][$qDATA]

are, but I was unsuccessful.

I honestly am trying to figure this out im not just posting everything i get errors with lol. I appreciate your patience.

Edited by ericnail
Link to comment
Share on other sites

It's some testdata. You mentioned that it works when the values go up. So I try to show that with abs you should receive an alert when the value goes down.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

It's some testdata. You mentioned that it works when the values go up. So I try to show that with abs you should receive an alert when the value goes down.

Ohh, I see what you're saying there.

But when I test it it doesn't work.

I have another script I included which gives the value to return. I tested the value with a simple msgbox to give me the current value and it is working and the number is updating. With the ABS, the script doesn't seem to notice spikes upwards either now. It Still notices upward spikes, but not downward.

Heres the full script:

#include "NetworkValues.au3"


HotKeySet("{ESC}","_Exit")


; These constants are fore readability
Global Const $qDATA = 0
Global Const $qTIME = 1
Global Const $qSIZE = 50

; This affects how our script works
Global Const $pollingFrequency = .5 * 1000; Poll every 1/2 second
Global Const $timeDiff = 2 * 1000
; The threashold for warning about spikes
Global Const $dataDiff = 30

; I hate global data structures, but it avoids tedious passing of
; values between functions
Global $qFront = 0 ;Holds the index of the most current time
Global $qBack = 0 ;Holds the index of the oldest time
Global $qSmall = 0 ;Holds the index of the smallest time between $qFront and $qBack
Global $Queue [$qSIZE][2] ;The array that will hold our queue
Global $logHandle = FileOpen ("SomeLogFilePath.txt", 1) ;Open the log file

; Start the timer
Local $timer = TimerInit()

; Zero out the first entry
$Queue [0][$qDATA] = 0
$Queue [0][$qTIME] = 0

While 1
    Sleep($pollingFrequency)
    ; Record the current usage and time
    Local $nowDATA = _GetNetworkUsage()
    Local $nowTIME = TimerDiff ($timer)
    
    ; Add it to the queue
    _AddToQueue ($nowDATA, $nowTIME)
    
    ; Check if it is smaller than the oldest low point
    If $nowDATA < $Queue[$qSmall][$qDATA] Then 
        $qSmall = $qFront
    ElseIf Abs($nowDATA - $Queue[$qSmall][$qDATA]) >= $dataDiff Then
        _AlertMe ()
        $qSmall = $qFront
    EndIf
WEnd

Func _AlertMe ()
    MsgBox(0, "Alert", "The Variable has shown a spike: ")
EndFunc

Func _AddToQueue ($data, $time)
    If $time - $Queue[$qBack][$qTIME] > $timeDiff Then ;If the last item in the queue has timed out
        ;Increment $qBack
        $qBack = Mod ($qBack + 1, $qSIZE)
        
        If $qBack - 1 = $qSmall Then ;Find the next smallest
            $qSmall = $qBack
            ;Custom loop to deal with the Queue, which is treated as a circle, with end and beginning connected
            Local $temp = $qBack
            While $temp <> $qFront
                If $Queue[$temp][$qDATA] < $Queue[$qSmall][$qDATA] Then $qSmall = $temp
                ;Increment $temp, which will wrap around at the end of the array
                $temp = Mod($temp + 1, $qSIZE)
            WEnd
        EndIf
    EndIf
    
    ;Move $qFront forward
    $qFront = Mod($qFront + 1, $qSIZE) ;Wraps around if we pass $qSIZE
    
    ;Increment $qBack if we are out of space in the queue
    If $qFront > $qBack AND $qFront - $qBack > $qSIZE Then 
        $qBack = Mod ($qBack + 1, $qSIZE)
        FileWriteLine ($logHandle, @YEAR & @MON & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " Size of Queue exeeded, please resize $qSIZE")
    ElseIf $qFront < $qBack AND ($qFront + 50) - $qBack > $qSIZE Then 
        $qBack = Mod ($qBack + 1, $qSIZE)
        FileWriteLine ($logHandle, @YEAR & @MON & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " Size of Queue exeeded, please resize $qSIZE")
    EndIf
    
    ;Insert our data in the new first slot
    $Queue[$qFront][$qDATA] = $data
    $Queue[$qFront][$qTIME] = $time
EndFunc

Func _GetNetworkUsage()
    
    Return $networkvalue ; A number between 1 and 100
    
EndFunc

Func _Exit()
    Exit
EndFunc

net.au3

Edited by ericnail
Link to comment
Share on other sites

Woot! I figured it out! (Finally)

Here lies the problem:

If $nowDATA < $Queue[$qSmall][$qDATA] Then 
        $qSmall = $qFront
    ElseIf $nowDATA - $Queue[$qSmall][$qDATA]  > $dataDiff Then
        _AlertMe ()
        $qSmall = $qFront
    EndIf

I needed to reverse the two and completely remove the first clause.

If ($Queue[$qSmall][$qDATA] - $nowDATA) > $dataDiff Then
        _AlertMe ()
        $qSmall = $qFront
    EndIf

Thanks for all the help guys! You've been tremendous!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...