Browse Source

ref(js): Consistently use getFieldDefinition (#38325)

Evan Purkhiser 2 years ago
parent
commit
c158a2d606

+ 2 - 1
static/app/stores/tagStore.tsx

@@ -106,7 +106,8 @@ const storeConfig: TagStoreDefinition = {
         key: FieldKey.TIMES_SEEN,
         name: 'Times Seen',
         isInput: true,
-        // Below values are required or else SearchBar will attempt to get values // This is required or else SearchBar will attempt to get values
+        // Below values are required or else SearchBar will attempt to get values
+        // This is required or else SearchBar will attempt to get values
         values: [],
         predefined: true,
       },

+ 13 - 7
static/app/utils/discover/fields.tsx

@@ -13,8 +13,8 @@ import {
   AggregationKey,
   DISCOVER_FIELDS,
   FieldKey,
-  FIELDS,
   FieldValueType,
+  getFieldDefinition,
   MEASUREMENT_FIELDS,
   SpanOpBreakdown,
   WebVital,
@@ -1022,8 +1022,10 @@ export function aggregateFunctionOutputType(
 
   // If the function is an inherit type it will have a field as
   // the first parameter and we can use that to get the type.
-  if (firstArg && FIELDS.hasOwnProperty(firstArg)) {
-    return FIELDS[firstArg].valueType as AggregationOutputType;
+  const fieldDef = getFieldDefinition(firstArg ?? '');
+
+  if (fieldDef !== null) {
+    return fieldDef.valueType as AggregationOutputType;
   }
 
   if (firstArg && isMeasurement(firstArg)) {
@@ -1058,8 +1060,10 @@ export function errorsAndTransactionsAggregateFunctionOutputType(
 
   // If the function is an inherit type it will have a field as
   // the first parameter and we can use that to get the type.
-  if (firstArg && FIELDS.hasOwnProperty(firstArg)) {
-    return FIELDS[firstArg].valueType as AggregationOutputType;
+  const fieldDef = getFieldDefinition(firstArg ?? '');
+
+  if (fieldDef !== null) {
+    return fieldDef.valueType as AggregationOutputType;
   }
 
   if (firstArg && isMeasurement(firstArg)) {
@@ -1199,8 +1203,10 @@ export function getColumnType(column: Column): ColumnType {
       return outputType;
     }
   } else if (column.kind === 'field') {
-    if (FIELDS.hasOwnProperty(column.field)) {
-      return FIELDS[column.field].valueType as ColumnType;
+    const fieldDef = getFieldDefinition(column.field);
+
+    if (fieldDef !== null) {
+      return fieldDef.valueType as ColumnType;
     }
     if (isMeasurement(column.field)) {
       return measurementType(column.field);

+ 2 - 2
static/app/utils/fields/index.ts

@@ -432,7 +432,7 @@ type AllFieldKeys =
   | keyof typeof SPAN_OP_FIELDS
   | FieldKey;
 
-export const FIELDS: Record<AllFieldKeys, FieldDefinition> = {
+const FIELD_DEFINITIONS: Record<AllFieldKeys, FieldDefinition> = {
   ...AGGREGATION_FIELDS,
   ...MEASUREMENT_FIELDS,
   ...SPAN_OP_FIELDS,
@@ -1014,5 +1014,5 @@ export const DISCOVER_FIELDS = [
 ];
 
 export const getFieldDefinition = (key: string): FieldDefinition | null => {
-  return FIELDS[key] ?? null;
+  return FIELD_DEFINITIONS[key] ?? null;
 };

+ 3 - 8
static/app/views/eventsV2/utils.tsx

@@ -37,12 +37,7 @@ import {
 } from 'sentry/utils/discover/fields';
 import {DisplayModes, TOP_N} from 'sentry/utils/discover/types';
 import {getTitle} from 'sentry/utils/events';
-import {
-  DISCOVER_FIELDS,
-  FIELDS,
-  FieldValueType,
-  getFieldDefinition,
-} from 'sentry/utils/fields';
+import {DISCOVER_FIELDS, FieldValueType, getFieldDefinition} from 'sentry/utils/fields';
 import localStorage from 'sentry/utils/localStorage';
 import {MutableSearch} from 'sentry/utils/tokenizeSearch';
 
@@ -102,8 +97,8 @@ export function decodeColumnOrder(
       const aggregate = AGGREGATIONS[col.function[0]];
       column.isSortable = aggregate && aggregate.isSortable;
     } else if (col.kind === 'field') {
-      if (FIELDS.hasOwnProperty(col.field)) {
-        column.type = FIELDS[col.field].valueType as ColumnValueType;
+      if (getFieldDefinition(col.field) !== null) {
+        column.type = getFieldDefinition(col.field)?.valueType as ColumnValueType;
       } else if (isMeasurement(col.field)) {
         column.type = measurementType(col.field);
       } else if (isSpanOperationBreakdownField(col.field)) {