Using: phind.
During the Covid-19 pandemic, one of the tasks beset me was to keep an eye on a bunch of corporate websites where the users were responsible for developing (and/or supporting the development of) covid vaccines. Using Katalon Studio, I wrote a Test Case that did just that.
Today, I wondered what a ChatGTP engine might produce given a similar brief. So, without further ado…
PROMPT: In groovy, write a loop that checks a website (URL) is up. It should check the website every 3 minutes. If the website is down, it should check again after 1 minute. If the website remains down, it should write a report to an array of strings, noting the time and date of the outage. The entire loop should execute for 7 days and then show the report and stop.
ANSWER
To achieve the desired functionality, you can use a combination of Groovy’s loop constructs, exception handling, and web services libraries. Here’s a step-by-step breakdown of a possible implementation:
- Initialize the required variables and import necessary libraries.
- Use a
while
loop to run the script for 7 days, usingSystem.currentTimeMillis()
to track the time.- Use a
try-catch
block to handle exceptions while checking the website status.- Use the
URL
class to check if the website is up or down, as shown in stackoverflow.com.- If the website is down, wait for 1 minute and check again. If it’s still down, record the outage in an array.
- Wait for 3 minutes before checking the website status again.
- After 7 days, exit the loop and display the report.
Here’s a sample implementation in Groovy:
import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit
def urlToCheck = "http://example.com"
def report = []
def startTime = System.currentTimeMillis()
def sevenDaysMillis = TimeUnit.DAYS.toMillis(7)
def threeMinutesMillis = TimeUnit.MINUTES.toMillis(3)
def oneMinuteMillis = TimeUnit.MINUTES.toMillis(1)
def isWebsiteUp(url) {
try {
new URL(url).getText()
return true
} catch (Exception e) {
return false
}
}
while (System.currentTimeMillis() - startTime < sevenDaysMillis) {
if (!isWebsiteUp(urlToCheck)) {
TimeUnit.MILLISECONDS.sleep(oneMinuteMillis)
if (!isWebsiteUp(urlToCheck)) {
def dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
def currentTime = dateFormat.format(new Date())
report << "Website down at: \${currentTime}"
}
}
TimeUnit.MILLISECONDS.sleep(threeMinutesMillis)
}
report.each { outage ->
println outage
}
ANSWER
This implementation covers the requirements in the question. It checks the website status every 3 minutes, and if the website is down, it checks again after 1 minute. If the website remains down, it records the outage time and date in the array. The loop runs for 7 days, after which it displays the report and stops.
I wasn’t entirely happy that my initial prompt covered enough of the request as it was put to me. So I started asking phind to make some changes:
PROMPT: Can you make the loop pause each day between the hours of 07:00 and 21:00?
ANSWER
To pause the loop between the hours of 07:00 and 21:00, you can use Groovy’sCalendar
class to get the current hour and add a conditional check to pause the loop during the specified time range. Here’s the updated implementation:
import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit
import java.util.Calendar
def urlToCheck = "http://example.com"
def report = []
def startTime = System.currentTimeMillis()
def sevenDaysMillis = TimeUnit.DAYS.toMillis(7)
def threeMinutesMillis = TimeUnit.MINUTES.toMillis(3)
def oneMinuteMillis = TimeUnit.MINUTES.toMillis(1)
def isWebsiteUp(url) {
try {
new URL(url).getText()
return true
} catch (Exception e) {
return false
}
}
while (System.currentTimeMillis() - startTime < sevenDaysMillis) {
def currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
if (currentHour >= 7 && currentHour < 21) {
TimeUnit.MILLISECONDS.sleep(threeMinutesMillis)
continue
}
if (!isWebsiteUp(urlToCheck)) {
TimeUnit.MILLISECONDS.sleep(oneMinuteMillis)
if (!isWebsiteUp(urlToCheck)) {
def dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
def currentTime = dateFormat.format(new Date())
report << "Website down at: \${currentTime}"
}
}
TimeUnit.MILLISECONDS.sleep(threeMinutesMillis)
}
report.each { outage ->
println outage
}
In this updated implementation, we first get the current hour using
Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
. Then, we check if the current hour is within the range of 07:00 and 21:00. If it is, we pause the loop for 3 minutes usingTimeUnit.MILLISECONDS.sleep(threeMinutesMillis)
and continue to the next iteration usingcontinue
. This will effectively pause the loop during the specified hours. The rest of the code remains the same.
I quickly scanned the code looking for the changes and noticed immediately the poor implementation of the pause-for-fourteen-hours section. Would phind respond to a suggestion without me having to explain in too much detail?
PROMPT: ** Suggestion: The time check between 07:00 and 21:00 sleeps the iteration for a mere 3 minutes. That is a waste of clock cycles.**
ANSWER
You’re right; checking every 3 minutes during the pause period is inefficient. To optimize the code, you can use theCalendar
class to calculate the time until 21:00 and sleep the loop for that duration. Here’s the updated implementation:
import java.text.SimpleDateFormat
import java.util.concurrent.TimeUnit
import java.util.Calendar
def urlToCheck = "http://example.com"
def report = []
def startTime = System.currentTimeMillis()
def sevenDaysMillis = TimeUnit.DAYS.toMillis(7)
def threeMinutesMillis = TimeUnit.MINUTES.toMillis(3)
def oneMinuteMillis = TimeUnit.MINUTES.toMillis(1)
def isWebsiteUp(url) {
try {
new URL(url).getText()
return true
} catch (Exception e) {
return false
}
}
while (System.currentTimeMillis() - startTime < sevenDaysMillis) {
def calendar = Calendar.getInstance()
def currentHour = calendar.get(Calendar.HOUR_OF_DAY)
if (currentHour >= 7 && currentHour < 21) {
calendar.set(Calendar.HOUR_OF_DAY, 21)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
def timeUntil21 = calendar.getTimeInMillis() - System.currentTimeMillis()
TimeUnit.MILLISECONDS.sleep(timeUntil21)
continue
}
if (!isWebsiteUp(urlToCheck)) {
TimeUnit.MILLISECONDS.sleep(oneMinuteMillis)
if (!isWebsiteUp(urlToCheck)) {
def dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
def currentTime = dateFormat.format(new Date())
report << "Website down at: \${currentTime}"
}
}
TimeUnit.MILLISECONDS.sleep(threeMinutesMillis)
}
report.each { outage ->
println outage
}
In this updated implementation, when the current hour is within the range of 07:00 and 21:00, we calculate the time until 21:00 using the
Calendar
class. We then sleep the loop for the calculated duration, effectively pausing the loop during the specified hours. This optimizes the loop by avoiding unnecessary checks every 3 minutes during the pause period.
Pretty impressive. I mean, it’s still not covering all my brief (as put to me in 2020) but it’s certainly getting close to something usable which could be taken further (by hand) later. And of course, some of the detail would be a little trickier to write out in a prompt, like:
if the website is down, go into “outage mode” and monitor the duration of the outage, and write that to the outage report, and when it’s back up, proceed as before.
But creating a prompt with such fine-grained detail would likely take me almost as long as writing the actual code.