1
0
mirror of https://github.com/Tygs/0bin.git synced 2023-08-10 21:13:00 +03:00

Merging counter

This commit is contained in:
max 2012-05-22 18:44:51 +07:00
commit 02a5aa6cf6
11 changed files with 143 additions and 13 deletions

37
stats.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4
"""
Exctract usefull infos from web server logs
"""
import re
# define your web server logs path
LOGS_PATH = "/var/log/nginx/access_0bin.log"
rexp = re.compile('(\d+\.\d+\.\d+\.\d+) - - \[([^\[\]:]+):(\d+:\d+:\d+) -(\d\d\d\d\)] ("[^"]*")(\d+) (-|\d+) ("[^"]*") (".*")\s*\Z')
f = open(LOGS_PATH, 'r')
for line in f:
a = rexp.match(line)
if not a is None:
# a.group(1) #IP address
# a.group(2) #day/month/year
# a.group(3) #time of day
# a.group(4) #timezone
# a.group(5) #request
# a.group(6) #code 200 for success, 404 for not found, etc.
# a.group(7) #bytes transferred
# a.group(8) #referrer
# a.group(9) #browser
print a.group(8) #referrer
f.close()

View File

@ -53,6 +53,12 @@ PORT = "8000"
USER = None
GROUP = None
# Display a tiny counter for pastes created.
# Be carreful if your site have to many pastes this can hurt your hard drive performances.
# Refresh counter interval. Default to every minute after a paste.
DISPLAY_COUNTER = True
REFRESH_COUNTER = 60 * 1
# Names/links to insert in the menu bar.
# Any link with "mailto:" will be escaped to prevent spam
MENU = (

View File

@ -112,6 +112,58 @@ class Paste(object):
return cls.load_from_file(cls.get_path(uuid))
def increment_counter(self):
"""
Increment pastes counter
"""
# simple counter incrementation
# using lock file to prevent multi access to the file
# could be improved.
path = settings.PASTE_FILES_ROOT
counter_file = os.path.join(path, 'counter')
lock_file = os.path.join(path, 'counter.lock')
if not os.path.isfile(lock_file):
try:
#make lock file
flock = open(lock_file, "w")
flock.write('lock')
flock.close()
# init counter (first time)
if not os.path.isfile(counter_file):
fcounter = open(counter_file, "w")
fcounter.write('1')
fcounter.close()
# get counter value
fcounter = open(counter_file, "r")
counter_value = fcounter.read(50)
fcounter.close()
try:
counter_value = long(counter_value) + 1
except ValueError:
counter_value = 1
# write new value to counter
fcounter = open(counter_file, "w")
fcounter.write(str(counter_value))
fcounter.close()
#remove lock file
os.remove(lock_file)
except (IOError, OSError):
if os.path.isfile(lock_file):
#remove lock file
os.remove(lock_file)
def save(self):
"""
Save the content of this paste to a file.

View File

@ -23,14 +23,16 @@ from bottle import (Bottle, run, static_file, view, request)
import clize
from paste import Paste
from utils import drop_privileges, dmerge
from utils import drop_privileges, dmerge, get_pastes_count
app = Bottle()
GLOBAL_CONTEXT = {
'settings': settings
'settings': settings,
'pastes_count': get_pastes_count(),
'refresh_counter': datetime.now()
}
@app.route('/')
@view('home')
@ -64,6 +66,19 @@ def create_paste():
expiration = request.forms.get('expiration', u'burn_after_reading')
paste = Paste(expiration=expiration, content=content)
paste.save()
# display counter
if settings.DISPLAY_COUNTER:
#increment paste counter
paste.increment_counter()
# if refresh time elapsed pick up new counter value
if GLOBAL_CONTEXT['refresh_counter'] + timedelta(seconds=settings.REFRESH_COUNTER) < datetime.now():
GLOBAL_CONTEXT['pastes_count'] = get_pastes_count()
GLOBAL_CONTEXT['refresh_counter'] = datetime.now()
return {'status': 'ok',
'paste': paste.uuid}

View File

@ -31,6 +31,10 @@
margin-bottom: 0px !important;
}
.center {
text-align: center;
}
.about span{
font-size: 10px;
}

View File

@ -5,6 +5,7 @@ import os
import glob
import tempfile
import sys
import locale
import default_settings
sys.path.append(default_settings.LIBS_DIR)
@ -53,6 +54,21 @@ def dmerge(*args):
return dictionary
def get_pastes_count():
"""
Return the number of pastes created (must have option DISPLAY_COUNTER enabled)
"""
locale.setlocale(locale.LC_ALL, 'en_US')
counter_path = settings.PASTE_FILES_ROOT
counter_file = os.path.join(counter_path, 'counter')
try:
with open(counter_file, "r") as f:
count = f.read(50)
f.close
except IOError:
count = 0
return locale.format("%d", long(float(count)), grouping=True)
class SettingsContainer(object):

View File

@ -33,4 +33,4 @@
</form>
%rebase base settings=settings
%rebase base settings=settings, pastes_count=pastes_count

View File

@ -125,13 +125,13 @@
<p>“Few persons can be made to believe that it is not quite an easy thing to invent a method of secret writing which shall baffle investigation. Yet it may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve...”</p>
<small>Edgar Allan Poe</small>
</blockquote>
<!-- <h4 id="pixels-total" >
<p>ø</p>
<strong>41,017,923,819</strong> pastes øbinned
</h4> -->
%if settings.DISPLAY_COUNTER:
<h4 id="pixels-total" >
<p>ø</p>
<strong>{{ pastes_count }}</strong> </br>pastes øbinned
</h4>
%end
</br>
<p class="greetings span12">

View File

@ -32,4 +32,4 @@
%rebase base settings=settings
%rebase base settings=settings, pastes_count=pastes_count

View File

@ -25,4 +25,4 @@
</form>
%rebase base settings=settings
%rebase base settings=settings, pastes_count=pastes_count

View File

@ -78,4 +78,4 @@
</div>
%rebase base settings=settings
%rebase base settings=settings, pastes_count=pastes_count