DARPA-ASKEM / terarium

https://app.terarium.ai
Apache License 2.0
12 stars 2 forks source link

[TASK]: Cache the project card image #3943

Closed YohannParis closed 3 weeks ago

YohannParis commented 3 weeks ago

Describe the task The image card generated with p5 with metadata counts needs to be cached in the project itself to not be regenerated everytime.

This is severaly slowing down the front-end.

Option Can be written in hmi-server with Java and some caching for faster retrieval:

import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
import javax.xml.bind.DatatypeConverter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class Placeholder extends PApplet {

    private int contributors;
    private int models;
    private int datasets;
    private int papers;

    private static final int IMAGE_WIDTH = 208;
    private static final int IMAGE_HEIGHT = 133;

    public Placeholder(int contributors, int models, int datasets, int papers) {
        this.contributors = Math.min(contributors, 25);
        this.models = Math.min(models, 25);
        this.datasets = Math.min(datasets, 25);
        this.papers = Math.min(papers, 25);
    }

    public void settings() {
        size(IMAGE_WIDTH, IMAGE_HEIGHT);
    }

    public void setup() {
        noLoop();
        background(248);
    }

    public void draw() {
        PGraphics pg = createGraphics(IMAGE_WIDTH, IMAGE_HEIGHT);
        pg.beginDraw();
        pg.background(248);
        drawContributors(pg, contributors);
        if (models > 0) drawModels(pg, models);
        if (datasets > 0) drawDatasets(pg, datasets);
        if (papers > 0) drawPapers(pg, papers);
        pg.endDraw();

        PImage img = pg.get();
        save("output.png");  // Optional: save image to file for debugging
        String base64Image = convertToBase64(img);
        System.out.println("Base64 Image: " + base64Image);
        // Here you can call Project.getMetadata().setPlaceholder(base64Image);
    }

    private void drawContributors(PGraphics pg, int contributors) {
        int maxContributors = 20;
        int scaleFactor = 15;
        float spacerX = maxContributors - (contributors / (float) maxContributors) * scaleFactor;
        float spacerY = maxContributors - (contributors / (float) maxContributors) * scaleFactor;
        float dotSize = spacerX;

        float randomShift = random(-60, 60);
        int fromColor = color(172 + randomShift / 4, 15, 180, 0.9f);
        int toColor = color(172 + randomShift, 20, 80, 0.9f);

        pg.noStroke();
        pg.colorMode(HSB);

        for (int side = -1; side <= 1; side += 2) {
            spacerX = maxContributors - (contributors / (float) maxContributors) * scaleFactor;
            spacerY = maxContributors - (contributors / (float) maxContributors) * scaleFactor;
            for (int iY = 10; iY < IMAGE_HEIGHT; iY += spacerY) {
                for (int iX = IMAGE_WIDTH / 2; iX < IMAGE_WIDTH && iX > -IMAGE_WIDTH; iX += spacerX * side) {
                    pg.fill(lerpColor(fromColor, toColor, iY / (float) IMAGE_HEIGHT));
                    pg.ellipse(iX, iY, (iY / (float) IMAGE_HEIGHT) * dotSize, (iY / (float) IMAGE_HEIGHT) * dotSize);

                    for (int glow = 2; glow < dotSize / 1.25; glow++) {
                        pg.fill(0, 0, 100, 0.05f);
                        pg.ellipse(iX - dotSize / 8, iY - dotSize / 8, (iY / (float) IMAGE_HEIGHT) * glow, (iY / (float) IMAGE_HEIGHT) * glow);
                    }
                }
                spacerY *= 1.1;
                spacerX *= 1.1;
            }
        }
    }

    private void drawModels(PGraphics pg, int models) {
        float x, y;
        float dotSize = 10;
        int spacing = 6;
        for (int i = 0; i < models; i++) {
            x = random(IMAGE_WIDTH);
            y = random(IMAGE_HEIGHT);
            pg.noStroke();
            pg.fill(255, 0, 0);
            pg.ellipse(x, y, dotSize, dotSize);

            for (int j = 2; j < dotSize / 1.25; j++) {
                pg.fill(255, 0, 0, 50);
                pg.ellipse(x - dotSize / 8, y - dotSize / 8, j, j);
            }
        }
    }

    private void drawDatasets(PGraphics pg, int datasets) {
        float x = IMAGE_WIDTH / 2;
        float y = IMAGE_HEIGHT / 2;
        float dotSize = 10;
        for (int i = 0; i < datasets; i++) {
            pg.noStroke();
            pg.fill(0, 255, 0);
            pg.ellipse(x, y, dotSize, dotSize);
            dotSize += 2;
            x += 15;
            y += 10;
            if (x > IMAGE_WIDTH) {
                x = 0;
            }
            if (y > IMAGE_HEIGHT) {
                y = 0;
            }
        }
    }

    private void drawPapers(PGraphics pg, int papers) {
        for (int i = 1; i <= papers; i++) {
            float xPos = map(i % 10, 0, 10, 20, IMAGE_WIDTH - 10, true);
            float yPos = random(10, IMAGE_HEIGHT - 20);
            float circleSize = map(papers, 0, 50, 20, 10, true) * random(1, 2);
            drawCircle(pg, xPos, yPos, circleSize);
        }
    }

    private void drawCircle(PGraphics pg, float cx, float cy, float circleSize) {
        pg.noStroke();
        pg.colorMode(HSB);
        float randomHue = random(50, 200);

        pg.fill(randomHue, 100, 50);
        pg.ellipse(cx, cy, circleSize, circleSize);

        for (int i = 1; i < 50; i++) {
            pg.fill(randomHue, 10, 100, 0.05f);
            pg.ellipse(cx - circleSize / 8, cy - circleSize / 8, circleSize * (i / 50.0f), circleSize * (i / 50.0f));
        }
    }

    private String convertToBase64(PImage img) {
        BufferedImage bImage = (BufferedImage) img.getNative();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, "png", bos);
            byte[] imageBytes = bos.toByteArray();
            return "data:image/png;base64," + DatatypeConverter.printBase64Binary(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        Placeholder placeholder = new Placeholder(10, 15, 5, 20);
        PApplet.runSketch(new String[]{"Placeholder"}, placeholder);
    }
}
YohannParis commented 3 weeks ago

This will be replaced by finite list of images.