Skip to content

Commit

Permalink
feat: fetch all users from Senegal when the app starts
Browse files Browse the repository at this point in the history
  • Loading branch information
bambadiagne committed Mar 17, 2024
1 parent 88d96a4 commit f507d4a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
36 changes: 19 additions & 17 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
import time
import threading
from flask import Flask, jsonify, request, send_file
from query import *
from flask_apscheduler import APScheduler
Expand All @@ -13,22 +13,17 @@
scheduler.start()


# Fetch all users from Senegal when the app starts
@app.before_first_request
def before_first_request():
thread = threading.Thread(target=get_all_senegalese_users,)
thread.start()


@scheduler.task('interval', id='do_fetch_senegal_users',
seconds=3600, misfire_grace_time=900)
def get_senegal_users():
start_time = time.time()
print('============================= START JOB =============================')
all_users = {'users': []}

all_users['users'] = user_fetcher()
with open("users.json", "w", encoding="utf-8",) as f:
json.dump(all_users,
f,
indent=4,
sort_keys=True)
print('============================= END JOB =============================')
print(f"JOB TAKEN TIME {time.time()-start_time} seconds")
return jsonify({"ok": "ok"})
get_all_senegalese_users()


@app.route('/users/contributions/senegal', methods=['GET'])
Expand Down Expand Up @@ -67,9 +62,10 @@ def list_users_by_location():


@app.route('/get-user-file', methods=['GET'])
def get_user_file():
def get_user_file():
return send_file('users.json')


@app.route('/users/<username>', methods=['GET'])
def get_user_by_user(username):

Expand All @@ -87,9 +83,15 @@ def get_user_by_user(username):
def page_not_found(e):
return {"message": "Ressource introuvable"}


if __name__ == '__main__':
app.run(debug=int(os.getenv('FLASK_DEBUG', False)), port=80, host="0.0.0.0")
app.run(
debug=int(
os.getenv(
'FLASK_DEBUG', False)), port=os.getenv(
'FLASK_PORT', 5000), host="0.0.0.0")


@app.route('/healthcheck')
def healthcheck():
return "ok", 200
return "ok", 200
20 changes: 18 additions & 2 deletions query.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import asyncio
import json
import os
import time
import logging
from python_graphql_client import GraphqlClient
import math

logging.basicConfig(level=logging.INFO)
DEFAULT_USERNAME = "torvalds"
BASE_URL = "https://api.github.com/graphql"
MAX_GITHUB_FETCH = 1000
Expand Down Expand Up @@ -46,6 +49,20 @@ def query_builder_string(query: dict,):
return grapqhql_query


def get_all_senegalese_users():
start_time = time.time()

all_users = {'users': []}

all_users['users'] = user_fetcher()
with open("users.json", "w", encoding="utf-8",) as f:
json.dump(all_users,
f,
indent=4,
sort_keys=True)
logging.info(f"JOB TAKEN TIME {time.time()-start_time} seconds")


def query_list_user(location='Senegal', after=''):
return """query ListUsers($query: String = "location: {}") {{
search(type: USER, query: $query, first: 100, {}) {{
Expand Down Expand Up @@ -156,7 +173,6 @@ def fetchAllSenegalese(query):
while (data['pageInfo']['hasNextPage']):
cursor = data['pageInfo']['endCursor']
try:
print(query)
data = handle_response(asyncio.run(
GRAPHQL_SERVEUR.execute_async(
query=query_all_senegalese(query, 50, cursor),
Expand Down

0 comments on commit f507d4a

Please sign in to comment.