liuyuqi-dellpc 5 years ago
parent
commit
590fa7b018
6 changed files with 124 additions and 18 deletions
  1. 37 17
      code/sort_by_disk.py
  2. 17 0
      code/t.py
  3. 15 1
      code/test_pandas.py
  4. 19 0
      code/time.py
  5. 10 0
      libs/__init__.py
  6. 26 0
      libs/save_result.py

+ 37 - 17
code/sort_by_disk.py

@@ -10,9 +10,12 @@
 import matplotlib
 
 matplotlib.use('Agg')
+
 import pandas as pd
 import matplotlib.pyplot as plt
 from configparser import ConfigParser
+import time
+import libs.save_result
 
 cf = ConfigParser()
 config_path = "../conf/config.ini"
@@ -72,14 +75,32 @@ def restrictApp(instance, deploy_list):
 
 # 执行部署方案
 def deplay():
-    mlength = df3["instanceid"].size()
-    while mlength > 0:
-        deployInstance(mlength)
-
-    result.to_csv("../submit/xx.csv")
-
-
-def deployInstance(mlength):
+    print("------------开始部署啦--------------")
+    start = time.time()
+    row, column = df3.shape
+    while row > 0:
+        deployInstance(row, j)
+        # 整个instace都遍历了,第j主机无法再放入一个,所以添加j+1主机
+        row, column = df3.shape
+        j = j + 1
+
+    # 部署完事
+    print("------------部署完啦--------------")
+    end = time.time()
+    print("总共耗时:", end - start, "秒")
+    print("总共需要主机数:", j)
+    print("部署方案前几条示意:", result.head())
+    libs.save_result.save_result(result)
+
+
+def deployInstance(mlength, j):
+    '''
+    根据限制部署实例到主机上
+    :param mlength: 根据剩余的instance数量循环
+    :param j: 第j台主机
+    :return: 暂未定返回值,None
+    '''
+    global is_deploy, tem_disk, tem_mem, tem_cpu, tem_P, tem_M, tem_PM
     for i in range(0, mlength):
         tem_disk = tem_disk + df3["disk"][i]  # 当前磁盘消耗
         tem_mem = tem_mem + df3["mem"][i]
@@ -88,9 +109,9 @@ def deployInstance(mlength):
         tem_M = tem_M + df3["M"][i]
         tem_PM = tem_PM + df3["PM"][i]
 
-        if tem_disk < tmp_stand_disk1:  # 磁盘够
-            # if 满足限制表条件,则把当前实例部署到这台主机上。
-            if is_deploy == True:
+        # if 满足限制表条件,则把当前实例部署到这台主机上。
+        if is_deploy == True:
+            if tem_disk < tmp_stand_disk1:  # 磁盘够
                 if restrictApp(instance=df3["instanceid"], deploy_list=deploy_list):
                     if tem_mem < tmp_stand_mem1:  # 内存够
                         if tem_cpu < tmp_stand_cpu1:  # CPU够
@@ -98,12 +119,11 @@ def deployInstance(mlength):
                                 if tem_P < tmp_stand_P:
                                     if tem_PM < tmp_stand_PM1:
                                         result["machine"][i] = "machine_" + i
-            else:
-                # 主机j没有部署实例,则先部署一个
-                result["machine"][i] = "machine_" + i
-                is_deploy = True
-    # 整个instace都遍历了,第j主机无法再放入一个,所以添加j+1主机
-    j = j + 1
+        else:
+            # 主机j没有部署实例,则先部署一个
+            result["machine"][i] = "machine_" + i
+            is_deploy = True
+    is_deploy = False
 
 
 def plotGroup():  # df3新建一列

+ 17 - 0
code/t.py

@@ -0,0 +1,17 @@
+# !/usr/bin/env python
+# -*- coding: utf-8 -*-
+'''
+@Auther :liuyuqi.gov@msn.cn
+@Time :2018/7/8 3:16
+@File :t.py
+'''
+
+row = 100
+
+while row > 0:
+    print(row)
+    row = row - 20
+
+import libs.save_result
+
+libs.save_result.save_result(list())

+ 15 - 1
code/test_pandas.py

@@ -7,6 +7,7 @@
 '''
 import pandas as pd
 
+
 def t1():
     a = [['a', '1.2', '4.2'], ['b', '70', '0.03'], ['x', '5', '0']]
     df = pd.DataFrame(a, columns=list("ABC"))
@@ -54,4 +55,17 @@ def t5():
     ser2.values
 
 
-t5()
+def t6():
+    df = pd.DataFrame([{"A": "11", "B": "12"}, {"A": "111", "B": "121"}, {"A": "1111", "B": "1211"}])
+
+    print(df)
+    print(df.columns.size)  # 列数 2
+    h, l = df.shape
+    print(h, l)
+    print(df.iloc[:, 0].size)  # 行数 3
+    print(df.ix[[0]].index.values[0])  # 索引值 0
+    print(df.ix[[0]].values[0][0])  # 第一行第一列的值 11
+    print(df.ix[[1]].values[0][1])  # 第二行第二列的值 121
+
+
+t6()

+ 19 - 0
code/time.py

@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+'''
+time和datetime两个包使用实例
+datetime是日期,time是时间。对应于手机日历和钟表两个应用。
+@Auther :liuyuqi.gov@msn.cn
+@Time :2018/7/8 2:52
+@File :time.py
+'''
+
+import datetime
+import time
+
+# time
+print(time.time())
+
+# datetime
+print(datetime.datetime.now())
+print()

+ 10 - 0
libs/__init__.py

@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+'''
+@Auther :liuyuqi.gov@msn.cn
+@Time :2018/7/8 2:57
+@File :__init__.py.py
+'''
+
+if __name__ == '__main__':
+    pass

+ 26 - 0
libs/save_result.py

@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+'''
+@Auther :liuyuqi.gov@msn.cn
+@Time :2018/7/8 2:57
+@File :save_result.py
+'''
+
+import datetime
+
+import pandas as pd
+
+
+def save_result(data):
+    '''
+    导出数据结果
+    :param data:
+    :return:
+    '''
+
+    head = ["instance", "machine"]
+    data = [["ss", "aa"], ["ss", "aa"], ["ss", "aa"], ["ss", "aa"]]
+
+    df = pd.DataFrame(data, columns=head)
+    df.to_csv(("../submit/submit_" + datetime.datetime.now().strftime('%Y%m%d_%H%M%S') + ".csv"), header=None,
+              index=False)