Kusto Timeseries fill up missing values for timechart - azure-data-explorer

I want to render a timechart which counts the SoftwareVersion based on 1 day steps. I have to fill up forward missing values per day and serial.
The data to start with is:
let swVersions = datatable(Date: datetime, SoftwareVersion: string, Serial: string) [
datetime(2022-01-24T13:18:20.8450657Z), '1.29.0', '310160039',
datetime(2022-01-26T06:01:41.8742421Z), '1.30.0', '310160039',
datetime(2022-01-26T12:12:23.2342343Z), '1.31.0', '310160039',
datetime(2022-01-28T12:10:14.3620707Z), '1.17.0', '310160039',
datetime(2022-01-24T05:48:58.9000481Z), '1.29.0', '310160040',
datetime(2022-01-24T10:22:23.4457354Z), '1.30.0', '310160040',
datetime(2022-01-24T15:52:16.2342152Z), '1.29.0', '310160040',
datetime(2022-01-25T05:48:58.9012738Z), '1.30.0', '310160040'];
So i need the data like this for rendering the timechart (expected):
let swVersions = datatable(Date: datetime, SoftwareVersion: string, Serial: string) [
datetime(2022-01-24T00:00:00.0000000Z), '1.29.0', '310160039',
datetime(2022-01-25T00:00:00.0000000Z), '1.29.0', '310160039',
datetime(2022-01-26T00:00:00.0000000Z), '1.31.0', '310160039',
datetime(2022-01-27T00:00:00.0000000Z), '1.31.0', '310160039',
datetime(2022-01-28T00:00:00.0000000Z), '1.17.0', '310160039',
datetime(2022-01-24T00:00:00.0000000Z), '1.29.0', '310160040',
datetime(2022-01-25T00:00:00.0000000Z), '1.30.0', '310160040',
datetime(2022-01-26T00:00:00.0000000Z), '1.30.0', '310160040',
datetime(2022-01-27T00:00:00.0000000Z), '1.30.0', '310160040',
datetime(2022-01-28T00:00:00.0000000Z), '1.30.0', '310160040'];
And summarize and rendering timechart like this:
let swVersions = datatable(Date: datetime, SoftwareVersion: string, Serial: string) [
datetime(2022-01-24T00:00:00.0000000Z), '1.29.0', '310160039',
datetime(2022-01-25T00:00:00.0000000Z), '1.29.0', '310160039',
datetime(2022-01-26T00:00:00.0000000Z), '1.31.0', '310160039',
datetime(2022-01-27T00:00:00.0000000Z), '1.31.0', '310160039',
datetime(2022-01-28T00:00:00.0000000Z), '1.17.0', '310160039',
datetime(2022-01-24T00:00:00.0000000Z), '1.29.0', '310160040',
datetime(2022-01-25T00:00:00.0000000Z), '1.30.0', '310160040',
datetime(2022-01-26T00:00:00.0000000Z), '1.30.0', '310160040',
datetime(2022-01-27T00:00:00.0000000Z), '1.30.0', '310160040',
datetime(2022-01-28T00:00:00.0000000Z), '1.30.0', '310160040'];
swVersions
| summarize count() by Date, SoftwareVersion
| render timechart
How can I do this?
I am very appreciated for your help.

render timechart with (accumulate=true)
let swVersions = datatable(Date: datetime, SoftwareVersion: string, Serial: string)
[
datetime(2022-01-24T13:18:20.8450657Z), '1.29.0', '310160039',
datetime(2022-01-26T06:01:41.8742421Z), '1.30.0', '310160039',
datetime(2022-01-26T12:12:23.2342343Z), '1.31.0', '310160039',
datetime(2022-01-28T12:10:14.3620707Z), '1.17.0', '310160039',
datetime(2022-01-24T05:48:58.9000481Z), '1.29.0', '310160040',
datetime(2022-01-24T10:22:23.4457354Z), '1.30.0', '310160040',
datetime(2022-01-24T15:52:16.2342152Z), '1.29.0', '310160040',
datetime(2022-01-25T05:48:58.9012738Z), '1.30.0', '310160040'
];
let swVersions_daily_version = swVersions | summarize arg_max(Date, *) by Serial, Date = startofday(Date);
let swVersions_adds = swVersions_daily_version | extend delta = 1;
let swVersions_drops =
swVersions_daily_version
| extend delta = -1
| partition hint.strategy=native by Serial
(
order by Date asc
| extend Date = next(Date)
| where isnotnull(Date)
)
;
let Date_start = toscalar(swVersions | summarize startofday(min(Date)));
let Date_end = toscalar(swVersions | summarize max(Date));
union swVersions_adds, swVersions_drops
| make-series sum(delta) on Date from Date_start to Date_end step 1d by SoftwareVersion
| render timechart with (accumulate=true)
Fiddle

