How to get a next value of a dataframe if a condition

Viewed 29

I have the following dataframe:

ID conversation Type of message Time of the message
131 user 09/09/22 01:45:59 PM
131 user 09/09/22 01:53:16 PM
131 user 09/09/22 01:53:33 PM
131 admin 09/09/22 02:01:13 PM
131 user 09/09/22 02:01:56 PM
131 admin 09/09/22 02:04:53 PM
131 admin 09/09/22 03:15:31 PM
131 admin 09/09/22 03:15:57 PM
131 user 09/09/22 03:17:00 PM
132 user 09/09/22 03:17:59 PM
132 admin 09/09/22 03:18:59 PM

I need to know how to get the time of the next admin message after the first user message in each block ( messages in sequency) to calculated the time between them. I'm using deluge (zoho) but I also tryed excel.

My deluge code:

hours = 1;
unix = horas * 3600 * 1000;
base = '01-Jan-1970 00:00:00';
allchats = {};
lista_mensagem = {};
header = Map();
header.put("authorization",'Bearer xxxxxxxxxxxxxxxxxxxx');
header.put("accept",'application/json');
header.put("content-type",'application/json');
//
raw = Map();
paginacao = Map();
subraw = Map();
subsubraw = List();
raw.put("query",subraw);
subraw.put("operator",'AND');
subraw.put("value",subsubraw);
//
condition1 = Map();
condition1.put("field",'statistics.last_close_at');
condition1.put("operator",'>');
condition1.put("value",(now.tostring().unixEpoch("GMT-3") - unix) / 1000);
//
//
subsubraw.add(condition1);
search_chats = invokeurl
[
    url :"https://api.intercom.io/conversations/search"
    type :POST
    parameters:raw.toString()
    headers:header
];
chats_fechados = search_chats.get("total_count");
// info chats_fechados;
//for each  chat in search_chats.get("conversations")
// {
//  allchats.add(chat.get("id"));
// }
// info allchats;
allchats.add('1585208');
for each  conversationId in allchats
{
    getUrl("https://httpstat.us/200?sleep=500");
    search_conversations = invokeurl
    [
        url :"https://api.intercom.io/conversations/" + conversationId
        type :GET
        headers:header
    ];
    first_admin_reply = search_conversations.get('statistics').get('first_admin_reply_at');
    ID_conversa = search_conversations.get('id');
    last_close_at = search_conversations.get('statistics').get('last_close_at');
    contact_id = search_conversations.get('contacts').get('contacts').get(0).get('id');
    author_source = search_conversations.get('source').get('author').get('type');
    if(first_admin_reply != null)
    {
        info first_admin_reply;
        lista_conversas = search_conversations.get('conversation_parts').get('conversation_parts');
        for each  mensagem in lista_conversas
        {
            if(mensagem.get('part_type') = 'comment' && mensagem.get('body') != null && mensagem.get('author').get('type') != 'bot')
            {
                if(mensagem.get('created_at') > first_admin_reply)
                {
                    texto = mensagem.get('body');
                    tipo = mensagem.get('author').get('type');
                    hora = mensagem.get('created_at');
                    if(mensagem.get('author').get('name') != null)
                    {
                        nome = mensagem.get('author').get('name');
                    }
                    dados = ID_conversa + ',' + tipo + ',' + hora + ',' + nome;
                    lista_mensagem.add(dados.tolist());
                }
            }
        }
        //info lista_mensagem;
        iteracao = 0;
        lista_posicao_admins = {};
        try 
        {
            for each  mensagem in lista_mensagem
            {
                if(mensagem.get(1) = 'admin')
                {
                    lista_posicao_admins.add(iteracao);
                }
                if(mensagem.get(0) = 'admin')
                {
                    lista_posicao_admins.add(iteracao);
                }
                iteracao = iteracao + 1;
            }
            //          info lista_posicao_admins;
            tempos_de_respostas = {};
            info_conversa = {};
            info lista_posicao_admins;
            for each  posicao_mensagem_admin in lista_posicao_admins
            {
                info posicao_mensagem_admin;
                if(posicao_mensagem_admin > 0)
                {
                    try 
                    {
                        if(lista_mensagem.get(posicao_mensagem_admin - 1).get(1) = 'user')
                        {
                            //info (lista_posicao_admins.indexof(posicao_mensagem_admin));
                            bloco = lista_mensagem.subList(lista_posicao_admins.get(lista_posicao_admins.indexof(posicao_mensagem_admin) - 1),posicao_mensagem_admin + 1);
                            //info bloco;
                            tempos_de_respostas.add(bloco.get(bloco.size() - 1).get(2) - bloco.get(1).get(2));
                            info_conversa.add(bloco.get(bloco.size() - 1).get(2) - bloco.get(1).get(2) + ',' + bloco.get(bloco.size() - 1).get(0) + ',' + bloco.get(bloco.size() - 1).get(1) + ',' + bloco.get(bloco.size() - 1).get(3));
                        }
                    }
                    catch (e)
                    {
                        //info e;
                        errorData = Map();
                        queryData = Map();
                        errorData.put("Conversa ID",ID_conversa);
                        errorData.put("Closing time",base.addseconds(last_close_at).addHour(-3).tostring("yyyy-MM-dd'T'HH:mm:ss-03:00"));
                        errorData.put("Erro",e.tostring());
                        registrar_erro = zoho.sheet.createRecords("j9yima4e8755a0e70480fb442ef1a6bb7f671","Erros",errorData,queryData,"zohocrmsheets");
                    }
                }
            }
            //          info ID_conversa;
            //          info tempos_de_respostas;
            //          info "tempos_de_respostas";
            maximo = tempos_de_respostas.largest();
            for each  conversa in info_conversa
            {
                if(conversa.get(0) = maximo)
                {
                    detalhes_conversa = conversa;
                }
            }
            //info detalhes_conversa;
            header_company = Map();
            header_company.put("authorization",'Bearer xxxxxxxxxxxxx');
            header_company.put("accept",'application/json');
            search_company = invokeurl
            [
                url :"https://api.intercom.io/contacts/" + contact_id + "/companies"
                type :GET
                headers:header_company
            ];
            if(search_company.contains('not_found') = false)
            {
                id_ideris = search_company.get('data').get(0).get('company_id');
                tier = search_company.get('data').get(0).get('custom_attributes').get('Tier');
            }
            else if(search_company.contains('not_found') = true)
            {
                id_ideris = "";
                tier = "";
            }
            ConversationData = Map();
            ConversationData.put("ID conversa",ID_conversa);
            ConversationData.put("Tempo maximo",detalhes_conversa.get(0));
            ConversationData.put("Analista name",detalhes_conversa.get(3));
            ConversationData.put("company id",id_ideris);
            ConversationData.put("tier",tier);
            ConversationData.put("Closing time",base.addseconds(last_close_at).addHour(-3).tostring("yyyy-MM-dd'T'HH:mm:ss-03:00"));
            queryData = Map();
            registrar_planilha = zoho.sheet.createRecords("xxxxxxxxxxxx1","Sheet1",ConversationData,queryData,"zohocrmsheets");
            //  maixmo = tempos_de_respostas.largest();
            info registrar_planilha;
        }
        catch (e)
        {
            errorData = Map();
            queryData = Map();
            errorData.put("Conversa ID",ID_conversa);
            errorData.put("Closing time",base.addseconds(last_close_at).addHour(-3).tostring("yyyy-MM-dd'T'HH:mm:ss-03:00"));
            registrar_erro = zoho.sheet.createRecords("axxxaaxxxxxxxxxx1","Erros",errorData,queryData,"zohocrmsheets");
        }
    }
    tempos_de_respostas = {};
    info_conversa = {};
    lista_posicao_admins = {};
    lista_mensagem = {};
    iteracao = 0;
}

0 Answers
Related