fcn-ispdb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. # statut: 4 = STATUS_OPEN (cf. dolibarr/htdocs/contrat/class/contrat.class.php
  16. cur.execute("""SELECT COUNT(*)
  17. FROM llx_contratdet d
  18. JOIN llx_product p on d.fk_product = p.rowid
  19. WHERE d.statut = 4
  20. AND p.ref LIKE %s""", (product,))
  21. return cur.fetchone()[0]
  22. # L'ancien format de service xDSL avec Nerim était ADSL-XX-X.
  23. # À supprimer quand il n'y en aura plus.
  24. subscribersADSL = getSubscriberCount('ADSL-%') + getSubscriberCount('DSL-%')
  25. subscibersVPN = getSubscriberCount('VPN-%')
  26. members = getMemberCount()
  27. f = open('/etc/fcntoolbox/ispdb-template.json', 'r')
  28. isp = json.load(f)
  29. f = open('/etc/fcntoolbox/ispdb.geojson', 'r')
  30. ispgeo = json.load(f)
  31. isp['coveredAreas'][0]['area'] = ispgeo
  32. isp["memberCount"] = members
  33. isp["subscriberCount"] = subscribersADSL + subscibersVPN
  34. isp["_DSL_subscriberCount"] = subscribersADSL
  35. isp["_VPN_subscriberCount"] = subscibersVPN
  36. print(json.dumps(isp, indent=True))