gen_doc_collector_page.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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().replace(" ", "_").replace("/", "-").replace("(", "").replace(")", "")+".md"
  41. names.append(f"[{name}]({link})")
  42. for integration_name in sorted(names):
  43. md += "- " + integration_name + "\n\n"
  44. outfile = pathlib.Path("./src/collectors/COLLECTORS.md")
  45. output = outfile.read_text().split("## Available Data Collection Integrations")[0]
  46. output += "## Available Data Collection Integrations\n<!-- AUTOGENERATED PART BY integrations/gen_doc_collector_page.py SCRIPT, DO NOT EDIT MANUALLY -->\n" + md
  47. outfile.write_text(output.rstrip('\n')+"\n")