Skip to content

Commit 7aeeab4

Browse files
author
Iñigo Medina (aka MacGyver)
authored
Merge pull request #80 from CartoDB/dev-center-release
Dev center release
2 parents f5c72ae + b0d73ad commit 7aeeab4

33 files changed

+2726
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
```python
2+
import argparse
3+
import logging
4+
import os
5+
import warnings
6+
7+
from carto.auth import APIKeyAuthClient
8+
from carto.datasets import DatasetManager
9+
10+
warnings.filterwarnings('ignore')
11+
12+
# python change_dataset_privacy.py tornados LINK
13+
14+
# Logger (better than print)
15+
logging.basicConfig(
16+
level=logging.INFO,
17+
format=' %(asctime)s - %(levelname)s - %(message)s',
18+
datefmt='%I:%M:%S %p')
19+
logger = logging.getLogger()
20+
21+
# set input arguments
22+
parser = argparse.ArgumentParser(
23+
description='Changes the privacy of a dataset')
24+
25+
parser.add_argument('dataset_name', type=str,
26+
help='The name of the dataset in CARTO')
27+
28+
parser.add_argument('privacy', type=str,
29+
help='One of: LINK, PUBLIC, PRIVATE')
30+
31+
parser.add_argument('--organization', type=str, dest='organization',
32+
default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '',
33+
help='Set the name of the organization' +
34+
' account (defaults to env variable CARTO_ORG)')
35+
36+
parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL',
37+
default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '',
38+
help='Set the base URL. For example:' +
39+
' https://username.carto.com/ ' +
40+
'(defaults to env variable CARTO_API_URL)')
41+
42+
parser.add_argument('--api_key', dest='CARTO_API_KEY',
43+
default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '',
44+
help='Api key of the account' +
45+
' (defaults to env variable CARTO_API_KEY)')
46+
47+
args = parser.parse_args()
48+
49+
# Set authentification to CARTO
50+
if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization:
51+
auth_client = APIKeyAuthClient(
52+
args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization)
53+
dataset_manager = DatasetManager(auth_client)
54+
dataset = dataset_manager.get(args.dataset_name)
55+
else:
56+
logger.error('You need to provide valid credentials, run with -h parameter for details')
57+
import sys
58+
sys.exit(1)
59+
60+
# PRIVATE, PUBLIC, LINK
61+
dataset.privacy = args.privacy
62+
dataset.save()
63+
64+
logger.info("Done!")
65+
```

