Skip to content

Commit

Permalink
Merge pull request #88 from pitthsls/beda42-master
Browse files Browse the repository at this point in the history
Beda42 master
  • Loading branch information
Wooble authored Aug 19, 2019
2 parents 2a6dbdf + 930b4d3 commit c357fcd
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pycounter/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@
u"Access denied category",
u"Reporting Period Total",
),
"PR1": (
u"Platform",
u"Publisher",
u"User Activity",
u"Reporting Period Total",
),
# FIXME: this is outputting counter 5 reports in 4 format for... reasons.
"TR_J1": (
u"Journal",
Expand Down
54 changes: 51 additions & 3 deletions pycounter/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,45 @@ def as_generic(self):
return data_line


class CounterPlatform(CounterEresource):
"""a COUNTER platform report line."""

def __init__(
self,
period=None,
metric=None,
month_data=None,
platform="",
publisher="",
):
super(CounterPlatform, self).__init__(
period=period,
metric=metric,
month_data=month_data,
title='', # no title for platform report
platform=platform,
publisher=publisher
)
self.isbn = None

def as_generic(self):
"""Return data for this line as list of COUNTER report cells."""
self._fill_months()

data_line = [self.platform, self.publisher, self.metric]
total_usage = 0
month_data = []

for data in self:
total_usage += data[2]
month_data.append(six.text_type(data[2]))

data_line.append(six.text_type(total_usage))
data_line.extend(month_data)

return data_line


def parse(filename, filetype=None, encoding="utf-8", fallback_encoding="latin-1"):
"""Parse a COUNTER file, first attempting to determine type.
Expand Down Expand Up @@ -719,10 +758,12 @@ def parse_generic(report_reader):
end_date = last_day(convert_date_column(header[last_col - 1]))
report.period = (start_date, end_date)

if report.report_type != "DB1" and report.report_version != 5:
if report.report_type not in ("DB1", "PR1") and report.report_version != 5:
# these reports do not have line with totals
six.next(report_reader)

if report.report_type == "DB2":
# this report has two lines of totals
six.next(report_reader)

for line in report_reader:
Expand Down Expand Up @@ -784,7 +825,8 @@ def _parse_line(line, report, last_col):
}
month_data = []
curr_month = report.period[0]
for data in line[5:]:
months_start_idx = 5 if report.report_type != 'PR1' else 4
for data in line[months_start_idx:]:
month_data.append((curr_month, format_stat(data)))
curr_month = next_month(curr_month)
if report.report_type.startswith("JR") or report.report_type == "TR_J1":
Expand All @@ -811,6 +853,10 @@ def _parse_line(line, report, last_col):
)
elif report.report_type.startswith("DB"):
return CounterDatabase(metric=line[3], month_data=month_data, **common_args)
elif report.report_type == "PR1":
# there is no title in the PR1 report
return CounterPlatform(metric=line[2], month_data=month_data, platform=line[0],
publisher=line[1], period=report.period)
raise PycounterException("Should be unreachable") # pragma: no cover


Expand All @@ -829,7 +875,7 @@ def _get_type_and_version(specifier):
report_version = int(rt_match.group(3))
else:
raise UnknownReportTypeError("No match in line: %s" % specifier)
if not any(report_type.startswith(x) for x in ("JR", "BR", "DB")):
if not any(report_type.startswith(x) for x in ("JR", "BR", "DB", "PR1")):
raise UnknownReportTypeError(report_type)

return report_type, report_version
Expand All @@ -851,6 +897,8 @@ def _year_from_header(header, report):
first_date_col = 8
elif report.report_type in ("DB1", "DB2") and report.report_version == 4:
first_date_col = 5
elif report.report_type == "PR1" and report.report_version == 4:
first_date_col = 4
year = int(header[first_date_col].split("-")[1])
if year < 100:
year += 2000
Expand Down
16 changes: 14 additions & 2 deletions pycounter/sushi.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ def raw_to_full(raw_report):
pdf_usage += int(inst.Count)
elif inst.MetricType == "ft_html":
html_usage += int(inst.Count)
elif report.report_type.startswith("DB"):
elif report.report_type.startswith("DB") or \
report.report_type == "PR1":
metrics_for_db[inst.MetricType].append(
(item_date, int(inst.Count))
)
Expand Down Expand Up @@ -343,5 +344,16 @@ def raw_to_full(raw_report):
month_data=month_data,
)
)

elif report.report_type == "PR1":
for metric_code, month_data in six.iteritems(metrics_for_db):
metric = pycounter.constants.DB_METRIC_MAP[metric_code]
report.pubs.append(
pycounter.report.CounterPlatform(
platform=platform,
publisher=publisher_name,
period=report.period,
metric=metric,
month_data=month_data,
)
)
return report
12 changes: 12 additions & 0 deletions pycounter/test/data/PR1.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Platform Report 1 (R4) Total Searches, Result Clicks, and Record Views by Month and Platform
A fake institution used for testing
00000001
Period covered by Report:
2018-01-01 to 2018-12-31
Date run:
2019-08-01
Platform Publisher User Activity Reporting Period Total Jan-2018 Feb-2018 Mar-2018 Apr-2018 May-2018 Jun-2018 Jul-2018 Aug-2018 Sep-2018 Oct-2018 Nov-2018 Dec-2018
search.alexanderstreet.com Alexander Street Press Regular Searches 299 91 41 13 21 44 8 0 0 36 36 7 2
search.alexanderstreet.com Alexander Street Press Searches-federated and automated 0 0 0 0 0 0 0 0 0 0 0 0 0
search.alexanderstreet.com Alexander Street Press Result Clicks 87 19 18 1 8 13 5 0 0 13 6 2 2
search.alexanderstreet.com Alexander Street Press Record Views 100 22 19 1 9 19 5 0 0 15 6 2 2
2 changes: 1 addition & 1 deletion pycounter/test/test_bad_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@pytest.mark.parametrize(
"report_type",
[u"Bogus Report 7 (R4)", u"Platform Report 1 (R4)"], # unsupported but valid
[u"Bogus Report 7 (R4)"],
)
def test_report_type(report_type):
"""Report type doesn't exist."""
Expand Down
33 changes: 33 additions & 0 deletions pycounter/test/test_pr1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Test COUNTER DB1 database report"""

from __future__ import absolute_import

import os
import unittest

import pycounter.report


class ParseCounter4Example(unittest.TestCase):
"""Tests for parsing C4 PR1"""

def setUp(self):
self.report = pycounter.report.parse(
os.path.join(os.path.dirname(__file__), "data/PR1.tsv")
)

def test_reportname(self):
self.assertEqual(self.report.report_type, u"PR1")

def test_stats(self):
publication = self.report.pubs[0]
self.assertEqual([x[2] for x in publication],
[91, 41, 13, 21, 44, 8, 0, 0, 36, 36, 7, 2])

def test_row_metric(self):
# test metric of the first row
jan_data = next(iter(self.report.pubs[0]))
self.assertEqual(jan_data[1], "Regular Searches")
# test metric of the second row
jan_data = next(iter(self.report.pubs[1]))
self.assertEqual(jan_data[1], "Searches-federated and automated")
3 changes: 3 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ ignored-argument-names=_.*|^ignored_|^unused_|.*_unused
defining-attr-methods=__init__,
__new__,
setUp

[DESIGN]
max-locals=16

0 comments on commit c357fcd

Please sign in to comment.