-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from HannesOberreiter/beta
Beta
- Loading branch information
Showing
11 changed files
with
212 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export const up = function (knex) { | ||
return knex.schema.alterTable('observations', (t) => { | ||
t.string('external_uuid', 65); | ||
}); | ||
}; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
export const down = function (knex) { | ||
return knex.schema.alterTable('observations', (t) => { | ||
t.dropColumn('external_uuid'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { FastifyRequest, FastifyReply } from 'fastify'; | ||
import { Observation } from '../models/observation.model.js'; | ||
import { raw } from 'objection'; | ||
import { RedisServer } from '../../servers/redis.server.js'; | ||
|
||
export default class PublicController { | ||
static async getVelutinaObservationsRecent( | ||
_req: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
reply.header('Cache-Control', 'public, max-age=21600'); | ||
|
||
const redis = RedisServer.client; | ||
const cached = await redis.get('cache:velutinaObservationsRecent'); | ||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const start = new Date(Date.now() - 1000 * 60 * 60 * 24 * 182); | ||
const end = new Date(Date.now()); | ||
const result = await Observation.query() | ||
.select( | ||
'location', | ||
raw('JSON_EXTRACT(data, "$.uri") as uri'), | ||
'observed_at', | ||
) | ||
.whereBetween('observed_at', [start, end]); | ||
|
||
redis.set( | ||
'cache:velutinaObservationsRecent', | ||
JSON.stringify(result), | ||
'EX', | ||
21600, | ||
); | ||
|
||
return result; | ||
} | ||
|
||
static async getVelutinaObservationsStats( | ||
_req: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
reply.header('Cache-Control', 'public, max-age=21600'); | ||
const res = await Observation.query().count('id as count'); | ||
return res[0]; | ||
} | ||
|
||
static async getVelutinaObservationsArray( | ||
_req: FastifyRequest, | ||
reply: FastifyReply, | ||
) { | ||
reply.header('Cache-Control', 'public, max-age=21600'); | ||
|
||
const redis = RedisServer.client; | ||
const cached = await redis.get('cache:velutinaObservationsArray'); | ||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const result = await Observation.query().select('location'); | ||
const res = []; | ||
for (const r of result) { | ||
res.push([(r as any).location.x, (r as any).location.y]); | ||
} | ||
|
||
redis.set( | ||
'cache:velutinaObservationsArray', | ||
JSON.stringify(res), | ||
'EX', | ||
21600, | ||
); | ||
|
||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
import { ZodTypeProvider } from 'fastify-type-provider-zod'; | ||
import PublicController from '../../controllers/public.controller.js'; | ||
|
||
export default function routes( | ||
instance: FastifyInstance, | ||
_options: any, | ||
done: any, | ||
) { | ||
const server = instance.withTypeProvider<ZodTypeProvider>(); | ||
|
||
server.get( | ||
'/velutina/observations/recent', | ||
{}, | ||
PublicController.getVelutinaObservationsRecent, | ||
); | ||
server.get( | ||
'/velutina/observations/stats', | ||
{}, | ||
PublicController.getVelutinaObservationsStats, | ||
); | ||
server.get( | ||
'/velutina/observations/array', | ||
{}, | ||
PublicController.getVelutinaObservationsArray, | ||
); | ||
|
||
done(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters