forked from segmentio/analytics-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_reader.py
145 lines (98 loc) · 3.5 KB
/
file_reader.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
import argparse
import json
import os
import random
import sys
import tempfile
import dateutil.parser as date_parser
from analytics.client import Client
clients = {}
def process_file(filename):
'''Processes the log file line by line'''
print 'Processing file', filename
with open(filename) as f:
for line in f:
process_line(line)
def process_line(line):
'''Tracks a single line by parsing its json and tracking'''
line = line.strip()
try:
contents = json.loads(line)
secret, action = contents['secret'], contents['action']
if action == 'track':
track(secret, contents)
elif action == 'identify':
identify(secret, contents)
elif action == 'alias':
alias(secret, contents)
else:
print 'Error processing call'
except Exception as e:
print 'Malformed JSON', e
def get_client(secret):
'''Returns or creates a new client by the secret'''
if secret not in clients:
clients[secret] = Client(secret)
return clients[secret]
def track(secret, contents):
'''Tracks an event'''
client = get_client(secret)
body = shared_properties(contents)
body['event'] = contents['event']
if contents['properties'] is not None:
body['properties'] = contents['properties']
return client.track(**body)
def identify(secret, contents):
'''Identifies a user with traits'''
client = get_client(secret)
body = shared_properties(contents)
if contents['traits'] is not None:
body['traits'] = contents['traits']
return client.identify(**body)
def alias(secret, contents):
'''Aliases from one user to the other'''
client = get_client(secret)
body = {
'from_id': contents['from'],
'to_id': contents['to'],
'timestamp': date_parser.parse(contents['timestamp']),
'context': contents['context']
}
return client.alias(**body)
def shared_properties(contents):
'''Returns the common properties between track and identify'''
return {
'user_id': contents['user_id'],
'timestamp': date_parser.parse(contents['timestamp']),
'context': contents['context']
}
'''
Processes the analytics.log file.
Requires: analytics-python (pip install analytics-python)
Usage:
python file_reader.py [--file /path/to/analytics.log]
'''
if __name__ == '__main__':
# Parse the file arguments
parser = argparse.ArgumentParser(
description='Read from the analytics.log file')
parser.add_argument('--file')
args = parser.parse_args()
default_file = os.path.join(tempfile.gettempdir(), 'analytics.log')
filename = args.file or default_file
# Use the same directory for processing the log as where it is stored.
# This prevents "Invalid cross-device link" errors.
processing_dir = os.path.dirname(filename)
processing_filename = os.path.join(processing_dir,
'analytics-%d.log' % random.randint(0, 1000))
if not os.path.exists(filename):
print 'Error: The filename you specified doesn\'t exist: ', filename
sys.exit(1)
# Move the file so that we don't read from constantly appended file.
# Then process it.
os.rename(filename, processing_filename)
process_file(processing_filename)
os.unlink(processing_filename)
for client in clients.values():
client.flush(async=False)
print 'Finished uploading analytics data'