Compare commits

..
124 Commits
Author SHA1 Message Date
brb ece4304306 added argocd manifests
pipeline / Test (push) Successful in 38s
2023-08-22 16:05:46 +02:00
brb 3d81eee094 added jenkinsfile
pipeline / Test (push) Successful in 35s
2023-07-26 12:09:01 +02:00
brb 448f757bd1 added copying of JDK 17 into image
pipeline / Test (push) Successful in 33s
Docker / Test (push) Successful in 30s
Docker / Publish (push) Successful in 27s
2023-07-24 16:24:01 +02:00
brb 89d29492e6 removed non utf8 character
pipeline / Test (push) Successful in 32s
2023-07-24 11:56:47 +02:00
brb fa191a02d3 installed packages
pipeline / Test (push) Successful in 32s
Docker / Test (push) Successful in 31s
Docker / Publish (push) Successful in 8s
2023-07-24 10:27:54 +02:00
brb 143c885eb8 changed default values
pipeline / Test (push) Successful in 31s
Docker / Test (push) Successful in 31s
Docker / Publish (push) Successful in 13s
2023-07-24 10:22:51 +02:00
brb 193292b254 added env var loading of user_id 2023-07-24 10:13:49 +02:00
brb 706ce7be9b removed unused variables 2023-07-24 10:11:06 +02:00
brb aeee735661 added mypy ignore
Docker / Test (push) Successful in 30s
Docker / Publish (push) Successful in 13s
pipeline / Test (push) Successful in 32s
2023-07-24 10:03:29 +02:00
brb c89cdd2653 added cronjob based timing
pipeline / Test (push) Failing after 32s
2023-07-24 09:59:46 +02:00
brb 375048b959 updated default values for testing 2023-07-24 09:59:24 +02:00
brb 118933af10 updated to install mypy type 2023-07-24 09:55:13 +02:00
brb 1939449a8f renamed default variable 2023-07-24 09:53:31 +02:00
brb 89cb193fe3 updated cronjob defaults 2023-07-24 09:38:40 +02:00
brb a3a692f2be added cronjob defaults 2023-07-24 09:37:51 +02:00
brb be69763f48 added cronjob decorator code 2023-07-24 09:37:41 +02:00
Brian Bjarke Jensen d13e069287 Merge pull request 'added separate flush interval' (#13) from main into release
Docker / Test (push) Successful in 39s
Docker / Publish (push) Successful in 13s
Reviewed-on: http://192.168.1.2:3000/brian/bandwidth_probing/pulls/13
2023-07-12 14:16:02 +02:00
brb bc0d2aced7 updated flake8 outputs
pipeline / Test (push) Successful in 39s
2023-07-12 14:11:35 +02:00
brb d872cfde43 flake8 compliant 2023-07-12 14:11:05 +02:00
brb 7a396a88fb updated workflow
pipeline / Test (push) Failing after 34s
2023-07-12 14:08:51 +02:00
brb 238188f4a5 introduced flake8 error 2023-07-12 14:08:37 +02:00
brb 63975169c0 mypy compliant
pipeline / Test (push) Successful in 38s
2023-07-12 14:04:10 +02:00
brb 387986e2d8 flake8 compliant 2023-07-12 14:03:41 +02:00
brb d5bcf04e61 updated readme
pipeline / Test (push) Failing after 38s
2023-07-12 14:01:06 +02:00
brb 12ccd1e586 added extra data flushing event with separate trigger time 2023-07-12 13:59:53 +02:00
brb e3fadbc22c added data_queue flush interval 2023-07-12 13:47:56 +02:00
brb ecfddfc69e added data_queue flush interval 2023-07-12 13:47:37 +02:00
brb caeace7907 updated to check for env var USER_ID 2023-07-12 13:46:01 +02:00
brb a456ccc536 updated to check for env var USER_ID 2023-07-12 13:35:40 +02:00
brb ae26182e23 updated default values 2023-07-12 13:26:12 +02:00
brb 652fe9af3a added checking for environment variables 2023-07-12 13:17:12 +02:00
Brian Bjarke Jensen 1760fc8136 Merge pull request 'added data queue' (#9) from main into release
Docker / Publish (push) Successful in 13s
Docker / Test (push) Successful in 39s
Reviewed-on: http://192.168.1.2:3000/brian/bandwidth_probing/pulls/9
2023-07-12 12:59:05 +02:00
brb cffcac8b37 added data queue
pipeline / Test (push) Successful in 39s
2023-07-12 12:54:52 +02:00
brb 907958484c removed unused step
pipeline / Test (push) Successful in 39s
Docker / Test (push) Successful in 39s
Docker / Publish (push) Successful in 8s
2023-07-12 12:08:42 +02:00
brb 9a1724742a added code to extract version number
Docker / Test (push) Successful in 39s
Docker / Publish (push) Successful in 7s
2023-07-12 11:54:31 +02:00
brb 20023ca9a9 added code to print version number
Docker / Publish (push) Failing after 6s
Docker / Test (push) Successful in 39s
2023-07-12 11:49:00 +02:00
brb 478d258a31 added code to extract version number
Docker / Test (push) Successful in 40s
Docker / Publish (push) Failing after 7s
2023-07-12 11:45:18 +02:00
brb eb8b1456ab added code to extract version number
Docker / Test (push) Successful in 40s
Docker / Publish (push) Failing after 5s
2023-07-12 11:39:44 +02:00
brb 4c99b6a1af testing how to set env vars
Docker / Test (push) Successful in 38s
Docker / Publish (push) Failing after 4s
2023-07-12 11:32:50 +02:00
brb 0becddf81f added code to extract version number
Docker / Test (push) Successful in 38s
Docker / Publish (push) Failing after 5s
2023-07-12 11:29:26 +02:00
brb 233c0ef977 added code to extract version number
Docker / Test (push) Successful in 40s
Docker / Publish (push) Failing after 5s
2023-07-12 11:27:46 +02:00
brb 11e3c866de added code to extract version number
Docker / Publish (push) Failing after 5s
Docker / Test (push) Successful in 39s
2023-07-12 11:06:29 +02:00
brb ce43e1fa38 added code to extract version number
Docker / Test (push) Successful in 39s
Docker / Publish (push) Failing after 5s
2023-07-12 11:01:40 +02:00
brb 9c9722615f added code to extract version number
Docker / Test (push) Successful in 38s
Docker / Publish (push) Failing after 5s
2023-07-12 10:42:15 +02:00
brb 9ad983cc1c added code to extract version number
Docker / Test (push) Successful in 38s
Docker / Publish (push) Failing after 5s
2023-07-12 10:27:41 +02:00
brb 10821f94df added code to extract version number
Docker / Test (push) Successful in 40s
Docker / Publish (push) Failing after 4s
2023-07-12 10:24:43 +02:00
brb 550551c4a0 updated to only run when tag is provided
Docker / Test (push) Successful in 40s
Docker / Publish (push) Failing after 6s
2023-07-12 10:08:13 +02:00
brb 5709c7152a updated to only run when tag is provided to release branch
pipeline / Test (push) Successful in 52s
Docker / Test (push) Successful in 38s
Docker / Publish (push) Failing after 6s
2023-07-11 18:41:05 +02:00
brb a8b27d8f21 ignored pushes to release branch without tag
pipeline / Test (push) Successful in 39s
2023-07-11 18:33:15 +02:00
brb 2cd449e130 renamed workflow file
Docker / Publish (push) Failing after 6s
Docker / Test (push) Successful in 39s
2023-07-11 18:24:06 +02:00
brb 91b22a781c renamed workflow file 2023-07-11 18:23:37 +02:00
brb 6f7201054b ignored branch release 2023-07-11 18:22:23 +02:00
brb 697973a454 added automatic detection of version number
Docker / Test (push) Successful in 41s
Docker / Publish (push) Failing after 5s
pipeline / Test (push) Successful in 39s
2023-07-11 18:11:52 +02:00
brb 77c749cfb8 renamed workflow
Docker / Test (push) Successful in 41s
Docker / Publish (push) Successful in 8s
pipeline / Test (push) Successful in 39s
2023-07-11 18:04:56 +02:00
brb aa358bd361 added workflow to build and publish container image
pipeline / Test (push) Successful in 39s
2023-07-11 18:01:31 +02:00
brb 3ecea06979 commented out publishing steps 2023-07-11 18:01:07 +02:00
brb 6a48b900c8 updated publish step commands
pipeline / Test (push) Successful in 38s
pipeline / Publish (push) Successful in 6s
2023-07-11 17:36:02 +02:00
brb 4fff9100e2 updated publish step commands
pipeline / Test (push) Successful in 38s
pipeline / Publish (push) Successful in 5s
2023-07-11 17:33:51 +02:00
brb a8dae35cd0 updated build step
pipeline / Test (push) Successful in 39s
pipeline / Publish (push) Successful in 6s
2023-07-11 17:27:47 +02:00
brb b62fb0c4fa removed unused step 2023-07-11 17:26:03 +02:00
brb 2d9b06be5d updated build step
pipeline / Show Gitea.ref (push) Successful in 4s
pipeline / Test (push) Successful in 38s
pipeline / Publish (push) Failing after 4s
2023-07-11 17:25:28 +02:00
brb 5280cd1d8b updated version number
pipeline / Show Gitea.ref (push) Successful in 4s
pipeline / Test (push) Successful in 37s
pipeline / Publish (push) Successful in 6s
2023-07-11 17:19:29 +02:00
brb e0ac9bb0cc changed publish workflow
pipeline / Show Gitea.ref (push) Successful in 4s
pipeline / Publish (push) Failing after 10s
pipeline / Test (push) Successful in 38s
2023-07-11 17:14:15 +02:00
brb c3203f024b added echo cmd
pipeline / Test (push) Successful in 39s
pipeline / Publish (push) Has been skipped
pipeline / Show Gitea.ref (push) Successful in 4s
2023-07-11 17:04:05 +02:00
brb 65621ed484 updated deploy job steps
pipeline / Deploy (push) Has been skipped
pipeline / Test (push) Successful in 38s
2023-07-11 16:46:51 +02:00
brb 29dd4543c3 updated deploy job steps
pipeline / Test (push) Successful in 1m2s
pipeline / Deploy (push) Failing after 5s
2023-07-11 16:39:49 +02:00
brb ef416de1a7 updated deploy job steps
pipeline / Deploy (push) Failing after 6s
pipeline / Test (push) Successful in 38s
2023-07-11 16:35:21 +02:00
brb d16c9aa5bc changed command
pipeline / Test (push) Successful in 39s
pipeline / Deploy (push) Successful in 5s
2023-07-11 15:55:38 +02:00
brb 455d82d0f0 removed comment
pipeline / demo (push) Successful in 6s
pipeline / Test (push) Successful in 39s
pipeline / Deploy (push) Successful in 49s
2023-07-11 15:41:15 +02:00
brb 182a2f70b6 added docker build command
pipeline / demo (push) Successful in 6s
pipeline / Test (push) Successful in 38s
pipeline / Deploy (push) Successful in 3s
2023-07-11 15:37:35 +02:00
brb a81e2a89c9 added demo job
pipeline / demo (push) Successful in 6s
pipeline / Test (push) Successful in 38s
pipeline / Deploy (push) Has been skipped
2023-07-11 15:33:24 +02:00
brb 2041d42e76 changed indentation
pipeline / Deploy (push) Has been skipped
pipeline / Test (push) Successful in 39s
2023-07-11 15:30:01 +02:00
brb c7c3631d8d added command
pipeline / Test (push) Successful in 38s
pipeline / Deploy (push) Successful in 4s
2023-07-11 15:23:27 +02:00
brb 2cdc184a1b added definition
pipeline / Test (push) Successful in 39s
pipeline / Deploy (push) Has been skipped
2023-07-11 15:08:54 +02:00
brb 2394bb856d updated script
pipeline / Test (push) Successful in 38s
pipeline / Deploy (push) Successful in 2s
2023-07-11 15:04:45 +02:00
brb 65a693706a updated deploy workflow
pipeline / Test (push) Successful in 39s
pipeline / Deploy (push) Successful in 3s
2023-07-11 15:02:55 +02:00
brb 1f716aeb21 updated flake8 outputs
pipeline / Test (push) Successful in 38s
pipeline / Deploy (push) Has been skipped
2023-07-11 14:32:29 +02:00
brb 2889601bc3 removed unused lines
pipeline / Test (push) Successful in 42s
pipeline / Deploy (push) Has been skipped
2023-07-11 14:22:14 +02:00
brb 1341fc5597 added runs-on
Python application / Test (push) Successful in 40s
Python application / Deploy (push) Has been skipped
2023-07-11 14:01:38 +02:00
brb 0b4413ce9c updated build step
Python application / Deploy (push) Successful in 2s
Python application / Test (push) Successful in 39s
2023-07-11 13:42:23 +02:00
brb a387e5894f included deploy step for release branch
Python application / Test (push) Successful in 38s
Python application / Deploy (push) Successful in 2s
2023-07-11 12:51:35 +02:00
brb 9668646a05 renamed run name
Python application / test (push) Successful in 39s
2023-07-11 11:27:07 +02:00
brb e6f0242775 added type checking 2023-07-11 11:25:00 +02:00
brb 35e40d7b98 added use of default values 2023-07-11 11:24:33 +02:00
brb 0a2fc865b1 updated default values 2023-07-11 11:08:17 +02:00
brb 212f8179d5 added use of default values 2023-07-11 11:08:05 +02:00
brb 7d5d2f52bd removed unused variable 2023-07-11 10:44:49 +02:00
brb ed70b9808b removed unused imports 2023-07-11 10:43:15 +02:00
brb cf1aab7caf added use of default values 2023-07-11 10:43:05 +02:00
brb e39c85327f added default values 2023-07-11 10:42:49 +02:00
brb 3f6e15c6e1 added mypy config 2023-07-11 10:21:40 +02:00
brb 3c1d93726e updated workflow
Python application / build (push) Successful in 31s
2023-07-11 10:04:06 +02:00
brb 4ccb357f7c fixed linting 2023-07-11 10:03:49 +02:00
brb cfbf544110 fixed linting 2023-07-11 10:00:44 +02:00
brb 5e5fb31767 removed unused imports 2023-07-11 09:59:34 +02:00
brb d4e5728015 added logging 2023-07-11 09:59:24 +02:00
brb a9818001d5 corrected linting 2023-07-11 09:58:09 +02:00
brb b51460b696 added logging 2023-07-11 09:57:38 +02:00
brb 056fe85d7c changed linting function call
Python application / build (push) Failing after 30s
2023-07-11 09:47:59 +02:00
brb 19de6354c5 specified image
Python application / build (push) Successful in 1m29s
2023-07-10 19:43:25 +02:00
brb 71946eab59 specified ubuntu version
Python application / build (push) Failing after 6s
2023-07-10 19:41:42 +02:00
brb e7073c03bd specified platform version
Python application / build (push) Failing after 6s
2023-07-10 19:40:11 +02:00
brb 3dc18eb8bd specified python version
Python application / build (push) Failing after 5s
2023-07-10 19:33:35 +02:00
brb f928346bda changed dependency to go directly to github
Python application / build (push) Failing after 20s
2023-07-10 19:31:58 +02:00
brb 83f1baae79 updated workflow
Python application / build (push) Failing after 3s
2023-07-10 19:28:40 +02:00
brb 43f7ff7822 removed unused file 2023-07-10 19:27:04 +02:00
brb bfb3aedfb5 updated workflow
Python application / build (push) Failing after 3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
2023-07-10 19:26:23 +02:00
brb 93451908df updated workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
Python application / build (push) Failing after 2s
2023-07-10 19:23:47 +02:00
brb 5bc9e78246 updated workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
Python application / Build-Service (push) Failing after 0s
2023-07-10 19:21:20 +02:00
brb c5511258c8 updated workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
Python application / Build-Service (push) Failing after 4s
2023-07-10 19:20:07 +02:00
brb a4806e2da7 updated workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
Python application / Build-Service (push) Failing after 4s
2023-07-10 19:18:30 +02:00
brb 61e902163c updated workflow
Python application / Build-Service (push) Failing after 3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
2023-07-10 19:16:16 +02:00
brb 9f055f0921 updated workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
Python application / Build-Service (push) Failing after 2s
2023-07-10 19:12:26 +02:00
brb 6e46747613 updated workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 6s
2023-07-10 19:09:14 +02:00
brb 932ac8afa1 added pep8 check pipeline workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
2023-07-10 18:51:13 +02:00
brb 69ad639fe6 added demo workflow
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 52s
2023-07-10 18:24:56 +02:00
brb f0caebbfda defined build of container 2023-07-10 16:46:53 +02:00
brb 3002990e43 defined build of container 2023-07-10 16:46:46 +02:00
brb 7a35d300e0 installed packages 2023-07-10 16:46:33 +02:00
brb 599e0e376e updated service description 2023-07-10 16:46:19 +02:00
brb b02c6d240f added code to run main loop of service 2023-07-10 16:45:49 +02:00
brb 2b2dd14493 added code to initialise service 2023-07-10 16:45:37 +02:00
brb 40b8b2aac6 added code to interact with database 2023-07-10 16:45:24 +02:00
brb b3ee0a5cc6 added function to measure bandwidth 2023-07-10 16:45:01 +02:00
17 changed files with 621 additions and 1 deletions
+30
View File
@@ -0,0 +1,30 @@
name: pipeline
run-name: ${{ gitea.actor }} is running script
runs-on: ubuntu-latest
on:
push:
branches-ignore:
- "release"
image: python:3.10
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: https://github.com/actions/setup-python@v3
with:
python-version: "3.10"
architecture: "x64"
- name: Install Packages
run: |
python -m pip install --upgrade pip
pip install flake8 mypy pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: PEP8 check
run: flake8 ./code --benchmark
- name: Type check
run: |
python3 -m pip install types-python-dateutil
mypy ./code
+54
View File
@@ -0,0 +1,54 @@
name: Docker
run-name: Build and Publish Service
runs-on: ubuntu-latest
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: https://github.com/actions/setup-python@v3
with:
python-version: "3.10"
architecture: "x64"
- name: Install Packages
run: |
python -m pip install --upgrade pip
pip install flake8 mypy pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: PEP8 check
run: flake8 ./code --benchmark
- name: Type check
run: |
python3 -m pip install types-python-dateutil
mypy ./code
publish:
name: Publish
runs-on: ubuntu-latest
needs: [test]
steps:
- uses: actions/checkout@v3
-
name: Set Environment Variables
id: vars
env:
repository: ${{gitea.repository}}
ref_name: ${{gitea.ref_name}}
run: |
echo $repository
echo ::set-output name=OWNER::${repository#/*}
echo ::set-output name=PROJECT::${repository#*/}
echo $ref_name
echo ::set-output name=TAG::${ref_name#refs/*/}
-
name: Push Image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: 192.168.1.2:5000/${{gitea.repository}}:${{steps.vars.outputs.TAG}}
+54
View File
@@ -0,0 +1,54 @@
###########
# BUILDER #
###########
# pull official base image
FROM python:3.10-bookworm as BUILDER
# install JDK 17
ENV JAVA_HOME=/opt/java/openjdk
COPY --from=eclipse-temurin:17-jre $JAVA_HOME $JAVA_HOME
ENV PATH="${JAVA_HOME}/bin:${PATH}"
# install Ookla's speedtest
RUN apt update && \
apt install -y speedtest-cli
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --upgrade pip && \
pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
#########
# FINAL #
#########
FROM python:3.10-bookworm
# create home direcroty and app user
RUN mkdir -p /home/app && \
addgroup --system app && \
adduser --system --group app
# install packages from builder
COPY --from=BUILDER /usr/src/app/wheels /wheels
COPY --from=BUILDER /usr/src/app/requirements.txt requirements.txt
RUN pip install --no-cache /wheels/*
# copy in files
WORKDIR /home/app
COPY ./code .
# change ownership to app user
RUN chown -R app:app /home/app
# change to the app user
USER app
# start execution
ENTRYPOINT [ "python", "main.py" ]
Vendored
+21
View File
@@ -0,0 +1,21 @@
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying..'
}
}
}
}
+19 -1
View File
@@ -1,3 +1,21 @@
# bandwidth_probing
Service that runs in a container and measures network bandwidth at a specified interval. The results are sent to a PostgreSQL database.
Service that runs in a container and measures network bandwidth at a specified interval. The results are sent to a MongoDB database. Logs are sent to discord channel.
## Environment Variables
```python
LOGGER_LEVEL: Literal['debug', 'info', 'warning', 'error', 'critical'] = 'debug'
USER_ID: int = 0 # id number 0 is reserved for testing
REPLICATES: int = 3 # number of replicates to be measured per event
TRIGGER_INTERVAL_SECONDS: float = 3600 # seconds between events
DATA_QUEUE_FLUSH_INTERVAL_SECONDS: float = 600 # seconds between attempts to push data stuck in data queue
DB_IP_ADDRESS: str = '192.168.1.2'
DB_NAME: str = 'bandwidth_probing'
DB_COLLECTION_NAME: str = 'data'
DISCORD_SERVICE_NAME: str = 'bandwidth_probing
DISCORD_WEBHOOK_URL: str = 'https://discord.com/api/webhooks/1127970367047225354/wNkoRex4OncMw11OCJg6atR_xgHS2VhLrku4jYIUw81Kr4sutdD3Tt-XAltG0UQy14rZ'
DISCORD_LOGGER_LEVEL: Literal['debug', 'info', 'warning', 'error', 'critical'] = 'warning'
```
+8
View File
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: bandwidth-probing
data:
USER_ID: "1"
SCHEDULE: "05 * * * *"
+37
View File
@@ -0,0 +1,37 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: bandwidth-probing
namespace: bandwidth-probing
labels:
environment: live
app: bandwidth-probing
spec:
replicas: 1
selector:
matchLabels:
app: bandwidth-probing
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
labels:
app: bandwidth-probing
spec:
containers:
- envFrom:
- configMapRef:
name: app-config
image: '192.168.1.2:5000/brian/bandwidth_probing:v1.3.2'
imagePullPolicy: Always
name: bandwidth-probing
resources:
limits:
cpu: 500m
memory: 256Mi
requests:
cpu: 50m
memory: 64Mi
+4
View File
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: bandwidth-probing
+23
View File
@@ -0,0 +1,23 @@
from speedtest import Speedtest
from dotenv import load_dotenv
import logging
def measure():
"""
Measure bandwidth usiing Ookla/speedtest.
"""
s = Speedtest()
s.get_servers(servers=[])
s.get_best_server()
s.download(threads=None)
s.upload(threads=None)
s.results.share()
logging.debug('finished')
return s.results.dict()
if __name__ == '__main__':
load_dotenv()
res = measure()
print(res)
+53
View File
@@ -0,0 +1,53 @@
from cron_converter import Cron
from configparser import ConfigParser
from pathlib import Path
from datetime import datetime
import dateutil
import time
import functools
import logging
# load default values
config = ConfigParser()
config.read(Path(__file__).parent / 'defaults.ini')
SCHEDULE = config.get('cron', 'schedule')
TZ = config.get('cron', 'tz')
def cronjob(
schedule: str = SCHEDULE,
tz: str = TZ
):
"""
decorator for running function as a cronjob.
"""
logging.info(f'started cronjob with schedule {schedule} and tz {tz}')
def decorator_cronjob(func):
@functools.wraps(func)
def wrapper_cronjob(*args, **kwargs):
scheduler = Cron(schedule).schedule(timezone_str=tz)
trigger_time = scheduler.next()
while True:
while trigger_time > datetime.now(tz=dateutil.tz.gettz(tz)):
# waiting loop
time.sleep(1)
# actual execution
try:
func(*args, **kwargs)
except Exception as e:
logging.warning(f'failed executing {func}: {e}')
raise
else:
# update trigger time
trigger_time = scheduler.next()
return wrapper_cronjob
return decorator_cronjob
if __name__ == '__main__':
@cronjob(schedule='* * * * *', tz=TZ)
def test_loop(msg: str):
print(msg)
test_loop('hello world')
+50
View File
@@ -0,0 +1,50 @@
from pymongo import MongoClient
from dotenv import load_dotenv
from configparser import ConfigParser
from pathlib import Path
import logging
import os
# load default values
config = ConfigParser()
config.read(Path(__file__).parent / 'defaults.ini')
DB_IP_ADDRESS = config.get('database', 'db_ip_address')
DB_NAME = config.get('database', 'db_name')
DB_COLLECTION_NAME = config.get('database', 'db_collection_name')
def connect(
ip_addr: str = DB_IP_ADDRESS,
db_name: str = DB_NAME,
collection_name: str = DB_COLLECTION_NAME
):
"""
Connect to MongoDB database and return collection.
"""
# load environment variables
load_dotenv()
ip_addr = os.getenv(
'DB_IP_ADDRESS',
default=DB_IP_ADDRESS
)
db_name = os.getenv(
'DB_NAME',
default=DB_NAME
)
collection_name = os.getenv(
'DB_COLLECTION_NAME',
default=DB_COLLECTION_NAME
)
# connect to database
client: MongoClient = MongoClient(ip_addr)
db = client[db_name]
collection = db[collection_name]
logging.debug('finished')
return collection
if __name__ == '__main__':
load_dotenv()
collection = connect()
print(collection)
+18
View File
@@ -0,0 +1,18 @@
[main]
logger_level=debug
user_id=0
replicates=3
[database]
db_ip_address=192.168.1.2
db_name=bandwidth_probing
db_collection_name=data
[discord]
webhook_url=https://discord.com/api/webhooks/1127970367047225354/wNkoRex4OncMw11OCJg6atR_xgHS2VhLrku4jYIUw81Kr4sutdD3Tt-XAltG0UQy14rZ
service_name=bandwidth_probing
logger_level=warning
[cron]
schedule=05 * * * *
tz=Europe/Paris
+71
View File
@@ -0,0 +1,71 @@
from discord_logging.handler import DiscordHandler
from dotenv import load_dotenv
from configparser import ConfigParser
from pathlib import Path
import logging
import os
# load default values
config = ConfigParser()
config.read(Path(__file__).parent / 'defaults.ini')
LOGGER_LEVEL = config.get('main', 'logger_level')
DISCORD_SERVICE_NAME = config.get('discord', 'service_name')
DISCORD_WEBHOOK_URL = config.get('discord', 'webhook_url')
DISCORD_LOGGER_LEVEL = config.get('discord', 'logger_level')
def initialise_app(
logger_level: str = LOGGER_LEVEL,
discord_service_name: str = DISCORD_SERVICE_NAME,
discord_webhook_url: str = DISCORD_WEBHOOK_URL,
discord_logger_level: str = DISCORD_LOGGER_LEVEL
) -> None:
"""
Convienience function that ensures eveything is ready
before running the main loop.
"""
# load environment variables
load_dotenv()
assert (
'USER_ID' in os.environ
), (
'environment variable USER_ID must be specified'
)
logger_level = os.getenv(
'LOGGER_LEVEL',
default=LOGGER_LEVEL
)
discord_service_name = os.getenv(
'DISCORD_SERVICE_NAME',
default=DISCORD_SERVICE_NAME
)
discord_webhook_url = os.getenv(
'DISCORD_WEBHOOK_URL',
default=DISCORD_WEBHOOK_URL
)
discord_logger_level = os.getenv(
'DISCORD_LOGGER_LEVEL',
default=DISCORD_LOGGER_LEVEL
)
# setup logging
fmt = '|%(asctime)s| %(levelname)s %(filename)s:%(funcName)s:%(message)s'
datefmt = '%Y-%m-%d %H:%M:%S'
level = getattr(logging, logger_level.upper())
logging.basicConfig(format=fmt, datefmt=datefmt, level=level)
# setup logging to discord
logger = logging.getLogger()
discord_handler = DiscordHandler(
service_name=discord_service_name,
webhook_url=discord_webhook_url,
)
discord_handler.setFormatter(logging.Formatter('%(message)s'))
level = getattr(logging, discord_logger_level.upper())
discord_handler.setLevel(level=level)
logger.addHandler(discord_handler)
logging.debug('finished')
if __name__ == '__main__':
initialise_app()
logging.warning('test message')
+132
View File
@@ -0,0 +1,132 @@
import time
import logging
import os
import json
from configparser import ConfigParser
from pathlib import Path
from queue import Queue
from initialise_app import initialise_app
from cron import cronjob
from bandwidth import measure as measure_bandwidth
from database import connect
# load default values
config = ConfigParser()
config.read(Path(__file__).parent / 'defaults.ini')
USER_ID = int(config.get('main', 'user_id'))
REPLICATES = int(config.get('main', 'replicates'))
SCHEDULE = config.get('cron', 'schedule')
TZ = config.get('cron', 'tz')
def flush_data(
data_queue: Queue,
user_id: int
) -> None:
# connect to database
db = connect()
# stop early
queue_size = data_queue.qsize()
if queue_size == 0:
logging.debug('queue already empty')
logging.debug('finished')
return
# flush queue
for i in range(queue_size):
# get data
data = data_queue.get()
# prepare payload
payload_dict = {
'user_id': user_id,
'data': data
}
# send to database
try:
db_id = db.insert_one(payload_dict).inserted_id
except Exception as e:
data_queue.put(data) # put data back in queue
logging.error('failed sending data to database')
logging.debug(
'failed pushing to database:\n'
f'{json.dumps(payload_dict, indent=4)}\n'
f'with error: {e}'
)
continue
else:
logging.debug(f'data sent to database received id: {db_id}')
logging.debug('finished')
def event(
data_queue: Queue,
user_id: int,
replicates: int = REPLICATES
) -> None:
# run event
for rep_num in range(replicates):
logging.debug(f'running replicate {rep_num+1} of {replicates}')
# do measurement
try:
data = measure_bandwidth()
except Exception as e:
logging.error(f'failed to measure bandwidth: {e}')
continue
# add data to queue
data_queue.put(data)
# polite pause
time.sleep(1)
# flush queue
flush_data(
data_queue=data_queue,
user_id=user_id
)
logging.debug('finished')
if __name__ == '__main__':
# setup
initialise_app()
data_queue: Queue = Queue()
user_id = int(os.getenv('USER_ID', default=USER_ID))
replicates = int(os.getenv('REPLICATES', default=REPLICATES))
schedule = os.getenv(
key='SCHEDULE',
default=SCHEDULE
)
tz = os.getenv(
key='TZ',
default=TZ
)
@cronjob(schedule=schedule, tz=tz)
def main_loop(
data_queue: Queue,
user_id: int,
replicates: int = REPLICATES
) -> None:
# run event
for rep_num in range(replicates):
logging.debug(f'running replicate {rep_num+1} of {replicates}')
# do measurement
try:
data = measure_bandwidth()
except Exception as e:
logging.error(f'failed to measure bandwidth: {e}')
continue
# add data to queue
data_queue.put(data)
# polite pause
time.sleep(1)
# flush queue
flush_data(
data_queue=data_queue,
user_id=user_id
)
logging.debug('finished')
# start main loop
main_loop(
data_queue=data_queue,
user_id=user_id,
replicates=replicates
)
+8
View File
@@ -0,0 +1,8 @@
version: "3.9"
services:
bandwidth-probing:
build: .
container_name: bandwidth-probing
restart: unless-stopped
env_file:
- .env
+10
View File
@@ -0,0 +1,10 @@
[mypy]
[mypy-discord_logging.*]
ignore_missing_imports = True
[mypy-speedtest.*]
ignore_missing_imports = True
[mypy-cron_converter.*]
ignore_missing_imports = True
+29
View File
@@ -0,0 +1,29 @@
aiosignal==1.3.1
async-timeout==4.0.2
attrs==23.1.0
certifi==2023.5.7
charset-normalizer==3.2.0
cron-converter==1.0.2
discord-webhook==1.1.0
dnspython==2.3.0
flake8==6.0.0
frozenlist==1.3.3
idna==3.4
mccabe==0.7.0
multidict==6.0.4
mypy==1.4.1
mypy-extensions==1.0.0
pycodestyle==2.10.0
pyflakes==3.0.1
pymongo==4.4.0
python-dateutil==2.8.2
python-dotenv==1.0.0
python-logging-discord-handler==0.1.4
requests==2.31.0
six==1.16.0
speedtest-cli==2.1.3
tomli==2.0.1
types-python-dateutil==2.8.19.14
typing_extensions==4.7.1
urllib3==2.0.3
yarl==1.9.2