Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
#175 backend changes in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwunderbar666 committed Aug 23, 2022
1 parent a8870ae commit 2d7b4a8
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 28 deletions.
32 changes: 4 additions & 28 deletions flaskinventory/templates/view/elements/source/audience.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,8 @@
{% if entry.get('audience_size|data_from') or entry.get('audience_size|datafrom') %}
<th>Data from</th>
{% endif %}
{% if entry.get('audience_size|daily_visitors') %}
<th>Daily Visitors</th>
{% endif %}
{% if entry.get('audience_size|likes') %}
<th>Likes</th>
{% endif %}
{% if entry.get('audience_size|subscribers') %}
<th>Subscribers</th>
{% endif %}
{% if entry.get('audience_size|copies_sold') %}
<th>Copies Sold</th>
{% endif %}
{% if entry.get('audience_size|followers') %}
<th>Followers</th>
{% if entry.get('audience_size|unit') %}
<th>{{entry.get('audience_size|unit')['0']|title }}</th>
{% endif %}
</tr>
</thead>
Expand All @@ -39,20 +27,8 @@
<a href="{{ entry.get('audience_size|data_from')[loop.index0|string] or entry.get('audience_size|datafrom')[loop.index0|string] }}" target="_blank">source</a>
</td>
{% endif %}
{% if entry.get('audience_size|daily_visitors') %}
<td>{{ "{0:,}".format(entry.get('audience_size|daily_visitors')[loop.index0|string] | int) }}</td>
{% endif %}
{% if entry.get('audience_size|likes') %}
<td>{{ "{0:,}".format(entry.get('audience_size|likes')[loop.index0|string] | int) }}</td>
{% endif %}
{% if entry.get('audience_size|subscribers') %}
<td>{{ "{0:,}".format(entry.get('audience_size|subscribers')[loop.index0|string] | int) }}</td>
{% endif %}
{% if entry.get('audience_size|copies_sold') %}
<td>{{ "{0:,}".format(entry.get('audience_size|copies_sold')[loop.index0|string] | int) }}</td>
{% endif %}
{% if entry.get('audience_size|followers') %}
<td>{{ "{0:,}".format(entry.get('audience_size|followers')[loop.index0|string] | int) }}</td>
{% if entry.get('audience_size|count') %}
<td>{{ "{0:,}".format(entry.get('audience_size|count')[loop.index0|string] | int) }}</td>
{% endif %}
</tr>

Expand Down
170 changes: 170 additions & 0 deletions tools/change_audience_size_structure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import pydgraph
import json
import sys
from colorama import init, deinit, Fore, Style

def main():
init()
print(Fore.RED + 'WARNING!' + Style.RESET_ALL + " You are about to irreversibly change all entries.")
user_warning = input('Are you sure you want to proceed? (y/n): ')

if user_warning.lower() != 'y':
print('Aborted')
sys.exit()


client_stub = pydgraph.DgraphClientStub('localhost:9080')
client = pydgraph.DgraphClient(client_stub)

print(Fore.YELLOW + 'Running: Websites: Daily Visitors ' + Style.RESET_ALL)

query_string = """
query {
c(func: eq(unique_name, "website")) {
u as uid
}
q(func: type("Source"))
@filter(has(audience_size) AND uid_in(channel, uid(u))) @cascade {
uid
audience_size @facets(gt(daily_visitors, 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|daily_visitors']
e['audience_size|unit'] = {'0': 'daily visitors'}
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|daily_visitors')

txn = client.txn()

try:
txn.mutate(set_obj=result['q'])
txn.commit()
finally:
txn.discard()

print(Fore.YELLOW + 'Running: Twitter / Instagram / Telegram / VK: Followers' + Style.RESET_ALL)

query_string = """
query {
c(func: type(Channel)) @filter(eq(unique_name, ["twitter", "telegram", "instagram", "vkontakte"])) {
u as uid
}
q(func: type("Source"))
@filter(has(audience_size) AND uid_in(channel, uid(u))) @cascade {
uid
audience_size @facets(gt(followers, 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|followers']
e['audience_size|unit'] = {'0': 'followers'}
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|followers')

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)

query_string = """
query {
c(func: type(Channel)) @filter(eq(unique_name, "print")) {
u as uid
}
q(func: type("Source"))
@filter(has(audience_size) AND uid_in(channel, uid(u))) @cascade {
uid
audience_size @facets(gt(subscribers, 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|subscribers']
e['audience_size|unit'] = {'0': 'subscribers'}
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|subscribers')

txn = client.txn()

try:
txn.mutate(set_obj=result['q'])
txn.commit()
finally:
txn.discard()



query_string = """
query {
c(func: type(Channel)) @filter(eq(unique_name, "print")) {
u as uid
}
q(func: type("Source"))
@filter(has(audience_size) AND uid_in(channel, uid(u))) @cascade {
uid
audience_size @facets(gt(copies_sold, 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|copies_sold']
e['audience_size|unit'] = {'0': 'copies sold'}
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|copies_sold')

txn = client.txn()

try:
txn.mutate(set_obj=result['q'])
txn.commit()
finally:
txn.discard()

print(Fore.GREEN + 'DONE!' + Style.RESET_ALL)
deinit()

if __name__ == '__main__':
main()

0 comments on commit 2d7b4a8

Please sign in to comment.