#!C:\Program Files\Python27\python.exe
# -*- coding: UTF-8 -*-
import os, sys, cgi, ssl
from datetime import date
# set path to lib and import
sys.path.insert(0, os.path.abspath("../rw_common"))
from lib import getRecordSet, ssi_to_tempfile, fmt
from templating import PageMacro
def sub_header(count, no_right_border=False):
return (''.join([('
Dlds | ' if month_dld else '') +
('Recs | ' if month_rec else '') for i in range(1, count)]) +
('Dlds | ' if sum_dld else '') +
('Recs | ' % ('' if no_right_border else border_right) if sum_rec else '')
)
def month_header(count, year, no_right_border=False):
months = '' if view == 3 else ''.join(['%02i | '
% (2 if view==2 else 1, 'align="middle"' if view==2 else '', i) for i in range(1, count)])
return (months +
'∑%i | '
% (2 if view in (2, 3) else 1, 'align="middle"' if view in (2, 3) else '', '' if no_right_border else border_right, year)
)
def year_header(count, year):
return '%s | ' % (2 if view==3 else 13 if view in (0, 1) else 26, '' if view==3 else year)
# Get parameter
# 1, 2, 3 - show months; downloads only/records only/both
# 4 - Annual overview
try:
view = int(cgi.FieldStorage().getvalue('view'))
except:
view = 0
month_dld = view in (0, 2)
month_rec = view in (1, 2)
sum_dld = view in (0, 2, 3)
sum_rec = view in (1, 2, 3)
border_right = 'border-right: thin solid' if view == 3 else ''
# load html template
tmpl = PageMacro('Content', PageMacro.DELMODE)
ssl._create_default_https_context = ssl._create_unverified_context
temp = ssi_to_tempfile("https://www.biocase.org/whats_biocase/_gbif_downloads.shtml")
tmpl.load('Content', temp)
# Fill in table headings
year, month = date.today().year, date.today().month - 1
if month == 0:
year -= 1
month = 12
if view == 3:
cols = 2 + 2 + 2
elif view == 2:
cols = 26 + 26 + 2 * (month+1)
else:
cols = 13 + 13 + month + 1
tmpl['col_defs'] = 'null, ' * cols
tmpl['scrollx'] = 'false' if view==3 else 'true'
tmpl['cur_view'] = str(view)
tmpl['sub_headers'] = sub_header(13) * 2 + sub_header(month+1, True)
tmpl['month_headers'] = month_header(13, year-2) + month_header(13, year-1) + month_header(month+1, year, True)
tmpl['year_headers'] = year_header(13, year-2) + year_header(13, year-1) + year_header(month+1, year)
# Get statistics from DB and insert into template
rs = getRecordSet("""select
'' || provider.name || '' as provname,
p2.d01, p2.r01, p2.d02, p2.r02, p2.d03, p2.r03, p2.d04, p2.r04, p2.d05, p2.r05, p2.d06, p2.r06, p2.d07, p2.r07, p2.d08, p2.r08, p2.d09, p2.r09, p2.d10, p2.r10, p2.d11, p2.r11, p2.d12, p2.r12, p2.dsum, p2.rsum,
p1.d01, p1.r01, p1.d02, p1.r02, p1.d03, p1.r03, p1.d04, p1.r04, p1.d05, p1.r05, p1.d06, p1.r06, p1.d07, p1.r07, p1.d08, p1.r08, p1.d09, p1.r09, p1.d10, p1.r10, p1.d11, p1.r11, p1.d12, p1.r12, p1.dsum, p1.rsum,
c.d01, c.r01, c.d02, c.r02, c.d03, c.r03, c.d04, c.r04, c.d05, c.r05, c.d06, c.r06, c.d07, c.r07, c.d08, c.r08, c.d09, c.r09, c.d10, c.r10, c.d11, c.r11, c.d12, c.r12, c.dsum, c.rsum
from registry.gbif_downloads as c
full outer join registry.gbif_downloads as p1 on (p1.prov_uuid = c.prov_uuid and p1.year = c.year-1)
full outer join registry.gbif_downloads as p2 on (p2.prov_uuid = c.prov_uuid and p2.year = c.year-2)
left join registry.provider on (c.prov_uuid = provider.uddi_business_key)
where c.year = %i""" % year)
rows = []
for r in rs:
rows += [{'row':
# Provider name
'%s | ' % r[0] +
# Prev 2 year
(''.join([('%s | ' % fmt(r[2*i-1]) if month_dld else '') +
('%s | ' % fmt(r[2*i]) if month_rec else '') for i in range(1, 13)])) +
('%s | ' % fmt(r[25]) if sum_dld else '') +
('%s | ' % (border_right, fmt(r[26])) if sum_rec else '') +
# Prev year
(''.join([('%s | ' % fmt(r[26 + 2*i-1]) if month_dld else '') +
('%s | ' % fmt(r[26 + 2*i]) if month_rec else '') for i in range(1, 13)])) +
('%s | ' % fmt(r[51]) if sum_dld else '') +
('%s | ' % (border_right, fmt(r[52])) if sum_rec else '') +
# Current year
(''.join([('%s | ' % fmt(r[52 + 2*i-1]) if month_dld else '') +
('%s | ' % fmt(r[52 + 2*i]) if month_rec else '') for i in range(1, month+1)])) +
('%s | ' % fmt(r[77]) if sum_dld else '') +
('%s | ' % fmt(r[78]) if sum_rec else '')
}]
tmpl.expand('Content', 'rows', rows)
# print HTML
print "Content-Type: text/html\n"
print tmpl
os.remove(temp)