docs/examples/admin/check-query.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
```python
2+
import argparse
3+
import logging
4+
import os
5+
import warnings
6+
7+
from carto.auth import APIKeyAuthClient
8+
from carto.datasets import DatasetManager
9+
from carto.sql import SQLClient
10+
11+
warnings.filterwarnings('ignore')
12+
13+
# python check_query.py "select version()"
14+
15+
# Logger (better than print)
16+
logging.basicConfig(
17+
level=logging.INFO,
18+
format=' %(asctime)s - %(levelname)s - %(message)s',
19+
datefmt='%I:%M:%S %p')
20+
logger = logging.getLogger()
21+
22+
# set input arguments
23+
parser = argparse.ArgumentParser(
24+
description='Check if query can be optimized')
25+
parser.add_argument('queryUser', type=str,
26+
help='Set query to analyze')
27+
28+
parser.add_argument('--organization', type=str, dest='organization',
29+
default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '',
30+
help='Set the name of the organization' +
31+
' account (defaults to env variable CARTO_ORG)')
32+
33+
parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL',
34+
default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '',
35+
help='Set the base URL. For example:' +
36+
' https://username.carto.com/ ' +
37+
'(defaults to env variable CARTO_API_URL)')
38+
39+
parser.add_argument('--api_key', dest='CARTO_API_KEY',
40+
default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '',
41+
help='Api key of the account' +
42+
' (defaults to env variable CARTO_API_KEY)')
43+
44+
args = parser.parse_args()
45+
46+
# Authenticate to CARTO account
47+
if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization:
48+
auth_client = APIKeyAuthClient(
49+
args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization)
50+
dataset_manager = DatasetManager(auth_client)
51+
else:
52+
logger.error('You need to provide valid credentials, run with -h parameter for details')
53+
import sys
54+
sys.exit(1)
55+
56+
# SQL wrapper
57+
sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY))
58+
59+
query = sql.send('EXPLAIN ANALYZE ' + args.queryUser)
60+
61+
for key, value in query.items():
62+
if key == 'rows':
63+
for itr in value:
64+
logger.info(itr)
65+
if key == 'time':
66+
logger.info(str(key) + ': ' + str(value))
67+
68+
query_arr = args.queryUser.upper().split()
69+
70+
71+
for i in query_arr:
72+
if i == '*':
73+
logger.warn('Do you need all columns? ' +
74+
'You can improve the performance ' +
75+
'by only selecting the needed ' +
76+
'columns instead of doing a \"SELECT *\" statement')
77+
if i == 'WHERE':
78+
logger.warn('Have you applied indexes on the columns ' +
79+
'that you use after the WHERE statement?')
80+
if i == 'the_geom' or i == 'the_geom_webmercator':
81+
logger.warn('If the geometry is a polygon,' +
82+
' have you simplified the geometries ' +
83+
'with the ST_Simplify() function?')
84+
```
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
```python
2+
import argparse
3+
import os
4+
import re
5+
import warnings
6+
7+
from carto.auth import APIKeyAuthClient
8+
from carto.datasets import DatasetManager
9+
from carto.sql import SQLClient
10+
11+
warnings.filterwarnings('ignore')
12+
13+
# set input arguments
14+
parser = argparse.ArgumentParser(
15+
description='Exports the CREATE TABLE scripts of all the account datasets')
16+
17+
parser.add_argument('--organization', type=str, dest='organization',
18+
default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '',
19+
help='Set the name of the organization' +
20+
' account (defaults to env variable CARTO_ORG)')
21+
22+
parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL',
23+
default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '',
24+
help='Set the base URL. For example:' +
25+
' https://username.carto.com/ ' +
26+
'(defaults to env variable CARTO_API_URL)')
27+
28+
parser.add_argument('--api_key', dest='CARTO_API_KEY',
29+
default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '',
30+
help='Api key of the account' +
31+
' (defaults to env variable CARTO_API_KEY)')
32+
33+
args = parser.parse_args()
34+
35+
# Authenticate to CARTO account
36+
if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization:
37+
auth_client = APIKeyAuthClient(
38+
args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization)
39+
dataset_manager = DatasetManager(auth_client)
40+
else:
41+
logger.error('You need to provide valid credentials, run with -h parameter for details')
42+
import sys
43+
sys.exit(1)
44+
45+
# SQL wrapper
46+
sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY))
47+
48+
# get username from base_url
49+
substring = re.search('https://(.+?).carto.com', args.CARTO_BASE_URL)
50+
if substring:
51+
username = substring.group(1)
52+
53+
# check all table name of account
54+
all_tables = []
55+
56+
tables = sql.send(
57+
"select pg_class.relname from pg_class, pg_roles, pg_namespace" +
58+
" where pg_roles.oid = pg_class.relowner and " +
59+
"pg_roles.rolname = current_user " +
60+
"and pg_namespace.oid = pg_class.relnamespace and pg_class.relkind = 'r'")
61+
62+
q = "select \
63+
'CREATE TABLE ' || relname || E'\n(\n' || \
64+
array_to_string( \
65+
array_agg( \
66+
' ' || column_name || ' ' || type || ' '|| not_null \
67+
) \
68+
, E',\n' \
69+
) || E'\n);\n' as create_table \
70+
from \
71+
( \
72+
select \
73+
distinct on (column_name) c.relname, a.attname AS column_name, \
74+
pg_catalog.format_type(a.atttypid, a.atttypmod) as type, \
75+
case \
76+
when a.attnotnull \
77+
then 'NOT NULL' \
78+
else 'NULL' \
79+
END as not_null \
80+
FROM pg_class c, \
81+
pg_attribute a, \
82+
pg_type t \
83+
WHERE c.relname = '{table_name}' \
84+
AND a.attnum > 0 \
85+
AND a.attrelid = c.oid \
86+
AND a.atttypid = t.oid \
87+
and a.attname not in ('cartodb_id', 'the_geom_webmercator') \
88+
ORDER BY column_name, a.attnum \
89+
) as tabledefinition \
90+
group by relname"
91+
92+
with open('create_table.sql', 'w') as f:
93+
for k, v in tables.items():
94+
if k == 'rows':
95+
for itr in v:
96+
try:
97+
dataset_name = itr['relname']
98+
print("Found dataset: " + dataset_name)
99+
result = sql.send(q.format(table_name=dataset_name))
100+
create_table = result['rows'][0]['create_table']
101+
f.write(create_table + "\n")
102+
except:
103+
print("Error while exporting: " + dataset_name)
104+
continue
105+
f.close()
106+
107+
print('\nScript exported')
108+
```

