Archive task results

Hi Team,

We have a requirement to archive all aggregation task results. So can you please let me know any way to achieve this

Hi @sureshbommareddy98,

In IIQ you cant archieve TaskResult, you maintain or delete.

If you want delete you must declare the number of day of deletion in Global Config → IdentityIQ Settings → Miscellaneous

and mark Prune Task Result flag on Perform Maintance Task
image
and run it.

It will delete all task older than XX days.

Otherwise, you can create a rule where you search the object that you want delete and eliminate them.

Hello Suresh,

Could you please share exact requirement?

We need to do archive of task results

Suppose we have aggregation task which runs daily, we need to do last 10 days task results archive

Suresh you can use schedule custom run rule task that will run daily using below sample code:-

import sailpoint.api.SailPointContext;
import sailpoint.object.TaskResult;
import sailpoint.tools.GeneralException;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TaskResultArchiver {

    public static void run(SailPointContext context) throws GeneralException {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, -10); // 10 days ago
        Date cutoffDate = cal.getTime();

        String hql = "from TaskResult tr where tr.name = 'YourAggregationTaskName' and tr.launchDate >= :cutoffDate";
        List<TaskResult> taskResults = context.createQuery(hql)
                .setParameter("cutoffDate", cutoffDate)
                .list();

        for (TaskResult tr : taskResults) {
             archiveToFile(tr);
            // Optionally remove it from DB
            // context.removeObject(tr); // only if you want to delete
        }
        context.commitTransaction();
    }
}

   private static void archiveToFile(TaskResult tr) {
        try {
            String fileName = "/opt/iiq/archive/" + tr.getName() + "_" + tr.getLaunchDate().getTime() + ".txt";
            FileWriter fw = new FileWriter(fileName);
            fw.write("Task Name: " + tr.getName() + "\n");
            fw.write("Status: " + tr.getStatus() + "\n");
            fw.write("Launch: " + tr.getLaunchDate() + "\n");
            fw.write("Complete: " + tr.getCompletionDate() + "\n");
            fw.write("Message: " + tr.getMessage() + "\n");
            fw.write("Owner: " + tr.getOwner() + "\n");
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

@asharma65

Thanks for the information, there will be a CSV file in the task results, how can we archive that and where it would be means path

For this try this you can try a taskresult rule to archive

import sailpoint.object.TaskResult;
import java.io.File;
import java.util.List;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

TaskResult taskResult = (TaskResult) ruleContext.get("taskResult");

// Archive directory - pls ensure this exists 
String archiveDir = "wherever u want to store/aggregation-results/";

List files = taskResult.getFiles();

if (files != null) {
    for (int i = 0; i < files.size(); i++) {
        File srcFile = (File) files.get(i);
        String fileName = srcFile.getName();
        File destFile = new File(archiveDir + fileName);

        // Copy file manually (since Files.copy is not available in beanshell)
        InputStream in = new FileInputStream(srcFile);
        OutputStream out = new FileOutputStream(destFile);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

      

Hello @sureshbommareddy98
For getting csv file from task result try below custom code:-


import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import sailpoint.object.JasperResult;
import sailpoint.object.TaskResult;
import sailpoint.reporting.JasperRenderer;

public List getTaskResultAttachment(TaskResult tr)
  {
    JasperResult jasperResult = tr.getReport();
	if(jasperResult != null)
	{
		JasperRenderer renderer = new JasperRenderer(jasperResult);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		renderer.renderToCSV(baos,"csv");
		FileOutputStream fos = new FileOutputStream("/your/archive/path/task_result.zip");
		fos.write(baos.toByteArray());
		fos.close();
		
	}
  }

Let me know if this helps.

Hi @sureshbommareddy98 Did you find solution?