Skip to content

Commit

Permalink
feat (Backend): Check every 24 hrs and add stats for players who do n…
Browse files Browse the repository at this point in the history
…ot exist in db #14
  • Loading branch information
cnguy committed May 27, 2018
1 parent 23e03e8 commit 63c86ce
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 20 deletions.
21 changes: 18 additions & 3 deletions api/StatsGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import RegionHelper from 'kayn/dist/lib/Utils/RegionHelper'

const { asPlatformID } = RegionHelper

import { Stats } from './mongodb'
import { Stats, Player } from './mongodb'

import ChampionStats from './entities/ChampionStats'
import OneTrick from './entities/OneTrick'
Expand Down Expand Up @@ -65,7 +65,11 @@ const storePlayerStats = (summonerId: number, json: RawPlayerStats) =>

const getPlayer = (summonerId: number) => Stats.findOne({ summonerId })

const main = async () => {
export enum Modes {
Update, BruteForceAll
}

const main = async (mode = Modes.BruteForceAll) => {
const kayn = Kayn()({
debugOptions: {
isEnabled: true,
Expand Down Expand Up @@ -156,7 +160,18 @@ const main = async () => {
console.log('starting:', rank, region, i, i + chunkSize)
console.log('======')
try {
await makeStats(rank, region, summoners.slice(i, i + chunkSize))
let sliceToProcess: RawSummoner[] = []
if (mode === Modes.BruteForceAll) {
sliceToProcess = summoners.slice(i, i + chunkSize)
} else if (mode == Modes.Update) {
// async filter not possible btw so just gonna use map
const summonersNotInDatabase = (await Promise.all(summoners.slice(i, i + chunkSize).map(async (summoner) => {
const count = await Stats.count({ summonerId: summoner.id }).exec()
return (count === 0) ? summoner : undefined
}))).filter(Boolean)
sliceToProcess = (summonersNotInDatabase as RawSummoner[])
}
await makeStats(rank, region, sliceToProcess)
} catch (exception) {
console.log('makeStats failed...', exception)
}
Expand Down
36 changes: 20 additions & 16 deletions api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as koaRouter from 'koa-router'
import * as mongoose from 'mongoose'

import generator from './OneTricksGenerator'
import StatsGenerator from './StatsGenerator'
import StatsGenerator, { Modes } from './StatsGenerator'
import { getStaticChampionByName } from './getStaticChampion'
import kayn from './kayn'
import MHGenerator from './MatchHistoryGenerator'
Expand Down Expand Up @@ -85,21 +85,25 @@ router.get('/static-champion-by-name/:name/id', async ctx => {
ctx.body = getStaticChampionByName(name).id
})

const main = async () => {
/*
import * as schedule from 'node-schedule'

const main = async (mode = Modes.Update) => {
try {
console.log('starting script')
const done = await generator()
console.log('script done:', done)
setInterval(async () => {
await generator()
}, 86400000)
} catch (exception) {
console.log(exception)
}*/
console.log('start')
const done = await StatsGenerator()
console.log('done')
// docker uses UTC time.
// 7:35 AM UTC => 12:35 AM PST.
schedule.scheduleJob('35 7 * * *', async () => {
console.log('STARTING STATS')
await StatsGenerator(mode)
console.log('END STATS')
if (mode === Modes.Update) {
console.log('START ONE TRICKS')
await generator()
console.log('END ONE TRICKS')
}
})
} catch (ex) {
console.error(ex)
}
}

// main()
main()
2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"koa-router": "^7.4.0",
"koa2-cors": "^2.0.5",
"mongoose": "^5.0.0",
"node-schedule": "^1.3.0",
"typescript": "^2.8.3"
},
"scripts": {
Expand All @@ -24,6 +25,7 @@
"@types/koa2-cors": "^2.0.1",
"@types/mongoose": "^5.0.12",
"@types/node": "^10.0.4",
"@types/node-schedule": "^1.2.2",
"@types/request": "^2.47.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
Expand Down
48 changes: 47 additions & 1 deletion api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@
"@types/mongodb" "*"
"@types/node" "*"

"@types/node-schedule@^1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/node-schedule/-/node-schedule-1.2.2.tgz#bb5beae99b7327b4f2ddc1834f1fdae4378891b0"
dependencies:
"@types/events" "*"
"@types/node" "*"

"@types/node@*", "@types/node@^10.0.4":
version "10.0.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.0.4.tgz#130598ee56e9a1210a53f557d64935571f05390d"
Expand Down Expand Up @@ -1229,6 +1236,13 @@ core-util-is@1.0.2, core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"

cron-parser@^2.4.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/cron-parser/-/cron-parser-2.5.0.tgz#ec1385a3aa8c63624256789ce2ecfa338384676a"
dependencies:
is-nan "^1.2.1"
moment-timezone "^0.5.0"

cryptiles@2.x.x:
version "2.0.5"
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
Expand Down Expand Up @@ -1287,7 +1301,7 @@ deep-is@~0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"

define-properties@^1.1.2:
define-properties@^1.1.1, define-properties@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.2.tgz#83a73f2fea569898fb737193c8f873caf6d45c94"
dependencies:
Expand Down Expand Up @@ -2135,6 +2149,12 @@ is-my-json-valid@^2.10.0:
jsonpointer "^4.0.0"
xtend "^4.0.0"

is-nan@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/is-nan/-/is-nan-1.2.1.tgz#9faf65b6fb6db24b7f5c0628475ea71f988401e2"
dependencies:
define-properties "^1.1.1"

is-number@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-number/-/is-number-2.1.0.tgz#01fcbbb393463a548f2f466cce16dece49db908f"
Expand Down Expand Up @@ -2457,6 +2477,10 @@ lodash@^4.13.1:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

long-timeout@0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/long-timeout/-/long-timeout-0.1.1.tgz#9721d788b47e0bcb5a24c2e2bee1a0da55dab514"

loose-envify@^1.0.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.3.1.tgz#d1a8ad33fa9ce0e713d65fdd0ac8b748d478c848"
Expand Down Expand Up @@ -2562,6 +2586,16 @@ mocha@^4.0.1:
mkdirp "0.5.1"
supports-color "4.4.0"

moment-timezone@^0.5.0:
version "0.5.17"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.17.tgz#3c8fef32051d84c3af174d91dc52977dcb0ad7e5"
dependencies:
moment ">= 2.9.0"

"moment@>= 2.9.0":
version "2.22.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.1.tgz#529a2e9bf973f259c9643d237fda84de3a26e8ad"

mongodb-core@3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/mongodb-core/-/mongodb-core-3.0.1.tgz#ff6dc36ee96ff596953d80a6840d6731bc92efed"
Expand Down Expand Up @@ -2644,6 +2678,14 @@ node-pre-gyp@^0.6.39:
tar "^2.2.1"
tar-pack "^3.4.0"

node-schedule@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/node-schedule/-/node-schedule-1.3.0.tgz#e7a7e816a7f2550d5b170bd106e765db28bdf030"
dependencies:
cron-parser "^2.4.0"
long-timeout "0.1.1"
sorted-array-functions "^1.0.0"

nopt@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/nopt/-/nopt-4.0.1.tgz#d0d4685afd5415193c8c7505602d0d17cd64474d"
Expand Down Expand Up @@ -3262,6 +3304,10 @@ sntp@2.x.x:
dependencies:
hoek "4.x.x"

sorted-array-functions@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/sorted-array-functions/-/sorted-array-functions-1.1.0.tgz#78fe5808ffa1beebac2ce9a22d76039dabc599ff"

source-map-support@^0.4.0, source-map-support@^0.4.15:
version "0.4.18"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.18.tgz#0286a6de8be42641338594e97ccea75f0a2c585f"
Expand Down

0 comments on commit 63c86ce

Please sign in to comment.