sqlalchemy-cf-analytics-engine 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. sqlalchemy_cf_analytics_engine-0.0.1/.github/workflows/ci.yml +30 -0
  2. sqlalchemy_cf_analytics_engine-0.0.1/.github/workflows/release.yml +57 -0
  3. sqlalchemy_cf_analytics_engine-0.0.1/.gitignore +13 -0
  4. sqlalchemy_cf_analytics_engine-0.0.1/CONTRIBUTING.md +50 -0
  5. sqlalchemy_cf_analytics_engine-0.0.1/LICENSE +201 -0
  6. sqlalchemy_cf_analytics_engine-0.0.1/PKG-INFO +114 -0
  7. sqlalchemy_cf_analytics_engine-0.0.1/README.md +94 -0
  8. sqlalchemy_cf_analytics_engine-0.0.1/dev/cloudflare/.env.example +2 -0
  9. sqlalchemy_cf_analytics_engine-0.0.1/dev/cloudflare/README.md +111 -0
  10. sqlalchemy_cf_analytics_engine-0.0.1/dev/cloudflare/probe.sh +57 -0
  11. sqlalchemy_cf_analytics_engine-0.0.1/dev/cloudflare/query.sh +23 -0
  12. sqlalchemy_cf_analytics_engine-0.0.1/dev/cloudflare/src/index.js +293 -0
  13. sqlalchemy_cf_analytics_engine-0.0.1/dev/cloudflare/wrangler.jsonc +21 -0
  14. sqlalchemy_cf_analytics_engine-0.0.1/pyproject.toml +47 -0
  15. sqlalchemy_cf_analytics_engine-0.0.1/src/sqlalchemy_cf_analytics_engine/__init__.py +96 -0
  16. sqlalchemy_cf_analytics_engine-0.0.1/src/sqlalchemy_cf_analytics_engine/dbapi.py +293 -0
  17. sqlalchemy_cf_analytics_engine-0.0.1/src/sqlalchemy_cf_analytics_engine/dialect.py +228 -0
  18. sqlalchemy_cf_analytics_engine-0.0.1/src/sqlalchemy_cf_analytics_engine/exceptions.py +43 -0
  19. sqlalchemy_cf_analytics_engine-0.0.1/src/sqlalchemy_cf_analytics_engine/py.typed +0 -0
  20. sqlalchemy_cf_analytics_engine-0.0.1/src/sqlalchemy_cf_analytics_engine/types.py +21 -0
  21. sqlalchemy_cf_analytics_engine-0.0.1/tests/__init__.py +0 -0
  22. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/backtick_identifier.txt +2 -0
  23. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/describe_table.txt +2 -0
  24. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/empty_core_in.txt +2 -0
  25. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/error_bad_sql.txt +2 -0
  26. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/error_transient_403.txt +2 -0
  27. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/error_unknown_column.txt +2 -0
  28. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/error_unknown_table.txt +192 -0
  29. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/error_write.txt +2 -0
  30. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/format_default.txt +31 -0
  31. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/join.txt +2 -0
  32. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/leading_comment.txt +20 -0
  33. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/like_filter.txt +22 -0
  34. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/limit_offset.txt +31 -0
  35. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/now_minus_interval.txt +22 -0
  36. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/order_by_unselected.txt +2 -0
  37. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/quoted_identifier.txt +22 -0
  38. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/quoted_value_backslash.txt +2 -0
  39. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/quoted_value_doubled.txt +2 -0
  40. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/select_all_columns.txt +329 -0
  41. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/select_zero_rows.txt +20 -0
  42. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/show_tables.txt +28 -0
  43. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/subquery.txt +35 -0
  44. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/subquery_alias.txt +2 -0
  45. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/table_alias.txt +2 -0
  46. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/time_grain_day.txt +27 -0
  47. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/time_grain_hour.txt +27 -0
  48. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/to_date_filter.txt +2 -0
  49. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/to_datetime_date_only.txt +2 -0
  50. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/to_datetime_filter.txt +22 -0
  51. sqlalchemy_cf_analytics_engine-0.0.1/tests/fixtures/responses/trailing_semicolon.txt +20 -0
  52. sqlalchemy_cf_analytics_engine-0.0.1/tests/responses.py +11 -0
  53. sqlalchemy_cf_analytics_engine-0.0.1/tests/test_binding.py +159 -0
  54. sqlalchemy_cf_analytics_engine-0.0.1/tests/test_cursor.py +84 -0
  55. sqlalchemy_cf_analytics_engine-0.0.1/tests/test_package.py +50 -0
  56. sqlalchemy_cf_analytics_engine-0.0.1/tests/test_query.py +57 -0
  57. sqlalchemy_cf_analytics_engine-0.0.1/tests/test_reflection.py +119 -0
