From 608624252466acf9f1d9ee1c1170bd4fe4d18d18 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 15 Nov 2017 11:00:25 +0100 Subject: Rename Pod -> Server --- server/models/pod/index.ts | 1 - server/models/pod/pod-interface.ts | 61 --------- server/models/pod/pod.ts | 248 ------------------------------------- 3 files changed, 310 deletions(-) delete mode 100644 server/models/pod/index.ts delete mode 100644 server/models/pod/pod-interface.ts delete mode 100644 server/models/pod/pod.ts (limited to 'server/models/pod') diff --git a/server/models/pod/index.ts b/server/models/pod/index.ts deleted file mode 100644 index d2bf50d4d..000000000 --- a/server/models/pod/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './pod-interface' diff --git a/server/models/pod/pod-interface.ts b/server/models/pod/pod-interface.ts deleted file mode 100644 index 6c5aab3fa..000000000 --- a/server/models/pod/pod-interface.ts +++ /dev/null @@ -1,61 +0,0 @@ -import * as Sequelize from 'sequelize' -import * as Promise from 'bluebird' - -// Don't use barrel, import just what we need -import { Pod as FormattedPod } from '../../../shared/models/pods/pod.model' -import { ResultList } from '../../../shared/models/result-list.model' - -export namespace PodMethods { - export type ToFormattedJSON = (this: PodInstance) => FormattedPod - - export type CountAll = () => Promise - - export type IncrementScores = (ids: number[], value: number) => Promise<[ number, PodInstance[] ]> - - export type List = () => Promise - - export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList > - - export type ListAllIds = (transaction: Sequelize.Transaction) => Promise - - export type ListRandomPodIdsWithRequest = (limit: number, tableWithPods: string, tableWithPodsJoins: string) => Promise - - export type ListBadPods = () => Promise - - export type Load = (id: number) => Promise - - export type LoadByHost = (host: string) => Promise - - export type RemoveAll = () => Promise - - export type UpdatePodsScore = (goodPods: number[], badPods: number[]) => void -} - -export interface PodClass { - countAll: PodMethods.CountAll - incrementScores: PodMethods.IncrementScores - list: PodMethods.List - listForApi: PodMethods.ListForApi - listAllIds: PodMethods.ListAllIds - listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest - listBadPods: PodMethods.ListBadPods - load: PodMethods.Load - loadByHost: PodMethods.LoadByHost - removeAll: PodMethods.RemoveAll - updatePodsScore: PodMethods.UpdatePodsScore -} - -export interface PodAttributes { - id?: number - host?: string - score?: number | Sequelize.literal // Sequelize literal for 'score +' + value -} - -export interface PodInstance extends PodClass, PodAttributes, Sequelize.Instance { - createdAt: Date - updatedAt: Date - - toFormattedJSON: PodMethods.ToFormattedJSON, -} - -export interface PodModel extends PodClass, Sequelize.Model {} diff --git a/server/models/pod/pod.ts b/server/models/pod/pod.ts deleted file mode 100644 index 6d270ad7f..000000000 --- a/server/models/pod/pod.ts +++ /dev/null @@ -1,248 +0,0 @@ -import { map } from 'lodash' -import * as Sequelize from 'sequelize' - -import { FRIEND_SCORE, PODS_SCORE } from '../../initializers' -import { logger, isHostValid } from '../../helpers' - -import { addMethodsToModel, getSort } from '../utils' -import { - PodInstance, - PodAttributes, - - PodMethods -} from './pod-interface' - -let Pod: Sequelize.Model -let toFormattedJSON: PodMethods.ToFormattedJSON -let countAll: PodMethods.CountAll -let incrementScores: PodMethods.IncrementScores -let list: PodMethods.List -let listForApi: PodMethods.ListForApi -let listAllIds: PodMethods.ListAllIds -let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest -let listBadPods: PodMethods.ListBadPods -let load: PodMethods.Load -let loadByHost: PodMethods.LoadByHost -let removeAll: PodMethods.RemoveAll -let updatePodsScore: PodMethods.UpdatePodsScore - -export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { - Pod = sequelize.define('Pod', - { - host: { - type: DataTypes.STRING, - allowNull: false, - validate: { - isHost: value => { - const res = isHostValid(value) - if (res === false) throw new Error('Host not valid.') - } - } - }, - score: { - type: DataTypes.INTEGER, - defaultValue: FRIEND_SCORE.BASE, - allowNull: false, - validate: { - isInt: true, - max: FRIEND_SCORE.MAX - } - } - }, - { - indexes: [ - { - fields: [ 'host' ], - unique: true - }, - { - fields: [ 'score' ] - } - ] - } - ) - - const classMethods = [ - countAll, - incrementScores, - list, - listForApi, - listAllIds, - listRandomPodIdsWithRequest, - listBadPods, - load, - loadByHost, - updatePodsScore, - removeAll - ] - const instanceMethods = [ toFormattedJSON ] - addMethodsToModel(Pod, classMethods, instanceMethods) - - return Pod -} - -// ------------------------------ METHODS ------------------------------ - -toFormattedJSON = function (this: PodInstance) { - const json = { - id: this.id, - host: this.host, - score: this.score as number, - createdAt: this.createdAt - } - - return json -} - -// ------------------------------ Statics ------------------------------ - -countAll = function () { - return Pod.count() -} - -incrementScores = function (ids: number[], value: number) { - const update = { - score: Sequelize.literal('score +' + value) - } - - const options = { - where: { - id: { - [Sequelize.Op.in]: ids - } - }, - // In this case score is a literal and not an integer so we do not validate it - validate: false - } - - return Pod.update(update, options) -} - -list = function () { - return Pod.findAll() -} - -listForApi = function (start: number, count: number, sort: string) { - const query = { - offset: start, - limit: count, - order: [ getSort(sort) ] - } - - return Pod.findAndCountAll(query).then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) -} - -listAllIds = function (transaction: Sequelize.Transaction) { - const query = { - attributes: [ 'id' ], - transaction - } - - return Pod.findAll(query).then(pods => { - return map(pods, 'id') - }) -} - -listRandomPodIdsWithRequest = function (limit: number, tableWithPods: string, tableWithPodsJoins: string) { - return Pod.count().then(count => { - // Optimization... - if (count === 0) return [] - - let start = Math.floor(Math.random() * count) - limit - if (start < 0) start = 0 - - const subQuery = `(SELECT DISTINCT "${tableWithPods}"."podId" FROM "${tableWithPods}" ${tableWithPodsJoins})` - const query = { - attributes: [ 'id' ], - order: [ - [ 'id', 'ASC' ] - ], - offset: start, - limit: limit, - where: { - id: { - [Sequelize.Op.in]: Sequelize.literal(subQuery) - } - } - } - - return Pod.findAll(query).then(pods => { - return map(pods, 'id') - }) - }) -} - -listBadPods = function () { - const query = { - where: { - score: { - [Sequelize.Op.lte]: 0 - } - } - } - - return Pod.findAll(query) -} - -load = function (id: number) { - return Pod.findById(id) -} - -loadByHost = function (host: string) { - const query = { - where: { - host: host - } - } - - return Pod.findOne(query) -} - -removeAll = function () { - return Pod.destroy() -} - -updatePodsScore = function (goodPods: number[], badPods: number[]) { - logger.info('Updating %d good pods and %d bad pods scores.', goodPods.length, badPods.length) - - if (goodPods.length !== 0) { - incrementScores(goodPods, PODS_SCORE.BONUS).catch(err => { - logger.error('Cannot increment scores of good pods.', err) - }) - } - - if (badPods.length !== 0) { - incrementScores(badPods, PODS_SCORE.PENALTY) - .then(() => removeBadPods()) - .catch(err => { - if (err) logger.error('Cannot decrement scores of bad pods.', err) - }) - } -} - -// --------------------------------------------------------------------------- - -// Remove pods with a score of 0 (too many requests where they were unreachable) -async function removeBadPods () { - try { - const pods = await listBadPods() - - const podsRemovePromises = pods.map(pod => pod.destroy()) - await Promise.all(podsRemovePromises) - - const numberOfPodsRemoved = pods.length - - if (numberOfPodsRemoved) { - logger.info('Removed %d pods.', numberOfPodsRemoved) - } else { - logger.info('No need to remove bad pods.') - } - } catch (err) { - logger.error('Cannot remove bad pods.', err) - } -} -- cgit v1.2.3