File size: 6,605 Bytes
9ac5ea2
ebe86df
38e70c4
 
 
 
 
ebe86df
b5bfbc4
38e70c4
 
ebe86df
 
 
 
c30da09
 
b5bfbc4
 
9ac5ea2
c30da09
 
b5bfbc4
 
 
 
 
 
38e70c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c30da09
 
 
 
 
 
e3dc221
c30da09
38e70c4
 
 
 
 
ffd3765
9ac5ea2
 
e3dc221
38e70c4
 
 
 
 
 
 
 
 
 
 
9ac5ea2
38e70c4
 
 
 
 
 
 
 
9ac5ea2
38e70c4
9ac5ea2
 
 
38e70c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bfbc4
38e70c4
 
 
9ac5ea2
38e70c4
b5bfbc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ac5ea2
b5bfbc4
 
 
 
 
 
 
 
38e70c4
 
 
 
 
 
 
 
 
 
 
9ac5ea2
38e70c4
 
 
 
 
 
 
 
 
 
 
 
 
 
b5bfbc4
38e70c4
b5bfbc4
12e35a6
38e70c4
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import collections
import os
from datetime import datetime, timedelta
import json
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import parse_qs, urlparse

from huggingface_hub import list_datasets, set_access_token, HfFolder
from datasets import load_dataset, DatasetDict
import numpy as np

HF_TOKEN = os.environ['HF_TOKEN']
set_access_token(HF_TOKEN)
HfFolder.save_token(HF_TOKEN)


datasets = {
    "stars": load_dataset("open-source-metrics/stars").sort('dates'),
    "issues": load_dataset("open-source-metrics/issues").sort('dates'),
    "pip": load_dataset("open-source-metrics/pip").sort('day')
}

# datasets = {
#     k1: DatasetDict({
#         k2: v2.select(range(0, len(v2), max(1, int(len(v2) / 1000)))) for k2, v2 in v1.items()
#     }) for k1, v1 in datasets.items()
# }


def running_mean(x, N, total_length=-1):
    cumsum = np.cumsum(np.insert(x, 0, 0))
    to_pad = max(total_length - len(cumsum), 0)
    return np.pad(cumsum[N:] - cumsum[:-N], (to_pad, 0)) / float(N)


class RequestHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        print(self.path)
        if self.path == "/":
            self.path = "index.html"

            return SimpleHTTPRequestHandler.do_GET(self)

        if self.path.startswith("/initialize"):
            dataset_keys = {k: set(v.keys()) for k, v in datasets.items()}
            dataset_keys['issues'].remove('transformers')
            dataset_with_most_splits = max([d for d in dataset_keys.values()], key=len)
            warnings = []

            for k, v in dataset_keys.items():
                if len(v) < len(dataset_with_most_splits):
                    warnings.extend(f"The {k} dataset does not contain all splits. Missing: {dataset_with_most_splits - v}")

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()

            # TODO: Send and display warnings
            dataset_with_most_splits = list(dataset_with_most_splits)
            dataset_with_most_splits.sort()
            self.wfile.write(json.dumps(list(dataset_with_most_splits)).encode("utf-8"))

            return SimpleHTTPRequestHandler

        if self.path.startswith("/retrievePipInstalls"):
            url = urlparse(self.path)
            query = parse_qs(url.query)
            library_names = query.get("input", None)[0]
            library_names = library_names.split(',')

            returned_values = {}
            for library_name in library_names:
                for i in datasets['pip'][library_name]:
                    if i['day'] in returned_values:
                        returned_values[i['day']][library_name] = i['num_downloads']
                    else:
                        returned_values[i['day']] = {library_name: i['num_downloads']}

            for library_name in library_names:
                for i in returned_values.keys():
                    if library_name not in returned_values[i]:
                        returned_values[i][library_name] = None

            returned_values = collections.OrderedDict(sorted(returned_values.items()))
            output = {l: [k[l] for k in returned_values.values()] for l in library_names}
            output['day'] = list(returned_values.keys())

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()

            self.wfile.write(json.dumps(output).encode("utf-8"))

            return SimpleHTTPRequestHandler

        if self.path.startswith("/retrieveStars"):
            url = urlparse(self.path)
            query = parse_qs(url.query)
            library_names = query.get("input", None)[0]
            library_names = library_names.split(',')

            returned_values = {}
            dataset_dict = datasets['stars']

            for library_name in library_names:
                dataset = dataset_dict[library_name]

                n = 0
                for i in dataset:
                    n += 1
                    if i['dates'] in returned_values:
                        returned_values[i['dates']][library_name] = n
                    else:
                        returned_values[i['dates']] = {library_name: n}

            for library_name in library_names:
                for i in returned_values.keys():
                    if library_name not in returned_values[i]:
                        returned_values[i][library_name] = None

            returned_values = collections.OrderedDict(sorted(returned_values.items()))
            output = {l: [k[l] for k in returned_values.values()][::-1] for l in library_names}
            output['day'] = list(returned_values.keys())[::-1]

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()

            self.wfile.write(json.dumps(output).encode("utf-8"))

            return SimpleHTTPRequestHandler

        if self.path.startswith("/retrieveIssues"):
            url = urlparse(self.path)
            query = parse_qs(url.query)
            library_names = query.get("input", None)[0]
            library_names = library_names.split(',')

            returned_values = {}
            dataset_dict = datasets['issues']

            for library_name in library_names:
                dataset = dataset_dict[library_name]

                n = 0
                for k, i in enumerate(dataset):
                    n += 1
                    if i['dates'] in returned_values:
                        returned_values[i['dates']][library_name] = n
                    else:
                        returned_values[i['dates']] = {library_name: n}

            for library_name in library_names:
                for i in returned_values.keys():
                    if library_name not in returned_values[i]:
                        returned_values[i][library_name] = None

            returned_values = collections.OrderedDict(sorted(returned_values.items()))
            output = {l: [k[l] for k in returned_values.values()][::-1] for l in library_names}
            output['day'] = list(returned_values.keys())[::-1]

            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()

            self.wfile.write(json.dumps(output).encode("utf-8"))

            return SimpleHTTPRequestHandler

        return SimpleHTTPRequestHandler.do_GET(self)


server = ThreadingHTTPServer(("", 7861), RequestHandler)

print("Running on port 7861")

server.serve_forever()