eschutho commented on a change in pull request #13102: URL: https://github.com/apache/superset/pull/13102#discussion_r584022905
########## File path: superset-frontend/src/SqlLab/components/QuerySearch.tsx ########## @@ -0,0 +1,283 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import React, { useState, useEffect } from 'react'; +import Select from 'src/components/Select'; +import Button from 'src/components/Button'; +import { styled, t, SupersetClient } from '@superset-ui/core'; +import { debounce } from 'lodash'; +import Loading from '../../components/Loading'; +import QueryTable from './QueryTable'; +import { + now, + epochTimeXHoursAgo, + epochTimeXDaysAgo, + epochTimeXYearsAgo, +} from '../../modules/dates'; +import { STATUS_OPTIONS, TIME_OPTIONS } from '../constants'; +import AsyncSelect from '../../components/AsyncSelect'; + +interface propTypes { + actions: { + addDangerToast: (msg: string) => void; + setDatabases: (data: Record<string, any>) => Record<string, any>; + }; + displayLimit: number; +} + +interface userMutatorProps { + value: number; + text: string; +} + +const TableWrapper = styled.div` + display: flex; + flex-direction: column; + flex: 1; + height: 100%; +`; + +const TableStyles = styled.div` + table { + background-color: ${({ theme }) => theme.colors.grayscale.light4}; + } + + .table > thead > tr > th { + border-bottom: ${({ theme }) => theme.gridUnit / 2}px solid + ${({ theme }) => theme.colors.grayscale.light2}; + background: ${({ theme }) => theme.colors.grayscale.light4}; + } +`; + +const StyledTableStylesContainer = styled.div` + overflow: auto; +`; +function QuerySearch({ actions, displayLimit }: propTypes) { + const [databaseId, setDatabaseId] = useState<any>(''); + const [userId, setUserId] = useState<string>(''); + const [searchText, setSearchText] = useState<string>(''); + const [from, setFrom] = useState<string>('28 days ago'); + const [to, setTo] = useState<string>('now'); + const [status, setStatus] = useState<string>('success'); + const [queriesArray, setQueriesArray] = useState<any>([]); + const [queriesLoading, setQueriesLoading] = useState<boolean>(true); + + const getTimeFromSelection = (selection: string) => { + switch (selection) { + case 'now': + return now(); + case '1 hour ago': + return epochTimeXHoursAgo(1); + case '1 day ago': + return epochTimeXDaysAgo(1); + case '7 days ago': + return epochTimeXDaysAgo(7); + case '28 days ago': + return epochTimeXDaysAgo(28); + case '90 days ago': + return epochTimeXDaysAgo(90); + case '1 year ago': + return epochTimeXYearsAgo(1); + default: + return null; + } + }; + + const insertParams = (baseUrl: string, params: string[]) => { + const validParams = params.filter(function (p) { + return p !== ''; + }); + return `${baseUrl}?${validParams.join('&')}`; + }; + + const refreshQueries = async () => { + setQueriesLoading(true); + const params = [ + userId && `user_id=${userId}`, + databaseId && `database_id=${databaseId}`, + searchText && `search_text=${searchText}`, + status && `status=${status}`, + from && `from=${getTimeFromSelection(from)}`, + to && `to=${getTimeFromSelection(to)}`, + ]; + + // make into async method + try { + const promise = await SupersetClient.get({ + endpoint: insertParams('/superset/search_queries', params), + }); + setQueriesArray(promise.json); + } catch (err) { + actions.addDangerToast(t('An error occurred when refreshing queries')); + } finally { + setQueriesLoading(false); + } + }; + + useEffect(() => { + refreshQueries(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + const onUserClicked = (userId: string) => { + setUserId(userId); + refreshQueries(); + }; + + const onDbClicked = (dbId: string) => { + setDatabaseId(dbId); + refreshQueries(); + }; + + const onKeyDown = (event: any) => { + if (event.keyCode === 13) { + refreshQueries(); + } + }; + + const onChange = (e: any) => { + e.persist(); Review comment: what does this do? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