Related

Rows to columns in azure data explorer (kusto)

I have a simple query:
let startDate = ago(7d);
let endDate = now();
let all_logs = materialize(MyTable | where TIMESTAMP > startDate and TIMESTAMP < endDate | where EventName == "SessionResult");
all_logs
| summarize
Total_Sessions = count(),
Successful_Sessions = countif(Successful == "True"),
Failed_Sessions = countif(Successful == "False")
| extend Success_Rate = round(100.0*Successful_Sessions/Total_Sessions, 2)
Which returns
How can I change this to:
Label Count Rate
Total_sessions 98 100
Successful_session 96 97.96
Failed_session 2 2.04
preferably without using the pivot plugin
you could try using the narrow() plugin: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/narrowplugin
For example:
let T = materialize(
MyTable
| where TIMESTAMP > startDate and TIMESTAMP < endDate
| where EventName == "SessionResult")
| summarize
Total_Sessions = count(),
Successful_Sessions = countif(Successful == "True"),
Failed_Sessions = countif(Successful == "False")
)
;
let total = toscalar(T | project total_sessions)
;
T
| evaluate narrow()
| project Label = Column, Count = Value, Rate = round(100.0 * tolong(Value) / total, 2)

Formatting NgbDate

I have a datepicker using NgbDate. I would like the format to spell out the month, hyphen, year. For example, August-2020. How can I format the following date this way?
effectiveDate = new NgbDate(date.year, date.month, date.day);
effectiveDate = effectiveDate.month.toString() + '-' +
effectiveDate.year.toString();
// the `effectiveDate` is of type `NgbDate`
effectiveDate = new NgbDate(date.year, date.month, date.day);
// you try to assign `string` to `NgbDate` which could work and it's very confusing
effectiveDate = effectiveDate.month.toString() + '-' +
effectiveDate.year.toString();
// instead it's better to create a variable for string value + string interpolation will make it easy to read
const formattedDate = `${effectiveDate.month}-${effectiveDate.year}`;
// or you can convert `NgbDate` into `Date` in the controller
this.jsDate = new Date(effectiveDate.year, effectiveDate.month - 1, effectiveDate.day);
// and use the `date` pipe in the component's HTML
jsDate | date: 'MM-yyyy'

Date and Time in Kotlin

