print_bags.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import requests
  2. import json
  3. #import csv
  4. from tabulate import tabulate
  5. from itertools import groupby
  6. from operator import itemgetter
  7. import numpy as np
  8. url = 'https://joystream2.yyagi.cloud/graphql'
  9. #url = 'https://query.joystream.org/graphql'
  10. file_name = "{}-12:00-objects.txt"
  11. file_server = "http://87.236.146.74:8000/"
  12. operators = [{'id':"0x2bc", 'bucket': 16},{'id':"alexznet", 'bucket': 2},{'id':"Craci_BwareLabs", 'bucket': 10},{'id':"GodsHunter", 'bucket': 6},{'id':"joystreamstats", 'bucket': 1},{'id':"l1dev", 'bucket': 4},{'id':"maxlevush", 'bucket': 3},{'id':"mmx1916", 'bucket': 9},{'id':"razumv", 'bucket': 11},{'id':"yyagi", 'bucket': 17}, {'id':"sieemma", 'bucket': 12} ]
  13. credential = {'username': '', 'password' :'joystream'}
  14. query_group = "storageWorkingGroup"
  15. max_backets = 5
  16. #def queryGrapql(query, url= 'https://query.joystream.org/graphql' ):
  17. def queryGrapql(query, url= 'https://joystream2.yyagi.cloud/graphql' ):
  18. headers = {'Accept-Encoding': 'gzip, deflate, br', 'Content-Type': 'application/json',
  19. 'Accept': 'application/json', 'Connection': 'keep-alive', 'DNT': '1',
  20. 'Origin': 'https://query.joystream.org' }
  21. response = requests.post(url, headers=headers, json=query)
  22. return response.json()['data']
  23. def get_bags(start_time='', end_time=''):
  24. if start_time and end_time :
  25. query = {"query": 'query MyQuery {{ storageBags( limit: 33000, offset: 0, where: {{createdAt_gt: "{}" , createdAt_lt: "{}"}}) {{ storageBuckets {{ id }} id deletedAt }} }}'.format(start_time, end_time) }
  26. else:
  27. query = {"query": 'query MyQuery { storageBags( limit: 33000, offset: 0) { storageBuckets { id } id deletedAt }} ' }
  28. data = queryGrapql(query)['storageBags']
  29. return data
  30. def get_objects(start_time='',end_time=''):
  31. if start_time and end_time :
  32. query_created = {"query":'query MyQuery {{ storageBags(limit: 33000, offset: 0,where: {{createdAt_gt: "{}" , createdAt_lt: "{}"}}) {{ storageBuckets {{ id }} id }} }}'.format(start_time, end_time) }
  33. else :
  34. query_created = {"query":'query MyQuery { storageBags(limit: 33000, offset: 0) { storageBuckets { id } id } }' }
  35. objects_created = queryGrapql(query_created)['storageBags']
  36. for obj in objects_created:
  37. obj['storageBagId'] = obj['storageBagId'].split(":")[2]
  38. return objects_created
  39. def get_bags_nums(start_time = '', end_time = ''):
  40. data_created, data_deleted = {},{}
  41. if start_time and end_time :
  42. query_created = {"query": 'query MyQuery {{ storageBags( limit: 33000, offset: 0, where: {{createdAt_gt: "{}" , createdAt_lt: "{}"}}) {{ storageBuckets {{ id }} id deletedAt }} }}'.format(start_time, end_time) }
  43. else :
  44. query_created = {"query": 'query MyQuery { storageBags(limit: 33000, offset:0) { storageBuckets { id } id deletedAt} }'}
  45. data_created = queryGrapql(query_created)['storageBags']
  46. return data_created
  47. def sort_bags_nums(data):
  48. for record in data:
  49. i = 0
  50. for item in record['storageBuckets']:
  51. record['storageBuckets_'+str(i)] = item['id']
  52. i += 1
  53. while i < max_backets :
  54. record['storageBuckets_'+str(i)] = 'X'
  55. i += 1
  56. record.pop('storageBuckets')
  57. return data
  58. def print_bags():
  59. data = get_bags()
  60. data = sort_bags_nums(data)
  61. print_table(data, master_key = 'id', sort_key = 'id')
  62. def print_table(data, master_key = '', sort_key = ''):
  63. if sort_key:
  64. data = sorted(data, key = itemgetter(sort_key), reverse=True)
  65. headers = [*data[0]]
  66. if master_key:
  67. headers.append(master_key)
  68. headers.remove(master_key)
  69. headers = [master_key] + headers
  70. table = []
  71. for line in data:
  72. row = []
  73. if master_key:
  74. value = line.pop(master_key)
  75. row.append(value)
  76. for key in [*line]:
  77. row.append(line[key])
  78. table.append(row)
  79. try:
  80. result = tabulate(table, headers, tablefmt="github")
  81. print(result)
  82. except UnicodeEncodeError:
  83. result = tabulate(table, headers, tablefmt="grid")
  84. print(result)
  85. if __name__ == '__main__':
  86. print_bags()