首页 > 代码库 > 实时推荐部分代码

实时推荐部分代码

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,
};

  

实时推荐部分代码