Skip to content

Commit

Permalink
Convert event date fields from integer to date objects (#354)
Browse files Browse the repository at this point in the history
* convert event date fields from integer to date objects

* chore: bump fix version
  • Loading branch information
sagar-thalwar authored Mar 14, 2024
1 parent 9ac7903 commit 107fedb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
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

0 comments on commit 107fedb

Please sign in to comment.