diff --git a/flaskinventory/edit/dgraph.py b/flaskinventory/edit/dgraph.py index b2a639d7..dcbe457c 100644 --- a/flaskinventory/edit/dgraph.py +++ b/flaskinventory/edit/dgraph.py @@ -36,13 +36,7 @@ def get_audience(uid): rows = [] # convert to list of dicts if 'audience_size' not in data.keys(): - cols = ['date'] - if data['channel']['unique_name'] == 'print': - cols += ['copies_sold', 'data_from'] - elif data['channel']['unique_name'] == 'facebook': - cols.append('likes') - else: - return False + cols = ['date', 'unit', 'count'] else: keys = [key for key in data.keys() if key.startswith('audience_size|')] for i, item in enumerate(data['audience_size']): diff --git a/tools/change_audience_size_structure.py b/tools/change_audience_size_structure.py index 62171384..a8bf7a10 100644 --- a/tools/change_audience_size_structure.py +++ b/tools/change_audience_size_structure.py @@ -88,6 +88,42 @@ def main(): finally: txn.discard() + print(Fore.YELLOW + 'Running: Facebook: Likes ' + Style.RESET_ALL) + + query_string = """ + query { + c(func: eq(unique_name, "facebook")) { + u as uid + } + q(func: type("Source")) + @filter(has(audience_size) AND uid_in(channel, uid(u))) @cascade { + uid + audience_size @facets(gt(likes, 0)) @facets + } + } + """ + + res = client.txn().query(query_string) + + result = json.loads(res.json) + + for e in result['q']: + e['audience_size|count'] = e['audience_size|likes'] + e['audience_size|unit'] = {'0': 'likes'} + if 'audience_size|datafrom' in e.keys(): + e['audience_size|data_from'] = e.pop('audience_size|datafrom') + + for e in result['q']: + e.pop('audience_size|likes') + + txn = client.txn() + + try: + txn.mutate(set_obj=result['q']) + txn.commit() + finally: + txn.discard() + print(Fore.YELLOW + 'Running: Print: subscribers, copies sold' + Style.RESET_ALL)