This commit is contained in:
		
							
								
								
									
										20
									
								
								.drone.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								.drone.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
kind: pipeline
 | 
			
		||||
type: docker
 | 
			
		||||
name: default
 | 
			
		||||
 | 
			
		||||
steps:
 | 
			
		||||
  - name: code-analysis
 | 
			
		||||
    image: aosapps/drone-sonar-plugin
 | 
			
		||||
    settings:
 | 
			
		||||
      sonar_host:
 | 
			
		||||
        from_secret: SONAR_HOST
 | 
			
		||||
      sonar_token:
 | 
			
		||||
        from_secret: SONAR_CODE
 | 
			
		||||
 | 
			
		||||
  - name: ms-teams
 | 
			
		||||
    image: kuperiu/drone-teams
 | 
			
		||||
    settings:
 | 
			
		||||
      webhook:
 | 
			
		||||
        from_secret: TEAMS_WEBHOOK
 | 
			
		||||
    when:
 | 
			
		||||
      status: [ failure ]
 | 
			
		||||
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -128,4 +128,4 @@ dmypy.json
 | 
			
		||||
 | 
			
		||||
# Pyre type checker
 | 
			
		||||
.pyre/
 | 
			
		||||
 | 
			
		||||
.idea/
 | 
			
		||||
							
								
								
									
										110
									
								
								benchmark.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										110
									
								
								benchmark.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,110 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
 | 
			
		||||
import logging
 | 
			
		||||
import os
 | 
			
		||||
import sys
 | 
			
		||||
import time
 | 
			
		||||
import json
 | 
			
		||||
import datetime
 | 
			
		||||
import sentry_sdk
 | 
			
		||||
from sentry_sdk.integrations.logging import LoggingIntegration
 | 
			
		||||
import paho.mqtt.client
 | 
			
		||||
import requests
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
Main Entrypoint
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
__author__ = "@tormakris"
 | 
			
		||||
__copyright__ = "Copyright 2020, Birbnetes Team"
 | 
			
		||||
__module_name__ = "app"
 | 
			
		||||
__version__text__ = "1"
 | 
			
		||||
 | 
			
		||||
SENTRY_DSN = os.environ.get("SENTRY_DSN")
 | 
			
		||||
RELEASE_ID = os.environ.get("RELEASE_ID", "test")
 | 
			
		||||
RELEASEMODE = os.environ.get("RELEASEMODE", "dev")
 | 
			
		||||
 | 
			
		||||
DEVICE_ID = os.environ.get("DEVICE_ID", "devraspi")
 | 
			
		||||
 | 
			
		||||
MQTT_USERNAME = ""
 | 
			
		||||
MQTT_PASSWORD = ""
 | 
			
		||||
MQTT_HOSTNAME = ""
 | 
			
		||||
MQTT_PORT = ""
 | 
			
		||||
 | 
			
		||||
STARTTIME = time.time()
 | 
			
		||||
ENDTIME = time.time()
 | 
			
		||||
 | 
			
		||||
URL = "https://birb.k8s.kmlabz.com/sample"
 | 
			
		||||
FILE = 'CommonStarling_100962.wav'
 | 
			
		||||
 | 
			
		||||
sentry_logging = LoggingIntegration(
 | 
			
		||||
    level=logging.INFO,
 | 
			
		||||
    event_level=logging.ERROR
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
if SENTRY_DSN:
 | 
			
		||||
    sentry_sdk.init(
 | 
			
		||||
        dsn=SENTRY_DSN,
 | 
			
		||||
        integrations=[sentry_logging],
 | 
			
		||||
        traces_sample_rate=1.0,
 | 
			
		||||
        send_default_pii=True,
 | 
			
		||||
        release=RELEASE_ID,
 | 
			
		||||
        environment=RELEASEMODE,
 | 
			
		||||
        _experiments={"auto_enabling_integrations": True}
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def mqtt_on_connect(client, userdata, flags, rc):
 | 
			
		||||
    client.subscribe(f"command/benchmark-script")
 | 
			
		||||
    logging.info("Sending Message")
 | 
			
		||||
    files = {
 | 
			
		||||
        "file": (
 | 
			
		||||
            os.path.basename(FILE), open(FILE, 'rb').read(), 'audio/wave', {'Content-length': os.path.getsize(FILE)}),
 | 
			
		||||
        "description": (None, json.dumps({'date': datetime.now().isoformat(), 'device_id': '1'}), "application/json")
 | 
			
		||||
    }
 | 
			
		||||
    requests.post(URL, files=files)
 | 
			
		||||
    nonlocal STARTTIME
 | 
			
		||||
    STARTTIME = time.time()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def mqtt_on_command(client, userdata, message):
 | 
			
		||||
    try:
 | 
			
		||||
        msg = json.loads(message.payload.decode())
 | 
			
		||||
    except (UnicodeError, json.JSONDecodeError) as e:
 | 
			
		||||
        logging.error(f"MQTT Invalid message recieved: {e}")
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    if msg.get("command") == 'doAlert':
 | 
			
		||||
        nonlocal ENDTIME
 | 
			
		||||
        ENDTIME = time.time()
 | 
			
		||||
        elapsed = ENDTIME - STARTTIME
 | 
			
		||||
        logging.info(f"Elapsed time: {elapsed}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main() -> None:
 | 
			
		||||
    """
 | 
			
		||||
    Main function
 | 
			
		||||
    :return:
 | 
			
		||||
    """
 | 
			
		||||
    logging.basicConfig(stream=sys.stdout, format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s",
 | 
			
		||||
                        level=logging.DEBUG if '--debug' in sys.argv else logging.INFO)
 | 
			
		||||
 | 
			
		||||
    client = paho.mqtt.client.Client(client_id="benchmark-script")
 | 
			
		||||
    client.on_connect = mqtt_on_connect
 | 
			
		||||
    client.on_message = mqtt_on_command
 | 
			
		||||
 | 
			
		||||
    if MQTT_USERNAME:
 | 
			
		||||
        client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
 | 
			
		||||
 | 
			
		||||
    client.connect(MQTT_HOSTNAME, MQTT_PORT, 60)
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        client.loop_forever()
 | 
			
		||||
    except KeyboardInterrupt:
 | 
			
		||||
        logging.info("SIGINT recieved! Stopping...")
 | 
			
		||||
 | 
			
		||||
    client.disconnect()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
    main()
 | 
			
		||||
							
								
								
									
										3
									
								
								requirements.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								requirements.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
paho-mqtt
 | 
			
		||||
requests
 | 
			
		||||
sentry_sdk
 | 
			
		||||
		Reference in New Issue
	
	Block a user