Skip to content

Commit 7fa3d0b

Browse files
committed
feat(typescript): Convert querBuilder to TypeScript
1 parent 56c9084 commit 7fa3d0b

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@sentry/integrations": "^5.4.2",
2222
"@sentry/typescript": "^5.3.0",
2323
"@types/lodash": "^4.14.134",
24+
"@types/moment-timezone": "^0.5.12",
2425
"@types/react": "^16.7.0",
2526
"@types/react-dom": "^16.7.0",
2627
"algoliasearch": "^3.32.0",

src/sentry/static/sentry/app/views/organizationDiscover/data.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ export const PROMOTED_TAGS = [
2626
];
2727

2828
// All tags are assumed to be strings, except the following
29-
export const SPECIAL_TAGS = {
30-
os_rooted: TYPES.BOOLEAN,
29+
export const SPECIAL_TAGS: any = {
30+
os_rooted: TYPES.DATETIME,
3131
};
32-
3332
// Hide the following tags if they are returned from Snuba since these are
3433
// already mapped to user and release attributes
3534
export const HIDDEN_TAGS = ['sentry:user', 'sentry:release'];

src/sentry/static/sentry/app/views/organizationDiscover/queryBuilder.jsx renamed to src/sentry/static/sentry/app/views/organizationDiscover/queryBuilder.tsx

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import ConfigStore from 'app/stores/configStore';
1212
import MissingProjectWarningModal from './missingProjectWarningModal';
1313
import {COLUMNS, PROMOTED_TAGS, SPECIAL_TAGS, HIDDEN_TAGS} from './data';
1414
import {isValidAggregation} from './aggregations/utils';
15+
import {
16+
AggregationData,
17+
Column,
18+
Query,
19+
Project,
20+
Organization,
21+
SnubaResult,
22+
} from './types';
1523

1624
const API_LIMIT = 10000;
1725

@@ -24,7 +32,7 @@ const DEFAULTS = {
2432
limit: 1000,
2533
};
2634

27-
function applyDefaults(query) {
35+
function applyDefaults(query: any) {
2836
Object.entries(DEFAULTS).forEach(([key, value]) => {
2937
if (!(key in query)) {
3038
query[key] = value;
@@ -38,7 +46,7 @@ function applyDefaults(query) {
3846
* It applies sensible defaults if query parameters are not provided on
3947
* initialization.
4048
*/
41-
export default function createQueryBuilder(initial = {}, organization) {
49+
export default function createQueryBuilder(initial = {}, organization: Organization) {
4250
const api = new Client();
4351
let query = applyDefaults(initial);
4452

@@ -58,7 +66,7 @@ export default function createQueryBuilder(initial = {}, organization) {
5866
);
5967

6068
const columns = COLUMNS.map(col => ({...col, isTag: false}));
61-
let tags = [];
69+
let tags: Column[] = [];
6270

6371
return {
6472
getInternal,
@@ -81,6 +89,10 @@ export default function createQueryBuilder(initial = {}, organization) {
8189
* @returns {Promise<Void>}
8290
*/
8391
function load() {
92+
type TagData = {
93+
tags_key: string;
94+
};
95+
8496
return fetch({
8597
projects: projectsToFetchTags,
8698
fields: ['tags_key'],
@@ -89,16 +101,16 @@ export default function createQueryBuilder(initial = {}, organization) {
89101
range: '90d',
90102
turbo: true,
91103
})
92-
.then(res => {
104+
.then((res: SnubaResult) => {
93105
tags = res.data
94-
.filter(tag => !HIDDEN_TAGS.includes(tag.tags_key))
95-
.map(tag => {
96-
const type = SPECIAL_TAGS[tags.tags_key] || 'string';
106+
.filter((tag: TagData) => !HIDDEN_TAGS.includes(tag.tags_key))
107+
.map((tag: TagData) => {
108+
const type = SPECIAL_TAGS[tag.tags_key] || 'string';
97109
return {name: tag.tags_key, type, isTag: true};
98110
});
99111
})
100-
.catch(err => {
101-
tags = PROMOTED_TAGS.map(tag => {
112+
.catch(() => {
113+
tags = PROMOTED_TAGS.map((tag: string) => {
102114
const type = SPECIAL_TAGS[tag] || 'string';
103115
return {name: tag, type, isTag: true};
104116
});
@@ -160,11 +172,11 @@ export default function createQueryBuilder(initial = {}, organization) {
160172
* @param {*} value Value to update field to
161173
* @returns {Void}
162174
*/
163-
function updateField(field, value) {
175+
function updateField(field: string, value: any) {
164176
query[field] = value;
165177

166178
// Ignore non valid aggregations (e.g. user halfway inputting data)
167-
const validAggregations = query.aggregations.filter(agg =>
179+
const validAggregations = query.aggregations.filter((agg: AggregationData) =>
168180
isValidAggregation(agg, getColumns())
169181
);
170182

@@ -173,7 +185,7 @@ export default function createQueryBuilder(initial = {}, organization) {
173185
getColumns().find(f => f.name === orderbyField) !== undefined;
174186
const hasOrderFieldInSelectedFields = query.fields.includes(orderbyField);
175187
const hasOrderFieldInAggregations = query.aggregations.some(
176-
agg => orderbyField === agg[2]
188+
(agg: AggregationData) => orderbyField === agg[2]
177189
);
178190

179191
const hasInvalidOrderbyField = validAggregations.length
@@ -224,12 +236,12 @@ export default function createQueryBuilder(initial = {}, organization) {
224236
}
225237

226238
return api
227-
.requestPromise(endpoint, {includeAllArgs: true, method: 'POST', data})
239+
.requestPromise(endpoint, {includeAllArgs: true, method: 'POST', data} as any)
228240
.then(([responseData, _, utils]) => {
229241
responseData.pageLinks = utils.getResponseHeader('Link');
230242
return responseData;
231243
})
232-
.catch(err => {
244+
.catch(() => {
233245
throw new Error(t('An error occurred'));
234246
});
235247
}
@@ -261,7 +273,7 @@ export default function createQueryBuilder(initial = {}, organization) {
261273
return Promise.reject(new Error('Start date cannot be after end date'));
262274
}
263275

264-
return api.requestPromise(endpoint, {method: 'POST', data}).catch(() => {
276+
return api.requestPromise(endpoint, {method: 'POST', data} as any).catch(() => {
265277
throw new Error(t('Error with query'));
266278
});
267279
}
@@ -282,7 +294,7 @@ export default function createQueryBuilder(initial = {}, organization) {
282294
* @param {String} Type to fetch - currently either byDay or base
283295
* @returns {Object} Modified query to be run for that type
284296
*/
285-
function getQueryByType(originalQuery, type) {
297+
function getQueryByType(originalQuery: Query, type: string) {
286298
if (type === 'byDayQuery') {
287299
return {
288300
...originalQuery,
@@ -296,7 +308,9 @@ export default function createQueryBuilder(initial = {}, organization) {
296308
// If id or issue.id is present in query fields, always fetch the project.id
297309
// so we can generate links
298310
if (type === 'baseQuery') {
299-
return originalQuery.fields.some(field => field === 'id' || field === 'issue.id')
311+
return (originalQuery.fields || []).some(
312+
field => field === 'id' || field === 'issue.id'
313+
)
300314
? {
301315
...originalQuery,
302316
fields: uniq([...originalQuery.fields, 'project.id']),
@@ -323,13 +337,13 @@ export default function createQueryBuilder(initial = {}, organization) {
323337
* @param {Object} [q] optional query to reset to
324338
* @returns {Void}
325339
*/
326-
function reset(q = {}) {
340+
function reset(q: Query = {}) {
327341
const [validProjects, invalidProjects] = partition(q.projects || [], project =>
328342
defaultProjectIds.includes(project)
329343
);
330344

331345
if (invalidProjects.length) {
332-
openModal(deps => (
346+
openModal((deps: any) => (
333347
<MissingProjectWarningModal
334348
organization={organization}
335349
validProjects={validProjects}
@@ -345,6 +359,6 @@ export default function createQueryBuilder(initial = {}, organization) {
345359
}
346360
}
347361

348-
function getProjectIds(projects) {
362+
function getProjectIds(projects: Project[]) {
349363
return projects.map(project => parseInt(project.id, 10));
350364
}

src/sentry/static/sentry/app/views/organizationDiscover/types.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export type ConditionData = [
1010
string | number | boolean | null
1111
];
1212

13+
export type Query = {
14+
projects?: number[];
15+
fields?: string[];
16+
aggregations?: AggregationData[];
17+
conditions?: ConditionData[];
18+
orderby?: string;
19+
limit?: number;
20+
};
21+
1322
export type SnubaResult = {
1423
data: any[];
1524
meta: any;
@@ -35,3 +44,14 @@ export type DiscoverBaseProps = {
3544
columns: Column[];
3645
disabled: boolean;
3746
};
47+
48+
export type Organization = {
49+
id: string;
50+
slug: string;
51+
projects: any[];
52+
access: string[];
53+
};
54+
55+
export type Project = {
56+
id: string;
57+
};

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,13 @@
16901690
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.134.tgz#9032b440122db3a2a56200e91191996161dde5b9"
16911691
integrity sha512-2/O0khFUCFeDlbi7sZ7ZFRCcT812fAeOLm7Ev4KbwASkZ575TDrDcY7YyaoHdTOzKcNbfiwLYZqPmoC4wadrsw==
16921692

1693+
"@types/moment-timezone@^0.5.12":
1694+
version "0.5.12"
1695+
resolved "https://registry.yarnpkg.com/@types/moment-timezone/-/moment-timezone-0.5.12.tgz#0fb680c03db194fe8ff4551eaeb1eec8d3d80e9f"
1696+
integrity sha512-hnHH2+Efg2vExr/dSz+IX860nSiyk9Sk4pJF2EmS11lRpMcNXeB4KBW5xcgw2QPsb9amTXdsVNEe5IoJXiT0uw==
1697+
dependencies:
1698+
moment ">=2.14.0"
1699+
16931700
"@types/node@*", "@types/node@^10.0.5":
16941701
version "10.14.7"
16951702
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.14.7.tgz#1854f0a9aa8d2cd6818d607b3d091346c6730362"
@@ -9091,6 +9098,11 @@ [email protected], "moment@>= 2.9.0":
90919098
resolved "https://registry.yarnpkg.com/moment/-/moment-2.23.0.tgz#759ea491ac97d54bac5ad776996e2a58cc1bc225"
90929099
integrity sha512-3IE39bHVqFbWWaPOMHZF98Q9c3LDKGTmypMiTM2QygGXXElkFWIH7GxfmlwmY2vwa+wmNsoYZmG2iusf1ZjJoA==
90939100

9101+
moment@>=2.14.0:
9102+
version "2.24.0"
9103+
resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b"
9104+
integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==
9105+
90949106
moo@^0.4.3:
90959107
version "0.4.3"
90969108
resolved "https://registry.yarnpkg.com/moo/-/moo-0.4.3.tgz#3f847a26f31cf625a956a87f2b10fbc013bfd10e"

0 commit comments

Comments
 (0)