Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. # pip install MySQL-python
  2. import pymysql
  3.  
  4. FULL_LINE="jdbc:mysql://77.234.220.70:3306/competencyjpa?serverTimezone=Europe/Moscow"
  5. URL = "77.234.220.70"
  6. LOGIN = "mastercjpa"
  7. #TODO: it's so safe!!! XD
  8. PASS = "TestPassword"
  9. DB = 'competencyjpa'
  10. ADDITIONS = 'serverTimezone=Europe/Moscow'
  11.  
  12.  
  13. def main():
  14. print("starting")
  15. # getting first-&second- level skills amount
  16. conn = pymysql.connect(host=URL, user=LOGIN, password=PASS, db=DB)
  17. cursor = conn.cursor()
  18.  
  19. firstLevelSkillsReq = 'SELECT ID, NAME FROM SKILL \
  20. WHERE PARENT IS NULL'
  21. cursor.execute(firstLevelSkillsReq)
  22.  
  23. firstLevelCnt = 0
  24. result = cursor.fetchone()
  25. while result is not None:
  26. #print(result)
  27. result = cursor.fetchone()
  28. firstLevelCnt += 1
  29.  
  30. print('First level Skill amount is: ' + str(firstLevelCnt))
  31.  
  32. secondLevelSkillsReq = 'SELECT ID FROM SKILL \
  33. WHERE PARENT IN \
  34. (SELECT ID FROM SKILL \
  35. WHERE PARENT IS NULL)'
  36. cursor.execute(secondLevelSkillsReq)
  37.  
  38. secondLevelCnt = 0
  39. result = cursor.fetchone()
  40. while result is not None:
  41. #print(result)
  42. result = cursor.fetchone()
  43. secondLevelCnt += 1
  44.  
  45. print('Second level Skill amount is: ' + str(secondLevelCnt))
  46.  
  47. # RESIDENTS WITH COMPETENCY AMOUNT
  48. residentsReq = 'SELECT ID FROM PROFILE \
  49. WHERE ID IN \
  50. (SELECT owner FROM COMPETENCY \
  51. WHERE SKILL IN \
  52. (SELECT ID FROM SKILL \
  53. WHERE PARENT IN \
  54. (SELECT ID FROM SKILL \
  55. WHERE PARENT IS NULL OR PARENT IN \
  56. (SELECT ID FROM SKILL \
  57. WHERE PARENT IS NULL))))'
  58. cursor.execute(residentsReq)
  59.  
  60. residentCnt = 0
  61. result = cursor.fetchone()
  62. while result is not None:
  63. #print(result)
  64. result = cursor.fetchone()
  65. residentCnt += 1
  66.  
  67. print('resident cnt with 1 and 2 level group is: ' + str(residentCnt))
  68.  
  69. cursor.execute(secondLevelSkillsReq)
  70. residents12 = cursor.fetchall()
  71. #print(residents12)
  72.  
  73. def getStr(p):
  74. nds = '('
  75. for s in p:
  76. if s != p[0]:
  77. nds += ','
  78. nds += str(s[0])
  79. nds += ')'
  80. return nds
  81.  
  82. def children(p):
  83. #tricky
  84. res = ()
  85. nds = getStr(p)
  86. nodes = 'SELECT ID FROM SKILL WHERE PARENT IN ' + str(nds)
  87. cursor.execute(nodes)
  88. nodes = cursor.fetchall()
  89. res += nodes
  90.  
  91. for node in nodes:
  92. sql = 'SELECT ID FROM SKILL WHERE PARENT IN (' + str(node[0]) + ')'
  93. cursor.execute(sql)
  94. childrs = cursor.fetchall()
  95. if len(childrs) is 0:
  96. return res
  97.  
  98. res += children(childrs)
  99.  
  100. return res
  101.  
  102. def correctSplit(p):
  103. return p
  104.  
  105. for resident in residents12:
  106. allSkillsIds = children((resident,))
  107.  
  108. #drawing:
  109. sum = 0
  110. for skillId in getStr(allSkillsIds)[1:][:-1].split(","):
  111. amountQuer = 'SELECT ID FROM PROFILE \
  112. WHERE ID IN (SELECT owner FROM COMPETENCY \
  113. WHERE SKILL IN (' + skillId + '))'
  114. cursor.execute(amountQuer)
  115. amount = len(getStr(cursor.fetchall())[1:][:-1].split(","))
  116. sum += amount
  117. #print('Skill: ' + skillId + ' : people : ' + str(amount))
  118.  
  119. print(str(resident[0]) + ":" + str(sum))
  120.  
  121. conn.close()
  122.  
  123.  
  124. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement