{# Copyright 2020 Curtin University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Author: Richard Hosking #}
{#
The purpose of this script it to export the book metrics section from the book_product table
Primarily, the goal is to create a flat structure which is suitable for graphing in Kibana
#}

SELECT
    ISBN13 as product_id,
    work_id,
    work_family_id,
    onix.title as title,
    onix.authors,
    CAST(onix.published_year as INT64) as published_year,
    month.month,
    STRUCT(
      (SELECT SUM(value) FROM UNNEST(month.google_analytics.views_total_country)) as page_views,
      (SELECT SUM(value) FROM UNNEST(month.google_analytics.downloads_total_country)) as downloads,
      (SELECT SUM(value) FROM UNNEST(month.google_analytics.downloads_pdf_book_country)) as downloads_pdf_book,
      (SELECT SUM(value) FROM UNNEST(month.google_analytics.downloads_pdf_chapter_country)) as downloads_pdf_chapter,
      (SELECT SUM(value) FROM UNNEST(month.google_analytics.downloads_html_chapter_country)) as downloads_html_chapter,
      (SELECT SUM(value) FROM UNNEST(month.google_analytics.downloads_epub_book_country)) as downloads_epub_book,
      (SELECT SUM(value) FROM UNNEST(month.google_analytics.downloads_epub_chapter_country)) as downloads_epub_chapter,
      (SELECT SUM(value) FROM UNNEST(month.google_analytics.downloads_mobi_book_country)) as downloads_mobi_book
    ) as google_analytics,
    STRUCT((SELECT SUM(count) FROM UNNEST(month.crossref_events)) as count) as crossref_events,
    month.google_books_traffic,
    month.google_books_sales,
    STRUCT((SELECT SUM(Total_Item_Requests) FROM UNNEST(month.jstor_country)) as Total_Item_Requests) as jstor,
    STRUCT(
        month.irus_oapen.version,
        month.irus_oapen.title_requests,
        month.irus_oapen.total_item_investigations,
        month.irus_oapen.total_item_requests,
        month.irus_oapen.unique_item_investigations,
        month.irus_oapen.unique_item_requests
    ) as irus_oapen,
    STRUCT(
        month.irus_fulcrum.total_item_investigations,
        month.irus_fulcrum.total_item_requests,
        month.irus_fulcrum.unique_item_investigations,
        month.irus_fulcrum.unique_item_requests
    ) as irus_fulcrum,
    STRUCT(month.ucl_discovery.total_downloads) as ucl_discovery,
    STRUCT(month.worldreader.total_downloads) as worldreader,
    STRUCT(month.internet_archive.total_downloads) as internet_archive
FROM `{{ book_product_table_id }}`, UNNEST(months) as month
WHERE month.irus_fulcrum IS NOT NULL OR month.irus_oapen IS NOT NULL OR month.google_books_traffic IS NOT NULL OR month.google_books_sales IS NOT NULL OR month.google_analytics IS NOT NULL OR month.ucl_discovery IS NOT NULL OR month.worldreader IS NOT NULL OR month.internet_archive IS NOT NULL OR ARRAY_LENGTH(month.jstor_country) > 0 OR ARRAY_LENGTH(month.crossref_events) > 0