forked from zhlgh603/psiphon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsi_mail_stats.py
149 lines (114 loc) · 5.3 KB
/
psi_mail_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
#
# Copyright (c) 2013 - 2016, Psiphon Inc.
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from collections import defaultdict
from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions
import pynliner
import os
import sys
import json
import datetime
from time import time, mktime
from elasticsearch import ConflictError, NotFoundError, ConnectionTimeout, Elasticsearch, helpers as esHelpers
# Using the elasticsearch server enrty from a file
sys.path.append(os.path.abspath(os.path.join('.', 'Query')))
import psi_es_server_config as server_config
# Using the FeedbackDecryptor's mail capabilities
sys.path.append(os.path.abspath(os.path.join('..', 'EmailResponder')))
sys.path.append(os.path.abspath(os.path.join('..', 'EmailResponder', 'FeedbackDecryptor')))
import sender
from config import config
es = None
class ElasticsearchUnreachableException(Exception):
def __init__(self, passedHost):
self.passedHost = passedHost
def __unicode__(self):
return "The Elasticsearch cluster at '%s' is not reachable" % (self.passedHost)
def __str__(self):
return unicode(self).encode("utf-8")
# Main function to do the search based on query and time
def _get_connected(query_files, index_param):
res = None
startTime = time()
print("[%s] Starting query - 30 minute timeout" % datetime.datetime.now())
# "query.json" is JSON object in a file that is a valid elasticsearch query
with open(query_files, 'r') as f:
query = json.load(f)
res = es.search(index=index_param, body=query, request_timeout=1800)
print("[%s] Finished in %.2fs" % (datetime.datetime.now(), round((time()-startTime), 2)))
return res['aggregations']
def render_mail(data):
'''
Will throw exception if data does not match expected structure (that is,
if the template rendering fails).
'''
template_filename = 'psi_mail_stats.mako'
template_lookup = TemplateLookup(directories=[os.path.dirname(os.path.abspath(__file__))])
# SECURITY IMPORTANT: `'h'` in the `default_filters` list causes HTML
# escaping to be applied to all expression tags (${...}) in this
# template. Because we're output untrusted user-supplied data, this is
# essential.
template = Template(filename=template_filename,
default_filters=['unicode', 'h'],
lookup=template_lookup)
try:
rendered = template.render(data=data)
except:
raise Exception(exceptions.text_error_template().render())
# CSS in email HTML must be inline
rendered = pynliner.fromString(rendered)
return rendered
if __name__ == "__main__":
tables_data = {}
tables_data['table_columns'] = [
('Yesterday', '36 hours', '12 hours'),
('1 week ago', '204 hours', '180 hours'),
('Past Week', '180 hours', '12 hours'),
]
server_entry = server_config.ELASTICSEARCH_SERVER_IP_ADDRESS + ':' + server_config.ELASTICSEARCH_SERVER_PORT
try:
es = Elasticsearch(hosts=[server_entry], retry_on_timeout=True, max_retries=3)
if not es.ping():
raise ElasticsearchUnreachableException(elasticsearch)
# index_param = "psiphon-connected-{:%Y.%m.%d}".format(today)
# More eff way to query, only use 8 days index
index_connections = "psiphon-connected-*"
index_unique_users = "aggregated-connected-*"
index_page_views = "psiphon-page_views-*"
# Different query for Unique users and Connections
connections_result = _get_connected('./Query/query_connections.json', index_connections)
unique_users_result = _get_connected('./Query/query_unique_users.json', index_unique_users)
# Getting total number in all region
connections_total_result = _get_connected('./Query/query_connections_total.json', index_connections)
unique_users_total_result = _get_connected('./Query/query_unique_users_total.json', index_unique_users)
tables_data['connections'] = connections_result
tables_data['unique_users'] = unique_users_result
tables_data['connections_total'] = connections_total_result
tables_data['unique_users_total'] = unique_users_total_result
# page_views_result = _get_connected('query_page_views.json', index_page_views)
# print page_views_result
except ElasticsearchUnreachableException as e:
print("Could not initialize. The Elasticsearch cluster at '%s' is unavailable" % (e.passedHost))
html_body = render_mail(tables_data)
sender.send(config['statsEmailRecipients'],
config['emailUsername'],
'Psiphon 3 Stats',
repr(tables_data),
html_body)