-
-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Count Result Aggregation models and helpers from foundatio. Thi…
…s allows us fully typed aggregations via the api.
- Loading branch information
Showing
12 changed files
with
307 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
138 changes: 138 additions & 0 deletions
138
src/Exceptionless.Web/ClientApp/src/lib/features/shared/api/aggregations.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import type { | ||
BucketAggregate, | ||
DateHistogramBucket, | ||
ExtendedStatsAggregate, | ||
IAggregate, | ||
IBucket, | ||
KeyedBucket, | ||
MultiBucketAggregate, | ||
ObjectValueAggregate, | ||
PercentileItem, | ||
PercentilesAggregate, | ||
SingleBucketAggregate, | ||
StatsAggregate, | ||
TermsAggregate, | ||
TopHitsAggregate, | ||
ValueAggregate | ||
} from '../models'; | ||
|
||
export function average(aggregations: Record<string, IAggregate> | undefined, key: string): undefined | ValueAggregate { | ||
return tryGet<ValueAggregate>(aggregations, key); | ||
} | ||
|
||
export function cardinality(aggregations: Record<string, IAggregate> | undefined, key: string): undefined | ValueAggregate { | ||
return tryGet<ValueAggregate>(aggregations, key); | ||
} | ||
|
||
export function dateHistogram(aggregations: Record<string, IAggregate> | undefined, key: string): MultiBucketAggregate<DateHistogramBucket> | undefined { | ||
return getMultiBucketAggregate<DateHistogramBucket>(aggregations, key); | ||
} | ||
|
||
export function extendedStats(aggregations: Record<string, IAggregate> | undefined, key: string): ExtendedStatsAggregate | undefined { | ||
return tryGet<ExtendedStatsAggregate>(aggregations, key); | ||
} | ||
|
||
export function geoHash(aggregations: Record<string, IAggregate> | undefined, key: string): MultiBucketAggregate<KeyedBucket<string>> | undefined { | ||
return getMultiKeyedBucketAggregate<string>(aggregations, key); | ||
} | ||
|
||
export function getPercentile(agg: PercentilesAggregate, percentile: number): PercentileItem | undefined { | ||
return agg.items.find((i) => i.percentile === percentile); // Checked | ||
} | ||
|
||
export function max<T = number>(aggregations: Record<string, IAggregate> | undefined, key: string): undefined | ValueAggregate<T> { | ||
return tryGet<ValueAggregate<T>>(aggregations, key); | ||
} | ||
|
||
export function metric(aggregations: Record<string, IAggregate> | undefined, key: string): ObjectValueAggregate | undefined { | ||
const valueMetric = tryGet<ValueAggregate>(aggregations, key); | ||
if (valueMetric) { | ||
return <ObjectValueAggregate>{ | ||
data: valueMetric.data, | ||
value: valueMetric.value | ||
}; | ||
} | ||
|
||
return tryGet<ObjectValueAggregate>(aggregations, key); | ||
} | ||
|
||
export function min<T = number>(aggregations: Record<string, IAggregate> | undefined, key: string): undefined | ValueAggregate<T> { | ||
return tryGet<ValueAggregate<T>>(aggregations, key); | ||
} | ||
|
||
export function missing(aggregations: Record<string, IAggregate> | undefined, key: string): SingleBucketAggregate | undefined { | ||
return tryGet<SingleBucketAggregate>(aggregations, key); | ||
} | ||
|
||
export function percentiles(aggregations: Record<string, IAggregate> | undefined, key: string): PercentilesAggregate | undefined { | ||
return tryGet<PercentilesAggregate>(aggregations, key); | ||
} | ||
|
||
export function stats(aggregations: Record<string, IAggregate> | undefined, key: string): StatsAggregate | undefined { | ||
return tryGet<StatsAggregate>(aggregations, key); | ||
} | ||
|
||
export function sum(aggregations: Record<string, IAggregate> | undefined, key: string): undefined | ValueAggregate { | ||
return tryGet<ValueAggregate>(aggregations, key); | ||
} | ||
|
||
export function terms<TKey = string>(aggregations: Record<string, IAggregate> | undefined, key: string): TermsAggregate<TKey> | undefined { | ||
const bucket = tryGet<BucketAggregate>(aggregations, key); | ||
if (!bucket) { | ||
return; | ||
} | ||
|
||
return <TermsAggregate<TKey>>{ | ||
buckets: getKeyedBuckets<TKey>(bucket.items), | ||
data: bucket.data | ||
}; | ||
} | ||
|
||
export function topHits<T = unknown>(aggregations: Record<string, IAggregate>): TopHitsAggregate<T> | undefined { | ||
return tryGet<TopHitsAggregate<T>>(aggregations, 'tophits'); | ||
} | ||
|
||
function getKeyedBuckets<TKey>(items: IBucket[]): KeyedBucket<TKey>[] { | ||
return items | ||
.filter((bucket): bucket is KeyedBucket<TKey> => 'key' in bucket) | ||
.map((bucket) => ({ | ||
aggregations: bucket.aggregations, | ||
key: bucket.key, // NOTE: May have to convert to proper type | ||
key_as_string: bucket.key_as_string, | ||
total: bucket.total | ||
})); | ||
} | ||
|
||
function getMultiBucketAggregate<TBucket extends IBucket>( | ||
aggregations: Record<string, IAggregate> | undefined, | ||
key: string | ||
): MultiBucketAggregate<TBucket> | undefined { | ||
const bucket = tryGet<BucketAggregate>(aggregations, key); | ||
if (!bucket) { | ||
return; | ||
} | ||
|
||
return <MultiBucketAggregate<TBucket>>{ | ||
buckets: bucket.items, | ||
data: bucket.data | ||
}; | ||
} | ||
|
||
function getMultiKeyedBucketAggregate<TKey>( | ||
aggregations: Record<string, IAggregate> | undefined, | ||
key: string | ||
): MultiBucketAggregate<KeyedBucket<TKey>> | undefined { | ||
const bucket = tryGet<BucketAggregate>(aggregations, key); | ||
if (!bucket) { | ||
return; | ||
} | ||
|
||
return <MultiBucketAggregate<KeyedBucket<TKey>>>{ | ||
buckets: getKeyedBuckets<TKey>(bucket.items), | ||
data: bucket.data | ||
}; | ||
} | ||
|
||
function tryGet<TAggregate extends IAggregate>(aggregations: Record<string, IAggregate> | undefined, key: string): TAggregate | undefined { | ||
return aggregations?.[key] as TAggregate; | ||
} |
File renamed without changes.
96 changes: 96 additions & 0 deletions
96
src/Exceptionless.Web/ClientApp/src/lib/features/shared/models/aggregations.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import type { IAggregate } from '.'; | ||
|
||
export interface AggregationsHelper { | ||
aggregations: Record<string, IAggregate>; | ||
} | ||
|
||
export interface BucketAggregate extends IAggregate { | ||
items: IBucket[]; | ||
total: number; | ||
} | ||
|
||
export interface BucketAggregateBase extends AggregationsHelper, IAggregate { | ||
aggregations: Record<string, IAggregate>; | ||
} | ||
|
||
export interface BucketBase extends AggregationsHelper, IBucket { | ||
aggregations: Record<string, IAggregate>; | ||
} | ||
|
||
export interface DateHistogramBucket extends KeyedBucket<number> { | ||
date: string; // This needs to be converted to a date. | ||
} | ||
|
||
export interface ExtendedStatsAggregate extends StatsAggregate { | ||
std_deviation?: number; | ||
std_deviation_bounds?: StandardDeviationBounds; | ||
sum_of_squares?: number; | ||
variance?: number; | ||
} | ||
|
||
export interface IBucket { | ||
data?: Record<string, unknown>; | ||
} | ||
|
||
export interface KeyedBucket<T> extends BucketBase { | ||
key: T; | ||
key_as_string: string; | ||
total?: number; | ||
} | ||
|
||
export type MetricAggregateBase = IAggregate; | ||
|
||
export interface MultiBucketAggregate<TBucket extends IBucket> extends BucketAggregateBase { | ||
buckets: TBucket[]; | ||
} | ||
|
||
export interface ObjectValueAggregate extends MetricAggregateBase { | ||
value: unknown; | ||
} | ||
|
||
export interface PercentileItem { | ||
percentile: number; | ||
value?: number; | ||
} | ||
|
||
export interface PercentilesAggregate extends MetricAggregateBase { | ||
items: PercentileItem[]; | ||
} | ||
|
||
export interface RangeBucket extends BucketBase { | ||
from?: number; | ||
from_as_string?: string; | ||
key: string; | ||
to?: number; | ||
to_as_string?: string; | ||
total: number; | ||
} | ||
|
||
export interface SingleBucketAggregate extends BucketAggregateBase { | ||
total: number; | ||
} | ||
|
||
export interface StandardDeviationBounds { | ||
lower?: number; | ||
upper?: number; | ||
} | ||
|
||
export interface StatsAggregate extends MetricAggregateBase { | ||
average?: number; | ||
count: number; | ||
max?: number; | ||
min?: number; | ||
sum?: number; | ||
} | ||
|
||
export type TermsAggregate<TKey> = MultiBucketAggregate<KeyedBucket<TKey>>; | ||
|
||
export interface TopHitsAggregate<T> extends MetricAggregateBase { | ||
hits: T[]; | ||
max_score?: number; | ||
total: number; | ||
} | ||
|
||
export interface ValueAggregate<T = number> extends MetricAggregateBase { | ||
value: T; | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Exceptionless.Web/ClientApp/src/lib/features/shared/models/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { CountResult, type IAggregate } from '$generated/api'; | ||
|
||
export * from './aggregations'; |
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
Oops, something went wrong.