Skip to content

Commit

Permalink
add doc_sphinx workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-jun-xiang committed Sep 20, 2023
1 parent 1de0cdf commit dc08b9d
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 18 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/sphinx.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Docs
on:
push:
branches: ['main']
permissions:
contents: write
jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- name: Install dependencies
run: |
pip install -e .
pip install sphinx sphinx_rtd_theme
- name: Sphinx build
run: |
cd docs
make html
- name: Deploy to Github Pages
uses: peaceiris/actions-gh-pages@v3
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
with:
publish_branch: gh-pages
github_token: ${{ secrets.GITHUB_TOKEN }} # the default token which no need create
publish_dir: docs/build/html # the path of doc
force_orphan: true
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = source
BUILDDIR = build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
46 changes: 46 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
import os
import sys
sys.path.insert(0, os.path.abspath('../..'))

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'pyga4'
copyright = '2023, Lin-jun-xiang'
author = 'Lin-jun-xiang'
release = '0.1.0'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
'sphinx.ext.intersphinx',
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx.ext.mathjax',
'sphinx.ext.viewcode',
'sphinx_rtd_theme',
'sphinx.ext.githubpages',
]

templates_path = ['_templates']
exclude_patterns = []

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "sphinx_rtd_theme"
html_static_path = ['_static']

# Replace 'view source code' with 'Edit on Github'
# https://stackoverflow.com/questions/62904172/how-do-i-replace-view-page-source-with-edit-on-github-links-in-sphinx-rtd-th
html_context = {
'display_github': True,
'github_user': 'Lin-jun-xiang',
'github_repo': 'pyga4',
'github_version': 'main/docs/source/',
}
22 changes: 22 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.. pyga4 documentation master file, created by
sphinx-quickstart on Mon Sep 18 17:02:12 2023.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
pyga4's documentation!
======================

.. automodule:: pya4.model.bigquery
:members:
:undoc-members:
:show-inheritance:

.. automodule:: pyga4.analytic.analytic
:members:
:undoc-members:
:show-inheritance:

.. automodule:: pyga4.analytic.data_transform
:members:
:undoc-members:
:show-inheritance:
2 changes: 0 additions & 2 deletions ga4/analytic/__init__.py

This file was deleted.

1 change: 0 additions & 1 deletion ga4/model/__init__.py

This file was deleted.

2 changes: 2 additions & 0 deletions pyga4/analytic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from pyga4.analytic.analytic import DeviceAnalytic, EventAnalytic, UserAnalytic
from pyga4.analytic.data_transform import Transformer
17 changes: 13 additions & 4 deletions ga4/analytic/analytic.py → pyga4/analytic/analytic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from collections import Counter
from typing import Any

from ga4.model.bigquery import Ga4Table
from pyga4.model.bigquery import Ga4Table


class BaseAnalytic:
def __init__(self, table: Ga4Table) -> None:
self.table = table

@staticmethod
def attribute_distribution(table: Ga4Table, attribute_name: str) -> Counter:
attribute_list = getattr(table, attribute_name)
Expand All @@ -17,7 +20,13 @@ class UserAnalytic(BaseAnalytic):
"""The features of user"""

def __init__(self, table: Ga4Table) -> None:
self.table = table
super().__init__(table)

@property
def user_id_distribution(self) -> Counter:
return self.attribute_distribution(
self.table, 'user_id_list'
)

@property
def countries_distribution(self) -> Counter:
Expand All @@ -30,7 +39,7 @@ class DeviceAnalytic(BaseAnalytic):
"""The features of technology"""

def __init__(self, table: Ga4Table) -> None:
self.table = table
super().__init__(table)

@property
def os_distribution(self) -> Counter:
Expand Down Expand Up @@ -67,7 +76,7 @@ class EventAnalytic(BaseAnalytic):
"""The features of event"""

def __init__(self, table: Ga4Table) -> None:
self.table = table
super().__init__(table)

@property
def pages_distribution(self) -> Counter:
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions pyga4/model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pyga4.model.bigquery import BaseTable, Ga4Table
21 changes: 10 additions & 11 deletions ga4/model/bigquery.py → pyga4/model/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ class Client:
Examples
--------
```python
# Setup dry_run = True
Client().query_config.dry_run = True
```
"""
query_config = bigquery.QueryJobConfig(
dry_run = False,
use_query_cache = False
)
def __init__(self, client) -> None:
def __init__(self, client, project_id: str) -> None:
self.client = client
self.project_id = project_id


class BaseTable(Client):
"""Connect to the data set from bigquery"""
def __init__(self, client, dataset_name: str) -> None:
super().__init__(client)
def __init__(self, client, project_id: str, dataset_name: str) -> None:
super().__init__(client, project_id)
self.dataset_name = dataset_name
self._table_id = None

Expand Down Expand Up @@ -78,8 +78,7 @@ def to_dataframe(self):

class Ga4Table(BaseTable):
def __init__(self, client, project_id: str, dataset_name: str) -> None:
super().__init__(client, dataset_name)
self.project_id = project_id
super().__init__(client, project_id, dataset_name)
self._query_job = None

def _query_template(self, query_target: str) -> list:
Expand Down Expand Up @@ -204,17 +203,17 @@ def page_location_list(self) -> list:
"""Return all event of page location from data table"""
query = f"""
SELECT
event_params.value.string_value AS param_string_value
param.value.string_value AS param_string_value
FROM
`{self.project_id}.{self.dataset_name}.{self.table_id}`
UNNEST(event_params) AS event_params
`{self.project_id}.{self.dataset_name}.{self.table_id}`,
UNNEST(event_params) AS param
WHERE
event_params.key = "page_location"
param.key = "page_location" OR param.key IS NULL
"""
self._query_job = self.client.query(query, job_config=self.query_config)
results = self._query_job.result()

return [row.page_location for row in results]
return [row.param_string_value for row in results]

@calculate_bytes_processed
def query(self, query: str) -> list:
Expand Down

0 comments on commit dc08b9d

Please sign in to comment.