docs/examples/admin/kill-query.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
```python
2+
import argparse
3+
import logging
4+
import os
5+
import warnings
6+
7+
from carto.auth import APIKeyAuthClient
8+
from carto.sql import SQLClient
9+
10+
warnings.filterwarnings('ignore')
11+
12+
# Logger (better than print)
13+
logging.basicConfig(
14+
level=logging.INFO,
15+
format=' %(asctime)s - %(levelname)s - %(message)s',
16+
datefmt='%I:%M:%S %p')
17+
logger = logging.getLogger()
18+
19+
# set input arguments
20+
parser = argparse.ArgumentParser(
21+
description='Kills a running query')
22+
23+
parser.add_argument('pid', type=str,
24+
default=None,
25+
help='Set the pid of the query to kill')
26+
27+
parser.add_argument('--organization', type=str, dest='organization',
28+
default=os.environ['CARTO_ORG'] if 'CARTO_ORG' in os.environ else '',
29+
help='Set the name of the organization' +
30+
' account (defaults to env variable CARTO_ORG)')
31+
32+
parser.add_argument('--base_url', type=str, dest='CARTO_BASE_URL',
33+
default=os.environ['CARTO_API_URL'] if 'CARTO_API_URL' in os.environ else '',
34+
help='Set the base URL. For example:' +
35+
' https://username.carto.com/ ' +
36+
'(defaults to env variable CARTO_API_URL)')
37+
38+
parser.add_argument('--api_key', dest='CARTO_API_KEY',
39+
default=os.environ['CARTO_API_KEY'] if 'CARTO_API_KEY' in os.environ else '',
40+
help='Api key of the account' +
41+
' (defaults to env variable CARTO_API_KEY)')
42+
43+
args = parser.parse_args()
44+
45+
if args.CARTO_BASE_URL and args.CARTO_API_KEY and args.organization:
46+
auth_client = APIKeyAuthClient(
47+
args.CARTO_BASE_URL, args.CARTO_API_KEY, args.organization)
48+
else:
49+
logger.error('You need to provide valid credentials, run with -h parameter for details')
50+
import sys
51+
sys.exit(1)
52+
53+
# SQL wrapper
54+
sql = SQLClient(APIKeyAuthClient(args.CARTO_BASE_URL, args.CARTO_API_KEY))
55+
56+
queries = "SELECT pg_cancel_backend('" + args.pid + \
57+
"') from pg_stat_activity where usename=current_user;"
58+
59+
try:
60+
sql.send(queries)
61+
logger.info('Query killed')
62+
except:
63+
logger.warn('Something went wrong')
64+
```

0 commit comments

Comments
 (0)