Add support for matomo reports

This commit is contained in:
Peter J. Holzer 2023-02-15 14:44:29 +01:00
parent 10e29c3e39
commit 0976e4b5ef
1 changed files with 78 additions and 31 deletions

View File

@ -1,4 +1,5 @@
#!/home/hjp/venv/screen-sizes/bin/python #!/home/hjp/venv/screen-sizes/bin/python
import argparse
import json import json
import sys import sys
@ -6,24 +7,30 @@ import PIL.Image
import PIL.ImageDraw import PIL.ImageDraw
import PIL.ImageFont import PIL.ImageFont
sizes = [] ap = argparse.ArgumentParser()
with open(sys.argv[1]) as fh: ap.add_argument("--format", choices=("audit", "matomo"), required=True)
ap.add_argument("file")
args = ap.parse_args()
if args.format == "audit":
sizes = []
with open(args.file) as fh:
for ln in fh: for ln in fh:
audit_record = json.loads(ln[ln.find("{"):]) audit_record = json.loads(ln[ln.find("{"):])
sizes.append(audit_record["data"]["device_info"]["viewport"]) sizes.append(audit_record["data"]["device_info"]["viewport"])
widths = sorted(set(s["width"] for s in sizes)) widths = sorted(set(s["width"] for s in sizes))
heights = sorted(set(s["height"] for s in sizes)) heights = sorted(set(s["height"] for s in sizes))
width = widths[-1] width = widths[-1]
height = heights[-1] height = heights[-1]
print(width, height) print(width, height)
dist = [ dist = [
[0] * width [0] * width
for y in range(height) for y in range(height)
] ]
for i, s in enumerate(sizes): for i, s in enumerate(sizes):
w = s["width"] w = s["width"]
h = s["height"] h = s["height"]
print(i, w, h) print(i, w, h)
@ -31,21 +38,61 @@ for i, s in enumerate(sizes):
for x in range(w): for x in range(w):
dist[y][x] += 1 dist[y][x] += 1
dist_w = [0] * width dist_w = [0] * width
for i, s in enumerate(sizes): for i, s in enumerate(sizes):
w = s["width"] w = s["width"]
for x in range(w): for x in range(w):
dist_w[x] += 1 dist_w[x] += 1
dist_h = [0] * height dist_h = [0] * height
for i, s in enumerate(sizes): for i, s in enumerate(sizes):
h = s["height"] h = s["height"]
for y in range(h): for y in range(h):
dist_h[y] += 1 dist_h[y] += 1
elif args.format == "matomo":
sizes = []
with open(args.file) as fh:
data = json.load(fh)
for dp in data:
w, _, h = dp["label"].partition("x")
w = int(w)
h = int(h)
sizes.append({"width": w, "height": h, "count": dp["nb_visits"]})
widths = sorted(set(s["width"] for s in sizes))
heights = sorted(set(s["height"] for s in sizes))
width = widths[-1]
height = heights[-1]
print(width, height)
dist = [
[0] * width
for y in range(height)
]
for i, s in enumerate(sizes):
w = s["width"]
h = s["height"]
print(i, w, h)
for y in range(h):
for x in range(w):
dist[y][x] += s["count"]
dist_w = [0] * width
for i, s in enumerate(sizes):
w = s["width"]
for x in range(w):
dist_w[x] += s["count"]
dist_h = [0] * height
for i, s in enumerate(sizes):
h = s["height"]
for y in range(h):
dist_h[y] += s["count"]
origin_x = origin_y = 150 origin_x = origin_y = 150
img = PIL.Image.new('L', (width+origin_x, height+origin_y)) img = PIL.Image.new('L', (width+origin_x, height+origin_y))
max_val = len(sizes) max_val = dist[0][0]
for y in range(height): for y in range(height):
for x in range(width): for x in range(width):
img.putpixel((x+origin_x, y+origin_y), int(dist[y][x] * 255 / max_val)) img.putpixel((x+origin_x, y+origin_y), int(dist[y][x] * 255 / max_val))