-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add frame utils to grafana-utils (#47)
* Add frame utils to grafana-utils * Prepare grafana-utils release 1.4.0 * Remove unused code * Add build for server and client environments to reduce bundle size * Prepare grafana-utils release 1.4.1 * Change output for client build and update package exports * Prepare grafana-utils release 1.4.2 * Fix tests * Add several esm output for client * Prepare grafana-utils release 1.4.3
- Loading branch information
Showing
10 changed files
with
249 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from './constants'; | ||
export * from './grafana/types'; | ||
export * from './privateTransformers'; | ||
export * from './query'; | ||
export * from './types'; | ||
export * from './utils'; | ||
export * from './variables'; |
2 changes: 1 addition & 1 deletion
2
packages/grafana-utils/src/index.test.ts → packages/grafana-utils/src/server.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { FieldType, toDataFrame } from '@grafana/data'; | ||
|
||
import { findField, getFieldValues, getLastFieldValue } from './frame'; | ||
|
||
describe('Frame utils', () => { | ||
describe('findField', () => { | ||
it('Should find first suitable field', () => { | ||
const frame1 = toDataFrame({ | ||
fields: [ | ||
{ | ||
name: 'field', | ||
type: FieldType.number, | ||
}, | ||
], | ||
}); | ||
const frame2 = toDataFrame({ | ||
fields: [ | ||
{ | ||
name: 'field', | ||
type: FieldType.string, | ||
}, | ||
], | ||
}); | ||
|
||
const result = findField([frame1, frame2], (field) => field.name === 'field'); | ||
|
||
expect(result).toBeDefined(); | ||
expect(result?.name).toEqual('field'); | ||
expect(result?.type).toEqual(FieldType.number); | ||
}); | ||
|
||
it('Should return nothing if no field found', () => { | ||
const frame1 = toDataFrame({ | ||
fields: [ | ||
{ | ||
name: 'field', | ||
type: FieldType.number, | ||
}, | ||
], | ||
}); | ||
|
||
const result = findField([frame1], (field) => field.name === '123'); | ||
|
||
expect(result).not.toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('getFieldValues', () => { | ||
it('Should return field values', () => { | ||
const series = [ | ||
toDataFrame({ | ||
fields: [ | ||
{ name: 'name', type: FieldType.string, values: ['file1.png', 'file1.png', ''] }, | ||
{ | ||
name: 'media', | ||
type: FieldType.string, | ||
values: ['1', '2', ''], | ||
}, | ||
], | ||
}), | ||
]; | ||
|
||
const mediaData = getFieldValues(series, 'media'); | ||
expect(mediaData).toHaveLength(3); | ||
expect(mediaData[0]).toEqual(series[0].fields[1].values[0]); | ||
expect(mediaData[1]).toEqual(series[0].fields[1].values[1]); | ||
}); | ||
|
||
it('Should take field with specified type or name', () => { | ||
const dataFrames = [ | ||
toDataFrame({ | ||
fields: [ | ||
{ name: 'name', type: FieldType.string, values: ['file1.png', 'file1.png', ''] }, | ||
{ | ||
name: 'media', | ||
type: FieldType.number, | ||
values: [1, 2, 0], | ||
}, | ||
], | ||
}), | ||
]; | ||
|
||
const mediaData = getFieldValues(dataFrames, 'media', FieldType.string); | ||
|
||
expect(mediaData).toHaveLength(0); | ||
expect(mediaData).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('getLastFieldValue', () => { | ||
it('Should return last field value', () => { | ||
const frame = toDataFrame({ | ||
fields: [ | ||
{ | ||
name: 'value', | ||
values: [1, 2, 3], | ||
}, | ||
], | ||
}); | ||
|
||
expect(getLastFieldValue([frame], 'value')).toEqual(3); | ||
}); | ||
|
||
it('Should return nothing if no values', () => { | ||
const frame = toDataFrame({ | ||
fields: [ | ||
{ | ||
name: 'value', | ||
values: [], | ||
}, | ||
], | ||
}); | ||
|
||
expect(getLastFieldValue([frame], 'value')).not.toBeDefined(); | ||
}); | ||
|
||
it('Should return nothing if no field', () => { | ||
const frame = toDataFrame({ | ||
fields: [ | ||
{ | ||
name: 'value', | ||
values: [1, 2], | ||
}, | ||
], | ||
}); | ||
|
||
expect(getLastFieldValue([frame], '')).not.toBeDefined(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { DataFrame, Field, FieldType } from '@grafana/data'; | ||
|
||
/** | ||
* Find field | ||
*/ | ||
export const findField = <TValue = unknown>( | ||
series: DataFrame[], | ||
predicateFn: (field: Field) => boolean | ||
): Field<TValue> | undefined => { | ||
for (let i = 0; i < series.length; i += 1) { | ||
const frame = series[i]; | ||
|
||
const field = frame.fields.find((field) => predicateFn(field)); | ||
|
||
/** | ||
* Field found | ||
*/ | ||
if (field) { | ||
return field; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Get field values | ||
* @param series | ||
* @param fieldName | ||
* @param fieldType | ||
*/ | ||
export const getFieldValues = <TValue>(series: DataFrame[], fieldName: string, fieldType?: FieldType): TValue[] => { | ||
const field = findField<TValue>(series, (field) => { | ||
const isTypeEqual = fieldType ? field.type === fieldType : true; | ||
|
||
return isTypeEqual && field.name === fieldName; | ||
}); | ||
|
||
if (!field) { | ||
return []; | ||
} | ||
|
||
return field.values; | ||
}; | ||
|
||
/** | ||
* Get last field value | ||
* @param series | ||
* @param fieldName | ||
* @param fieldType | ||
*/ | ||
export const getLastFieldValue = <TValue>(series: DataFrame[], fieldName: string, fieldType?: FieldType): TValue => { | ||
const values = getFieldValues<TValue>(series, fieldName, fieldType); | ||
|
||
return values[values.length - 1]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './frame'; |