How to exclude weekends in Kusto query? - azure-application-insights

Is there a way to exclude weekends between two dates using existing Kusto commands? I cannot find the right function in here https://kusto.azurewebsites.net/docs/queryLanguage/query-essentials/readme.html

I think Dmitry's comment might be out of date as it does not work. 0th index represents Sunday, and 6th index represents Saturday. A more specific working example to exclude weekends would be the following:
| where dayofweek(Timestamp) != time(6.00:00:00)
| where dayofweek(Timestamp) != time(0.00:00:00)
Which can be further condensed if desired.

You can use | where dayofweek(timestamp) < 6 to exclude Saturday and Sunday from your query. Please refer to dayofweek() doc.

Deriving from Void.Massive answer.
To improve readability, I just created variables for Saturday/Sunday, as I don't use this logic that often, or if I am sharing, I wanted to make this logic a little easier on the reader.
let Saturday = time(6.00:00:00);
let Sunday = time(0.00:00:00);
//
| where dayofweek(Timestamp) != Saturday
| where dayofweek(Timestamp) != Sunday

One more option is to do !in check.
let T = datatable(timestamp: datetime)
[
datetime("2022-04-03"), // Sunday
datetime("2022-04-04"), // Monday
datetime("2022-04-05"), // Tuesday
datetime("2022-04-06"), // Wednesday
datetime("2022-04-07"), // Thursday
datetime("2022-04-08"), // Friday
datetime("2022-04-09"), // Saturday
datetime("2022-04-10"), // Sunday
];
T
// 0d = Sunday, 6d = Saturday
| where dayofweek(timestamp) !in (0d, 6d)

As someone already pointed out, Dmitry's comment is out of date and does not work. Other answers here are better.
However I think that a different but better way to do this nowadays is to add this condition to a where clause:
and dayofweek(timestamp) between (1d .. 5d)
Where 1d is Monday and 5d is Friday

Related

Correct way to get accurate time in Rust?

I'm trying to get accurate time with:
use chrono::{DateTime, Local, Utc};
use std::time::SystemTime;
fn main() {
println!(
"Local.now() {}",
Local::now().format("%H:%m:%S").to_string()
);
println!("Utc.now() {}", Utc::now().format("%H:%m:%S").to_string());
let system_time = SystemTime::now();
let stime: DateTime<Utc> = system_time.into();
println!("SystemTime.now() {}", stime.format("%H:%m:%S"));
}
However, if I run it:
$ date && target/debug/mybin
Sun Jan 15 04:08:19 PM CET 2023
Local.now() 16:01:19
Utc.now() 15:01:19
SystemTime.now() 15:01:19
I don't know where comes from the shift, but I want to know what's the correct way to get the right time?
The %m token inserts the current month's number, which is 1 because it is January. You probably want %M instead, which inserts the minute number. So you are correctly obtaining the current time, but are incorrectly displaying it by using the month number in the place where you'd expect to see the minute number.
See chrono's strftime documentation for a complete list of formatting codes.

Groovy: Date and Time comparisons with a slight delay

