# Language: Lowdefy YAML, Project: DatAasee, License: MIT, Author: Christian Himpe

# Dashboard
id: "stats"
type: "PageSiderMenu"
properties:
  title:
    _string.concat:
      - _global: "title"
      - " | Statistics"
  header:
    color:
      _global: "color"
  logo:
    _global: "logo"

requests:
  - _ref: "templates/ready.yaml"

  - id: "stats_request"
    type: "AxiosHttp"
    connectionId: "datalake_api"
    properties:
      url: "/database"
      method: "get"
      timeout: 400000
      params:
        language: "sql"
        query: |
          SELECT $total[0].count AS total,
                 $resourceTypes:{ifnull(resourceType.name,'Unknown') AS name, data} AS resourceTypes,
                 $licenses:{ifnull(license.name,'Unknown') AS name, data} AS licenses,
                 $languages:{ifnull(language.name,'Unknown') AS name, data} AS languages,
                 $categories:{ifnull(categories.asRID().name,'Unknown') AS name, data} AS categories,
                 $subjects:{ifnull(subjects,'Unknown') AS name, data} AS subjects,
                 $schemas:{ifnull(rawFormat.name,'Unknown') AS name, data} AS schemas,
                 $sources:{ifnull(source.data,'Unknown') AS name, data} AS sources,
                 $keywords:{ifnull(keywords,'Unknown') AS name, data} AS keywords,
                 $years:{ifnull(publicationYear.asString(),'Unknown') AS name, data} AS years
          LET $total = (SELECT count(*) AS count FROM Metadata),
              $resourceTypes = (SELECT resourceType, count(*) AS data FROM Metadata GROUP BY resourceType ORDER BY data DESC LIMIT 100),
              $licenses = (SELECT license, count(*) AS data FROM Metadata GROUP BY license ORDER BY data DESC LIMIT 100),
              $languages = (SELECT language, count(*) AS data FROM Metadata GROUP BY language ORDER BY data DESC LIMIT 100),
              $categories = (SELECT categories, count(*) AS data FROM (SELECT categories FROM Metadata WHERE categories <> [] UNWIND categories) GROUP BY categories ORDER BY data DESC LIMIT 100),
              $subjects = (SELECT subjects, count(*) AS data FROM (SELECT subjects.name AS subjects FROM Metadata UNWIND subjects) GROUP BY subjects ORDER BY data DESC LIMIT 100),
              $schemas = (SELECT rawFormat, count(*) AS data FROM Metadata GROUP BY rawFormat ORDER BY data DESC LIMIT 100),
              $sources = (SELECT source, count(*) AS data FROM Metadata GROUP BY source ORDER BY data DESC LIMIT 10),
              $years = (SELECT publicationYear, count(*) AS data FROM Metadata GROUP BY publicationYear ORDER BY data DESC LIMIT 10),
              $keywords = (SELECT keywords, count(*) AS data FROM (SELECT keywords FROM Metadata WHERE keywords IS NOT NULL UNWIND keywords) GROUP BY keywords ORDER BY data DESC LIMIT 10);

events:
  onMountAsync:
    - id: "ready_enter"
      type: "Request"
      params: "ready_request"

    - id: "stats_enter"
      type: "Request"
      params: "stats_request"

    - id: "stats_throw"
      type: "Throw"
      params:
        throw:
          _eq:
            - _request: "stats_request.status"
            - 404
        message: "Statistics not available"

areas:
  content:
    gutter: 16
    blocks:
## Page Title
      - id: "heading"
        type: "Title"
        properties:
          content: "Statistics Overview"
          level: 1
## Content Card
      - id: "content"
        type: "Card"
        areas:
          content:
            gutter: 16
            blocks:
## Totals Card
              - id: "shares_heading"
                type: "Title"
                properties:
                  content:
                    _string.concat:
                      - "Total Metadata Records: "
                      - _request: "stats_request.data.data.attributes.result.0.total"
                  level: 4
                visible:
                  _ne:
                    - _request: "stats_request.data.data.attributes.result.0.total"
                    - null
              - id: "shares_loading"
                type: "Spinner"
                properties:
                  size: "large"
                visible:
                  _eq:
                    - _request: "stats_request.data.data.attributes.result.0.total"
                    - null
## Top-10 Title
              - id: "top10_card"
                type: "Card"
                properties:
                  bordered: false
                  size: "small"
                  title: "Top 10"
                  headerStyle:
                    font-size: "large"
                areas:
                  content:
                    gutter: 16
                    blocks:
## Top Sources (Bar)
                      - id: "bar_sources"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Source Size"
                              left: "center"
                            xAxis:
                              type: "category"
                              axisLabel:
                                rotate: 60
                              data:
                                _array.map:
                                  on:
                                    _request: "stats_request.data.data.attributes.result.0.sources"
                                  callback:
                                    _function:
                                      __args: "0.name"
                            yAxis:
                              type: "value"
                            series:
                              - type: "bar"
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.sources"
                                    callback:
                                      _function:
                                        __args: "0.data"
## Top Year (Bar)
                      - id: "bar_years"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Publication Years"
                              left: "center"
                            xAxis:
                              type: "category"
                              axisLabel:
                                rotate: 60
                              data:
                                _array.map:
                                  on:
                                    _request: "stats_request.data.data.attributes.result.0.years"
                                  callback:
                                    _function:
                                      __args: "0.name"
                            yAxis:
                              type: "value"
                            series:
                              - type: "bar"
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.years"
                                    callback:
                                      _function:
                                        __args: "0.data"
## Top Keywords (Bar)
                      - id: "bar_keywords"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Keywords"
                              left: "center"
                            xAxis:
                              type: "category"
                              axisLabel:
                                rotate: 60
                              data:
                                _array.map:
                                  on:
                                    _request: "stats_request.data.data.attributes.result.0.keywords"
                                  callback:
                                    _function:
                                      __args: "0.name"
                            yAxis:
                              type: "value"
                            series:
                              - type: "bar"
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.keywords"
                                    callback:
                                      _function:
                                        __args: "0.data"
## Shares Title
              - id: "shares_card"
                type: "Card"
                properties:
                  bordered: false
                  size: "small"
                  title: "Shares By"
                  headerStyle:
                    font-size: "large"
                areas:
                  content:
                    gutter: 16
                    blocks:
## By Resource Type (Pie)
                      - id: "pie_types"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Resource Types"
                              left: "center"
                            series:
                              - type: "pie"
                                minShowLabelAngle: 1
                                animation: false
                                emphasis:
                                  scale: false
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.resourceTypes"
                                    callback:
                                      _function:
                                        name:
                                          __args: "0.name"
                                        value:
                                          __args: "0.data"
## By Categories (Pie)
                      - id: "pie_categories"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Categories"
                              left: "center"
                            series:
                              - type: "pie"
                                minShowLabelAngle: 1
                                animation: false
                                emphasis:
                                  scale: false
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.categories"
                                    callback:
                                      _function:
                                        name:
                                          __args: "0.name"
                                        value:
                                          __args: "0.data"
## By License (Pie)
                      - id: "pie_licenses"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Licenses"
                              left: "center"
                            series:
                              - type: "pie"
                                minShowLabelAngle: 1
                                animation: false
                                emphasis:
                                  scale: false
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.licenses"
                                    callback:
                                      _function:
                                        name:
                                          __args: "0.name"
                                        value:
                                          __args: "0.data"
## By Subject (Pie)
                      - id: "pie_subjects"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Subjects"
                              left: "center"
                            series:
                              - type: "pie"
                                minShowLabelAngle: 1
                                animation: false
                                emphasis:
                                  scale: false
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.subjects"
                                    callback:
                                      _function:
                                        name:
                                          __args: "0.name"
                                        value:
                                          __args: "0.data"
## By Language (Pie)
                      - id: "pie_languages"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Languages"
                              left: "center"
                            series:
                              - type: "pie"
                                minShowLabelAngle: 1
                                animation: false
                                emphasis:
                                  scale: false
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.languages"
                                    callback:
                                      _function:
                                        name:
                                          __args: "0.name"
                                        value:
                                          __args: "0.data"
## By Schemas (Pie)
                      - id: "pie_schemas"
                        type: "EChart"
                        layout:
                          span: 8
                        properties:
                          init:
                            renderer: "svg"
                          option:
                            title:
                              text: "Schemas"
                              left: "center"
                            series:
                              - type: "pie"
                                minShowLabelAngle: 1
                                animation: false
                                emphasis:
                                  scale: false
                                data:
                                  _array.map:
                                    on:
                                      _request: "stats_request.data.data.attributes.result.0.schemas"
                                    callback:
                                      _function:
                                        name:
                                          __args: "0.name"
                                        value:
                                          __args: "0.data"

  sider:
    _ref: "templates/sider.yaml"

  footer:
    _ref: "templates/footer.yaml"
