--OpenAIRE Views

DROP VIEW IF EXISTS ${usageStatsDB}.openaire_piwikresult_views_monthly_tmp;

CREATE OR REPLACE VIEW ${usageStatsDB}.openaire_result_views_monthly_tmp
AS SELECT entity_id, reflect('java.net.URLDecoder', 'decode', entity_id) AS id,
COUNT(entity_id) as views, SUM(CASE WHEN referrer_name LIKE '%openaire%' THEN 1 ELSE 0 END)
AS openaire_referrer, CONCAT(YEAR(timestamp), '/', LPAD(MONTH(timestamp), 2, '0')) AS month, source
FROM ${usageStatsDB}.piwiklogdistinct where action='action'
and (source_item_type='oaItem' or source_item_type='repItem')
GROUP BY entity_id, CONCAT(YEAR(timestamp), '/', LPAD(MONTH(timestamp), 2, '0')), source;

DROP TABLE IF EXISTS ${usageStatsDB}.openaire_views_stats_tmp;

CREATE TABLE IF NOT EXISTS ${usageStatsDB}.openaire_views_stats_tmp
AS SELECT 'OpenAIRE' as source, d.id as repository_id, ro.id as result_id, month as date,
max(views) AS count, max(openaire_referrer) AS openaire
FROM ${usageStatsDB}.openaire_result_views_monthly_tmp p, ${statsDB}.datasource d,
${statsDB}.result_oids ro WHERE p.source=d.piwik_id AND p.id=ro.oid AND ro.oid!='200'
AND ro.oid!='204' AND ro.oid!='404' AND ro.oid!='400' AND ro.oid!='503'
AND d.id!='re3data_____::7b0ad08687b2c960d5aeef06f811d5e6' GROUP BY d.id, ro.id, month;

CREATE TABLE IF NOT EXISTS ${usageStatsDB}.openaire_pageviews_stats_tmp AS SELECT
'OpenAIRE' as source, d.id as repository_id, ro.id as result_id, month as date, max(views) AS count
FROM ${usageStatsDB}.openaire_result_views_monthly_tmp p, ${statsDB}.datasource d, ${statsDB}.result_oids ro
WHERE p.source=${portalMatomoID} AND p.source=d.piwik_id and p.id=ro.id AND ro.oid!='200'
AND ro.oid!='204' AND ro.oid!='404' AND ro.oid!='400' AND ro.oid!='503'
AND d.id!='re3data_____::7b0ad08687b2c960d5aeef06f811d5e6' GROUP BY d.id, ro.id, month;
