gen_doc_collector_page.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. This script reads the integrations/integrations.js file and generates the list of data collection integrations inside collectors/COLLECTORS.md, with proper links that Learn can replace into Learn links.
  3. """
  4. import json
  5. import pathlib
  6. # Open integrations/integrations.js and extract the dictionaries
  7. with open('integrations/integrations.js') as dataFile:
  8. data = dataFile.read()
  9. categories_str = data.split("export const categories = ")[1].split("export const integrations = ")[0]
  10. integrations_str = data.split("export const categories = ")[1].split("export const integrations = ")[1]
  11. categories = json.loads(categories_str)
  12. integrations = json.loads(integrations_str)
  13. cat_dict = {}
  14. data_col_cat = {}
  15. def recursive(categories):
  16. for category in categories:
  17. data_col_cat[category['id']] = category['name']
  18. if category['children']:
  19. recursive(category['children'])
  20. recursive(categories[1]['children'])
  21. def construct_dict(array, integration):
  22. for element in array:
  23. if element not in cat_dict:
  24. cat_dict[element] = list()
  25. cat_dict[element].append(integration)
  26. md = ""
  27. for integration in integrations:
  28. if integration['integration_type'] == "collector":
  29. construct_dict(integration['meta']['monitored_instance']['categories'], integration)
  30. for category_id, integrations in sorted(cat_dict.items()):
  31. heading = '#' * len(category_id.split('.'))
  32. for cat in data_col_cat:
  33. if cat == category_id:
  34. name = data_col_cat[cat]
  35. md += f'#{heading} {name}\n\n'
  36. names = []
  37. for integration in integrations:
  38. name = integration['meta']['monitored_instance']['name']
  39. link = (integration['edit_link'].replace("metadata.yaml", "") +
  40. "integrations/" + name.lower().
  41. replace(" ", "_").
  42. replace("/", "-").
  43. replace("(", "").
  44. replace(")", "") + ".md")
  45. names.append(f"[{name}]({link})")
  46. for integration_name in sorted(names):
  47. md += "- " + integration_name + "\n\n"
  48. outfile = pathlib.Path("./src/collectors/COLLECTORS.md")
  49. output = outfile.read_text().split("## Available Data Collection Integrations")[0]
  50. output += "## Available Data Collection Integrations\n<!-- AUTOGENERATED PART BY integrations/gen_doc_collector_page.py SCRIPT, DO NOT EDIT MANUALLY -->\n" + md
  51. outfile.write_text(output.rstrip('\n') + "\n")