##// END OF EJS Templates
partitions: add some logging
ergo -
Show More
@@ -1,151 +1,153 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # AppEnlight Enterprise Edition, including its added features, Support
19 19 # services, and proprietary license terms, please see
20 20 # https://rhodecode.com/licenses/
21 21
22 22 from pyramid.view import view_config
23 23 from appenlight.models import DBSession, Datastores
24 24 from appenlight.forms import get_partition_deletion_form
25 25
26 26 import logging
27 27
28 28 from zope.sqlalchemy import mark_changed
29 29 from datetime import datetime
30 30 import sqlalchemy as sa
31 31
32 32 log = logging.getLogger(__name__)
33 33
34 34
35 35 def get_partition_stats():
36 36 table_query = """
37 37 SELECT table_name
38 38 FROM information_schema.tables
39 39 GROUP BY table_name
40 40 ORDER BY table_name
41 41 """
42 42
43 43 permanent_partitions = {}
44 44 daily_partitions = {}
45 45
46 46 def is_int(data):
47 47 try:
48 48 int(data)
49 49 return True
50 50 except Exception:
51 51 pass
52 52 return False
53 53
54 54 def add_key(key, holder):
55 55 if not ix_time in holder:
56 56 holder[ix_time] = {'pg': [], 'elasticsearch': []}
57 57
58 58 for partition in list(Datastores.es.aliases().keys()):
59 59 if not partition.startswith('rcae'):
60 60 continue
61 61 split_data = partition.split('_')
62 62 permanent = False
63 63 # if we dont have a day then treat it as permanent partion
64 64 if False in list(map(is_int, split_data[-3:])):
65 65 ix_time = datetime(year=int(split_data[-2]),
66 66 month=int(split_data[-1]),
67 67 day=1).date()
68 68 permanent = True
69 69 else:
70 70 ix_time = datetime(year=int(split_data[-3]),
71 71 month=int(split_data[-2]),
72 72 day=int(split_data[-1])).date()
73 73
74 74 ix_time = str(ix_time)
75 75 if permanent:
76 76 add_key(ix_time, permanent_partitions)
77 77 if ix_time not in permanent_partitions:
78 78 permanent_partitions[ix_time]['elasticsearch'] = []
79 79 permanent_partitions[ix_time]['elasticsearch'].append(partition)
80 80 else:
81 81 add_key(ix_time, daily_partitions)
82 82 if ix_time not in daily_partitions:
83 83 daily_partitions[ix_time]['elasticsearch'] = []
84 84 daily_partitions[ix_time]['elasticsearch'].append(partition)
85 85
86 86 for row in DBSession.execute(table_query):
87 87 splitted = row['table_name'].split('_')
88 88 if 'p' in splitted:
89 89 # dealing with partition
90 90 split_data = [int(x) for x in splitted[splitted.index('p') + 1:]]
91 91 if len(split_data) == 3:
92 92 ix_time = datetime(split_data[0], split_data[1],
93 93 split_data[2]).date()
94 94 ix_time = str(ix_time)
95 95 add_key(ix_time, daily_partitions)
96 96 daily_partitions[ix_time]['pg'].append(row['table_name'])
97 97 else:
98 98 ix_time = datetime(split_data[0], split_data[1], 1).date()
99 99 ix_time = str(ix_time)
100 100 add_key(ix_time, permanent_partitions)
101 101 permanent_partitions[ix_time]['pg'].append(row['table_name'])
102 102
103 103 return permanent_partitions, daily_partitions
104 104
105 105
106 106 @view_config(route_name='section_view', permission='root_administration',
107 107 match_param=['section=admin_section', 'view=partitions'],
108 108 renderer='json', request_method='GET')
109 109 def index(request):
110 110 permanent_partitions, daily_partitions = get_partition_stats()
111 111
112 112 return {"permanent_partitions": sorted(list(permanent_partitions.items()),
113 113 key=lambda x: x[0], reverse=True),
114 114 "daily_partitions": sorted(list(daily_partitions.items()),
115 115 key=lambda x: x[0], reverse=True)}
116 116
117 117
118 118 @view_config(route_name='section_view', request_method='POST',
119 119 match_param=['section=admin_section', 'view=partitions_remove'],
120 120 renderer='json', permission='root_administration')
121 121 def partitions_remove(request):
122 122 permanent_partitions, daily_partitions = get_partition_stats()
123 123 pg_partitions = []
124 124 es_partitions = []
125 125 for item in list(permanent_partitions.values()) + list(daily_partitions.values()):
126 126 es_partitions.extend(item['elasticsearch'])
127 127 pg_partitions.extend(item['pg'])
128 128 FormCls = get_partition_deletion_form(es_partitions, pg_partitions)
129 129 form = FormCls(es_index=request.unsafe_json_body['es_indices'],
130 130 pg_index=request.unsafe_json_body['pg_indices'],
131 131 confirm=request.unsafe_json_body['confirm'],
132 132 csrf_context=request)
133 133 if form.validate():
134 134 for ix in form.data['es_index']:
135 log.warning('deleting ES partition: {}'.format(ix))
135 136 Datastores.es.delete_index(ix)
136 137 for ix in form.data['pg_index']:
138 log.warning('deleting PG partition: {}'.format(ix))
137 139 stmt = sa.text('DROP TABLE %s CASCADE' % sa.text(ix))
138 140 session = DBSession()
139 141 session.connection().execute(stmt)
140 142 mark_changed(session)
141 143
142 144 for field, error in form.errors.items():
143 145 msg = '%s: %s' % (field, error[0])
144 146 request.session.flash(msg, 'error')
145 147
146 148 permanent_partitions, daily_partitions = get_partition_stats()
147 149 return {
148 150 "permanent_partitions": sorted(
149 151 list(permanent_partitions.items()), key=lambda x: x[0], reverse=True),
150 152 "daily_partitions": sorted(
151 153 list(daily_partitions.items()), key=lambda x: x[0], reverse=True)}
General Comments 0
You need to be logged in to leave comments. Login now