Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private parseTime() {
// when absolute time is saved in json it is turned to a string
if (_.isString(this.time.from) && this.time.from.indexOf('Z') >= 0) {
this.time.from = dateTime(this.time.from).utc();
}
if (_.isString(this.time.to) && this.time.to.indexOf('Z') >= 0) {
this.time.to = dateTime(this.time.to).utc();
}
}
if (!labels && stream.labels) {
labels = parseLabels(stream.labels);
}
const times = new ArrayVector([]);
const timesNs = new ArrayVector([]);
const lines = new ArrayVector([]);
const uids = new ArrayVector([]);
for (const entry of stream.entries) {
const ts = entry.ts || entry.timestamp;
// iso string with nano precision, will be truncated but is parse-able
times.add(ts);
// So this matches new format, we are loosing precision here, which sucks but no easy way to keep it and this
// is for old pre 1.0.0 version Loki so probably does not affect that much.
timesNs.add(dateTime(ts).valueOf() + '000000');
lines.add(entry.line);
uids.add(createUid(ts, stream.labels, entry.line));
}
return constructDataFrame(times, timesNs, lines, uids, labels, reverse, refId);
}
timeRange(): TimeRange {
// make copies if they are moment (do not want to return out internal moment, because they are mutable!)
const raw = {
from: isDateTime(this.time.from) ? dateTime(this.time.from) : this.time.from,
to: isDateTime(this.time.to) ? dateTime(this.time.to) : this.time.to,
};
const timezone: TimeZone = this.dashboard ? this.dashboard.getTimezone() : undefined;
return {
from: dateMath.parse(raw.from, false, timezone),
to: dateMath.parse(raw.to, true, timezone),
raw: raw,
};
}
_timeOffset(time: number, offset: string, subtract = false): number {
let incr = 5;
let unit = 'm';
const parts = /^(\d+)(\w)/.exec(offset);
if (parts && parts.length === 3) {
incr = parseInt(parts[1], 10);
unit = parts[2];
}
const t = dateTime(time);
if (subtract) {
incr *= -1;
}
return t.add(incr, unit as DurationUnit).valueOf();
}
const stringToDateTime = (value: string | DateTime, roundUp?: boolean, timeZone?: TimeZone): DateTime => {
if (isDateTime(value)) {
if (timeZone === 'utc') {
return value.utc();
}
return value;
}
if (value.indexOf('now') !== -1) {
if (!dateMath.isValid(value)) {
return dateTime();
}
const parsed = dateMath.parse(value, roundUp, timeZone);
return parsed || dateTime();
}
return dateTimeForTimeZone(timeZone, value, TIME_FORMAT);
};
formatDate(date: DateTimeInput, format?: string) {
date = isDateTime(date) ? date : dateTime(date);
format = format || 'YYYY-MM-DD HH:mm:ss';
const timezone = this.getTimezone();
return timezone === 'browser' ? dateTime(date).format(format) : toUtc(date).format(format);
}
getIndexList(from: any, to: any) {
if (!this.interval) {
return this.pattern;
}
const intervalInfo = intervalMap[this.interval];
const start = dateTime(from)
.utc()
.startOf(intervalInfo.startOf);
const endEpoch = dateTime(to)
.utc()
.startOf(intervalInfo.startOf)
.valueOf();
const indexList = [];
while (start.valueOf() <= endEpoch) {
indexList.push(start.format(this.pattern));
start.add(1, intervalInfo.amount);
}
return indexList;
}
}
formatBasicDate(date) {
const now = this.dashboard.timezone === 'browser' ? dateTime() : toUtc();
const then = this.dashboard.timezone === 'browser' ? dateTime(date) : toUtc(date);
return then.from(now);
}
getRelativeTime(date: DateTimeInput) {
date = isDateTime(date) ? date : dateTime(date);
return this.timezone === 'browser' ? dateTime(date).fromNow() : toUtc(date).fromNow();
}
getFrom(options: any) {
const from = options.range.from;
return `datetime(${dateTime(from)
.startOf('minute')
.toISOString()})`;
}