forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply V10 beta period feedback / fixes (getredash#5611)
The following PR's were cherry-picked: * Excel & CSV query runner (getredash#2478) * Pin python3 image version (getredash#5570) * Fix: Edit Source button disappeared for users without CanEdit perms (getredash#5568) * Fix: Specify the protobuf version (getredash#5608) Plus one additional change exclusive to this branch: * Replace reference to yarn with NPM This happened because we cherry-picked getredash#5570 but did not also incorporate getredash#5541 into V10. Co-authored-by: deecay <deecay@users.noreply.github.com> Co-authored-by: Levko Kravets <levko.ne@gmail.com> Co-authored-by: zoomdot <gninggoon@gmail.com>
- Loading branch information
1 parent
92e5d78
commit f312adf
Showing
7 changed files
with
205 additions
and
3 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import logging | ||
import yaml | ||
import requests | ||
import io | ||
|
||
from redash import settings | ||
from redash.query_runner import * | ||
from redash.utils import json_dumps | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import pandas as pd | ||
import numpy as np | ||
enabled = True | ||
except ImportError: | ||
enabled = False | ||
|
||
|
||
class CSV(BaseQueryRunner): | ||
should_annotate_query = False | ||
|
||
@classmethod | ||
def name(cls): | ||
return "CSV" | ||
|
||
@classmethod | ||
def enabled(cls): | ||
return enabled | ||
|
||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
'type': 'object', | ||
'properties': {}, | ||
} | ||
|
||
def __init__(self, configuration): | ||
super(CSV, self).__init__(configuration) | ||
self.syntax = "yaml" | ||
|
||
def test_connection(self): | ||
pass | ||
|
||
def run_query(self, query, user): | ||
path = "" | ||
ua = "" | ||
args = {} | ||
try: | ||
args = yaml.safe_load(query) | ||
path = args['url'] | ||
args.pop('url', None) | ||
ua = args['user-agent'] | ||
args.pop('user-agent', None) | ||
|
||
if is_private_address(path) and settings.ENFORCE_PRIVATE_ADDRESS_BLOCK: | ||
raise Exception("Can't query private addresses.") | ||
except: | ||
pass | ||
|
||
try: | ||
response = requests.get(url=path, headers={"User-agent": ua}) | ||
workbook = pd.read_csv(io.BytesIO(response.content),sep=",", **args) | ||
|
||
df = workbook.copy() | ||
data = {'columns': [], 'rows': []} | ||
conversions = [ | ||
{'pandas_type': np.integer, 'redash_type': 'integer',}, | ||
{'pandas_type': np.inexact, 'redash_type': 'float',}, | ||
{'pandas_type': np.datetime64, 'redash_type': 'datetime', 'to_redash': lambda x: x.strftime('%Y-%m-%d %H:%M:%S')}, | ||
{'pandas_type': np.bool_, 'redash_type': 'boolean'}, | ||
{'pandas_type': np.object, 'redash_type': 'string'} | ||
] | ||
labels = [] | ||
for dtype, label in zip(df.dtypes, df.columns): | ||
for conversion in conversions: | ||
if issubclass(dtype.type, conversion['pandas_type']): | ||
data['columns'].append({'name': label, 'friendly_name': label, 'type': conversion['redash_type']}) | ||
labels.append(label) | ||
func = conversion.get('to_redash') | ||
if func: | ||
df[label] = df[label].apply(func) | ||
break | ||
data['rows'] = df[labels].replace({np.nan: None}).to_dict(orient='records') | ||
|
||
json_data = json_dumps(data) | ||
error = None | ||
except KeyboardInterrupt: | ||
error = "Query cancelled by user." | ||
json_data = None | ||
except Exception as e: | ||
error = "Error reading {0}. {1}".format(path, str(e)) | ||
json_data = None | ||
|
||
return json_data, error | ||
|
||
def get_schema(self): | ||
raise NotSupported() | ||
|
||
register(CSV) |
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 logging | ||
import yaml | ||
import requests | ||
|
||
from redash import settings | ||
from redash.query_runner import * | ||
from redash.utils import json_dumps | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import pandas as pd | ||
import xlrd | ||
import openpyxl | ||
import numpy as np | ||
enabled = True | ||
except ImportError: | ||
enabled = False | ||
|
||
class Excel(BaseQueryRunner): | ||
should_annotate_query = False | ||
|
||
@classmethod | ||
def enabled(cls): | ||
return enabled | ||
|
||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
'type': 'object', | ||
'properties': {}, | ||
} | ||
|
||
def __init__(self, configuration): | ||
super(Excel, self).__init__(configuration) | ||
self.syntax = "yaml" | ||
|
||
def test_connection(self): | ||
pass | ||
|
||
def run_query(self, query, user): | ||
path = "" | ||
ua = "" | ||
args = {} | ||
try: | ||
args = yaml.safe_load(query) | ||
path = args['url'] | ||
args.pop('url', None) | ||
ua = args['user-agent'] | ||
args.pop('user-agent', None) | ||
|
||
if is_private_address(path) and settings.ENFORCE_PRIVATE_ADDRESS_BLOCK: | ||
raise Exception("Can't query private addresses.") | ||
except: | ||
pass | ||
|
||
try: | ||
response = requests.get(url=path, headers={"User-agent": ua}) | ||
workbook = pd.read_excel(response.content, **args) | ||
|
||
df = workbook.copy() | ||
data = {'columns': [], 'rows': []} | ||
conversions = [ | ||
{'pandas_type': np.integer, 'redash_type': 'integer',}, | ||
{'pandas_type': np.inexact, 'redash_type': 'float',}, | ||
{'pandas_type': np.datetime64, 'redash_type': 'datetime', 'to_redash': lambda x: x.strftime('%Y-%m-%d %H:%M:%S')}, | ||
{'pandas_type': np.bool_, 'redash_type': 'boolean'}, | ||
{'pandas_type': np.object, 'redash_type': 'string'} | ||
] | ||
labels = [] | ||
for dtype, label in zip(df.dtypes, df.columns): | ||
for conversion in conversions: | ||
if issubclass(dtype.type, conversion['pandas_type']): | ||
data['columns'].append({'name': label, 'friendly_name': label, 'type': conversion['redash_type']}) | ||
labels.append(label) | ||
func = conversion.get('to_redash') | ||
if func: | ||
df[label] = df[label].apply(func) | ||
break | ||
data['rows'] = df[labels].replace({np.nan: None}).to_dict(orient='records') | ||
|
||
json_data = json_dumps(data) | ||
error = None | ||
except KeyboardInterrupt: | ||
error = "Query cancelled by user." | ||
json_data = None | ||
except Exception as e: | ||
error = "Error reading {0}. {1}".format(path, str(e)) | ||
json_data = None | ||
|
||
return json_data, error | ||
|
||
def get_schema(self): | ||
raise NotSupported() | ||
|
||
register(Excel) |
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