实时推荐部分代码

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;
// 获取用户的观看电影的卡片
let getreadCards = async(opts) => {
  opts = opts || {};
  let id = opts.id;
  if (!id) {
    return {
      total: 0,
      data: []
    };
  }
  let 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;
};
// 获取每个卡片对应的电影
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 获得用户看过和不喜欢看的视频  userprefer
let 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 用户的实时记录
  let readRecords = timed(‘personRec.readRecords‘, async() => {
    let doRead = await RecDB.findOne({
      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;
};
// 获取用户的离线三个月的记录
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.findOne({
      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;
};
  // 获取在线用户推荐数据
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‘);
   // 获取分组对应的 collection
  let collectionName = config.groupMapping[group];
  console.log(collectionName);
  let macId = opts.macId;
  if (!macId || !collectionName) {
    return {
      total: 0,
      data: []
    };
  }
  let 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(),
    getUserRecords({macId}),
    getoldRecords({macId})
  ]);
  // console.log(‘----------------‘);
  // console.log(oldrecord);
  // console.log(‘----------------‘);
  // console.log(macId, collectionName, res)
  for (let video of record) {
  // console.log(video)
    let append = await getRealtimeVideos({videoId: video, group: ‘B‘ });
    let appendLabel = await getreadCards({id: video});
    realtimeCard = _.uniq(_.concat(realtimeCard, appendLabel));
    // console.log(JSON.stringify(append));
    realtime = _.concat(realtime, append);
  }
  // 将获取的卡片转换成电影
  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) => {
    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,
};

  

时间: 2025-01-06 04:35:52

实时推荐部分代码的相关文章

WordPress官方推荐的代码高亮插件

今天花了大半天的时间,终于选择好自己心仪的wordpress代码高亮插件,同时它也是wordpress官方推荐的代码语法高亮插件.在选择的过程中,我对比和试用了若干个代码高亮wordpress插件,包括有国人开发的wp-codebox.codecolorer.wp-syntax等,但这些插件不是很久没有更新和最新的wordpress不兼容导致安装不上,就是功能无法满足我的要求.直到我在wordpress官方网站支持文档里看到了我要的效果,以及在文章最后找到了wordpress推荐的代码高亮插件.

Windows Live Writer推荐SyntaxHighlighter代码着色插件

博客园内置支持SyntaxHighlighter代码着色,代码着色语法:<pre class='brush:编程语言'>代码</pre>. 需要注意的是:如何你使用SyntaxHighlighter代码着色,需要将默认编辑器改为TinyMCE.如果使用CuteEditor,CuteEditor会自动去除代码中的空格,造成代码格式破坏. 如果您使用Windows Live Writer写博客(配置步骤),通过Windows Live Writer代码着色插件,可以方便进行Syntax

php 实时推送代码

网站质量不错的网站可以在百度站长平台/数据提交/sitemap栏目下看到实时推送的功能, 目前这个工具是邀请开放, 百度的实时推送的api接口可以实时推送我们新发布的文章, 保证百度在第一时间收录. 百度站长平台 http://zhanzhang.baidu.com/ 打开百度站长平台, 点开实时推送的添加新数据接口获得带token的api推送地址: http://ping.baidu.com/sitemap?site=www.yourdomain.com&resource_name=sitem

Maxim实时时钟芯片设计指南5791-关于编写健壮的实时时钟控制代码的提示

用DS12C887设计一个万年历,虽然反复查看说明书,还是出各种的错误. 因此,从美信官网查询资料,翻译的不太通,凑合着对照看. 原文链接 Tips for Writing Bulletproof Real-Time Clock Control Code 关于编写健壮的实时时钟控制代码的提示 摘要 : With the use of proper device initialization and code sequencing, accurate timekeeping and clock a

蚂蚁金服核心技术:百亿特征实时推荐算法揭秘

本文来自蚂蚁金服人工智能部认知计算组的基础算法团队,文章提出一整套创新算法与架构,通过对TensorFlow底层的弹性改造,解决了在线学习的弹性特征伸缩和稳定性问题,并以GroupLasso和特征在线频次过滤等自研算法优化了模型稀疏性,在支付宝核心推荐业务获得了uvctr的显著提升,并较大地提升了链路效率. 0.综述 在线学习(Online learning)由于能捕捉用户的动态行为,实现模型快速自适应,进而成为提升推荐系统性能的重要工具.然而它对链路和模型的稳定性,训练系统的性能都提出了很高的

文本框实时响应搜索代码

JS代码: $(function () { $("#areaName").keyup(function (evt) { ChangeCoords(); //控制查询结果div坐标 var k = window.event ? evt.keyCode : evt.which; //输入框的id为areaName,这里监听输入框的keyup事件 //不为空 && 不为上箭头或下箭头或回车 if ($("#areaName").val() != "

推荐一个代码行数统计工具cloc

代码行数统计工具cloc,它不但能统计代码行数,还能自动过滤掉代码中的注释,使用起来也很方便,强力推荐. 安装很方便,Ubuntu上直接udo apt-get install cloc就可以了 下面用mysql来做个例子吧,首先进入mysql的源代码文件夹: [email protected] ~/aproject $ cd mysql-5.6.19/ [email protected] ~/aproject/mysql-5.6.19 $ cloc . defined(%hash) is dep

网站推荐的代码自动生成软件实际使用感触

今天测试下我下载的三个代码自动生成软件: Thelei Code Generator 1.1 Autojava myGeneration 实践结果: Thelei Code Generator 1.1 这个软件相当垃圾,生成的项目我为了图简单放在了桌面上,也幸亏是放在了桌面上,桌面上就是写软件快捷方式,没有什么重要且大的文件,如果是放在了磁盘是,此次操作后果不堪设想.原因是我点击生成之前,有个选项英文提示是生成之前先删除目标文件,ok,我存放在了桌面上,结果什么都没有生成,竟然把我桌面上的文件全

2018程序员必备的4大网站推荐(代码、编程、SQL等)

科技时代,程序员是一个热门行业,也是一个高薪行业,很多人很羡慕程序员,其实作为一名合格的程序员,要不断的提升自己,因为科技也在不断进步,所以,接下来小编分享5个程序员必备网站,包括代码.编程.SQL等,需要的朋友可以收藏起来慢慢学哦. 一.百度 百度是我们常用的中文搜索引擎,它包含各个方面的信息,当然程序员方面的知识也不少,包括Javaccript.CSS.代码.软件等等,应有尽有. 二.w3cschool 一个专业的编程入门学习及技术文档查询网站,包括html.PHP.Java等数十种编程语言