Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert event date fields from integer to date objects #354

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cognite/cognite-grafana-datasource",
"version": "4.1.1",
"version": "4.1.2",
"description": "Cognite Data Fusion datasource",
"repository": "https://github.com/cognitedata/cognite-grafana-datasource",
"author": "Cognite AS",
Expand Down
4 changes: 2 additions & 2 deletions src/cdf/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export interface TimeSeriesAggregateDatapoint {

export interface CogniteEvent {
id: number;
lastUpdatedTime: string;
createdTime: string;
lastUpdatedTime: number;
createdTime: number;
externalId?: string;
startTime?: number;
endTime?: number;
Expand Down
66 changes: 66 additions & 0 deletions src/datasources/EventsDatasource.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { CogniteEvent } from 'cdf/types';
import { convertEventsDateFields } from './EventsDatasource';

describe('convertEventsDateFields', () => {
it('should convert date fields to Date objects when start time is available', () => {
const events: CogniteEvent[] = [
{
id: 1,
createdTime: 1708415906649,
lastUpdatedTime: 1708415906786,
startTime: 1708415893288,
},
];
const result = convertEventsDateFields(events as CogniteEvent[]);
expect(result[0].createdTime).toBeInstanceOf(Date);
expect(result[0].lastUpdatedTime).toBeInstanceOf(Date);
expect(result[0].startTime).toBeInstanceOf(Date);
});

it('should convert date fields to Date objects when end time is available', () => {
const events: CogniteEvent[] = [
{
id: 1,
createdTime: 1708415906649,
lastUpdatedTime: 1708415906786,
endTime: 1708415893288,
},
];
const result = convertEventsDateFields(events as CogniteEvent[]);
expect(result[0].createdTime).toBeInstanceOf(Date);
expect(result[0].lastUpdatedTime).toBeInstanceOf(Date);
expect(result[0].endTime).toBeInstanceOf(Date);
});

it('should convert date fields to Date objects when both start and end time are available', () => {
const events: CogniteEvent[] = [
{
id: 1,
createdTime: 1708415906649,
lastUpdatedTime: 1708415906786,
startTime: 1708415893288,
endTime: 1708415893288,
},
];
const result = convertEventsDateFields(events as CogniteEvent[]);
expect(result[0].createdTime).toBeInstanceOf(Date);
expect(result[0].lastUpdatedTime).toBeInstanceOf(Date);
expect(result[0].startTime).toBeInstanceOf(Date);
expect(result[0].endTime).toBeInstanceOf(Date);
});

it('should not convert date fields to Date objects when they are not available', () => {
const events: CogniteEvent[] = [
{
id: 1,
createdTime: 1708415906649,
lastUpdatedTime: 1708415906786,
},
];
const result = convertEventsDateFields(events as CogniteEvent[]);
expect(result[0].createdTime).toBeInstanceOf(Date);
expect(result[0].lastUpdatedTime).toBeInstanceOf(Date);
expect(result[0].startTime).toBeUndefined();
expect(result[0].endTime).toBeUndefined();
});
});
14 changes: 13 additions & 1 deletion src/datasources/EventsDatasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ const convertEventsToAnnotations = (events: CogniteEvent[], timeRangeEnd: number
}))
}

export const convertEventsDateFields = (events: CogniteEvent[]) => {
return events.map(({ createdTime, lastUpdatedTime, startTime, endTime, ...rest }) => {
return {
...rest,
createdTime: new Date(createdTime),
lastUpdatedTime: new Date(lastUpdatedTime),
...(startTime && { startTime: new Date(startTime) }),
...(endTime && { endTime: new Date(endTime) }),
}
});
}

export class EventsDatasource {
constructor(private connector: Connector) { }
async query(options: DataQueryRequest<CogniteQuery>): Promise<DataQueryResponse> {
Expand Down Expand Up @@ -66,7 +78,7 @@ export class EventsDatasource {
return Promise.all(
targets.map(async (target) => {
const resEvents = await this.fetchEventsForTarget(target, timeRange);
const items = isAnnotationTarget(target) ? convertEventsToAnnotations(resEvents, timeRange[1]) : resEvents;
const items = isAnnotationTarget(target) ? convertEventsToAnnotations(resEvents, timeRange[1]) : convertEventsDateFields(resEvents);
const df = convertItemsToDataFrame(items, target.eventQuery?.columns ?? [], "Events", target.refId);
return df
})
Expand Down