@@ -0,0 +1,30 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ fail-fast: false
16
+ matrix:
17
+ python-version: ["3.11", "3.12"]
18
+ steps:
19
+ - uses: actions/checkout@v7
20
+ - uses: actions/setup-python@v7
21
+ with:
22
+ python-version: ${{ matrix.python-version }}
23
+ - name: Install
24
+ run: pip install -e ".[dev]"
25
+ - name: Lint
26
+ run: ruff check .
27
+ - name: Check formatting
28
+ run: ruff format --check .
29
+ - name: Test
30
+ run: pytest
@@ -0,0 +1,57 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ tags: ["v*"]
6
+
7
+ permissions:
8
+ contents: read
9
+
10
+ jobs:
11
+ build:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v7
15
+ with:
16
+ fetch-depth: 0
17
+ persist-credentials: false
18
+ - uses: actions/setup-python@v7
19
+ with:
20
+ python-version: "3.12"
21
+ - name: Check the tagged commit is on main
22
+ run: |
23
+ if ! git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then
24
+ echo "Tag $GITHUB_REF_NAME is not on main"
25
+ exit 1
26
+ fi
27
+ - name: Check the tag matches the package version
28
+ run: |
29
+ version=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])")
30
+ if [ "v$version" != "$GITHUB_REF_NAME" ]; then
31
+ echo "Tag $GITHUB_REF_NAME does not match version $version in pyproject.toml"
32
+ exit 1
33
+ fi
34
+ - name: Build
35
+ run: |
36
+ pip install build
37
+ python -m build
38
+ - uses: actions/upload-artifact@v7
39
+ with:
40
+ name: dist
41
+ path: dist/
42
+
43
+ publish:
44
+ needs: build
45
+ runs-on: ubuntu-latest
46
+ environment:
47
+ name: pypi
48
+ url: https://pypi.org/p/sqlalchemy-cf-analytics-engine
49
+ permissions:
50
+ id-token: write
51
+ steps:
52
+ - uses: actions/download-artifact@v8
53
+ with:
54
+ name: dist
55
+ path: dist/
56
+ # v1.14.3; pinned because this job can publish to PyPI
57
+ - uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086
@@ -0,0 +1,13 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ build/
5
+ dist/
6
+ venv/
7
+ .venv/
8
+ .pytest_cache/
9
+ .coverage
10
+ .env
11
+ .wrangler/
12
+ node_modules/
13
+ .ruff_cache/
@@ -0,0 +1,50 @@
1
+ # Contributing
2
+
3
+ ## Setup
4
+
5
+ Use Python 3.11 or newer. From the root of your clone, create a virtual
6
+ environment and install the package with its development dependencies.
7
+
8
+ ```bash
9
+ python3 -m venv .venv
10
+ source .venv/bin/activate
11
+ python -m pip install -e ".[dev]"
12
+ ```
13
+
14
+ ## Before you push
15
+
16
+ Run the same checks as CI. GitHub Actions runs them on Python 3.11 and 3.12
17
+ for pull requests and pushes to `main`.
18
+
19
+ ```bash
20
+ ruff check .
21
+ ruff format --check .
22
+ pytest
23
+ ```
24
+
25
+ Use `ruff check --fix .` to apply available lint fixes and `ruff format .`
26
+ to format the code. Review the diff before committing.
27
+
28
+ ## Tests
29
+
30
+ Keep the test suite runnable without Cloudflare credentials. When testing how
31
+ the driver handles a SQL API response, look for a matching sample in
32
+ [tests/fixtures/responses](tests/fixtures/responses). To query a live dataset
33
+ or record a new sample, follow the [Cloudflare test setup](dev/cloudflare/README.md).
34
+
35
+ ## Branches and pull requests
36
+
37
+ Start each change on a branch based on the latest `main`.
38
+
39
+ ```bash
40
+ git fetch origin
41
+ git switch --no-track -c my-change origin/main
42
+ ```
43
+
44
+ Keep the pull request focused on one change and include tests for changes in
45
+ behavior. Push your branch with `git push -u origin my-change`, open a pull
46
+ request against `main`, and describe what changed and how you tested it.
47
+
48
+ If your branch needs an update from `main`, run `git fetch origin` and
49
+ `git rebase origin/main`. After rebasing a branch you already pushed,
50
+ update it with `git push --force-with-lease`.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,114 @@
1
+ Metadata-Version: 2.5
2
+ Name: sqlalchemy-cf-analytics-engine
3
+ Version: 0.0.1
4
+ Summary: Read-only DBAPI driver and SQLAlchemy dialect for Cloudflare Workers Analytics Engine
5
+ Project-URL: Homepage, https://github.com/sqlalchemy-cf-wae/sqlalchemy-cf-analytics-engine
6
+ Author: Prithvi Seran, Eva Abdul, Dibya Jyoti Goswami, Jonathan Manuel
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Topic :: Database
13
+ Requires-Python: >=3.11
14
+ Requires-Dist: httpx<1,>=0.27
15
+ Requires-Dist: sqlalchemy<3,>=2.0
16
+ Provides-Extra: dev
17
+ Requires-Dist: pytest>=8; extra == 'dev'
18
+ Requires-Dist: ruff<0.17.0,>=0.16.8; extra == 'dev'
19
+ Description-Content-Type: text/markdown
20
+
21
+ # sqlalchemy-cf-analytics-engine
22
+
23
+ Query [Cloudflare Workers Analytics Engine](https://developers.cloudflare.com/analytics/analytics-engine/)
24
+ datasets as tables with SQLAlchemy 2.x. The package includes a read-only
25
+ SQLAlchemy dialect and Python DBAPI driver for Cloudflare's SQL API. 
26
+
27
+ ## Installation
28
+
29
+ Use Python 3.11 or newer. From the root of a cloned repository, install the
30
+ package into your Python environment.
31
+
32
+ ```bash
33
+ python -m pip install .
34
+ ```
35
+
36
+ ## Connect and list your datasets
37
+
38
+ You'll need your Cloudflare account ID and an API token with **Account
39
+ Analytics: Read** access to that account. For tools that accept a SQLAlchemy
40
+ connection string, use the following URI, including the trailing `@`.
41
+ The driver connects directly to Cloudflare's API.
42
+
43
+ ```text
44
+ cfwae://{account_id}:{api_token}@
45
+ ```
46
+
47
+ In Python, set the environment variables `CFWAE_ACCOUNT_ID` and
48
+ `CFWAE_API_TOKEN` in your shell, then run this example to print the name
49
+ of each available dataset.
50
+
51
+ ```python
52
+ import os
53
+
54
+ from sqlalchemy import URL, create_engine, inspect
55
+
56
+ url = URL.create(
57
+ "cfwae",
58
+ username=os.environ["CFWAE_ACCOUNT_ID"],
59
+ password=os.environ["CFWAE_API_TOKEN"],
60
+ )
61
+ engine = create_engine(url)
62
+ for dataset in inspect(engine).get_table_names():
63
+ print(dataset)
64
+ ```
65
+
66
+ For help creating a token or populating a sample dataset, see the
67
+ [Cloudflare setup guide](dev/cloudflare/README.md).
68
+
69
+ ## Query sampled data
70
+
71
+ Each stored row represents `_sample_interval` events. To estimate the event
72
+ count, sum that column. Reuse the engine from above and replace `api_requests`
73
+ with a dataset name from your account.
74
+
75
+ ```python
76
+ from sqlalchemy import text
77
+
78
+ with engine.connect() as connection:
79
+ result = connection.execute(text("SELECT SUM(_sample_interval) FROM api_requests"))
80
+ print(result.scalar_one())
81
+ ```
82
+
83
+ | Metric | SQL expression |
84
+ | --- | --- |
85
+ | Number of events | `SUM(_sample_interval)` |
86
+ | Sum of `double1` | `SUM(_sample_interval * double1)` |
87
+ | Average of `double1` | `SUM(_sample_interval * double1) / SUM(_sample_interval)` |
88
+
89
+ ## Development
90
+
91
+ From the repository root, install an editable copy with the development tools.
92
+
93
+ ```bash
94
+ python3.11 -m venv .venv
95
+ source .venv/bin/activate
96
+ python -m pip install -e ".[dev]"
97
+ ```
98
+
99
+ Run the same lint, formatting, and test checks as CI. The tests use recorded
100
+ API responses and run without Cloudflare credentials.
101
+
102
+ ```bash
103
+ ruff check .
104
+ ruff format --check .
105
+ pytest
106
+ ```
107
+
108
+ To apply available lint fixes, run `ruff check --fix .`. To format the code,
109
+ run `ruff format .`. See [Contributing](CONTRIBUTING.md) for the pull request
110
+ workflow.
111
+
112
+ ## License
113
+
114
+ Apache License 2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,94 @@
1
+ # sqlalchemy-cf-analytics-engine
2
+
3
+ Query [Cloudflare Workers Analytics Engine](https://developers.cloudflare.com/analytics/analytics-engine/)
4
+ datasets as tables with SQLAlchemy 2.x. The package includes a read-only
5
+ SQLAlchemy dialect and Python DBAPI driver for Cloudflare's SQL API. 
6
+
7
+ ## Installation
8
+
9
+ Use Python 3.11 or newer. From the root of a cloned repository, install the
10
+ package into your Python environment.
11
+
12
+ ```bash
13
+ python -m pip install .
14
+ ```
15
+
16
+ ## Connect and list your datasets
17
+
18
+ You'll need your Cloudflare account ID and an API token with **Account
19
+ Analytics: Read** access to that account. For tools that accept a SQLAlchemy
20
+ connection string, use the following URI, including the trailing `@`.
21
+ The driver connects directly to Cloudflare's API.
22
+
23
+ ```text
24
+ cfwae://{account_id}:{api_token}@
25
+ ```
26
+
27
+ In Python, set the environment variables `CFWAE_ACCOUNT_ID` and
28
+ `CFWAE_API_TOKEN` in your shell, then run this example to print the name
29
+ of each available dataset.
30
+
31
+ ```python
32
+ import os
33
+
34
+ from sqlalchemy import URL, create_engine, inspect
35
+
36
+ url = URL.create(
37
+ "cfwae",
38
+ username=os.environ["CFWAE_ACCOUNT_ID"],
39
+ password=os.environ["CFWAE_API_TOKEN"],
40
+ )
41
+ engine = create_engine(url)
42
+ for dataset in inspect(engine).get_table_names():
43
+ print(dataset)
44
+ ```
45
+
46
+ For help creating a token or populating a sample dataset, see the
47
+ [Cloudflare setup guide](dev/cloudflare/README.md).
48
+
49
+ ## Query sampled data
50
+
51
+ Each stored row represents `_sample_interval` events. To estimate the event
52
+ count, sum that column. Reuse the engine from above and replace `api_requests`
53
+ with a dataset name from your account.
54
+
55
+ ```python
56
+ from sqlalchemy import text
57
+
58
+ with engine.connect() as connection:
59
+ result = connection.execute(text("SELECT SUM(_sample_interval) FROM api_requests"))
60
+ print(result.scalar_one())
61
+ ```
62
+
63
+ | Metric | SQL expression |
64
+ | --- | --- |
65
+ | Number of events | `SUM(_sample_interval)` |
66
+ | Sum of `double1` | `SUM(_sample_interval * double1)` |
67
+ | Average of `double1` | `SUM(_sample_interval * double1) / SUM(_sample_interval)` |
68
+
69
+ ## Development
70
+
71
+ From the repository root, install an editable copy with the development tools.
72
+
73
+ ```bash
74
+ python3.11 -m venv .venv
75
+ source .venv/bin/activate
76
+ python -m pip install -e ".[dev]"
77
+ ```
78
+
79
+ Run the same lint, formatting, and test checks as CI. The tests use recorded
80
+ API responses and run without Cloudflare credentials.
81
+
82
+ ```bash
83
+ ruff check .
84
+ ruff format --check .
85
+ pytest
86
+ ```
87
+
88
+ To apply available lint fixes, run `ruff check --fix .`. To format the code,
89
+ run `ruff format .`. See [Contributing](CONTRIBUTING.md) for the pull request
90
+ workflow.
91
+
92
+ ## License
93
+
94
+ Apache License 2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,2 @@
1
+ CFWAE_ACCOUNT_ID=
2
+ CFWAE_API_TOKEN=
@@ -0,0 +1,111 @@
1
+ # Cloudflare test setup
2
+
3
+ Use this guide to query live Workers Analytics Engine datasets, generate sample
4
+ data, or record SQL API responses for the test suite. The unit tests use
5
+ recorded responses and run without a Cloudflare account. For local development,
6
+ see [Contributing](../../CONTRIBUTING.md).
7
+
8
+ From the root of your clone, open a terminal and change directories. Run the
9
+ remaining commands from this directory.
10
+
11
+ ```bash
12
+ cd dev/cloudflare
13
+ ```
14
+
15
+ ## Configure access to the SQL API
16
+
17
+ 1. Sign in to the [Cloudflare dashboard](https://dash.cloudflare.com/) and
18
+ select the account that owns the datasets. For a new account, open
19
+ **Storage & databases → Analytics Engine** and select **Enable**.
20
+ 2. Under **My Profile → API Tokens**, create a custom token. Select the
21
+ **Account Analytics** permission with **Read** access for the account.
22
+ Copy the token when Cloudflare displays it.
23
+ 3. Create a local configuration file from the example.
24
+
25
+ ```bash
26
+ cp .env.example .env
27
+ ```
28
+
29
+ In the dashboard, press **⌘K** (macOS) or **Ctrl+K** (Windows or Linux)
30
+ to open **Search**. Type **Copy account ID** and select the result.
31
+ You can also copy the ID under **Workers & Pages → Account Details**.
32
+
33
+ In `.env`, set `CFWAE_ACCOUNT_ID` to the copied ID and `CFWAE_API_TOKEN`
34
+ to the token you created. Git ignores `.env`.
35
+
36
+ ## Query an existing dataset
37
+
38
+ List the datasets available to your account. The script reads `.env` and
39
+ prints the response body followed by the HTTP status.
40
+
41
+ ```bash
42
+ ./query.sh "SHOW TABLES FORMAT JSON"
43
+ ```
44
+
45
+ If the list includes `api_requests`, run the following query to count requests
46
+ by HTTP method, weighted by the sampling interval. If you need sample data,
47
+ deploy the Worker using the steps below.
48
+
49
+ ```bash
50
+ ./query.sh "SELECT blob1, SUM(_sample_interval) AS n FROM api_requests GROUP BY blob1 FORMAT JSON"
51
+ ```
52
+
53
+ ## Deploy the sample Worker
54
+
55
+ Use this step when you need to populate your own account. The Worker simulates
56
+ traffic for an online store and writes to two datasets every five minutes.
57
+
58
+ | Dataset | One row represents | Example fields |
59
+ | ---------------- | ------------------ | ---------------------------------------------- |
60
+ | `api_requests` | An API request | Account ID, HTTP method, path, status, latency |
61
+ | `product_events` | A storefront event | User ID, event name, page URL, SKU, quantity |
62
+
63
+ See the [Worker source](src/index.js) for the full column mapping and the
64
+ [Wrangler configuration](wrangler.jsonc) for the dataset bindings and schedule.
65
+
66
+ Wrangler is Cloudflare’s command-line tool for deploying Workers. With Node.js
67
+ and npm installed, run the login command in your terminal. Wrangler opens a
68
+ browser. Approve the authorization request, then return to the terminal.
69
+
70
+ ```bash
71
+ npx wrangler@4 login
72
+ ```
73
+
74
+ Deploy the Worker using the account you configured for the SQL API.
75
+
76
+ ```bash
77
+ npx wrangler@4 deploy
78
+ ```
79
+
80
+ On your first deployment, Wrangler may prompt you to choose a `workers.dev`
81
+ subdomain. To generate data immediately, open the Worker URL printed by Wrangler
82
+ in your browser and append `/seed?n=200` to the URL.
83
+
84
+ Analytics Engine creates each dataset on its first write. Allow a minute or two
85
+ for new data to become queryable, then run the queries above. Cloudflare assigns
86
+ timestamps at write time, so hourly and daily charts accumulate data as the
87
+ Worker runs.
88
+
89
+ ## Record test fixtures
90
+
91
+ With `api_requests` populated, run the probe script to refresh the recorded
92
+ responses used in offline tests.
93
+
94
+ ```bash
95
+ ./probe.sh
96
+ ```
97
+
98
+ The script overwrites the corresponding files in
99
+ [tests/fixtures/responses](../../tests/fixtures/responses) with the response
100
+ body and HTTP status for each query. Some queries intentionally exercise API
101
+ errors. Review the diff for unexpected responses and account-specific values
102
+ before committing.
103
+
104
+ ## Remove the sample Worker
105
+
106
+ When you finish collecting data, run the following command to remove the
107
+ Worker and stop scheduled writes.
108
+
109
+ ```bash
110
+ npx wrangler@4 delete
111
+ ```