I am new to kotlin. And I got a problem.
I have this code:
val sdf = SimpleDateFormat("dd.MM.yyyy")
val currentDate = sdf.format(Date())
println(currentDate)
val stringDate = "12.03.2015"
val dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH)
val millisecondsSinceEpoch = LocalDate.parse(stringDate, dateFormatter)
.atStartOfDay(ZoneOffset.UTC)
.toInstant()
.toEpochMilli()
println(millisecondsSinceEpoch)
val time = currentDate - millisecondsSinceEpoch
val Datee = sdf.format(time)
println(Datee)
But on the line:
val time = currentDate - millisecondsSinceEpoch
val Datee = sdf.format(time)
println(Datee)
I get the error:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
Please help me how you can fix this. I need to subtract the current date from the date that is in string.
UPDATE:
How to subtract one date from another correctly and get the difference in days?
I suggest you switch from the outdated java.util date/time API to the modern date/time API. Given below is the Java code for your requirement and I hope you should be able to convert the same into Kotlin. However, if you face any issue, I can convert the same into Kotlin code for you.
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Define format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH);
// Given date-time
ZonedDateTime givenDateTime = LocalDateTime.of(LocalDate.parse("12.03.2015", formatter), LocalTime.of(0, 0))
.atZone(ZoneId.of("Etc/UTC"));
// Now
ZonedDateTime zdtNow = ZonedDateTime.now(ZoneId.of("Etc/UTC"));
// Period between the two dates
Period period = Period.between(givenDateTime.toLocalDate(), zdtNow.toLocalDate());
// Given date-time with current year, month and day
ZonedDateTime adjusted = givenDateTime.with(LocalDate.now(ZoneId.of("Etc/UTC")));
// Duration between the two times
Duration duration = Duration.between(adjusted, zdtNow);
// Display each part of the period and duration
System.out.printf("%d years %d month %d days %d hours %d minutes %d seconds %d nanoseconds", period.getYears(),
period.getMonths(), period.getDays(), duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart(), duration.toNanosPart());
}
}
Output:
5 years 4 month 7 days 19 hours 30 minutes 37 seconds 507058000 nanoseconds
Using OffsetDateTime:
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Define format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH);
// Given date-time
OffsetDateTime givenDateTime = LocalDateTime.of(LocalDate.parse("12.03.2015", formatter), LocalTime.of(0, 0))
.atOffset(ZoneOffset.UTC);
// Now
OffsetDateTime odtNow = OffsetDateTime.now(ZoneOffset.UTC);
// Period between the two dates
Period period = Period.between(givenDateTime.toLocalDate(), odtNow.toLocalDate());
// Given date-time with current year, month and day
OffsetDateTime adjusted = givenDateTime.with(LocalDate.now(ZoneOffset.UTC));
// Duration between the two times
Duration duration = Duration.between(adjusted, odtNow);
// Display each part of the period and duration
System.out.printf("%d years %d month %d days %d hours %d minutes %d seconds %d nanoseconds", period.getYears(),
period.getMonths(), period.getDays(), duration.toHoursPart(), duration.toMinutesPart(),
duration.toSecondsPart(), duration.toNanosPart());
}
}
Following is the corrected version of your initial program. However as others pointed out it is advisable to use new java Time API.
There is nice article highlighting problem with old Java Date and Calendar API
https://programminghints.com/2017/05/still-using-java-util-date-dont/
import java.util.Date
import java.util.Locale
import java.time.Instant
import java.time.LocalDateTime
import java.time.LocalDate
import java.time.ZoneOffset
import java.text.SimpleDateFormat
import java.time.format.DateTimeFormatter
fun main(args: Array<String>) {
val sdf = SimpleDateFormat("dd.MM.yyyy")
val currentDate = Date()
val currentFormattedDate = sdf.format(currentDate)
println(currentFormattedDate)
val now = currentDate.getTime();
val stringDate = "12.03.2015"
val dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH)
val millisecondsSinceEpoch = LocalDate.parse(stringDate, dateFormatter)
.atStartOfDay(ZoneOffset.UTC)
.toInstant()
.toEpochMilli()
println(millisecondsSinceEpoch)
val time = now - millisecondsSinceEpoch
val Datee = sdf.format(time)
println(Datee)
}
Thanks everyone. But I decided to do this. And it seems like everything works)
fun daysString(dataend: String):String{
val dateFormat = SimpleDateFormat("dd.MM.yyyy")
val endDate = dateFormat.parse(dataend)
val currentDate = Date()
val time = endDate.time - currentDate.time
val days = time / 1000 / 3600 / 24
val strtoday = days.toString()
return strtoday
}
Now in the code I am using:
val data_end = "10.10.2020"
daysString(data_end)
and I get strtoday
Get your required Date and then can do this:
val sdf = SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH)
val theDate = sdf.parse(selectedDate)
val selectedDate = theDate!!.time/86400000 //.time gives milliseconds
val currentDate = sdf.parse(sdf.format(System.currentTimeMillis()))
val currentDate = currentDate!!.time/86400000 //86400000 milliseconds in a day
val diff = currentDate - selectedDate
println(diffInMinutes.toString()) //set it to any view or use as needed

Convert time to am/pm flutter

