Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
ledyba committed Jul 23, 2021
1 parent a82fbd0 commit 902b846
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions server/src/controller/omote/RandomSelectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as protocol from 'lib/protocol';

import Shelf from '../../shelf/Shelf';
import {formatMomentPath, formatMomentTime, MomentSummary} from '../../shelf/Moment';
import dayjs from "dayjs";

export interface RandomSelectionControllerInterface extends RequestGenericInterface {
Querystring: {
Expand All @@ -23,16 +24,28 @@ export default class RandomSelectionController {
const size = parseInt(req.query.size, 10);
const moments: MomentSummary[] = await this.shelf.findMomentSummariesByRandom(size);
const results: protocol.Moment.Search.Response[] = [];
const yearStarts: { [n: number]: dayjs.Dayjs } = {};
const yearLengths: { [n: number]: number } = {};
for (const m of moments) {
if(m.iconID === undefined) {
continue;
}
if(m.timestamp === undefined) {
continue;
}
const start = m.timestamp.startOf('year');
const end = m.timestamp.endOf('year');
const angle = (m.timestamp.diff(start) / end.diff(start)) * Math.PI * 2;
const year = m.timestamp.year();
let start = yearStarts[year];
if (start === undefined) {
start = m.timestamp.startOf('year');
yearStarts[year] = start;
}
let yearLength = yearLengths[year];
if (yearLength === undefined) {
const end = m.timestamp.endOf('year');
yearLength = end.diff(start);
yearLengths[year] = yearLength;
}
const angle = (m.timestamp.diff(start) / yearLength) * Math.PI * 2;
const p = formatMomentPath(m.timestamp);
results.push({
angle: angle,
Expand Down

0 comments on commit 902b846

Please sign in to comment.