Karmator icon indicating copy to clipboard operation
Karmator copied to clipboard

(Feature Regression) Reimplement the Web-server for karmator

Open pharaun opened this issue 10 years ago • 2 comments

Reimplement the web-server part of karmator.

In interest of getting this release out reasonably soon I'm going to focus on just the karmator part in IRC then dealing with the rest later (mainly the web-server portion)

pharaun avatar Sep 29 '14 07:09 pharaun

This is the last python thing unimplemented, the graph + karma api.

@interaction
def all_counts_graph(session, table, sortkey):
    counts = all_counts(session, table)
    from matplotlib.pyplot import figure
    fig = figure()
    plt = fig.add_subplot(111)
    x, up, down, names = [], [], [], []
    counts.sort(key=sortkey, reverse=True)
    for e, t in enumerate(counts):
        if sortkey(t) < 20:
            continue
        if e < 10:
            names.append(t[0])
        x.append(e)
        up.append(t[1][0])
        down.append(-t[1][1])
    plt.annotate('\n'.join(names), xy=(1, 1), xycoords='figure fraction',
                 xytext=(-20, -20), textcoords='offset points', ha='right', va='top',
                 bbox=dict(boxstyle='round', fc='0.95'))
    plt.set_xlim([x[0], x[-1]])
    plt.fill_between(x, y1=0, y2=up, color='g', alpha=0.8)
    plt.fill_between(x, y1=down, y2=0, color='r', alpha=0.8)
    io = StringIO()
    fig.savefig(io, format='svg')
    return io.getvalue()

class KarmaWeb(object):
    app = Klein()

    def __init__(self, Session):
        self.Session = Session

    @app.route('/')
    def index(self, request):
        return TagElement(tags.h1('hi!'))

    sortkeys = {
        'up': lambda t: t[1][0],
        'down': lambda t: t[1][1],
        'both': lambda t: t[1][0] + t[1][1],
        'karma': lambda t: t[1][0] - t[1][1],
    }

    tables = {
        'receiving': models.karma_in,
        'giving': models.karma_out,
    }

    @app.route('/<string:table>/<string:sortkey>/votes.svg')
    def vote_chart(self, request, table, sortkey):
        request.setHeader('Content-Type', 'image/svg+xml')
        return models.all_counts_graph(
            self.Session, self.tables[table], self.sortkeys[sortkey])

    @app.route('/api/karma.json')
    def karma_dump(self, request):
        request.setHeader('Content-Type', 'application/json')
        d = models.all_karma(self.Session)
        d.addCallback(lambda r: json.dumps(dict(tuple(x) for x in r)))
        return d

    @app.route('/api/all-counts.json')
    def karma_count_dump(self, request):
        request.setHeader('Content-Type', 'application/json')
        d = models.all_counts(self.Session, models.karma_in)
        d.addCallback(json.dumps)
        return d

pharaun avatar Oct 04 '14 09:10 pharaun

Instead of a webserver maybe worth to upload a file into slack with a graph rendered.

pharaun avatar Dec 09 '20 09:12 pharaun