├── .circleci └── config.yml ├── .flake8 ├── .gitignore ├── .pre-commit-config.yaml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── Procfile ├── README.md ├── app.json ├── bin ├── daily_total.py ├── get_count.py └── weekly_total.py ├── config.py ├── ochazuke ├── __init__.py ├── api │ ├── __init__.py │ └── views.py ├── helpers.py ├── models.py └── web │ ├── __init__.py │ └── views.py ├── requirements-dev.txt ├── requirements.txt ├── runtime.txt ├── setup.py ├── tests ├── __init__.py ├── fixtures │ ├── firefox-interventions.json │ └── triage.json └── unit │ ├── __init__.py │ ├── test_api.py │ ├── test_helpers.py │ ├── test_tools_helpers.py │ └── test_web.py └── tools ├── __init__.py └── helpers.py /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Python CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-python/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | docker: 9 | # specify the version you desire here 10 | # use `-browsers` prefix for selenium tests, e.g. `3.7.3-browsers` 11 | - image: cimg/python:3.9.2 12 | environment: 13 | FLASK_CONFIG: testing 14 | DATABASE_URL: postgresql://root@localhost/circle_test 15 | # Specify service dependencies here if necessary 16 | # CircleCI maintains a library of pre-built images 17 | # documented at https://circleci.com/docs/2.0/circleci-images/ 18 | - image: cimg/postgres:14.2 19 | 20 | 21 | working_directory: ~/repo 22 | 23 | steps: 24 | - checkout 25 | 26 | # Download and cache dependencies 27 | - restore_cache: 28 | keys: 29 | - v1-dependencies-{{ checksum "requirements.txt" }} 30 | # fallback to using the latest cache if no exact match is found 31 | - v1-dependencies- 32 | 33 | - run: 34 | name: install dependencies 35 | command: | 36 | python3 -m venv venv 37 | . venv/bin/activate 38 | pip install -r requirements-dev.txt 39 | 40 | - save_cache: 41 | paths: 42 | - venv 43 | key: v1-dependencies-{{ checksum "requirements.txt" }} 44 | 45 | # run tests! 46 | # this example uses Django's built-in test-runner 47 | # other common Python testing frameworks include pytest and nose 48 | # https://pytest.org 49 | # https://nose.readthedocs.io 50 | - run: 51 | name: run tests 52 | command: | 53 | . venv/bin/activate 54 | flake8 --exclude=venv 55 | nose2 56 | 57 | - store_artifacts: 58 | path: test-reports 59 | destination: test-reports -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = E203, W503 3 | max-line-length = 88 4 | max-complexity = 18 5 | select = B,C,E,F,W,T4,B9 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # General things to ignore 2 | .DS_Store 3 | settings.cfg 4 | *.log* 5 | data/* 6 | .vscode/ 7 | 8 | # Byte-compiled / optimized / DLL files 9 | __pycache__/ 10 | *.py[cod] 11 | 12 | # Flask stuff: 13 | instance/ 14 | .webassets-cache 15 | 16 | # virtualenv 17 | .venv 18 | venv/ 19 | .env 20 | env 21 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/psf/black 3 | rev: stable 4 | hooks: 5 | - id: black 6 | language_version: python3.9 7 | - repo: https://gitlab.com/pycqa/flake8 8 | rev: 3.9.2 9 | hooks: 10 | - id: flake8 11 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Mozilla Community Participation Guidelines 2 | 3 | This repository is governed by Mozilla's code of conduct and etiquette guidelines. 4 | For more details, please read the 5 | [Mozilla Community Participation Guidelines](https://www.mozilla.org/about/governance/policies/participation/). 6 | 7 | ## How to Report 8 | For more information on how to report violations of the Community Participation Guidelines, please read our [How to Report](https://www.mozilla.org/about/governance/policies/participation/reporting/) page. 9 | 10 | ## Project Specific Etiquette 11 | 12 | ### Our Pledge 13 | 14 | In the interest of fostering an open and welcoming environment, we as 15 | contributors and maintainers pledge to make participation in our project and 16 | our community a harassment-free experience for everyone, regardless of age, body 17 | size, disability, ethnicity, gender identity and expression, level of experience, 18 | nationality, personal appearance, race, religion, or sexual identity and 19 | orientation. 20 | 21 | ### Our Standards 22 | 23 | Examples of behavior that contribute to creating a positive environment 24 | include: 25 | 26 | * Using welcoming and inclusive language 27 | * Being respectful of differing viewpoints and experiences 28 | * Gracefully accepting constructive criticism 29 | * Focusing on what is best for the community 30 | * Showing empathy towards other community members 31 | 32 | Examples of unacceptable behavior by participants include: 33 | 34 | * The use of sexualized language or imagery and unwelcome sexual attention or 35 | advances 36 | * Trolling, insulting/derogatory comments, and personal or political attacks 37 | * Public or private harassment 38 | * Publishing others' private information, such as a physical or electronic 39 | address, without explicit permission 40 | * Other conduct which could reasonably be considered inappropriate in a 41 | professional setting 42 | 43 | ### Our Responsibilities 44 | 45 | Project maintainers are responsible for clarifying the standards of acceptable 46 | behavior and are expected to take appropriate and fair corrective action in 47 | response to any instances of unacceptable behavior. 48 | 49 | Project maintainers have the right and responsibility to remove, edit, or 50 | reject comments, commits, code, wiki edits, issues, and other contributions 51 | that are not aligned to this Code of Conduct, or to ban temporarily or 52 | permanently any contributor for other behavior that they deem inappropriate, 53 | threatening, offensive, or harmful. 54 | 55 | ### Scope 56 | 57 | This Code of Conduct applies both within project spaces and in public spaces 58 | when an individual is representing the project or its community. Examples of 59 | representing a project or community include using an official project e-mail 60 | address, posting via an official social media account, or acting as an appointed 61 | representative at an online or offline event. Representation of a project may be 62 | further defined and clarified by project maintainers. 63 | 64 | ### Enforcement 65 | 66 | Instances of abuse, harassment, or otherwise unacceptable behavior may be 67 | reported by contacting the project team at [Mike](mailto:miket@mozilla.com) / [@miketaylr](https://github.com/miketaylr) or [Karl](mailto:kdubost@mozilla.com) / [@karlcow](https://github.com/karlcow) or [Guillaume](mailto:magsout@gmail.com) / [@magsout](https://github.com/magsout). All 68 | complaints will be reviewed and investigated and will result in a response that 69 | is deemed necessary and appropriate to the circumstances. The project team is 70 | obligated to maintain confidentiality with regard to the reporter of an incident. 71 | Further details of specific enforcement policies may be posted separately. 72 | 73 | Project maintainers who do not follow or enforce the Code of Conduct in good 74 | faith may face temporary or permanent repercussions as determined by other 75 | members of the project's leadership. 76 | 77 | ### Attribution 78 | 79 | This project specific code of conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 80 | available at [http://contributor-covenant.org/version/1/4][version] 81 | 82 | [homepage]: http://contributor-covenant.org 83 | [version]: http://contributor-covenant.org/version/1/4/ 84 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn "ochazuke:create_app('production')" 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ochazuke 2 | 3 | Ochazuke is the server part for the webcompat metrics project. A [webcompat metrics client](https://github.com/webcompat/webcompat-metrics-client/) is being developed in parallel. 4 | 5 | ## Prerequisites 6 | This is built to be used with Python 3. 7 | 8 | ## Objectives 9 | * [ ] [First prototype](https://github.com/mozilla/webcompat-team-okrs/issues/1) by the end of 2018Q2 10 | 11 | ### [Code of Conduct][coc] 12 | 13 | Webcompat has adopted a Code of Conduct that we expect project participants to adhere to. Please read [the full text][coc] so that you can understand what actions will and will not be tolerated. 14 | 15 | ## Project name: Ochazuke 16 | [Ochazuke no aji](https://en.wikipedia.org/wiki/The_Flavor_of_Green_Tea_over_Rice) is a movie by Yasujirō Ozu. Ochazuke is the name of a delicious dish in Japan, very simple, and with a great variety of flavours and ingredients. 17 | 18 | [coc]: https://github.com/webcompat/webcompat-metrics-server/blob/master/CODE_OF_CONDUCT.md 19 | -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "addons": [ 3 | "heroku-postgresql", 4 | "scheduler" 5 | ], 6 | "buildpacks": [ 7 | { 8 | "url": "heroku/python" 9 | } 10 | ], 11 | "env": { 12 | "FLASK_APP": { 13 | "required": true 14 | } 15 | }, 16 | "formation": { 17 | "web": { 18 | "quantity": 1 19 | } 20 | }, 21 | "name": "webcompat-metrics-server", 22 | "scripts": { 23 | }, 24 | "stack": "heroku-20" 25 | } 26 | -------------------------------------------------------------------------------- /bin/daily_total.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """ 7 | Get the total issues reported each day on webcompat. 8 | """ 9 | 10 | import sys 11 | import logging 12 | import datetime 13 | import time 14 | import json 15 | import sqlalchemy 16 | from urllib.parse import urljoin 17 | from urllib.request import Request 18 | from urllib.request import urlopen 19 | 20 | from ochazuke import create_app 21 | from ochazuke.models import db 22 | from ochazuke.models import DailyTotal 23 | 24 | # Config 25 | SEARCH_URL = "https://api.github.com/search/" 26 | QUERY = "issues?q=repo:webcompat/web-bugs+created:{yesterday}" 27 | LOGGER = logging.getLogger(__name__) 28 | 29 | 30 | def get_remote_file(url): 31 | """Request URL.""" 32 | req = Request(url) 33 | req.add_header("User-agent", "webcompatMonitor") 34 | req.add_header("Accept", "application/vnd.github.v3+json") 35 | json_response = urlopen(req, timeout=240) 36 | return json_response 37 | 38 | 39 | def get_issue_count(json_response): 40 | """Get the number of issues (open or closed).""" 41 | json_data = json.load(json_response) 42 | if not json_data["incomplete_results"]: 43 | return json_data["total_count"] 44 | else: 45 | return None 46 | 47 | 48 | def main(): 49 | """Core program to fetch and process data from GitHub.""" 50 | # NOTE: This works as expected if script is scheduled in UTC 51 | today = datetime.date.today() 52 | yesterday = today - datetime.timedelta(days=1) 53 | yesterday = yesterday.isoformat() 54 | # Insert yesterday's date into search query in format: 2019-01-30 55 | query = QUERY.format(yesterday=yesterday) 56 | url = urljoin(SEARCH_URL, query) 57 | json_response = get_remote_file(url) 58 | issue_count = get_issue_count(json_response) 59 | if not issue_count: 60 | # If results are incomplete, retry after 3 min 61 | time.sleep(360) 62 | issue_count = get_issue_count(json_response) 63 | if not issue_count: 64 | # On a second failure, log an error 65 | msg = "Daily count failed for {yesterday}!".format(yesterday=yesterday) 66 | LOGGER.warning(msg) 67 | return 68 | # Create an app context and store the data in the database 69 | app = create_app("production") 70 | with app.app_context(): 71 | total = DailyTotal(day=yesterday, count=issue_count) 72 | db.session.add(total) 73 | try: 74 | db.session.commit() 75 | msg = "Successfully wrote {day} data in DailyTotal table.".format( 76 | day=yesterday 77 | ) 78 | LOGGER.info(msg) 79 | # Catch error and attempt to recover by resetting staged changes. 80 | except sqlalchemy.exc.SQLAlchemyError as error: 81 | db.session.rollback() 82 | msg = ( 83 | "Yikes! Failed to write data for {day} in " "DailyTotal table: {err}" 84 | ).format(day=yesterday, err=error) 85 | LOGGER.warning(msg) 86 | 87 | 88 | if __name__ == "__main__": 89 | sys.exit(main()) 90 | -------------------------------------------------------------------------------- /bin/get_count.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """ 7 | Get count of milestones data. 8 | """ 9 | 10 | import datetime 11 | import json 12 | import sys 13 | import sqlalchemy 14 | import logging 15 | from urllib.parse import urljoin 16 | from urllib.request import Request 17 | from urllib.request import urlopen 18 | 19 | from ochazuke import create_app 20 | from ochazuke.models import db 21 | from ochazuke.models import IssuesCount 22 | 23 | # Config 24 | URL_REPO = 'https://api.github.com/repos/webcompat/web-bugs/milestones/' 25 | MILESTONES = { 26 | 'non-compat': (1, 'closed'), 27 | 'needstriage': (2, 'open'), 28 | 'needsdiagnosis': (3, 'open'), 29 | 'needscontact': (4, 'open'), 30 | 'contactready': (5, 'open'), 31 | 'sitewait': (6, 'open'), 32 | 'duplicate': (7, 'closed'), 33 | 'invalid': (8, 'closed'), 34 | 'wontfix': (9, 'closed'), 35 | 'worksforme': (10, 'closed'), 36 | 'incomplete': (11, 'closed'), 37 | 'fixed': (12, 'closed'), 38 | } 39 | LOGGER = logging.getLogger(__name__) 40 | 41 | 42 | def get_remote_file(url): 43 | """Request URL.""" 44 | req = Request(url) 45 | req.add_header('User-agent', 'webcompatMonitor') 46 | req.add_header('Accept', 'application/vnd.github.v3+json') 47 | json_response = urlopen(req, timeout=240) 48 | return json_response 49 | 50 | 51 | def extract_issues_count(json_response, status): 52 | """Extract the number of open issues.""" 53 | if status == 'open': 54 | status = 'open_issues' 55 | else: 56 | status = 'closed_issues' 57 | json_data = json.load(json_response) 58 | return json_data[status] 59 | 60 | 61 | def newtime(timestamp): 62 | """convert from local to UTC.""" 63 | local_time = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S") 64 | # 2018-02-27T00:00:03Z 65 | UTC_OFFSET_TIMEDELTA = datetime.datetime.utcnow() - datetime.datetime.now() 66 | new_time = local_time + UTC_OFFSET_TIMEDELTA 67 | utc_time = new_time.strftime("%Y-%m-%dT%H:%M:%SZ") 68 | return utc_time 69 | 70 | 71 | def main(): 72 | """Core program.""" 73 | # Get the milestone we need from the command line. 74 | if len(sys.argv) != 2: 75 | sys.exit('BYE: too many, too few arguments.') 76 | milestone = sys.argv[1] 77 | # Check we have the right argument. 78 | if milestone in MILESTONES: 79 | # make sure the code is a string. 80 | urlcode = str(MILESTONES[milestone][0]) 81 | status = MILESTONES[milestone][1] 82 | else: 83 | sys.exit('BYE: Not a valid argument.') 84 | # Extract data from GitHub 85 | url = urljoin(URL_REPO, urlcode) 86 | json_response = get_remote_file(url) 87 | issues_count = extract_issues_count(json_response, status) 88 | # Compute the date 89 | now = newtime(datetime.datetime.now().isoformat(timespec='seconds')) 90 | 91 | # Create an app context and store the data in the database 92 | app = create_app('production') 93 | with app.app_context(): 94 | iss_count = IssuesCount( 95 | timestamp=now, 96 | count=issues_count, 97 | milestone=milestone) 98 | db.session.add(iss_count) 99 | try: 100 | db.session.commit() 101 | msg = ("Successfully wrote MILESTONE {milestone} count for {now} " 102 | "to IssuesCount table.").format( 103 | milestone=milestone, 104 | now=now) 105 | LOGGER.info(msg) 106 | # Catch error and attempt to recover by resetting staged changes. 107 | except sqlalchemy.exc.SQLAlchemyError as error: 108 | db.session.rollback() 109 | msg = ("Yikes! Failed to write MILESTONE {milestone} count for " 110 | "{now} in IssuesCount table. {error}").format( 111 | milestone=milestone, 112 | now=now, 113 | error=error) 114 | LOGGER.warning(msg) 115 | 116 | 117 | if __name__ == "__main__": 118 | sys.exit(main()) 119 | -------------------------------------------------------------------------------- /bin/weekly_total.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """ 7 | Compute and store the total issues reported each week on webcompat. 8 | """ 9 | 10 | import sys 11 | import logging 12 | import datetime 13 | import sqlalchemy 14 | 15 | from ochazuke import create_app 16 | from ochazuke.models import db 17 | from ochazuke.models import DailyTotal 18 | from ochazuke.models import WeeklyTotal 19 | 20 | # Config 21 | LOGGER = logging.getLogger(__name__) 22 | 23 | 24 | def main(): 25 | """Code to query DB for a week of counts, sum them, and store result.""" 26 | # NOTE: This works as expected if script is scheduled in UTC 27 | today = datetime.date.today() 28 | weekday = today.isoweekday() 29 | if weekday != 1: 30 | # If not Monday, abandon script and exit 31 | msg = ( 32 | "Day of week is {} -- not Monday. " "Weekly count script exited." 33 | ).format(weekday) 34 | LOGGER.warning(msg) 35 | sys.exit() 36 | monday = today - datetime.timedelta(days=7) 37 | sunday = today - datetime.timedelta(days=1) 38 | # Put last Monday and yesterday's dates into format: 2019-01-30 39 | monday = monday.isoformat() 40 | sunday = sunday.isoformat() 41 | 42 | # Create an app context and store the data in the database 43 | app = create_app("production") 44 | with app.app_context(): 45 | date_range = DailyTotal.day.between(monday, sunday) 46 | LOGGER.info("MONDAY: {}".format(monday)) 47 | LOGGER.info("SUNDAY: {}".format(sunday)) 48 | LOGGER.info("DATE_RANGE {}".format(date_range)) 49 | week_list = DailyTotal.query.filter(date_range).all() 50 | LOGGER.info("COUNTS FOR WEEK {}".format(week_list)) 51 | week_total = 0 52 | if not week_list: 53 | # On a query failure, log an error 54 | msg = "Weekly count query failed for {}!".format(monday) 55 | LOGGER.warning(msg) 56 | return 57 | for day in week_list: 58 | week_total += day.count 59 | weekly_count = WeeklyTotal(monday=monday, count=week_total) 60 | db.session.add(weekly_count) 61 | try: 62 | db.session.commit() 63 | msg_tmp = "Successfully wrote count for {} in WeeklyTotal table." 64 | msg = (msg_tmp).format(monday) 65 | LOGGER.info(msg) 66 | # Catch error and attempt to recover by resetting staged changes. 67 | except sqlalchemy.exc.SQLAlchemyError as error: 68 | db.session.rollback() 69 | msg = ( 70 | "Yikes! Failed to write data for {week} in WeeklyTotal table: {err}" 71 | ).format(week=monday, err=error) 72 | LOGGER.warning(msg) 73 | 74 | 75 | if __name__ == "__main__": 76 | sys.exit(main()) 77 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Configuration for Ochazuke.""" 7 | 8 | import os 9 | 10 | 11 | def fix_uri(uri): 12 | if uri.startswith("postgres://"): 13 | uri = uri.replace("postgres://", "postgresql://", 1) 14 | return uri 15 | 16 | 17 | class Config: 18 | """Set Flask configuration vars from .env file.""" 19 | 20 | # General 21 | TESTING = False 22 | FLASK_DEBUG = False 23 | 24 | @staticmethod 25 | def init_app(app): 26 | pass 27 | 28 | 29 | class DevelopmentConfig(Config): 30 | """Special class for development purpose""" 31 | 32 | # export FLASK_ENV=development 33 | # on your local computer 34 | DEBUG = True 35 | # Database 36 | SQLALCHEMY_DATABASE_URI = os.environ.get("DEV_DATABASE_URL") 37 | 38 | 39 | class TestingConfig(Config): 40 | """Special class for testing purpose""" 41 | 42 | TESTING = True 43 | DEBUG = True 44 | FLASK_DEBUG = True 45 | # Database 46 | SQLALCHEMY_DATABASE_URI = os.environ.get("TEST_DATABASE_URL") or "sqlite://" # noqa 47 | SQLALCHEMY_DATABASE_URI = fix_uri(SQLALCHEMY_DATABASE_URI) 48 | SQLALCHEMY_TRACK_MODIFICATIONS = False 49 | 50 | 51 | class ProductionConfig(Config): 52 | """Production Ready Config.""" 53 | 54 | TESTING = False 55 | DEBUG = False 56 | SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL") 57 | SQLALCHEMY_DATABASE_URI = fix_uri(SQLALCHEMY_DATABASE_URI) 58 | 59 | @classmethod 60 | def init_app(cls, app): 61 | Config.init_app(app) 62 | 63 | 64 | config = { 65 | "development": DevelopmentConfig, 66 | "testing": TestingConfig, 67 | "production": ProductionConfig, 68 | # Secure Fallback 69 | "default": DevelopmentConfig, 70 | } 71 | -------------------------------------------------------------------------------- /ochazuke/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | 7 | """Create Ochazuke: the webcompat-metrics-server Flask application.""" 8 | 9 | import logging 10 | 11 | from flask import Flask 12 | from flask_sqlalchemy import SQLAlchemy 13 | 14 | from config import config 15 | 16 | 17 | db = SQLAlchemy() 18 | 19 | 20 | def create_app(config_name): 21 | """Create the main webcompat metrics server app.""" 22 | # create and configure the app 23 | app = Flask(__name__) 24 | app.config.from_object(config[config_name]) 25 | config[config_name].init_app(app) 26 | # DB init 27 | db.init_app(app) 28 | # Blueprint 29 | configure_blueprints(app) 30 | return app 31 | 32 | 33 | def configure_blueprints(app): 34 | """Define the blueprints for the project.""" 35 | # Web views for humans 36 | from ochazuke.web import web_blueprint 37 | app.register_blueprint(web_blueprint) 38 | # Views for API clients 39 | from ochazuke.api import api_blueprint 40 | app.register_blueprint(api_blueprint, url_prefix='/data') 41 | 42 | 43 | # Logging Capabilities 44 | # To benefit from the logging, you may want to add: 45 | # app.logger.info(Thing_To_Log) 46 | # it will create a line with the following format 47 | # (2015-09-14 20:50:19) INFO: Thing_To_Log 48 | logging.basicConfig(format='(%(asctime)s) %(levelname)s: %(message)s', 49 | datefmt='%Y-%m-%d %H:%M:%S %z', level=logging.INFO) 50 | -------------------------------------------------------------------------------- /ochazuke/api/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | from flask import Blueprint 7 | 8 | api_blueprint = Blueprint('api', __name__) 9 | 10 | from ochazuke.api import views # noqa -------------------------------------------------------------------------------- /ochazuke/api/views.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Set of routes for Ochazuke app.""" 7 | 8 | import json 9 | 10 | from flask import abort 11 | 12 | # from flask import current_app as app 13 | from flask import request 14 | from flask import Response 15 | 16 | from urllib.error import HTTPError 17 | 18 | from ochazuke.api import api_blueprint 19 | from ochazuke.helpers import get_weekly_data 20 | from ochazuke.helpers import get_timeline_data 21 | from ochazuke.helpers import is_valid_args 22 | from ochazuke.helpers import is_valid_category 23 | from ochazuke.helpers import normalize_date_range 24 | from tools.helpers import get_remote_data 25 | from tools.helpers import url_with_params 26 | 27 | 28 | @api_blueprint.route("/weekly-counts") 29 | def weekly_reports_data(): 30 | """Route for weekly bug reports.""" 31 | if not request.args: 32 | abort(404) 33 | if not is_valid_args(request.args): 34 | abort(404) 35 | # Extract the dates 36 | from_date = request.args.get("from") 37 | to_date = request.args.get("to") 38 | # Adding the extra day for weekly reports isn't necessary, but won't hurt 39 | start, end = normalize_date_range(from_date, to_date) 40 | # Fetch the data 41 | timeline = get_weekly_data(from_date, to_date) 42 | # Prepare the response 43 | response_object = { 44 | "about": "Weekly Count of New Issues Reported", 45 | "numbering_of_weeks": "ISO calendar", 46 | "timeline": timeline, 47 | } 48 | response = Response( 49 | response=json.dumps(response_object), 50 | status=200, 51 | mimetype="application/json", 52 | ) 53 | response.headers.add("Access-Control-Allow-Origin", "*") 54 | response.headers.add("Access-Control-Allow-Credentials", "true") 55 | response.headers.add("Vary", "Origin") 56 | return response 57 | 58 | 59 | @api_blueprint.route("/-timeline") 60 | def issues_count_data(category): 61 | """Route for issues count.""" 62 | if not is_valid_category(category): 63 | abort(404) 64 | if not request.args: 65 | abort(404) 66 | if not is_valid_args(request.args): 67 | abort(404) 68 | # Extract the dates 69 | from_date = request.args.get("from") 70 | to_date = request.args.get("to") 71 | start, end = normalize_date_range(from_date, to_date) 72 | # Grab the data 73 | timeline = get_timeline_data(category, start, end) 74 | # Prepare the response 75 | about = "Hourly {category} issues count".format(category=category) 76 | response_object = { 77 | "about": about, 78 | "date_format": "w3c", 79 | "timeline": timeline, 80 | } 81 | response = Response( 82 | response=json.dumps(response_object), 83 | status=200, 84 | mimetype="application/json", 85 | ) 86 | response.headers.add("Access-Control-Allow-Origin", "*") 87 | response.headers.add("Access-Control-Allow-Credentials", "true") 88 | response.headers.add("Vary", "Origin") 89 | return response 90 | 91 | 92 | @api_blueprint.route("/triage-bugs") 93 | def triage_bugs(): 94 | """Returns the list of issues which are currently in triage.""" 95 | url = "https://api.github.com/repos/webcompat/web-bugs/issues?sort=created&per_page=100&direction=asc&milestone=2" # noqa 96 | json_data = get_remote_data(url) 97 | response = Response( 98 | response=json_data, status=200, mimetype="application/json" 99 | ) 100 | response.headers.add("Access-Control-Allow-Origin", "*") 101 | response.headers.add("Access-Control-Allow-Credentials", "true") 102 | response.headers.add("Vary", "Origin") 103 | return response 104 | 105 | 106 | @api_blueprint.route("/tsci-doc") 107 | def tsci_doc(): 108 | """Returns the current ID of the spreadsheet where TSCI is calculated.""" 109 | url = "https://tsci.webcompat.com/currentDoc.json" # noqa 110 | json_data = get_remote_data(url) 111 | response = Response( 112 | response=json_data, status=200, mimetype="application/json" 113 | ) 114 | response.headers.add("Access-Control-Allow-Origin", "*") 115 | response.headers.add("Access-Control-Allow-Credentials", "true") 116 | response.headers.add("Vary", "Origin") 117 | return response 118 | 119 | 120 | @api_blueprint.route("/firefox-interventions") 121 | def firefox_interventions(): 122 | """Returns historical counters for Firefox Interventions.""" 123 | url = url_with_params( 124 | "https://arewehotfixingthewebyet.com/data.json", 125 | { 126 | "distribution": request.args.get("distribution"), 127 | "type": request.args.get("type"), 128 | "start": request.args.get("start"), 129 | "end": request.args.get("end") 130 | } 131 | ) 132 | try: 133 | json_data = get_remote_data(url) 134 | except HTTPError: 135 | json_data = "[]" 136 | response = Response( 137 | response=json_data, status=200, mimetype="application/json" 138 | ) 139 | response.headers.add("Access-Control-Allow-Origin", "*") 140 | response.headers.add("Access-Control-Allow-Credentials", "true") 141 | response.headers.add("Vary", "Origin") 142 | return response 143 | -------------------------------------------------------------------------------- /ochazuke/helpers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Some helpers for the data processing section.""" 7 | 8 | import datetime 9 | import json 10 | 11 | from ochazuke import logging 12 | from ochazuke.models import IssuesCount 13 | from ochazuke.models import WeeklyTotal 14 | 15 | 16 | def get_days(from_date, to_date): 17 | """Create the list of dates spanning two dates. 18 | 19 | A date is a string 'YYYY-MM-DD' 20 | A date is considered to be starting at 00:00:00. 21 | An invalid date format should be ignored and return None. 22 | The same from_date and to_date should return from_date. 23 | """ 24 | date_format = "%Y-%m-%d" 25 | try: 26 | start = datetime.datetime.strptime(from_date, date_format) 27 | end = datetime.datetime.strptime(to_date, date_format) 28 | # we assume that the person is requesting one day 29 | if start == end: 30 | return [from_date] 31 | except Exception: 32 | return None 33 | else: 34 | dates = [] 35 | delta = end - start 36 | days = delta.days 37 | if days < 0: 38 | end = start 39 | days = abs(days) 40 | for n in range(0, days + 1): 41 | new_date = end - datetime.timedelta(days=n) 42 | dates.append(new_date.strftime(date_format)) 43 | return dates 44 | 45 | 46 | def get_timeline_slice(timeline, dates_list): 47 | """Return a partial timeline including only a predefined list of dates.""" 48 | sliced_data = [ 49 | dated_data 50 | for dated_data in timeline 51 | if dated_data["timestamp"][:10] in dates_list 52 | ] 53 | return sliced_data 54 | 55 | 56 | def get_json_slice(timeline, from_date, to_date): 57 | """Return a partial JSON timeline.""" 58 | dates = get_days(from_date, to_date) 59 | full_data = json.loads(timeline) 60 | partial_data = get_timeline_slice(full_data["timeline"], dates) 61 | full_data["timeline"] = partial_data 62 | return json.dumps(full_data) 63 | 64 | 65 | def is_valid_args(args): 66 | """Check if the arguments we receive are valid.""" 67 | if args: 68 | try: 69 | from_date = args["from"] 70 | to_date = args["to"] 71 | except Exception: 72 | return False 73 | try: 74 | date_format = "%Y-%m-%d" 75 | datetime.datetime.strptime(from_date, date_format) 76 | datetime.datetime.strptime(to_date, date_format) 77 | except Exception: 78 | return False 79 | else: 80 | return True 81 | return False 82 | 83 | 84 | def is_valid_category(category): 85 | """Check if the category is acceptable.""" 86 | VALID_CATEGORY = [ 87 | "needsdiagnosis", 88 | "needstriage", 89 | "needscontact", 90 | "contactready", 91 | "sitewait", 92 | ] 93 | if category in VALID_CATEGORY: 94 | return True 95 | return False 96 | 97 | 98 | def normalize_date_range(from_date, to_date): 99 | """Add a day to the to_date so dates are inclusive in a database query. 100 | 101 | A date is a string 'YYYY-MM-DD' 102 | A date is considered to be starting at 00:00:00. 103 | An invalid date format should be ignored and return None. 104 | The same from_date and to_date should return from_date. 105 | """ 106 | date_format = "%Y-%m-%d" 107 | try: 108 | start = datetime.datetime.strptime(from_date, date_format) 109 | end = datetime.datetime.strptime(to_date, date_format) 110 | except Exception: 111 | return None 112 | else: 113 | end = end + datetime.timedelta(days=1) 114 | end_date = end.strftime(date_format) 115 | start_date = start.strftime(date_format) 116 | return start_date, end_date 117 | 118 | 119 | def get_timeline_data(category, start, end): 120 | """Query the data in the DB for a defined category.""" 121 | # Extract the list of issues 122 | date_range = IssuesCount.timestamp.between(start, end) 123 | logging.info("DATE_RANGE {}".format(date_range)) 124 | category_issues = IssuesCount.query.filter_by(milestone=category) 125 | logging.info("CATEGORY {}".format(category_issues)) 126 | issues_list = ( 127 | category_issues.filter(date_range).order_by(IssuesCount.timestamp.asc()).all() 128 | ) 129 | logging.info("ISSUES {}".format(issues_list)) 130 | timeline = [ 131 | {"count": issue.count, "timestamp": issue.timestamp.isoformat() + "Z"} 132 | for issue in issues_list 133 | ] 134 | return timeline 135 | 136 | 137 | def get_weekly_data(start, end): 138 | """Query the data in the DB for weekly bug counts.""" 139 | # Extract the list of issues 140 | date_range = WeeklyTotal.monday.between(start, end) 141 | msg = ( 142 | "DATE_RANGE {dr}, where timestamp_1 = {t1}" " and timestamp_2 = {t2}" 143 | ).format(dr=date_range, t1=start, t2=end) 144 | logging.info(msg) 145 | reports_list = ( 146 | WeeklyTotal.query.filter(date_range).order_by(WeeklyTotal.monday.asc()).all() 147 | ) 148 | logging.info("REPORTS {}".format(reports_list)) 149 | timeline = [ 150 | {"count": report.count, "timestamp": report.monday.isoformat() + "Z"} 151 | for report in reports_list 152 | ] 153 | return timeline 154 | -------------------------------------------------------------------------------- /ochazuke/models.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Models and methods for working with the database.""" 7 | 8 | from ochazuke import db 9 | 10 | 11 | class DailyTotal(db.Model): 12 | """Define a DailyTotal for new issues filed. 13 | 14 | An daily total has: 15 | 16 | * a unique table id 17 | * a day representing the date that corresponds to the total 18 | * a count of the issues filed on this date 19 | """ 20 | __tablename__ = 'daily_total' 21 | id = db.Column(db.Integer, primary_key=True) 22 | day = db.Column(db.DateTime, nullable=False) 23 | count = db.Column(db.Integer, nullable=False) 24 | 25 | def __repr__(self): 26 | """Return a representation of a DailyTotal.""" 27 | return "\n\n\n\n**URL**: http://example.org/dashboard\n\n**Browser / Version**: Firefox 57.0\n**Operating System**: Mac OS X 10.13\n**Tested Another Browser**: No\n\n**Problem type**: Site is not usable\n**Description**: testing the dashboard...\n**Steps to Reproduce**:\n\n\n\n\n_From [webcompat.com](https://webcompat.com/) with ❤️_", 28 | "events_url": "https://api.github.com/repos/webcompat/webcompat-tests/issues/1/events", 29 | "labels_url": "https://api.github.com/repos/webcompat/webcompat-tests/issues/1/labels{/name}", 30 | "author_association": "MEMBER", 31 | "comments_url": "https://api.github.com/repos/webcompat/webcompat-tests/issues/1/comments", 32 | "html_url": "https://github.com/webcompat/webcompat-tests/issues/1", 33 | "updated_at": "2017-11-10T12:00:00Z", 34 | "user": { 35 | "following_url": "https://api.github.com/users/webcompat-bot/following{/other_user}", 36 | "events_url": "https://api.github.com/users/webcompat-bot/events{/privacy}", 37 | "organizations_url": "https://api.github.com/users/webcompat-bot/orgs", 38 | "url": "https://api.github.com/users/webcompat-bot", 39 | "gists_url": "https://api.github.com/users/webcompat-bot/gists{/gist_id}", 40 | "html_url": "https://github.com/webcompat-bot", 41 | "subscriptions_url": "https://api.github.com/users/webcompat-bot/subscriptions", 42 | "avatar_url": "https://avatars3.githubusercontent.com/u/8862693?v=4", 43 | "repos_url": "https://api.github.com/users/webcompat-bot/repos", 44 | "received_events_url": "https://api.github.com/users/webcompat-bot/received_events", 45 | "gravatar_id": "", 46 | "starred_url": "https://api.github.com/users/webcompat-bot/starred{/owner}{/repo}", 47 | "site_admin": false, 48 | "login": "webcompat-bot", 49 | "type": "User", 50 | "id": 8862693, 51 | "followers_url": "https://api.github.com/users/webcompat-bot/followers" 52 | }, 53 | "milestone": { 54 | "description": "Issues which needs to be triaged", 55 | "title": "needstriage", 56 | "url": "https://api.github.com/repos/webcompat/webcompat-tests/milestones/1", 57 | "labels_url": "https://api.github.com/repos/webcompat/webcompat-tests/milestones/1/labels", 58 | "created_at": "2017-11-08T03:39:06Z", 59 | "creator": { 60 | "following_url": "https://api.github.com/users/karlcow/following{/other_user}", 61 | "events_url": "https://api.github.com/users/karlcow/events{/privacy}", 62 | "organizations_url": "https://api.github.com/users/karlcow/orgs", 63 | "url": "https://api.github.com/users/karlcow", 64 | "gists_url": "https://api.github.com/users/karlcow/gists{/gist_id}", 65 | "html_url": "https://github.com/karlcow", 66 | "subscriptions_url": "https://api.github.com/users/karlcow/subscriptions", 67 | "avatar_url": "https://avatars0.githubusercontent.com/u/505230?v=4", 68 | "repos_url": "https://api.github.com/users/karlcow/repos", 69 | "received_events_url": "https://api.github.com/users/karlcow/received_events", 70 | "gravatar_id": "", 71 | "starred_url": "https://api.github.com/users/karlcow/starred{/owner}{/repo}", 72 | "site_admin": false, 73 | "login": "karlcow", 74 | "type": "User", 75 | "id": 505230, 76 | "followers_url": "https://api.github.com/users/karlcow/followers" 77 | }, 78 | "number": 1, 79 | "html_url": "https://github.com/webcompat/webcompat-tests/milestone/1", 80 | "updated_at": "2017-11-08T06:22:16Z", 81 | "due_on": null, 82 | "state": "open", 83 | "closed_issues": 0, 84 | "open_issues": 10, 85 | "closed_at": null, 86 | "id": 2744172 87 | }, 88 | "locked": false, 89 | "url": "https://api.github.com/repos/webcompat/webcompat-tests/issues/1", 90 | "created_at": "2015-02-01T12:00:00Z", 91 | "assignees": [] 92 | }, 93 | { 94 | "labels": [ 95 | { 96 | "url": "https://api.github.com/repos/webcompat/webcompat-tests/labels/browser-chrome", 97 | "color": "006b75", 98 | "default": false, 99 | "id": 90454697, 100 | "name": "browser-chrome" 101 | }, 102 | { 103 | "url": "https://api.github.com/repos/webcompat/webcompat-tests/labels/browser-firefox", 104 | "color": "d4c5f9", 105 | "default": false, 106 | "id": 182807004, 107 | "name": "browser-firefox" 108 | } 109 | ], 110 | "number": 2, 111 | "assignee": null, 112 | "repository_url": "https://api.github.com/repos/webcompat/webcompat-tests", 113 | "closed_at": null, 114 | "id": 2, 115 | "title": "dashboard.example.com - site is not usable", 116 | "comments": 0, 117 | "state": "open", 118 | "body": "\n\n\n\n**URL**: http://dashboard.example.com/\n\n**Browser / Version**: Firefox 58.0\n**Operating System**: Mac OS X 10.13\n**Tested Another Browser**: Yes\n\n**Problem type**: Site is not usable\n**Description**: triage dashboard testing\n**Steps to Reproduce**:\n\n\n\n\n_From [webcompat.com](https://webcompat.com/) with ❤️_", 119 | "events_url": "https://api.github.com/repos/webcompat/webcompat-tests/issues/2/events", 120 | "labels_url": "https://api.github.com/repos/webcompat/webcompat-tests/issues/2/labels{/name}", 121 | "author_association": "MEMBER", 122 | "comments_url": "https://api.github.com/repos/webcompat/webcompat-tests/issues/2/comments", 123 | "html_url": "https://github.com/webcompat/webcompat-tests/issues/2", 124 | "updated_at": "2015-02-20T12:00:00Z", 125 | "user": { 126 | "following_url": "https://api.github.com/users/karlcow/following{/other_user}", 127 | "events_url": "https://api.github.com/users/karlcow/events{/privacy}", 128 | "organizations_url": "https://api.github.com/users/karlcow/orgs", 129 | "url": "https://api.github.com/users/karlcow", 130 | "gists_url": "https://api.github.com/users/karlcow/gists{/gist_id}", 131 | "html_url": "https://github.com/karlcow", 132 | "subscriptions_url": "https://api.github.com/users/karlcow/subscriptions", 133 | "avatar_url": "https://avatars0.githubusercontent.com/u/505230?v=4", 134 | "repos_url": "https://api.github.com/users/karlcow/repos", 135 | "received_events_url": "https://api.github.com/users/karlcow/received_events", 136 | "gravatar_id": "", 137 | "starred_url": "https://api.github.com/users/karlcow/starred{/owner}{/repo}", 138 | "site_admin": false, 139 | "login": "karlcow", 140 | "type": "User", 141 | "id": 505230, 142 | "followers_url": "https://api.github.com/users/karlcow/followers" 143 | }, 144 | "milestone": { 145 | "description": "Issues which needs to be triaged", 146 | "title": "needstriage", 147 | "url": "https://api.github.com/repos/webcompat/webcompat-tests/milestones/1", 148 | "labels_url": "https://api.github.com/repos/webcompat/webcompat-tests/milestones/1/labels", 149 | "created_at": "2017-09-05T03:39:06Z", 150 | "creator": { 151 | "following_url": "https://api.github.com/users/karlcow/following{/other_user}", 152 | "events_url": "https://api.github.com/users/karlcow/events{/privacy}", 153 | "organizations_url": "https://api.github.com/users/karlcow/orgs", 154 | "url": "https://api.github.com/users/karlcow", 155 | "gists_url": "https://api.github.com/users/karlcow/gists{/gist_id}", 156 | "html_url": "https://github.com/karlcow", 157 | "subscriptions_url": "https://api.github.com/users/karlcow/subscriptions", 158 | "avatar_url": "https://avatars0.githubusercontent.com/u/505230?v=4", 159 | "repos_url": "https://api.github.com/users/karlcow/repos", 160 | "received_events_url": "https://api.github.com/users/karlcow/received_events", 161 | "gravatar_id": "", 162 | "starred_url": "https://api.github.com/users/karlcow/starred{/owner}{/repo}", 163 | "site_admin": false, 164 | "login": "karlcow", 165 | "type": "User", 166 | "id": 505230, 167 | "followers_url": "https://api.github.com/users/karlcow/followers" 168 | }, 169 | "number": 1, 170 | "html_url": "https://github.com/webcompat/webcompat-tests/milestone/1", 171 | "updated_at": "2017-11-08T06:22:16Z", 172 | "due_on": null, 173 | "state": "open", 174 | "closed_issues": 0, 175 | "open_issues": 10, 176 | "closed_at": null, 177 | "id": 2744172 178 | }, 179 | "locked": false, 180 | "url": "https://api.github.com/repos/webcompat/webcompat-tests/issues/2", 181 | "created_at": "2015-02-20T12:00:00Z", 182 | "assignees": [] 183 | } 184 | ] 185 | -------------------------------------------------------------------------------- /tests/unit/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | -------------------------------------------------------------------------------- /tests/unit/test_api.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Main testing module for Webcompat Metrics Server.""" 7 | import json 8 | import os 9 | import unittest 10 | from unittest.mock import patch 11 | 12 | from ochazuke import create_app 13 | from ochazuke import db 14 | 15 | 16 | DATA = [ 17 | {"count": "485", "timestamp": "2018-05-16T02:00:00Z"}, 18 | {"count": "485", "timestamp": "2018-05-17T03:00:00Z"}, 19 | {"count": "485", "timestamp": "2018-05-18T04:00:00Z"}, 20 | ] 21 | 22 | WEEKLY_DATA = [ 23 | {"count": 471, "timestamp": "2019-05-20T00:00:00Z"}, 24 | {"count": 392, "timestamp": "2019-05-27T00:00:00Z"}, 25 | {"count": 407, "timestamp": "2019-06-03T00:00:00Z"}, 26 | ] 27 | 28 | TSCI_ID = [{"currentDoc": "8sXj8GqhrdJhQdLk44"}] 29 | 30 | 31 | def json_data(filename): 32 | """Return a tuple with the content and its signature.""" 33 | current_root = os.path.realpath(os.curdir) 34 | fixtures_path = "tests/fixtures" 35 | path = os.path.join(current_root, fixtures_path, filename) 36 | with open(path, "r") as f: 37 | json_event = json.dumps(json.load(f)) 38 | return json_event 39 | 40 | 41 | class APITestCase(unittest.TestCase): 42 | """General Test Cases for views.""" 43 | 44 | def setUp(self): 45 | """Set up tests.""" 46 | self.app = create_app("testing") 47 | self.app_context = self.app.app_context() 48 | self.app_context.push() 49 | db.create_all() 50 | # Initialize a DB? 51 | self.client = self.app.test_client() 52 | 53 | def tearDown(self): 54 | db.session.remove() 55 | db.drop_all() 56 | self.app_context.pop() 57 | 58 | @patch("ochazuke.api.views.get_weekly_data") 59 | def test_weeklydata(self, mock_get): 60 | """Send back on /data/weekly-counts a JSON.""" 61 | mock_get.return_value = WEEKLY_DATA 62 | rv = self.client.get( 63 | "/data/weekly-counts?from=2019-05-16&to=2019-06-04" 64 | ) 65 | self.assertIn( 66 | ('{"count": 392, "timestamp": "2019-05-27T00:00:00Z"}'), 67 | rv.data.decode(), 68 | ) 69 | self.assertEqual(rv.status_code, 200) 70 | self.assertEqual(rv.mimetype, "application/json") 71 | self.assertTrue("Access-Control-Allow-Origin" in rv.headers.keys()) 72 | self.assertEqual("*", rv.headers["Access-Control-Allow-Origin"]) 73 | self.assertTrue("Vary" in rv.headers.keys()) 74 | self.assertEqual("Origin", rv.headers["Vary"]) 75 | self.assertTrue( 76 | "Access-Control-Allow-Credentials" in rv.headers.keys() 77 | ) 78 | self.assertEqual( 79 | "true", rv.headers["Access-Control-Allow-Credentials"] 80 | ) 81 | 82 | @patch("ochazuke.api.views.get_timeline_data") 83 | def test_needsdiagnosis_valid_param(self, mock_timeline): 84 | """Valid parameters on /needsdiagnosis-timeline.""" 85 | mock_timeline.return_value = DATA 86 | url = "/data/needsdiagnosis-timeline?from=2018-05-16&to=2018-05-18" 87 | rv = self.client.get(url) 88 | self.assertIn( 89 | '{"count": "485", "timestamp": "2018-05-17T03:00:00Z"}', 90 | rv.data.decode(), 91 | ) 92 | self.assertIn( 93 | '"about": "Hourly needsdiagnosis issues count"', rv.data.decode() 94 | ) 95 | self.assertEqual(rv.status_code, 200) 96 | self.assertEqual(rv.mimetype, "application/json") 97 | self.assertTrue("Access-Control-Allow-Origin" in rv.headers.keys()) 98 | self.assertEqual("*", rv.headers["Access-Control-Allow-Origin"]) 99 | self.assertTrue("Vary" in rv.headers.keys()) 100 | self.assertEqual("Origin", rv.headers["Vary"]) 101 | self.assertTrue( 102 | "Access-Control-Allow-Credentials" in rv.headers.keys() 103 | ) 104 | self.assertEqual( 105 | "true", rv.headers["Access-Control-Allow-Credentials"] 106 | ) 107 | 108 | @patch("ochazuke.api.views.get_remote_data") 109 | def test_triage_stats(self, mock_get): 110 | """/data/triage-bugs sends back JSON.""" 111 | mock_get.return_value = json_data("triage.json") 112 | rv = self.client.get("/data/triage-bugs") 113 | self.assertIn( 114 | '"title": "example.org - dashboard test"', rv.data.decode() 115 | ), 116 | self.assertEqual(rv.status_code, 200) 117 | self.assertEqual(rv.mimetype, "application/json") 118 | self.assertTrue("Access-Control-Allow-Origin" in rv.headers.keys()) 119 | self.assertEqual("*", rv.headers["Access-Control-Allow-Origin"]) 120 | self.assertTrue("Vary" in rv.headers.keys()) 121 | self.assertEqual("Origin", rv.headers["Vary"]) 122 | self.assertTrue( 123 | "Access-Control-Allow-Credentials" in rv.headers.keys() 124 | ) 125 | self.assertEqual( 126 | "true", rv.headers["Access-Control-Allow-Credentials"] 127 | ) 128 | 129 | @patch("ochazuke.api.views.get_remote_data") 130 | def test_tsci_id(self, mock_get): 131 | """/data/tsci-doc sends back JSON.""" 132 | mock_get.return_value = json.dumps(TSCI_ID) 133 | rv = self.client.get("/data/tsci-doc") 134 | self.assertIn('"currentDoc": "8sXj8GqhrdJhQdLk44"', rv.data.decode()) 135 | self.assertEqual(rv.status_code, 200) 136 | self.assertEqual(rv.mimetype, "application/json") 137 | self.assertTrue("Access-Control-Allow-Origin" in rv.headers.keys()) 138 | self.assertEqual("*", rv.headers["Access-Control-Allow-Origin"]) 139 | self.assertTrue("Vary" in rv.headers.keys()) 140 | self.assertEqual("Origin", rv.headers["Vary"]) 141 | self.assertTrue( 142 | "Access-Control-Allow-Credentials" in rv.headers.keys() 143 | ) 144 | self.assertEqual( 145 | "true", rv.headers["Access-Control-Allow-Credentials"] 146 | ) 147 | 148 | def test_needsdiagnosis_without_params(self): 149 | """/data/needsdiagnosis-timeline without params fail.""" 150 | rv = self.client.get("/data/needsdiagnosis-timeline") 151 | self.assertEqual(rv.status_code, 404) 152 | 153 | def test_needsdiagnosis_invalid_param(self): 154 | """Ignore invalid parameters on /needsdiagnosis-timeline.""" 155 | rv = self.client.get("/data/needsdiagnosis-timeline?blah=foo") 156 | self.assertEqual(rv.status_code, 404) 157 | 158 | def test_needsdiagnosis_invalid_param_values(self): 159 | """Ignore invalid parameters values on /needsdiagnosis-timeline.""" 160 | rv = self.client.get("/data/needsdiagnosis-timeline?from=foo&to=bar") 161 | self.assertEqual(rv.status_code, 404) 162 | 163 | @patch("ochazuke.api.views.get_remote_data") 164 | def firefox_interventions(self, mock_get): 165 | """/data/triage-bugs sends back JSON.""" 166 | mock_get.return_value = json_data("firefox-interventions.json") 167 | rv = self.client.get("/data/firefox-interventions?distribution=upstream&type=all&end=2020-01-01") # noqa 168 | self.assertEqual(rv.status_code, 200) 169 | self.assertEqual(rv.mimetype, "application/json") 170 | self.assertTrue("Access-Control-Allow-Origin" in rv.headers.keys()) 171 | self.assertEqual("*", rv.headers["Access-Control-Allow-Origin"]) 172 | self.assertTrue("Vary" in rv.headers.keys()) 173 | self.assertEqual("Origin", rv.headers["Vary"]) 174 | self.assertTrue( 175 | "Access-Control-Allow-Credentials" in rv.headers.keys() 176 | ) 177 | self.assertEqual( 178 | "true", rv.headers["Access-Control-Allow-Credentials"] 179 | ) 180 | 181 | 182 | if __name__ == "__main__": 183 | unittest.main() 184 | -------------------------------------------------------------------------------- /tests/unit/test_helpers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Main testing module for Webcompat Metrics Server.""" 7 | import unittest 8 | 9 | from werkzeug.datastructures import ImmutableMultiDict 10 | 11 | from ochazuke import create_app 12 | from ochazuke import helpers 13 | 14 | 15 | DATA = [{"count": "485", "timestamp": "2018-05-15T01:00:00Z"}, 16 | {"count": "485", "timestamp": "2018-05-16T02:00:00Z"}, 17 | {"count": "485", "timestamp": "2018-05-17T03:00:00Z"}, 18 | {"count": "485", "timestamp": "2018-05-18T04:00:00Z"}, 19 | ] 20 | 21 | 22 | class HelpersTestCase(unittest.TestCase): 23 | """General Test Cases for helpers.""" 24 | 25 | def setUp(self): 26 | """Set up tests.""" 27 | self.app = create_app('testing') 28 | self.app_context = self.app.app_context() 29 | self.app_context.push() 30 | self.client = self.app.test_client() 31 | 32 | def tearDown(self): 33 | self.app_context.pop() 34 | 35 | def test_date_range(self): 36 | """Given from_date and to_date, return a list of days.""" 37 | from_date = '2018-01-02' 38 | to_date = '2018-01-04' 39 | days = ['2018-01-02', '2018-01-03', '2018-01-04'] 40 | self.assertCountEqual(helpers.get_days(from_date, to_date), days) 41 | self.assertCountEqual(helpers.get_days(to_date, from_date), days) 42 | 43 | def test_date_range_invalid(self): 44 | """Given an invalid date, return None for the range.""" 45 | from_date = '2018-01-02T23:00' 46 | to_date = '2018-01-04' 47 | self.assertEqual(helpers.get_days(from_date, to_date), None) 48 | 49 | def test_date_range_same_day(self): 50 | """Given a same day range, return the one day range.""" 51 | from_date = '2018-01-02' 52 | to_date = '2018-01-02' 53 | self.assertEqual(helpers.get_days(from_date, to_date), ['2018-01-02']) 54 | 55 | def test_get_timeline_slice(self): 56 | """Given a list of dates, return the appropriate slice of data.""" 57 | dates = ['2018-05-16', '2018-05-17'] 58 | sliced = [{"count": "485", "timestamp": "2018-05-16T02:00:00Z"}, 59 | {"count": "485", "timestamp": "2018-05-17T03:00:00Z"}] 60 | self.assertEqual(helpers.get_timeline_slice(DATA, dates), sliced) 61 | 62 | def test_get_timeline_slice_out_of_range(self): 63 | """Empty list if the dates list and the timeline do not match.""" 64 | dates = ['2018-04-16'] 65 | full_list = [{"count": "485", "timestamp": "2018-05-15T01:00:00Z"}, 66 | {"count": "485", "timestamp": "2018-05-16T02:00:00Z"}] 67 | self.assertEqual(helpers.get_timeline_slice(full_list, dates), []) 68 | 69 | def test_is_valid_args(self): 70 | """Return True or False depending on the args.""" 71 | self.assertTrue(helpers.is_valid_args(ImmutableMultiDict( 72 | [('from', '2018-05-16'), ('to', '2018-05-18')]))) 73 | self.assertFalse(helpers.is_valid_args(ImmutableMultiDict([]))) 74 | self.assertFalse(helpers.is_valid_args(ImmutableMultiDict( 75 | [('bar', 'foo')]))) 76 | self.assertFalse(helpers.is_valid_args(ImmutableMultiDict( 77 | [('from', 'bar'), ('to', 'foo')]))) 78 | 79 | def test_normalize_date_range(self): 80 | """Test dates normalization.""" 81 | self.assertEqual( 82 | helpers.normalize_date_range('2019-01-01', '2019-01-03'), 83 | ('2019-01-01', '2019-01-04')) 84 | self.assertEqual( 85 | helpers.normalize_date_range('not_date', '2019-01-03'), 86 | None) 87 | self.assertEqual( 88 | helpers.normalize_date_range('2019-01-01', '2019-01-01'), 89 | ('2019-01-01', '2019-01-02')) 90 | 91 | 92 | if __name__ == '__main__': 93 | unittest.main() 94 | -------------------------------------------------------------------------------- /tests/unit/test_tools_helpers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Main testing module for Webcompat Metrics Server.""" 7 | import unittest 8 | 9 | from tools import helpers 10 | 11 | 12 | class ToolsHelpersTestCase(unittest.TestCase): 13 | """General Test Cases for global helpers.""" 14 | 15 | def test_url_with_params(self): 16 | """Given a dict of parameters and a URL, returns an encoded URL.""" 17 | url = "https://example.com/test" 18 | parameters = { 19 | "test": "example", 20 | "hello=world": "this/needs/encoding" 21 | } 22 | expected = "https://example.com/test?test=example&hello%3Dworld=this%2Fneeds%2Fencoding" # noqa 23 | 24 | self.assertEqual(helpers.url_with_params(url, parameters), expected) 25 | 26 | 27 | if __name__ == '__main__': 28 | unittest.main() 29 | -------------------------------------------------------------------------------- /tests/unit/test_web.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Main testing module for Web views on Webcompat Metrics Server.""" 7 | import unittest 8 | 9 | from ochazuke import create_app 10 | 11 | 12 | class WebTestCase(unittest.TestCase): 13 | """General Test Cases for views.""" 14 | 15 | def setUp(self): 16 | """Set up tests.""" 17 | self.app = create_app('testing') 18 | self.app_context = self.app.app_context() 19 | self.app_context.push() 20 | self.client = self.app.test_client() 21 | 22 | def tearDown(self): 23 | self.app_context.pop() 24 | 25 | def test_index(self): 26 | """Test the index page.""" 27 | rv = self.client.get('/') 28 | self.assertIn('Welcome to ochazuke', rv.data.decode()) 29 | 30 | 31 | if __name__ == '__main__': 32 | unittest.main() 33 | -------------------------------------------------------------------------------- /tools/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | -------------------------------------------------------------------------------- /tools/helpers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # This Source Code Form is subject to the terms of the Mozilla Public 4 | # License, v. 2.0. If a copy of the MPL was not distributed with this 5 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 | """Some helpers for the tools section.""" 7 | 8 | from urllib.parse import urlencode 9 | from urllib.request import Request 10 | from urllib.request import urlopen 11 | 12 | 13 | def get_remote_data(url): 14 | """Request URL.""" 15 | req = Request(url) 16 | req.add_header('User-agent', 'webcompatMonitor') 17 | req.add_header('Accept', 'application/json') 18 | json_response = urlopen(req, timeout=240).read() 19 | return json_response 20 | 21 | 22 | def url_with_params(url, params): 23 | """Builds a full URL with encoded parameters.""" 24 | return url + "?" + urlencode(params) 25 | --------------------------------------------------------------------------------