Skip to content

Commit

Permalink
plot like graph
Browse files Browse the repository at this point in the history
  • Loading branch information
Ozoniuss committed Apr 6, 2024
1 parent 0318aa4 commit 2cf59c2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
49 changes: 45 additions & 4 deletions outbox/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import datetime
import json
import socket
from time import sleep
from typing import Counter
from collections import Counter
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
Expand All @@ -20,7 +19,7 @@

HOST = "127.0.0.1"

PORT = 13311
PORT = 13312

@dataclass
class ArticleLikedEvent:
Expand All @@ -37,28 +36,70 @@ class Message:
class Ack:
id: int

def generate_like_graph(events: list[ArticleLikedEvent]):

totals = []
for e in events:
totals.append(str(datetime.datetime(
e.timestamp.year,
e.timestamp.month,
e.timestamp.day,
e.timestamp.hour,
e.timestamp.minute,
e.timestamp.second)))

totalsDict = dict(Counter(totals))
xaxis, yaxis = [], []
for k in sorted(totalsDict.keys()):
xaxis.append(k)
yaxis.append(totalsDict[k])

#define data
df = pd.DataFrame({
'time':xaxis,
'seconds': yaxis})

#plot time series
plt.plot(df.time, df.seconds, linewidth=3)
#add title and axis labels
plt.title('Likes by second')
plt.xlabel('Second')
plt.ylabel('Likes')

plt.savefig('data.png')

receivedEvents: list[ArticleLikedEvent] = []


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
c = 0
with conn:
print(f"Connected by {addr}")
while True:
try:
c += 1
data = conn.recv(1024 * 1024 * 10).decode('UTF-8')
print("received data", data)
message = Message(**json.loads(str(data)))
message.events = [ArticleLikedEvent(**e) for e in message.events]

receivedEvents.extend(message.events)


ack = json.dumps(asdict(Ack(id=message.id)))
conn.sendall(bytes(ack,encoding="utf-8"))
all = [e['event_id'] for e in receivedEvents]
all = [e.event_id for e in receivedEvents]
print("allevents", all, len(all))

if c == 10:

break
except KeyboardInterrupt:
generate_like_graph(receivedEvents)

except Exception as e:
print("exception", e)
break
Expand Down
2 changes: 1 addition & 1 deletion outbox/producer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
}

// Initialize poller
poller, err := articles.NewLikedArticlesPoller(storage, 5*time.Second, "localhost:13311")
poller, err := articles.NewLikedArticlesPoller(storage, 5*time.Second, "localhost:13312")
if err != nil {
fmt.Printf("could not start poller: %s\n", err.Error())
os.Exit(1)
Expand Down

0 comments on commit 2cf59c2

Please sign in to comment.