fcn-ispdb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/python3
  2. import configparser
  3. import json
  4. import psycopg2
  5. config = configparser.RawConfigParser()
  6. config.read('/etc/fcntoolbox/config.ini')
  7. configdb = config['dolibarr']
  8. conn = psycopg2.connect(database=configdb['database'],
  9. user=configdb['user'], password=configdb['password'])
  10. cur = conn.cursor()
  11. def getMemberCount():
  12. cur.execute("SELECT count(*) FROM llx_adherent WHERE statut = '1'")
  13. return cur.fetchone()[0]
  14. def getSubscriberCount(product):
  15. cur.execute("""SELECT COUNT(*)
  16. FROM llx_contratdet d
  17. JOIN llx_product p on d.fk_product = p.rowid
  18. WHERE d.date_ouverture IS NOT null
  19. AND d.date_cloture IS null
  20. AND p.ref LIKE %s""", (product,))
  21. return cur.fetchone()[0]
  22. subscribersADSL = getSubscriberCount('ADSL-%')
  23. subscibersVPN = getSubscriberCount('VPN-%')
  24. members = getMemberCount()
  25. f = open('/etc/fcntoolbox/ispdb-template.json', 'r')
  26. isp = json.load(f)
  27. f = open('/etc/fcntoolbox/ispdb.geojson', 'r')
  28. ispgeo = json.load(f)
  29. isp['coveredAreas'][0]['area'] = ispgeo
  30. isp["memberCount"] = members
  31. isp["subscriberCount"] = subscribersADSL + subscibersVPN
  32. isp["_DSL_subscriberCount"] = subscribersADSL
  33. isp["_VPN_subscriberCount"] = subscibersVPN
  34. print(json.dumps(isp, indent=True))