Skip to content

Commit e14d879

Browse files
authored
feat: util methods to manipulate graphqlTimeRange (#1271)
* feat: util methods to manipulate graphqlTimeRange
1 parent 612c72a commit e14d879

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

projects/observability/src/public-api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,6 @@ export * from './shared/components/label-detail/label-detail.module';
382382
// Bar Gauge
383383
export * from './shared/components/bar-gauge/bar-gauge.component';
384384
export * from './shared/components/bar-gauge/bar-gauge.module';
385+
386+
// Time Range utils
387+
export * from './shared/utils/time-range';

projects/observability/src/shared/graphql/model/schema/timerange/graphql-time-range.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TimeRange } from '@hypertrace/common';
1+
import { TimeDuration, TimeRange, TimeUnit } from '@hypertrace/common';
22

33
export class GraphQlTimeRange {
44
public static fromTimeRange(timeRange: Pick<TimeRange, 'startTime' | 'endTime'>): GraphQlTimeRange {
@@ -13,4 +13,11 @@ export class GraphQlTimeRange {
1313
endTime: typeof this.to === 'number' ? new Date(this.to) : this.to
1414
};
1515
}
16+
17+
public asTimeDuration(): TimeDuration {
18+
return new TimeDuration(
19+
this.asArgumentObject().endTime.getTime() - this.asArgumentObject().startTime.getTime(),
20+
TimeUnit.Millisecond
21+
);
22+
}
1623
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { GraphQlTimeRange } from '../graphql/model/schema/timerange/graphql-time-range';
2+
import { getPreviousTimeRange } from './time-range';
3+
4+
describe('Time Range Util', () => {
5+
const oneHourRange = new GraphQlTimeRange(new Date(1637298000000), new Date(1637301600000));
6+
test('computes previous time window as expected', () => {
7+
const previousTimeRange = getPreviousTimeRange(oneHourRange);
8+
expect(new Date(previousTimeRange.from).getTime()).toEqual(1637294400000);
9+
expect(new Date(previousTimeRange.to).getTime()).toEqual(1637298000000);
10+
});
11+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { TimeDuration, TimeUnit } from '@hypertrace/common';
2+
import { GraphQlTimeRange } from '../graphql/model/schema/timerange/graphql-time-range';
3+
4+
export const getPreviousTimeRange = (timeRange: GraphQlTimeRange): GraphQlTimeRange => {
5+
const requiredDuration: TimeDuration = timeRange.asTimeDuration();
6+
7+
return new GraphQlTimeRange(
8+
timeRange.asArgumentObject().startTime.getTime() - requiredDuration.getAmountForUnit(TimeUnit.Millisecond),
9+
timeRange.asArgumentObject().startTime.getTime()
10+
);
11+
};

0 commit comments

Comments
 (0)