{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "acc6aa2a-0f1d-43f3-985d-cd37c4a5ac6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output:\n",
      "Program starts execution!\n",
      "args: Namespace(col_rp='实际功率', col_tp='理论功率', mongodb_connection='mongodb://root:Jydl3377@192.168.1.33:27017', mongodb_database='algorithm_platform', mongodb_read_table='J00260NWP_agcavc', mongodb_write_table='J00260NWP_machines')\n",
      "threshold 25.962767129908098\n",
      "Figure(30000x600)\n",
      "Collection 'J00260NWP_machines already exist, deleted successfully!\n",
      "data inserted successfully!\n",
      "Program execution ends!\n",
      "\n",
      "Error:\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import subprocess\n",
    "\n",
    "# 构建命令\n",
    "command = [\n",
    "    \"python\", \"processing_limit_power_by_machines.py\",\n",
    "    \"--col_rp\", \"实际功率\",\n",
    "    \"--col_tp\", \"理论功率\",\n",
    "    \"--mongodb_connection\", \"mongodb://root:Jydl3377@192.168.1.33:27017\",\n",
    "    \"--mongodb_database\", \"algorithm_platform\",\n",
    "    \"--mongodb_read_table\", \"J00260NWP_agcavc\",\n",
    "    \"--mongodb_write_table\", \"J00260NWP_machines\"\n",
    "]\n",
    "\n",
    "# 使用 subprocess.run 来执行命令\n",
    "result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)\n",
    "\n",
    "# 输出结果和错误信息\n",
    "print(\"Output:\")\n",
    "print(result.stdout)\n",
    "print(\"Error:\")\n",
    "print(result.stderr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ba8d36c7-cae7-427b-9980-ad8dadc442b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>_id</th>\n",
       "      <th>C_TIME</th>\n",
       "      <th>理论功率</th>\n",
       "      <th>第1个设备轮毂风速</th>\n",
       "      <th>实际功率</th>\n",
       "      <th>实时环境数据</th>\n",
       "      <th>预测功率</th>\n",
       "      <th>NWP预测总辐射</th>\n",
       "      <th>NWP-10米预测风速</th>\n",
       "      <th>NWP-10米预测风向</th>\n",
       "      <th>...</th>\n",
       "      <th>NWP-低云量</th>\n",
       "      <th>NWP-中云量</th>\n",
       "      <th>NWP-高云量</th>\n",
       "      <th>NWP-总云量</th>\n",
       "      <th>NWP-晴空总辐射</th>\n",
       "      <th>NWP-法相直辐射</th>\n",
       "      <th>NWP-总降水强度</th>\n",
       "      <th>NWP-温度</th>\n",
       "      <th>NWP-湿度</th>\n",
       "      <th>NWP-气压</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>66ecc86f1075358d129a1d56</td>\n",
       "      <td>2024-05-02 22:30:00</td>\n",
       "      <td>130.05</td>\n",
       "      <td>9.63</td>\n",
       "      <td>104.76</td>\n",
       "      <td>-0.99</td>\n",
       "      <td>92.52</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.6477</td>\n",
       "      <td>202.7945</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0173</td>\n",
       "      <td>20.2918</td>\n",
       "      <td>20.3927</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14.0818</td>\n",
       "      <td>23.4918</td>\n",
       "      <td>892.3327</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>66ecc86f1075358d129a1d57</td>\n",
       "      <td>2024-05-02 22:45:00</td>\n",
       "      <td>130.51</td>\n",
       "      <td>9.72</td>\n",
       "      <td>104.76</td>\n",
       "      <td>-0.99</td>\n",
       "      <td>93.24</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.9228</td>\n",
       "      <td>202.6482</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0191</td>\n",
       "      <td>20.3009</td>\n",
       "      <td>20.3864</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>13.8264</td>\n",
       "      <td>24.0436</td>\n",
       "      <td>892.3809</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 34 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                        _id               C_TIME    理论功率  第1个设备轮毂风速    实际功率  \\\n",
       "0  66ecc86f1075358d129a1d56  2024-05-02 22:30:00  130.05       9.63  104.76   \n",
       "1  66ecc86f1075358d129a1d57  2024-05-02 22:45:00  130.51       9.72  104.76   \n",
       "\n",
       "   实时环境数据   预测功率  NWP预测总辐射  NWP-10米预测风速  NWP-10米预测风向  ...  NWP-低云量  NWP-中云量  \\\n",
       "0   -0.99  92.52       0.0       3.6477     202.7945  ...      0.0   0.0173   \n",
       "1   -0.99  93.24       0.0       3.9228     202.6482  ...      0.0   0.0191   \n",
       "\n",
       "   NWP-高云量  NWP-总云量  NWP-晴空总辐射  NWP-法相直辐射  NWP-总降水强度   NWP-温度   NWP-湿度  \\\n",
       "0  20.2918  20.3927        0.0        0.0        0.0  14.0818  23.4918   \n",
       "1  20.3009  20.3864        0.0        0.0        0.0  13.8264  24.0436   \n",
       "\n",
       "     NWP-气压  \n",
       "0  892.3327  \n",
       "1  892.3809  \n",
       "\n",
       "[2 rows x 34 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pymongo import MongoClient\n",
    "import pandas as pd\n",
    "client = MongoClient(\"mongodb://root:Jydl3377@192.168.1.33:27017\")\n",
    "# 选择数据库（如果数据库不存在，MongoDB 会自动创建）\n",
    "db = client[\"algorithm_platform\"]\n",
    "collection = db['J00260NWP_machines']\n",
    "data_from_db = collection.find()  # 这会返回一个游标（cursor）\n",
    "\n",
    "# 将游标转换为列表，并创建 pandas DataFrame\n",
    "df_from_db = pd.DataFrame(list(data_from_db))\n",
    "client.close()\n",
    "# 打印读取到的数据\n",
    "df_from_db.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0bb0ca05-9437-4001-b4a3-0f2462688eee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9713, 34)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_from_db.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03343b25-0d46-4353-8ae5-4a2278513473",
   "metadata": {},
   "outputs": [],
   "source": [
    "docker run processing_limit_power_by_machines     --col_rp \"实际功率\"     --col_tp \"理论功率\"   --mongondb_connection \"mongodb://root:Jydl3377@192.168.1.33:27017\"     --mongondb_database \"algorithm_platform\"     --mongondb_read_table \"J00260NWP_agcavc\"     --mongondb_write_table \"J00260NWP_machines\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cd776c9-c45b-4287-801c-1ae243c17070",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-09-27 11:28:37,433 - waitress - INFO - Serving on http://0.0.0.0:10087\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Program starts execution!\n",
      "Program starts execution!\n",
      "********* {'col_rp': '实际功率', 'col_tp': '理论功率', 'mongondb_connection': 'mongodb://root:Jydl3377@192.168.1.33:27017', 'mongondb_database': 'algorithm_platform', 'mongondb_read_table': 'J00260NWP_agcavc', 'mongondb_write_table': 'J00260NWP_machines'} *******\n",
      "Program execution ends!\n",
      "Program starts execution!\n",
      "********* {'col_rp': '实际功率', 'col_tp': '理论功率', 'mongodb_connection': 'mongodb://root:Jydl3377@192.168.1.33:27017', 'mongodb_database': 'algorithm_platform', 'mongodb_read_table': 'J00260NWP_agcavc', 'mongodb_write_table': 'J00260NWP_machines'} *******\n",
      "threshold 25.962767129908098\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 32622 (\\N{CJK UNIFIED IDEOGRAPH-7F6E}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 20449 (\\N{CJK UNIFIED IDEOGRAPH-4FE1}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 27700 (\\N{CJK UNIFIED IDEOGRAPH-6C34}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 24179 (\\N{CJK UNIFIED IDEOGRAPH-5E73}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 35745 (\\N{CJK UNIFIED IDEOGRAPH-8BA1}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 31639 (\\N{CJK UNIFIED IDEOGRAPH-7B97}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 21306 (\\N{CJK UNIFIED IDEOGRAPH-533A}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 38388 (\\N{CJK UNIFIED IDEOGRAPH-95F4}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 38480 (\\N{CJK UNIFIED IDEOGRAPH-9650}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 30005 (\\N{CJK UNIFIED IDEOGRAPH-7535}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 23454 (\\N{CJK UNIFIED IDEOGRAPH-5B9E}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 38469 (\\N{CJK UNIFIED IDEOGRAPH-9645}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 21151 (\\N{CJK UNIFIED IDEOGRAPH-529F}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 29575 (\\N{CJK UNIFIED IDEOGRAPH-7387}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 29702 (\\N{CJK UNIFIED IDEOGRAPH-7406}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 35770 (\\N{CJK UNIFIED IDEOGRAPH-8BBA}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n",
      "C:\\Users\\awgln\\AppData\\Roaming\\Python\\Python39\\site-packages\\IPython\\core\\pylabtools.py:152: UserWarning: Glyph 28857 (\\N{CJK UNIFIED IDEOGRAPH-70B9}) missing from current font.\n",
      "  fig.canvas.print_figure(bytes_io, **kw)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 2597636 characters]",
      "text/plain": [
       "<Figure size 30000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collection 'J00260NWP_machines already exist, deleted successfully!\n",
      "data inserted successfully!\n",
      "Program execution ends!\n"
     ]
    }
   ],
   "source": [
    "%run processing_limit_power_by_machines.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eaf69e3c-cc2d-442e-9fcc-5fe6e0a82af2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "url = 'http://localhost:10087/processing_limit_power_by_machines'\n",
    "    data = {\n",
    "        'col_rp': '实际功率',\n",
    "        'col_tp': '理论功率',\n",
    "        'mongondb_connection': 'mongodb://root:Jydl3377@192.168.1.33:27017',\n",
    "        'mongondb_database': 'algorithm_platform',\n",
    "        'mongondb_read_table': 'J00260NWP_agcavc',\n",
    "        'mongondb_write_table': 'J00260NWP_machines',\n",
    "        # 添加其他参数\n",
    "    }\n",
    "response = requests.post(url, data=data) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf5ebae3-db2e-469c-af38-f6001ca70639",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (panddlets)",
   "language": "python",
   "name": "panddlets"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
