azure_cdn: use subgroups, attempt 2

This commit is contained in:
Iain Learmonth 2022-04-25 15:05:28 +01:00
parent b59d334689
commit 95a39f759a
2 changed files with 18 additions and 17 deletions

View file

@ -1,4 +1,5 @@
import datetime
from collections import defaultdict
from app import app
from app.extensions import db
@ -8,6 +9,20 @@ from app.terraform import BaseAutomation
class ProxyAutomation(BaseAutomation):
def get_subgroups(self):
conn = db.engine.connect()
result = conn.execute("""
SELECT origin.group_id, proxy.psg, COUNT(proxy.id) FROM proxy, origin
WHERE proxy.origin_id = origin.id
AND proxy.destroyed IS NULL
AND proxy.provider = :provider
GROUP BY origin.group_id, proxy.psg;
""", provider=self.provider)
subgroups = defaultdict(lambda: defaultdict(lambda: 0))
for row in result:
subgroups[row[0]][row[1]] = row[2]
return subgroups
def create_missing_proxies(self):
origins = Origin.query.all()
for origin in origins:
@ -44,6 +59,7 @@ class ProxyAutomation(BaseAutomation):
Proxy.provider == self.provider,
Proxy.destroyed == None
).all(),
subgroups = self.get_subgroups(),
global_namespace=app.config['GLOBAL_NAMESPACE'],
bypass_token=app.config['BYPASS_TOKEN'],
**{