<commit_before>package com.shelloid.node.saal.common.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Queue;

import com.shelloid.common.messages.MessageFields;
import com.shelloid.common.messages.MessageTypes;
import com.shelloid.common.messages.ShelloidMessage;
import com.shelloid.query.vo.Literal;
import com.shelloid.querylib.TimedBuffer;
import com.shelloid.querylib.TimedBufferFactory;

public class LogAction
{
    private static LogAction logAction;
    private final TimedBufferFactory<LogEntry> fact;

    private LogAction(File filesDir)
    {
        TimedBufferFactory.configure(filesDir);
        fact = TimedBufferFactory.getInstance(LogEntry.class);
    }

    public boolean log(long currentTs, Object[] params, String streamId, HashMap<String, Literal> vars)
    {
        TimedBuffer<LogEntry> buf = fact.getBuffer(streamId + ".stream");
        long expiryTime = 0;
        HashMap<String, Literal> loggableVars;
        if (params.length > 0)
        {
            int startIdx = 0;
            if (params[0] instanceof Long)
            {
                expiryTime = (Long) params[0];
                startIdx = 1;
            }
            if (params.length > startIdx)
            {
                loggableVars = new HashMap<String, Literal>();
                while (startIdx < params.length)
                {
                    String key = params[startIdx].toString();
                    if (vars.containsKey(key))
                    {
                        loggableVars.put(key, vars.get(key));
                    }
                    startIdx++;
                }
            }
            else
            {
                loggableVars = vars;
            }
        }
        else
        {
            loggableVars = vars;
        }
        return buf.addItem(currentTs, expiryTime, new LogEntry(loggableVars));
    }

    public ShelloidMessage getMessages(String streamId)
    {
        TimedBuffer<LogEntry> buf = fact.getBuffer(streamId + ".stream");
        Queue<LogEntry> msgs = buf.getItems();
        ShelloidMessage msg = new ShelloidMessage();
        msg.put(MessageFields.type, MessageTypes.STREAM_MSG);
        msg.put(MessageFields.streamId, streamId);
        msg.put(MessageFields.subType, MessageTypes.STREAM_LOG);
        ArrayList<HashMap<String, Object>> msgsArray = new ArrayList<HashMap<String, Object>>();
        while (msgs.peek() != null)
        {
            LogEntry entry = msgs.remove();
            HashMap<String, Literal> emsg = entry.msg;
            HashMap<String, String> logs = new HashMap<String, String>();
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put(MessageFields.timestamp, entry.timestamp + "");
            for (Entry<String, Literal> e : emsg.entrySet())
            {
                String val = "";
                Literal l = e.getValue();
                switch (l.kind)
                {
                    case Literal.DOUBLE:
                    {<commit_msg>regular commit<commit_after>package com.shelloid.node.saal.common.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Queue;

import com.shelloid.common.messages.MessageFields;
import com.shelloid.common.messages.MessageTypes;
import com.shelloid.common.messages.ShelloidMessage;
import com.shelloid.query.vo.Literal;
import com.shelloid.querylib.TimedBuffer;
import com.shelloid.querylib.TimedBufferFactory;

public class LogAction
{
    private static final String TAG = "LogAction";
    private static LogAction logAction;
    private final TimedBufferFactory<LogEntry> fact;

    private LogAction(File filesDir)
    {
        TimedBufferFactory.configure(filesDir);
        fact = TimedBufferFactory.getInstance(LogEntry.class);
    }

    @Override
    public void finalize() throws Throwable
    {
        super.finalize();
    }

    public boolean log(long currentTs, Object[] params, String streamId, HashMap<String, Literal> vars)
    {
        boolean res = false;
        try
        {
            TimedBuffer<LogEntry> buf = fact.getBuffer(streamId + ".stream");
            long expiryTime = 0;
            HashMap<String, Literal> loggableVars;
            if (params.length > 0)
            {
                int startIdx = 0;
                if (params[0] instanceof Long)
                {
                    expiryTime = (Long) params[0];
                    startIdx = 1;
                }
                if (params.length > startIdx)
                {
                    loggableVars = new HashMap<String, Literal>();
                    while (startIdx < params.length)
                    {
                        String key = params[startIdx].toString();
                        if (vars.containsKey(key))
                        {
                            loggableVars.put(key, vars.get(key));
                        }
                        startIdx++;
                    }
                }
                else
                {
                    loggableVars = vars;
                }
            }
            else
            {
                loggableVars = vars;
            }
            res = buf.addItem(currentTs, expiryTime, new LogEntry(loggableVars));
            buf.cleanup();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return res;
    }

    public ShelloidMessage getMessages(String streamId)
    {
        TimedBuffer<LogEntry> buf = fact.getBuffer(streamId + ".stream");
        Queue<LogEntry> msgs = buf.getItems();
        ShelloidMessage msg = new ShelloidMessage();
        msg.put(MessageFields.type, MessageTypes.STREAM_MSG);
        msg.put(MessageFields.streamId, streamId);
        msg.put(MessageFields.subType, MessageTypes.STREAM_LOG);
        ArrayList<HashMap<String, Object>> msgsArray = new ArrayList<HashMap<String, Object>>();
        while (msgs.peek() != null)
        {
            LogEntry entry = msgs.remove();
            HashMap<String, Literal> emsg = entry.msg;
            HashMap<String, String> logs = new HashMap<String, String>();
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put(MessageFields.timestamp, entry.timestamp + "");
            for (Entry<String, Literal> e : emsg.entrySet())
            {
                String val = "";
                Literal l = e.getValue();
                switch (l.kind)
                {
                    case Literal.DOUBLE:
                    {
{'fraction_non_alphanumeric': 0.09998496466696738, 'fraction_numerical': 0.0018042399639152007, 'mean_word_length': 1.7453569954601733, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5632266', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1760, 'n_words': 367}