How to convert the time to am/pm ?
I have this output
I/flutter (17720): 9:00:00
I/flutter (17720): 11:00:00
I/flutter (17720): 12:00:00
This is what I have tried
final item = snapshot.data[index];
print("Time " + item['time'].toString());
DateTime dateTime = DateTime.parse(item['time'].toString());
print(DateUtil().formattedTime(dateTime));
DateUtil
String formattedTime(DateTime dateTime) {
return DateFormat().add_jm().format(dateTime);
}
Error
I/flutter (17720): Time 09:00:00
════════ Exception caught by widgets library
═══════════════════════════════════ The following FormatException was
thrown building Tags(dirty, state: TagsState#b3a2f): Invalid date
format 09:00:00
Use this code:
DateFormat('hh:mm a').format(DateTime.now());
According to the intl library, it states that a represents AM/PM.
You can use the intl library https://pub.dev/packages/intl
and format your DateTime
DateFormat.yMEd().add_jms().format(DateTime.now());
Output:
'Thu, 5/23/2013 10:21:47 AM'
DateFormat class
DateFormat is for formatting and parsing dates in a locale-sensitive manner.
Convert Time
print(DateFormat.jm().format(DateFormat("hh:mm:ss").parse("14:15:00")));
Output : 3:20 AM
Convert Date
print(DateFormat('yyyy-MMMM-dd').format("2021-05-14 00:00:00.000"));
Output : 2021-May-14
EXAMPLE :
Time Picker
Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay picked = await showTimePicker(
context: context,
initialTime: _con.selectedTime,
);
if (picked != null) {
String selTime =
picked.hour.toString() + ':' + picked.minute.toString() + ':00';
print(DateFormat.jm().format(DateFormat("hh:mm:ss").parse(selTime)));
}}
Date Picker
_selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: _con.selectDateAndTime,
firstDate: DateTime(2021),
lastDate: DateTime(2040),
);
if (picked != null) {
print(picked);
}}
this function can helps to convert only time, Without date
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
String formatedTime(TimeOfDay selectedTime) {
DateTime tempDate = DateFormat.Hms().parse(selectedTime.hour.toString() +
":" +
selectedTime.minute.toString() +
":" +
'0' +
":" +
'0');
var dateFormat = DateFormat("h:mm a");
return (dateFormat.format(tempDate));
}
Here is my solution a little simpler to understand I guess. And there is no need to add any library.
String getFormattedDateTime({d = "2022-11-15 14:21:37.152"}){
List<String> months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
List<String> days = [
'Monday',
'Tuseday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
];
final dateTimeObj = DateTime.parse(d);
// date format
String fdate = "${days[dateTimeObj.weekday - 1].substring(0, 3)}, ${months[dateTimeObj.month-1].substring(0, 3)}-${dateTimeObj.day}";
// time format
String time = "${(dateTimeObj.hour > 12 ? dateTimeObj.hour - 12 : dateTimeObj.hour).abs()}:${dateTimeObj.minute} ${dateTimeObj.hour >= 12 ? "PM" : "AM"}";
return "$fdate $time";
}
This function will return formatted String something like this Tus, Nov-15 4:54 PM again you can format it accordingly.

Match datetime property value in response value array list using groovy

I am trying to match if my datetime variable present in array of multiple dates but error popup displayed.
Error :
assert responseStartDateTime.contains(requestStartDateTime) | | | | false 2018-01-16T04:30:00 [2018-01-16T04:30:00, 2018-01-16T06:00:00]
Groovy code :
import groovy.json.JsonSlurper
import java.text.SimpleDateFormat
//request local time
def StartDateTime = context.expand('${#Project#StartDateTime}')
log.info 'Request StartTime : ' + StartDateTime
def EndDateTime = context.expand('${#Project#EndDateTime}')
log.info 'Request EndTime : ' + EndDateTime
//Remove Z from the request time
def outputDateFormat = "yyyy-MM-dd'T'HH:mm:ss"
start = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(StartDateTime)
end = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(EndDateTime)
def requestStartDateTime = "${start.format(outputDateFormat)}"
log.info 'Request StartTime : ' + requestStartDateTime
def requestEndDateTime = "${end.format(outputDateFormat)}"
log.info 'Request EndTime : ' + requestEndDateTime
def ResponseMessage = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
//Store response local time result to variable
def responseStartDateTime = jsonSlurper.MeetingItems.TimeFrom
log.info 'Response StartTime : ' + responseStartDateTime
def responseEndDateTime = jsonSlurper.MeetingItems.TimeTo
log.info 'Response EndTime : ' + responseEndDateTime
//Assert request local time with response local time
assert responseStartDateTime.contains(requestStartDateTime)
assert responseEndDateTime.contains(requestEndDateTime)
Property values :
StartDateTime - 2018-01-16T04:30:00.000Z
EndDateTime - 2018-01-16T04:45:00.000Z

Resources