Real-time recommendation Section code

Source: Internet
Author: User

Import _ from ' Lodash ', import cfg from '. /cfg/cfg '; import {Response} from '. /shared/lib/response '; import {recdb} from '. /lib/mongo.js '; import {timed} from '. /utils/metrics '; let config = cfg.recommend.mongo;//gets the user's Watch movie card Let Getreadcards = Async (opts) + {opts = opts | |  {};  Let id = opts.id;  if (!id) {return {total:0, data: []};        Readcards = timed (' Personrec.readcards ', async () = {Let Doread = await Recdb.findone ({query: {    Id:opts.id}, Collection: ' label '});  return doread;  });  Let res = await readcards (); Let results = res?  Res.result:null; Results = Results | |  [];  Let videos = _.map (results, ' cards '); Return videos;};/ /get each card corresponding to the movie Let Getrealtimecardvideos = Async (opts) = {opts = opts | |  {};  Let cards = opts.cards;  if (!cards) {return {total:0, data: []}; } Let Read = Timed (' Personrec.realtimecardvideos ', async () + = {Let Doread = await Recdb.findone ({query: {Cards:opts.cards}, collection: ' Label1 '});  return doread;  });  Let res = await read (); res = res?   Res.result:null;  Console.log (Json.stringify (res)); res = Res | |  [];  Let videos = _.chain (res). ForEach ((val) = = {Val.type = ' realtimecard ';  }). value (); Return videos;};/ /Getuserprefer Get the video that the user has seen and dislike to watch userpreferlet Getuserprefer = Async (opts) + {opts = opts | |  {};  Let macId = Opts.macid;  Let reslist = [];  Let res = await recdb.read ({///query: {//Mac:macid//},//collection: ' Userprefer '//});        Let read = timed (' Personalrec.readuserprefervideos ', async () + = {Let Doread = await Recdb.read ({query: {    MAC:MACID}, Collection: ' Userprefer '});  return doread;  });  Let res = await read ();    _.foreach (Res, (val) + = {Let notlike = [];    let seen = [];    if (!val.isnotlike) {notlike = _.split (val.isnotlike, ': '); } if (!val.isseen) {seen = _.split (VAL.isseen, ': ');  } reslist = _.concat (Notlike, seen);  }); return reslist;}; Let Getuserrecords = Async (opts) = {opts = opts | |  {};  Let macId = Opts.macid;  if (!macid) {return {total:0, data: []}; }//Userrecord user's real-time record let ReadRecords = timed (' Personrec.readrecords ', async () + = Doread = await Recdb.fin    Done ({query: {mac:opts.macId}, collection: ' Temp '});  return doread;  });  Let res = await readrecords (); Let result = res?  Res.vv:null; result = Result | |  [];  Let videos = _.map (result, ' id '); Return videos;};/ /Get the user's offline three-month record let Getoldrecords = Async (opts) + {opts = opts | |  {};  Let macId = Opts.macid;  if (!macid) {return {total:0, data: []}; }//Oldrecord Let Readoldrecords = timed (' Personrec.readoldrecords ', async () = {Let Doread = await Recdb.findon    E ({query: {mac:opts.macId}, collection: ' Userrecord '});  return doread;  }); Let res = await readoldrecords (); Let record = res?  Res.result:null; Record = Record | |  [];  Let videos = _.map (record, ' id ');  return videos;}; Get online user referral data Let Getrealtimevideos = Async (opts) = {opts = opts | |  {};  Let videoid = opts.videoid;  if (!videoid) {return {total:0, data: []};        } Let Read = Timed (' Personrec.realtimevideos ', async () + = {Let Doread = await Recdb.findone ({query: {    VideoId:opts.videoId, Group:opts.group}, collection: ' Related '});  return doread;  });  Let res = await read (); res = res?   Res.result:null;  Console.log (Json.stringify (res)); res = Res | |  [];    Let videos = _.chain (res). ForEach ((val) = = {Val.group = Opts.group;  Val.type = ' realtime ';  }). value (); return videos;}; Let Getpersonalvideos = Async (opts) = {opts = opts | |  {};  Let page = Opts.page;  Let pageSize = opts.pagesize;  Let realtime = [];  Let Realtimecard = []; Let group = Opts.group | | ConfIg.defaultgroup;   Console.log (group+ ' 11111 ');  Gets the group corresponding collection let CollectionName = Config.groupmapping[group];  Console.log (CollectionName);  Let macId = Opts.macid;  if (!macid | |!collectionname) {return {total:0, data: []}; Readvideos = timed (' Personalrec.readpersonalvideos ', async () = {Let Doread = await Recdb.read ({query    : {mac:macid}, collection:collectionname});  return doread;  }); Let [Userprefers, res, record, oldrecord] = await promise.all ([Getuserprefer ({macId}), Readvideos (), Getuserreco  RDS ({macId}), Getoldrecords ({macId})]);  Console.log ('----------------');  Console.log (Oldrecord);  Console.log ('----------------'); Console.log (MacId, CollectionName, res) for (let video of the record) {//Console.log (VIDEO) Let append = await getre    Altimevideos ({videoid:video, group: ' B '});    Let Appendlabel = await getreadcards ({id:video}); Realtimecard = _.uniq (_.concat (Realtimecard, Appendlabel));    Console.log (json.stringify (append));  Realtime = _.concat (realtime, append);  }//Convert the acquired card into a movie let Cardvideos = [];    For (Let Labe of Realtimecard) {Let card = await Getrealtimecardvideos ({cards:labe});  Cardvideos = _.uniq (_.concat (Cardvideos, card), ' id ');  } console.log (Cardvideos);  Userprefers = _.concat (userprefers, record);     Let real = _.chain (realtime). Map (val) + = {Let temp = {id:val.id, group:val.group, Type:val.type    };    Console.log (json.stringify (temp));  return temp;  }). filter (val =!_.includes (userprefers, Val.id)). Value (); res = res?  Res.result:null; res = res?  Res[0].result:null;  Console.log (RES); res = res?  Res.split (', '): [];    Let videos = _.chain (res). Map (val) and {Let [id, weight] = val.split (': ');  return {ID, weight, Group, type: ' personal '}; }). filter (val =!_.includes (userprefers, Val.id)). Value ();//Console.log (JSON. Stringify (real));//videos = _.uniq (_.concat (real, videos), ' id ');  Videos = _.uniq (_.concat (Cardvideos, _.concat (real, videos)), ' id ');  Let start = (page-1) * pageSize;  Let end = page * PAGESIZE;  Let Returnvideos = _.slice (videos, start, end); return {total:videos.length, data:returnvideos};};  Let Realtimerechandler = Async (CTX) + = {Let query = Ctx.query;  Let page = Query.page; Let page = parseint (query.page, 10) | |  1; Let pageSize = parseint (query.pagesize, 10) | |  10;  Let macId = Query.macid;  Let group = Query.group;  Let res = await Getpersonalvideos ({page, pageSize, MacId, group});  Let PageCount = Math.ceil (res.total/pagesize);  return new Response ({data: {page, pageSize, PageCount, MacId, total:res.total, Videos: Res.data});}; Export {Realtimerechandler,};

  

Real-time recommendation Section code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.