001/*
002 * Copyright 2025 devteam@scivicslab.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing,
011 * software distributed under the License is distributed on an
012 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied.  See the License for the
014 * specific language governing permissions and limitations
015 * under the License.
016 */
017
018package com.scivicslab.actoriac.report.sections.basic;
019
020import com.scivicslab.actoriac.log.DistributedLogStore;
021import com.scivicslab.pojoactor.core.Action;
022import com.scivicslab.pojoactor.core.ActionResult;
023import com.scivicslab.pojoactor.workflow.IIActorRef;
024import com.scivicslab.pojoactor.workflow.IIActorSystem;
025
026import java.sql.Connection;
027import java.util.logging.Logger;
028
029/**
030 * IIAR wrapper for {@link GpuSummarySection}.
031 *
032 * <p>Exposes the POJO's methods as actions via {@code @Action} annotations.
033 * Handles database connection and session ID retrieval from the system.</p>
034 *
035 * <h2>Usage in workflow YAML:</h2>
036 * <pre>{@code
037 * - actor: loader
038 *   method: createChild
039 *   arguments: ["reportBuilder", "gpuSummary", "...GpuSummarySectionIIAR"]
040 * }</pre>
041 *
042 * <p>GPU information is collected from workflow logs that contain GPU INFO
043 * markers. The workflow must first collect GPU information using a
044 * sub-workflow that outputs nvidia-smi or ROCm data.</p>
045 *
046 * @author devteam@scivicslab.com
047 * @since 2.16.0
048 */
049public class GpuSummarySectionIIAR extends IIActorRef<GpuSummarySection> {
050
051    private static final Logger logger = Logger.getLogger(GpuSummarySectionIIAR.class.getName());
052
053    /**
054     * Constructs the IIAR with a new POJO instance.
055     *
056     * @param actorName the actor name
057     * @param system the actor system
058     */
059    public GpuSummarySectionIIAR(String actorName, IIActorSystem system) {
060        super(actorName, new GpuSummarySection(), system);
061        initializeFromSystem();
062    }
063
064    /**
065     * Initializes the POJO with database connection and session ID.
066     */
067    private void initializeFromSystem() {
068        // Get database connection from DistributedLogStore
069        DistributedLogStore logStore = DistributedLogStore.getInstance();
070        if (logStore != null) {
071            Connection conn = logStore.getConnection();
072            if (conn != null) {
073                object.setConnection(conn);
074                logger.fine("GpuSummarySectionIIAR: initialized database connection");
075            }
076        }
077
078        // Get session ID from nodeGroup
079        long sessionId = getSessionIdFromNodeGroup();
080        if (sessionId >= 0) {
081            object.setSessionId(sessionId);
082            logger.fine("GpuSummarySectionIIAR: initialized sessionId=" + sessionId);
083        }
084    }
085
086    /**
087     * Retrieves session ID from nodeGroup actor.
088     */
089    private long getSessionIdFromNodeGroup() {
090        if (actorSystem == null || !(actorSystem instanceof IIActorSystem)) {
091            return -1;
092        }
093        IIActorSystem iiSystem = (IIActorSystem) actorSystem;
094
095        IIActorRef<?> nodeGroup = iiSystem.getIIActor("nodeGroup");
096        if (nodeGroup == null) {
097            return -1;
098        }
099
100        ActionResult result = nodeGroup.callByActionName("getSessionId", "");
101        if (result.isSuccess()) {
102            try {
103                return Long.parseLong(result.getResult());
104            } catch (NumberFormatException e) {
105                logger.warning("GpuSummarySectionIIAR: invalid sessionId: " + result.getResult());
106            }
107        }
108        return -1;
109    }
110
111    // ========================================================================
112    // Actions - expose POJO methods
113    // ========================================================================
114
115    @Action("generate")
116    public ActionResult generate(String args) {
117        String content = object.generate();
118        return new ActionResult(true, content);
119    }
120
121    @Action("getTitle")
122    public ActionResult getTitle(String args) {
123        String title = object.getTitle();
124        return new ActionResult(true, title != null ? title : "");
125    }
126}