跳转到主要内容

用于管理从WIS2.0订阅和下载的Python包

项目描述

WIS2 Downloader

订阅WIS2网络最新数据的后端工具

License Badge Super-Linter Unit-Tests

WIS2 Downloader是一个基于Flask的Python应用程序,允许您连接到WIS2全球代理,管理主题层次结构的订阅,并配置它们相关的下载目录。

功能

  • 动态订阅管理:无需重新启动服务或更改配置文件,即可快速添加或删除订阅。
  • 监控下载统计信息:通过/metrics端点访问Prometheus指标,非常适合Grafana可视化
  • 多线程支持:配置下载工作进程的数量以提高数据下载效率。

入门指南

1. 安装

python -m pip install wis2downloader

2. 配置

在您的本地目录中创建一个名为config.json的文件,该文件符合以下模式

schema:
  type: object
  properties:
    base_url:
      type: string
      description:
        Base URL for the wis2downloader service.
      example: http://localhost:5050
    broker_hostname:
      type: string
      description: The hostname of the global broker to subscribe to.
      example: globalbroker.meteo.fr
    broker_password:
      type: string
      description: The password to use when connecting to the specified global broker.
      example: everyone
    broker_port:
      type: number
      description: The port the global broker is using for the specified protocol.
      example: 443
    broker_protocol:
      type: string
      description: The protocol (websockets or tcp) to use when connecting to the global broker.
      example: websockets
    broker_username:
      type: string
      description: The username to use when connecting to the global broker.
      example: everyone
    download_workers:
      type: number
      description: The number of download worker threads to spawn.
      example: 1
    download_dir:
      type: string
      description: The path to download data to on the server/computer running the wis2downloader.
      example: ./downloads
    flask_host:
      type: string
      description: Network interface on which flask should listen when run in dev mode.
      example: 0.0.0.0
    flask_port:
      type: number
      description: The port on which flask should listen when run in dev mode.
      example: 5050
    log_level:
      type: string
      description: Log level to use
      example: DEBUG
    log_path:
      type: string
      description: Path to write log files to.
      example: ./logs
    min_free_space:
      type: number
      description:
        Minimum free space (GB) to leave on download volume / disk after download.
        Files exceeding limit will not be saved.
      example: 10
    save_logs:
      type: boolean
      description: Write log files to disk (true) or stdout (false)
      example: false
    mqtt_session_info:
      type: string
      description:
        File to save session information (active subscriptions and MQTT client id) to.
        Used to persist subscriptions on restart.
      example: mqtt_session.json
    validate_topics:
      type: boolean
      description: Whether to validate the specified topic against the published WIS2 topic hierarchy.
      example: true

以下是一个示例

{
    "base_url": "http://localhost:5050",
    "broker_hostname": "globalbroker.meteo.fr",
    "broker_password": "everyone",
    "broker_port": 443,
    "broker_protocol": "websockets",
    "broker_username": "everyone",
    "download_workers": 1,
    "download_dir": "downloads",
    "flask_host": "0.0.0.0",
    "flask_port": 5050,
    "log_level": "DEBUG",
    "log_path": "logs",
    "min_free_space": 10,
    "mqtt_session_info" : "mqtt_session.json",
    "save_logs": false,
    "validate_topics": true
}

3. 运行

  1. 设置一个环境变量,指定config.json文件的路径。

Linux (bash)

export WIS2DOWNLOADER_CONFIG=<path_to_your_config_file>

Windows (命令提示符)

set WIS2DOWNLOADER_CONFIG=<path_to_your_config_file>

Windows (PowerShell)

$env:WIS2DOWNLOADER_CONFIG = <path_to_your_config_file>
  1. 启动下载器

开发模式(Windows和Linux)

wis2downloader

使用gunicorn(仅限Linux)

gunicorn --bind 0.0.0.0:5050 -w 1 wis2downloader.app:app

注意:由于下载器会启动额外的线程和持久化MQTT连接,因此仅支持一个工作进程。

Flask应用程序现在应该正在运行。如果您需要停止应用程序,可以在终端中使用Ctrl+C进行操作。

维护和监控订阅

下载器的API定义可以在本地运行时的/swagger端点找到,请参阅[http://localhost:5050/swagger]。这包括尝试不同的端点。

添加订阅

可以通过向/subscriptions端点发送POST请求来添加订阅。请求体应该是JSON编码的,并遵循以下模式

schema:
  type: object
  properties:
    topic:
      type: string
      description: The WIS2 topic to subscribe to
      example: cache/a/wis2/+/data/core/weather/surface-based-observations/#
    target:
      type: string
      description: Sub directory to save data to
      example: surface-obs
  required:
    - topic

在此示例中,将订阅从任何WIS2中心发布的所有通知到surface-based-observations主题,并将下载的数据写入download_dirsurface-obs子目录。

注意

  1. 如果未指定target,则默认为发布数据的主题。
  2. 使用+通配符来指定单级上的任何匹配,如上面的示例中匹配WIS2中心。
  3. 使用#通配符匹配出现在或低于该级别的任何主题。在上面的示例中,任何在cache/a/wis2/+/data/core/weather/surface-based-observations以下发布的主题都将匹配。

添加订阅的示例CURL命令

curl -X 'POST' \
  'http://127.0.0.1:5050/subscriptions' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
      "topic": "cache/a/wis2/+/data/core/weather/surface-based-observations/#",
      "target": "surface-obs"
  }'

删除订阅

可以通过向/subscriptions/{topic}端点发送DELETE请求来删除订阅,其中{topic}是要取消订阅的主题。

删除订阅的示例CURL命令

curl -X DELETE http://localhost:5050/subscriptions/cache/a/wis2/%2B/data/core/weather/%23

此操作取消了cache/a/wis2/+/data/core/weather/#订阅。注意需要对+%2B)和#%23)符号进行URL编码。

列出订阅

可以通过对/subscriptions端点发送GET请求来列出当前订阅。

列出订阅的示例CURL命令

curl http://localhost:5050/subscriptions

应该以JSON对象的形式返回活动订阅的列表。

查看下载指标

通过向/metrics端点发送GET请求可以找到下载器的Prometheus指标。

查看下载指标的示例CURL命令

curl http://localhost:5050/metrics

错误和问题

所有错误、增强和问题都在GitHub上管理。

联系方式

项目详情


下载文件

下载适用于您平台文件的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

wis2downloader-0.3.1.tar.gz (28.1 KB 查看哈希值

上传时间

构建分布

wis2downloader-0.3.1-py3-none-any.whl (29.0 KB 查看哈希值

上传时间 Python 3

由以下组织支持