-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathusage.py
executable file
·91 lines (77 loc) · 2.95 KB
/
usage.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2017 Jordi Mas i Hernandez <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import os
import datetime
from shutil import copyfile
'''
This class keeps a log of the usage of a service
- For usage write a line on the file with the date
- At the number of days specified cleans old entries
'''
class Usage(object):
FILE = "usage.txt"
DAYS_TO_KEEP = 7
def __init__(self):
self.rotate = True
def _get_time_now(self):
return datetime.datetime.utcnow()
def log(self):
try:
with open(self.FILE, "a+") as file_out:
current_time = self._get_time_now().strftime('%Y-%m-%d %H:%M:%S')
file_out.write('{0}\n'.format(current_time))
if self.rotate and self._is_old_line(self._read_first_line()):
self._rotate_file()
except Exception as exception:
print("Error:" + str(exception))
pass
def get_stats(self, date_requested):
count = 0
try:
with open(self.FILE, "r") as file_in:
for line in file_in:
datetime_no_newline = line[:len(line)-1]
line_datetime = datetime.datetime.strptime(datetime_no_newline, '%Y-%m-%d %H:%M:%S')
if line_datetime.date() == date_requested.date():
count = count + 1
except:
pass
return str(count)
def _read_first_line(self):
try:
with open(self.FILE, "r") as f:
first = f.readline()
return first
except IOError:
return None
def _is_old_line(self, line):
if line is None:
return False
line = line[:len(line)-1]
line_datetime = datetime.datetime.strptime(line, '%Y-%m-%d %H:%M:%S')
return line_datetime < self._get_time_now() - datetime.timedelta(days = self.DAYS_TO_KEEP)
def _rotate_file(self):
TEMP = "usage.bak"
copyfile(self.FILE, TEMP)
with open(TEMP, "r") as temp:
with open(self.FILE, "w") as new:
for line in temp:
if self._is_old_line(line) is False:
new.write(line)