{# 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 subject (bic) year metrics section from the book_product table
Primarily, the goal is to create a flat structure which is suitable for graphing in Kibana
#}

SELECT
    month,
    count(ISBN13) as unique_books,
    STRUCT(
      SUM(google_analytics.page_views) as page_views,
      SUM(google_analytics.downloads) as downloads,
      SUM(google_analytics.downloads_pdf_book) as downloads_pdf_book,
      SUM(google_analytics.downloads_pdf_chapter) as downloads_pdf_chapter,
      SUM(google_analytics.downloads_html_chapter) as downloads_html_chapter,
      SUM(google_analytics.downloads_epub_book) as downloads_epub_book,
      SUM(google_analytics.downloads_epub_chapter) as downloads_epub_chapter,
      SUM(google_analytics.downloads_mobi_book) as downloads_mobi_book
    ) as google_analytics,
    STRUCT(SUM(crossref_events.count) as count) as crossref_events,
    STRUCT(
        SUM(google_books_traffic.Book_Visits_BV_) as Book_Visits_BV_,
        SUM(google_books_traffic.BV_with_Pages_Viewed) as BV_with_Pages_Viewed,
        SUM(google_books_traffic.Non_Unique_Buy_Clicks) as Non_Unique_Buy_Clicks,
        SUM(google_books_traffic.BV_with_Buy_Clicks) as BV_with_Buy_Clicks,
        SUM(google_books_traffic.Buy_Link_CTR) as Buy_Link_CTR,
        SUM(google_books_traffic.Pages_Viewed) as Pages_Viewed
    ) as google_books_traffic,
    STRUCT(SUM(google_books_sales.qty) as qty) as google_books_sales,
    STRUCT(SUM(jstor.Total_Item_Requests) as Total_Item_Requests) as jstor,
    STRUCT(
        SUM(irus_oapen.title_requests) as title_requests,
        SUM(irus_oapen.total_item_investigations) as total_item_investigations,
        SUM(irus_oapen.total_item_requests) as total_item_requests,
        SUM(irus_oapen.unique_item_investigations) as unique_item_investigations,
        SUM(irus_oapen.unique_item_requests) as unique_item_requests
    ) as irus_oapen,
    STRUCT(
        SUM(irus_fulcrum.total_item_investigations) as total_item_investigations,
        SUM(irus_fulcrum.total_item_requests) as total_item_requests,
        SUM(irus_fulcrum.unique_item_investigations) as unique_item_investigations,
        SUM(irus_fulcrum.unique_item_requests) as unique_item_requests
    ) as irus_fulcrum,
FROM (
    SELECT
        ISBN13,
        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,
    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 ARRAY_LENGTH(month.crossref_events) > 0 OR ARRAY_LENGTH(month.jstor_country) > 0)
GROUP BY month
ORDER BY month DESC