qlands / elasticfeeds

A Python library for managing feeds using ElasticSearch
Other
19 stars 4 forks source link

Helpers to aggregate totals #4

Closed wweevv-johndpope closed 1 year ago

wweevv-johndpope commented 1 year ago

Is there any kinda of helpers available that could do

"UserA, userB and 7 others liked your post."

Q) when viewing the feeds - is it a use case to then perform operations on this? Like "usera posted xyz..." and then user can like this.

qlands commented 1 year ago

Likes, posts, views, etc. are activities defined in https://www.w3.org/TR/activitystreams-core/#model.

So, for example, you can state with ElasticFeeds that UserA[actor] posted[activity] XYZ[object] and then UserB[actor] likes[activity] XYZ[object].

Then have a look at this aggregator: elasticfeeds/aggregators/recenttypeobject.py and see if that would give you "UserA, userB and 7 others liked your post." based on the object ID of you "XYZ post".

Aggregators are just classes that encapsulate ElasticSearch queries. As long as you declare Activities, Actor, and Objects, you can query them as you like. With Elasticsearch, you can query which actors liked[verbe] a post[object] ordered by date and return only the details of two but give you the total. With that information, you can construct "UserA, userB, and 7 others liked your post."

wweevv-johndpope commented 1 year ago

cool - thanks

I did some wrangling in chatgpt and got this https://chat.openai.com/share/6ecf6c1b-acad-4511-ae7f-2a69f0e68bf7 I see also these a whole suit of aggregation functions - have to dig into https://logz.io/blog/elasticsearch-aggregations/#syntax

I'm planning to deploy to aws / opensearch shortly. we should bump the elasticsearch to 8.


def generate_abbreviated_highlight(actor_names, verb, object_, total_actors):
    if total_actors == 1:
        return f"{actor_names[0]} {verb} {object_}."
    elif total_actors == 2:
        return f"{actor_names[0]} and {actor_names[1]} {verb} {object_}."
    else:
        remaining_count = total_actors - 2
        return f"{actor_names[0]}, {actor_names[1]} and {remaining_count} others {verb} {object_}."

# Initialize a dictionary to store summarized activities
summarized_activities = {}

# Example list of activities (replace this with your actual activity data)
activities = [
    {"actor": "UserA", "verb": "liked", "object": "your post"},
    {"actor": "UserB", "verb": "liked", "object": "your post"},
    {"actor": "UserC", "verb": "liked", "object": "your post"},
    # Add more activities here
]

# Summarize the activities
for activity in activities:
    actor = activity["actor"]
    verb = activity["verb"]
    object_ = activity["object"]

    if (verb, object_) in summarized_activities:
        summarized_activities[(verb, object_)]["actors"].append(actor)
        summarized_activities[(verb, object_)]["count"] += 1
    else:
        summarized_activities[(verb, object_)] = {"actors": [actor], "count": 1}

# Generate and print the summarized highlights
for (verb, object_), data in summarized_activities.items():
    actor_names = data["actors"]
    total_actors = data["count"]
    highlight = generate_abbreviated_highlight(actor_names, verb, object_, total_actors)
    print(highlight)
johndpope commented 1 year ago

I got around most problems just adding index= to some calls in manager. this fork is broken - I used elk docker maybe should have persisted with the docker in here - but mines working. https://github.com/johndpope/elasticfeeds/commit/e00b0e9a1832d66256131a672a2ef6e0b4667724

going to look crafting html / that can show some feeds.