jeudi 8 novembre 2018

How to get the top engaged users?

I'm using graph api in my laravel application.I have a stats module when insights of a page are displayed in charts,tables.. .I'm trying to get the top engaged users per page this is my code :

if(Cache::has('timeline'.$id))
{
    // $timeline is a variable containing all the posts from the last 28 days
    $timeline     = Cache::get('timeline'.$id);
    //comments of every post
    foreach ($timeline as $key => $value) 
    {
        if(Cache::has('comments'.$value['id']))
        {
            $results = Cache::get('comments'.$value['id']);
            foreach ($results as $key2 => $value2) 
            {
                $users_comments[] = json_decode($value2,true);
            }
        }
        else
        {
            $results = $fb->get('/'.$value['id'].'/comments?fields=id,message,from{name,picture}&limit=1000000')->getGraphEdge();
            foreach ($results as $key2 => $value2) 
            {
                $users_comments[] = json_decode($value2,true);
            }
        }
    }
    $result = array();
    foreach ($users_comments as $key => $value) 
    {
        if(!(in_array($value['from']['id'],array_column($result,'id'))))
        {
            $result[] = ['nb'=>1,'name'=>$value['from']['name'],'id'=>$value['from']['id'],'picture'=>$value['from']['picture']['url']]; 
        }
        else
        {
            foreach ($result as $key2 => $value2) {
                if($value['from']['id'] == $value2['id'])
                {
                    $result[$key2]['nb'] = $result[$key2]['nb']+1;
                }
            }

        }

    }
    usort($result, array($this,'cmp'));
    $tops       = array_slice($result, 0, 6);
    //Cache::put('tops'.$id,$tops, 30);
}

Example of "$timeline":

[  
 {  
  id:"1522553147754743_2223304471012937",
  message:"Fini l’été! Fini le bronzage! La meilleure période pour commencer 
  vos séances de laser est la saison d’hiver mais les brûlures qui les 
  causent sont moches. Pour les éviter, appliquez votre produit miracle 
  PHYTÉAL gel apaisant anti brûlures et rougeurs à base d’aloevera. PHYTÉAL, 
  votre partenaire beauté!",
  type:"photo",
  created_time:{  
     date:"2018-11-08 09:03:46.000000",
     timezone_type:1,
     timezone:"+00:00"
  },
  full_picture:"https://scontent.xx.fbcdn.net/v/t1.0-9/p720x720/45634836_2223303954346322_8836260595742277632_o.jpg?_nc_cat=107&_nc_ht=scontent.xx&oh=3ff01a1f254e3808b829d8cbca139737&oe=5C439300",
  from:{  
     name:"Phytéal",
     picture:{  
        height:50,
        is_silhouette:false,
        url:"https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/44870737_2204034426273275_4202106881835008000_n.jpg?_nc_cat=105&_nc_ht=scontent.xx&oh=9dd2390b32be4d89b756ad980e99bc37&oe=5C7D061E",
        width:50
     },
     id:"1522553147754743"
    }
 }
]

I am getting the results i need but i wanted to know is there better way to do it so i can optimize my code and have less waiting time for response cause i already have many other requests ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire