-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
134 lines (111 loc) · 3.85 KB
/
app.py
File metadata and controls
134 lines (111 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
from flask import Flask, jsonify, request, send_file
from query import *
from flask_apscheduler import APScheduler
from flask_cors import CORS
import os
app = Flask(__name__)
CORS(app)
scheduler = APScheduler()
def run_senegal_users_fetch():
app.logger.info("Starting scheduled Senegal users fetch")
get_all_senegalese_users()
app.logger.info("Completed scheduled Senegal users fetch")
@scheduler.task(
'interval',
id='do_fetch_senegal_users',
seconds=int(os.getenv('FETCH_INTERVAL_SECONDS', 3600)),
misfire_grace_time=900,
max_instances=1,
coalesce=True,
)
def scheduled_fetch_senegal_users():
run_senegal_users_fetch()
@app.route('/technos', methods=['GET'])
def fetch_technos():
json_file = open('techno_stats.json', 'r')
data = json.loads(json_file.read())
techno_types = ['backend', 'frontend', 'mobile']
final_data={
'technos_repartition':{},
'technos_types':{},
'total_repos':0
}
for user in data:
for t in techno_types:
if t in user.get('technologies', {}):
for techno, count in user['technologies'][t].items():
final_data['technos_types'][t] = final_data['technos_types'].get(t, 0) + count
final_data['technos_repartition'][techno]=final_data['technos_repartition'].get(techno,0) + count
final_data['total_repos']+=user['repos_analyzed']
symfony_group = 0
to_remove = []
# Symfony group packages
for techno in final_data['technos_repartition']:
if 'symfony' in techno.lower():
symfony_group += final_data['technos_repartition'][techno]
to_remove.append(techno)
for techno in to_remove:
del final_data['technos_repartition'][techno]
if symfony_group > 0:
final_data['technos_repartition']['symfony'] = symfony_group
return jsonify(final_data)
@app.route('/users/contributions/senegal', methods=['GET'])
def fetch_senegal_users():
json_file = open('users.json', 'r')
data = json.loads(json_file.read())
return jsonify(data)
@app.route('/users/search', methods=['GET'])
def list_users_by_location():
query_args = request.args
variables = {'query': query_builder_string(query_args)}
users = []
data = handle_response(
user_fetcher(
query_list_user(
query_builder_string(query_args),
query_args.get('after')),
variables=variables,
single_fetch=True),
"search")
if (data.get('message')):
return jsonify(data)
cursor = data['pageInfo']['endCursor']
users.extend(data['nodes'])
while (data["pageInfo"]['hasNextPage']):
data = handle_response(user_fetcher(query_list_user(
query_builder_string(query_args),
cursor), variables=variables, single_fetch=True), "search")
if (data.get('message')):
return jsonify(data)
cursor = data["pageInfo"]['endCursor']
users.extend(data['nodes'])
return jsonify(users)
@app.route('/get-user-file', methods=['GET'])
def get_user_file():
return send_file('users.json')
@app.route('/users/<username>', methods=['GET'])
def get_user_by_user(username):
data = handle_response(
user_fetcher(
query_get_one_user(username),
single_fetch=True),
"user")
if (data.get("message")):
return jsonify(data)
return jsonify(format_user(data))
@app.errorhandler(404)
def page_not_found(e):
return {"message": "Ressource introuvable"}
@app.route('/healthcheck')
def healthcheck():
return "ok", 200
scheduler.init_app(app)
if not scheduler.running:
scheduler.start()
if __name__ == '__main__':
app.run(
debug=int(
os.getenv(
'FLASK_DEBUG', False)), port=os.getenv(
'FLASK_PORT', 5000), host="0.0.0.0")