--IRUS Stats

CREATE TABLE IF NOT EXISTS ${usageStatsDB}.irus_downloads_stats_tmp
(`source` string,
`repository_id` string,
`result_id` string,
`date`	string,
`count` bigint,
`openaire`	bigint);

INSERT INTO ${usageStatsDB}.irus_downloads_stats_tmp
SELECT s.source, d.id AS repository_id,
ro.id as result_id, CONCAT(YEAR(date), '/', LPAD(MONTH(date), 2, '0')) as date, s.count, '0'
FROM ${usageRawDataDB}.sushilog s, ${statsDB}.datasource_oids d, ${statsDB}.result_oids ro
WHERE s.repository=d.oid AND s.rid=ro.oid AND metric_type='ft_total' AND s.source='IRUS-UK';

CREATE TABLE IF NOT EXISTS ${usageStatsDB}.irus_R5_stats_tmp
(`source` string,
`repository_id` string,
`result_id` string,
`date`	string,
`views` bigint,
`downloads`	bigint,
`openaire`	bigint);

INSERT INTO ${usageStatsDB}.irus_R5_stats_tmp
SELECT s.source, d.id AS repository_id,
ro.id as result_id, CONCAT(YEAR(date), '/', LPAD(MONTH(date), 2, '0')) as date,
(s.total_item_investigations-s.total_item_requests) as views, s.total_item_requests as downloads, '0'
FROM ${usageRawDataDB}.sushilog_cop_r5 s, ${statsDB}.datasource_oids d, ${statsDB}.result_oids ro
WHERE s.repository=d.oid AND s.rid=ro.oid AND s.source='IRUS-UK';