So I have the following script:
import groovy.time.TimeCategory
def dueDate = context.expand( '${Test 4 - create user task#Response#$[\'_embedded\'][\'userTaskDtoList\'][0][\'dueDate\']}' )
def date = new Date(messageExchange.getTimestamp())
use(groovy.time.TimeCategory){
after24Hours = (date + 24.hours).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC')) }
assert dueDate == after24Hours
What I'm trying to do with this is take the date and time from a REST request (dueDate - which comes in UTC format and with a 24h delay) and create a new date and time from the timestamp of the moment when that request has been sent, which is registered from my system. I then convert that time to UTC to accommodate the format from dueDate and add 24h to it. At the end I verify that the date and time from dueDate and after24Hours is the same.
The output does return the same time but in certain cases if there is a delay between the time the request is being sent and the time is received then the assertion will fail. This depends on the server, usually there is a difference of like 1 millisecond but I'm thinking that if the server will be slower at some point this will definitely be bigger.
What could I do to allow some margin of error in the assertion, maybe like a few seconds or even a couple of minutes?
Ok, so I managed to do this:
import groovy.time.*
def dueDate = context.expand( '${Test 4 - create user task#Response#$[\'_embedded\'][\'userTaskDtoList\'][0][\'dueDate\']}' )
def date = new Date(messageExchange.getTimestamp())
use(groovy.time.TimeCategory){
after24Hours = (date + 24.hours).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC'))
def date1 = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", dueDate)
def date2 = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", after24Hours)
TimeDuration difference = TimeCategory.minus(date2, date1)
log.info date1
log.info date2
assert difference < 2.minutes
}
The script seems to work and it does return an error only if the time is longer than the one I've set in the assertion.
Unfortunately I have another issue now.
For some reason, my date output looks like this:
Fri Oct 01 16:24:10 EEST 2021: INFO: Sat Oct 02 13:24:10 EEST 2021
Which is not the correct format. That date should appear in the Zulu format, after all when I parsed the dates that was the format that I used.
Am I missing something?
What could I do to allow some margin of error in the assertion, maybe
like a few seconds or even a couple of minutes?
Instead of asserting that they are equal, you could assert that the difference between them is less than a threshold that you get to define.
If you use something like AssertJ, and I'd recommend you do, then you can do something like the following:
assertThat(dueDate).isCloseTo(after24Hours, within(1, ChronoUnit.MINUTE));
This will give a small margin to the comparison of the dates, and should fix your issue.

Time subtraction in Aurelia

I would like to print the duration of an event that occurs between 'startDateTime' and 'endDateTime', expressed in minutes or seconds (if less than 1 minute).
In other words, ${startDateTime | dateFormat:"YYYY-MM-DD HH:mm"} is 2018-09-07 11:57 and ${startDateTime | dateFormat:"YYYY-MM-DD HH:mm"} is 2018-09-07 13:00.
What I would like to print is 63 minutes.
In PHP, I would do ->getTimestamp(), but in Aurelia I have no clue what to even try.
I did test with something like ${endDateTime| dateFormat:"HH:mm:ss" - startDateTime| dateFormat:"HH:mm:ss"} but this can't work as it doesn't convert the entire date time to seconds or minutes...
Therefore, is there a clean solution I can implement in my view?
I solved it using a value converter.
import moment = require("moment");
export class DurationValueConverter {
public toView(startAt, endAt) {
if (!endAt) {
// If end date is missing, use the current date and time.
endAt = moment();
}
const duration = moment.duration(moment(endAt).diff(moment(startAt)));
return duration.humanize();
}
}
Usage: ${startedAt | duration:endedAt}
What you want to have is relative time, It's on its way to browsers, but for now, you will have to use polyfill / library for it. One you can find is from yahoo: https://github.com/yahoo/intl-relativeformat

How to customize the TemporalAdjusters in Java 8?

I am working with Java 8 Date and Time utility. I am using the TemporalAdjuster interface and the implementations found in TemporalAdjusters to manipulate the calculations for a specific day or date.
My requirement is to calculate the day after 3 month and 2 days.
For example, today is 6th of Decemeber (i.e 2016-12-06 in YYYY-MM-DD) and after 3 months and 2 days, the date would be 8th of March, 2017 (i.e. 2017-03-08 in YYYY-MM-DD).
I tried two ways of doing this using the Date and Time utility as follows:
//First
LocalDate dayAfter3MonthsAnd2Days = LocalDate
.now()
.with(firstDayOfNextMonth()).plusMonths(2).plusDays(2);
//Second
LocalDate dayAfter3MonthsAnd2Days = LocalDate
.now()
.with(firstDayOfMonth()).plusMonths(3).plusDays(2);
Both of them returns the date as 3rd of March, 2017 (i.e. 2017-03-03) as follows:
dayAfter3MonthsAnd2Days = 2017-03-03
Is there any way i can customize the TemporalAdjusters and get the desired output? Or any other alternate to achieve the goal?
I found out that i can simply use the following:
LocalDate dayAfter3MonthsAnd2DaysNew = LocalDate
.now()
.plusMonths(3)
.plusDays(2);
Alongwith that, i can also use the custom TemporalAdjuster which uses the above same process to do manipulations as follows:
TemporalAdjuster ta = TemporalAdjusters.ofDateAdjuster(
(LocalDate d) -> d.plusMonths(3).plusDays(2));
LocalDate dayAfter3MonthsAnd2DaysCustom = LocalDate
.now()
.with(ta);
Although the previous way is simpler, but we can use more custom proceedings using ofDateAdjuster method of TemporalAdjuster.

Groovy - idiomatic way of coding The Last Weekday - First of Month

What is the Groovy idiomatic way of asking for the last past weekday (ex. Monday)?
(or current week, the first day of the week)?
Also is there something similar for asking for the current month, the first of month?
static Date firstDayInWeek(Date day) {
day.clearTime()
return day - day.calendarDate.dayOfWeek
}
A year ago, I wrote a simple DSL for calculating dates, this allows you to do:
def mondayInJuly = new DateDSL().with {
first.monday.in.july( 2011 )
}
which sets mondayInJuly to be a Date object representing
Mon Jul 04 00:00:00 UTC 2011
I need to update the script to allow you to use Groovy 1.8's more lenient parser
As you mention DateTime in tags, I suggest JodaTime is OK. So, we do:
def lastMonday =
new DateTime().withDayOfWeek(DateTimeConstants.MONDAY).minusWeeks(1)
def firstThisMoth = new DateTime().withDayOfMonth(1)

Resources