This repository was archived by the owner on Apr 11, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathop.py
More file actions
72 lines (66 loc) · 2.69 KB
/
Copy pathop.py
File metadata and controls
72 lines (66 loc) · 2.69 KB
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
# -*- coding: utf8 -*-
## Copyright (c) 2013 Stefan Thesing
##
##This file is part of Podstatty.
##
##Podstatty 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.
##
##Podstatty 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 Podstatty. If not, see http://www.gnu.org/licenses/.
import os, glob
from db import Filesizes, Stats
class Op:
"""
A class doing the actual data processing.
"""
def __init__(self, db):
self.db = db
def process_files(self, logfiles_path, exclude_strings):
#Get a list of all files in logfiles_path that match the naming
# scheme
filenames = glob.glob(logfiles_path + '/access_log_*filtered.txt')
# Process each file and store the data to database
for filename in filenames:
print "Coming up next: " +filename
self.db.add_file(filename, exclude_strings)
def dump_absoluste_all_to_csv(self, output_file):
# Calculate complete downloads
tuples = self.calculate_absolute_all()
# Dump the results into a csv file
if not os.path.exists(os.path.dirname(output_file)):
os.makedirs(os.path.dirname(output_file))
f = open(output_file, 'w')
f.write('filename;number_of_downloads\n')
for t in tuples:
f.write(t[0] + ';' + str(t[1]) + '\n')
f.close()
def calculate_absolute_all(self):
tuples = []
# Get all urls
urls = self.db.store.find(Filesizes.url)
for url in urls:
tuples.append(self.calculate_absolute(url))
return tuples
def calculate_absolute(self, url):
# Get all the traffic for this url
traffic = self.db.store.find(Stats.traffic, Stats.url == url)
# add it up
absolute_download = 0
for dl in traffic:
absolute_download = absolute_download + dl
# Get the filesize
filesize = self.db.store.find(Filesizes.filesize, Filesizes.url == url).one()
# Calculate complete downloads
# I use floor division because I want to truncate after the
# decimal point. In Python 2.x this doesn't make a difference,
# but in Python 3, it does.
complete_downloads = absolute_download//filesize
return [url, complete_downloads]