mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Refactor logs_model to clean up types (#51201)
* Chore: Minor refector of logs_model to clean up types * undo a change i didn't commit to * use isObservable instead * fix type
This commit is contained in:
parent
5b058d617d
commit
ffd9d9d0c5
@ -3205,17 +3205,8 @@ exports[`better eslint`] = {
|
|||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "9"]
|
[0, 0, 0, "Unexpected any. Specify a different type.", "9"]
|
||||||
],
|
],
|
||||||
"public/app/core/logsModel.ts:5381": [
|
"public/app/core/logsModel.ts:5381": [
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
[0, 0, 0, "Do not use any type assertions.", "0"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
|
[0, 0, 0, "Do not use any type assertions.", "1"]
|
||||||
[0, 0, 0, "Do not use any type assertions.", "2"],
|
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "3"],
|
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "4"],
|
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "5"],
|
|
||||||
[0, 0, 0, "Do not use any type assertions.", "6"],
|
|
||||||
[0, 0, 0, "Do not use any type assertions.", "7"],
|
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "8"],
|
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "9"],
|
|
||||||
[0, 0, 0, "Do not use any type assertions.", "10"]
|
|
||||||
],
|
],
|
||||||
"public/app/core/mod_defs.d.ts:5381": [
|
"public/app/core/mod_defs.d.ts:5381": [
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { size } from 'lodash';
|
import { size } from 'lodash';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable, from, isObservable } from 'rxjs';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
AbsoluteTimeRange,
|
AbsoluteTimeRange,
|
||||||
@ -8,6 +8,7 @@ import {
|
|||||||
DataQueryRequest,
|
DataQueryRequest,
|
||||||
DataQueryResponse,
|
DataQueryResponse,
|
||||||
DataSourceApi,
|
DataSourceApi,
|
||||||
|
DataSourceJsonData,
|
||||||
dateTimeFormat,
|
dateTimeFormat,
|
||||||
dateTimeFormatTimeAgo,
|
dateTimeFormatTimeAgo,
|
||||||
FieldCache,
|
FieldCache,
|
||||||
@ -106,13 +107,20 @@ export function filterLogLevels(logRows: LogRowModel[], hiddenLogLevels: Set<Log
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Series {
|
||||||
|
lastTs: number | null;
|
||||||
|
datapoints: Array<[number, number]>;
|
||||||
|
target: LogLevel;
|
||||||
|
color: string;
|
||||||
|
}
|
||||||
|
|
||||||
export function makeDataFramesForLogs(sortedRows: LogRowModel[], bucketSize: number): DataFrame[] {
|
export function makeDataFramesForLogs(sortedRows: LogRowModel[], bucketSize: number): DataFrame[] {
|
||||||
// currently interval is rangeMs / resolution, which is too low for showing series as bars.
|
// currently interval is rangeMs / resolution, which is too low for showing series as bars.
|
||||||
// Should be solved higher up the chain when executing queries & interval calculated and not here but this is a temporary fix.
|
// Should be solved higher up the chain when executing queries & interval calculated and not here but this is a temporary fix.
|
||||||
|
|
||||||
// Graph time series by log level
|
// Graph time series by log level
|
||||||
const seriesByLevel: any = {};
|
const seriesByLevel: Record<string, Series> = {};
|
||||||
const seriesList: any[] = [];
|
const seriesList: Series[] = [];
|
||||||
|
|
||||||
for (const row of sortedRows) {
|
for (const row of sortedRows) {
|
||||||
let series = seriesByLevel[row.logLevel];
|
let series = seriesByLevel[row.logLevel];
|
||||||
@ -459,12 +467,13 @@ export function logSeriesToLogsModel(logSeries: DataFrame[]): LogsModel | undefi
|
|||||||
}
|
}
|
||||||
|
|
||||||
const limits = logSeries.filter((series) => series.meta && series.meta.limit);
|
const limits = logSeries.filter((series) => series.meta && series.meta.limit);
|
||||||
const limitValue = Object.values(
|
const lastLimitPerRef = limits.reduce<Record<string, number>>((acc, elem) => {
|
||||||
limits.reduce((acc: any, elem: any) => {
|
acc[elem.refId ?? ''] = elem.meta?.limit ?? 0;
|
||||||
acc[elem.refId] = elem.meta.limit;
|
|
||||||
return acc;
|
return acc;
|
||||||
}, {})
|
}, {});
|
||||||
).reduce((acc: number, elem: any) => (acc += elem), 0) as number;
|
|
||||||
|
const limitValue = Object.values(lastLimitPerRef).reduce((acc, elem) => (acc += elem), 0);
|
||||||
|
|
||||||
if (limitValue > 0) {
|
if (limitValue > 0) {
|
||||||
meta.push({
|
meta.push({
|
||||||
@ -598,6 +607,7 @@ export function aggregateRawLogsVolume(
|
|||||||
extractLevel: (dataFrame: DataFrame) => LogLevel
|
extractLevel: (dataFrame: DataFrame) => LogLevel
|
||||||
): DataFrame[] {
|
): DataFrame[] {
|
||||||
const logsVolumeByLevelMap: Partial<Record<LogLevel, DataFrame[]>> = {};
|
const logsVolumeByLevelMap: Partial<Record<LogLevel, DataFrame[]>> = {};
|
||||||
|
|
||||||
rawLogsVolume.forEach((dataFrame) => {
|
rawLogsVolume.forEach((dataFrame) => {
|
||||||
const level = extractLevel(dataFrame);
|
const level = extractLevel(dataFrame);
|
||||||
if (!logsVolumeByLevelMap[level]) {
|
if (!logsVolumeByLevelMap[level]) {
|
||||||
@ -661,10 +671,10 @@ type LogsVolumeQueryOptions<T extends DataQuery> = {
|
|||||||
/**
|
/**
|
||||||
* Creates an observable, which makes requests to get logs volume and aggregates results.
|
* Creates an observable, which makes requests to get logs volume and aggregates results.
|
||||||
*/
|
*/
|
||||||
export function queryLogsVolume<T extends DataQuery>(
|
export function queryLogsVolume<TQuery extends DataQuery, TOptions extends DataSourceJsonData>(
|
||||||
datasource: DataSourceApi<T, any, any>,
|
datasource: DataSourceApi<TQuery, TOptions>,
|
||||||
logsVolumeRequest: DataQueryRequest<T>,
|
logsVolumeRequest: DataQueryRequest<TQuery>,
|
||||||
options: LogsVolumeQueryOptions<T>
|
options: LogsVolumeQueryOptions<TQuery>
|
||||||
): Observable<DataQueryResponse> {
|
): Observable<DataQueryResponse> {
|
||||||
const timespan = options.range.to.valueOf() - options.range.from.valueOf();
|
const timespan = options.range.to.valueOf() - options.range.from.valueOf();
|
||||||
const intervalInfo = getIntervalInfo(logsVolumeRequest.scopedVars, timespan);
|
const intervalInfo = getIntervalInfo(logsVolumeRequest.scopedVars, timespan);
|
||||||
@ -683,7 +693,10 @@ export function queryLogsVolume<T extends DataQuery>(
|
|||||||
data: [],
|
data: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const subscription = (datasource.query(logsVolumeRequest) as Observable<DataQueryResponse>).subscribe({
|
const queryResponse = datasource.query(logsVolumeRequest);
|
||||||
|
const queryObservable = isObservable(queryResponse) ? queryResponse : from(queryResponse);
|
||||||
|
|
||||||
|
const subscription = queryObservable.subscribe({
|
||||||
complete: () => {
|
complete: () => {
|
||||||
const aggregatedLogsVolume = aggregateRawLogsVolume(rawLogsVolume, options.extractLevel);
|
const aggregatedLogsVolume = aggregateRawLogsVolume(rawLogsVolume, options.extractLevel);
|
||||||
if (aggregatedLogsVolume[0]) {
|
if (aggregatedLogsVolume[0]) {
|
||||||
|
Loading…
Reference in New Issue
Block a user