Skip to content

Commit

Permalink
Merge pull request #133 from HannesOberreiter/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
HannesOberreiter committed Nov 10, 2023
2 parents 370ddc1 + c994245 commit 214aff0
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 24 deletions.
17 changes: 13 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

131 changes: 131 additions & 0 deletions src/api/controllers/statistic.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Feed } from '../models/feed.model.js';
import { Treatment } from '../models/treatment.model.js';
import { Checkup } from '../models/checkup.model.js';
import { FastifyReply, FastifyRequest } from 'fastify';
import httpErrors from 'http-errors';

export default class StatisticController {
static async getHiveCountTotal(req: FastifyRequest, reply: FastifyReply) {
Expand Down Expand Up @@ -685,4 +686,134 @@ export default class StatisticController {
const result = await query.orderBy('hive.name');
return { ...result };
}

static async getVarroa(req: FastifyRequest, reply: FastifyReply) {
const query = req.query as {
start_date: string;
end_date: string;
hive_ids: string[];
};

type ResultStats = {
hive_name: string;
varroa: {
min: number;
max: number;
avg: number;
};
};

const resultDatasetCheckup: any = {};
const resultDatasetTreatment: any = {};
const resultStats = [] as ResultStats[];

for (let i = 0; i < query.hive_ids.length; i++) {
if (i > 20) break;
const resultCheckup: any[] = [];
const hive_id = query.hive_ids[i];
const res = await Checkup.query()
.select(
'hive_id',
'varroa',
'date',
'type.name as type_name',
'hive.name as hive_name',
)
.leftJoinRelated('type')
.leftJoinRelated('hive')
.where({
'checkups.deleted': false,
'checkups.user_id': req.session.user.user_id,
'hive.deleted': false,
'checkups.hive_id': hive_id,
})
.whereBetween('checkups.date', [query.start_date, query.end_date]);

if (res.length === 0) continue;

resultStats[i] = {
hive_name: (res[0] as any)?.hive_name ?? '',
varroa: {
min: 0,
max: 0,
avg: 0,
},
};

let averageLength = 0;
res.map((v: any) => {
resultCheckup.push([
hive_id,
v.varroa,
v.date.toISOString().split('T')[0],
v.type_name,
v.hive_name,
]);

resultStats[i].varroa.max = Math.max(
resultStats[i].varroa.max,
v.varroa,
);
if (v.varroa > 0) {
averageLength++;
resultStats[i].varroa.min = Math.min(
resultStats[i].varroa.min === 0
? v.varroa
: resultStats[i].varroa.min,
v.varroa,
);
resultStats[i].varroa.avg += v.varroa;
}
});
if (averageLength > 0) {
resultStats[i].varroa.avg =
Math.round(
(resultStats[i].varroa.avg / averageLength + Number.EPSILON) * 100,
) / 100;
}
resultDatasetCheckup[hive_id] = resultCheckup;
}

for (let i = 0; i < query.hive_ids.length; i++) {
if (i > 20) break;
const resultTreatment: any[] = [];
const hive_id = query.hive_ids[i];
const res = await Treatment.query()
.select(
'hive_id',
'date',
'amount',
'type.name as type_name',
'hive.name as hive_name',
)
.leftJoinRelated('type')
.leftJoinRelated('hive')
.where({
'treatments.deleted': false,
'treatments.user_id': req.session.user.user_id,
'hive.deleted': false,
'treatments.hive_id': hive_id,
})
.whereBetween('treatments.date', [query.start_date, query.end_date]);

if (res.length === 0) continue;
res.map((v: any) => {
resultTreatment.push([
hive_id,
v.amount,
v.date.toISOString().split('T')[0],
v.type_name,
v.hive_name,
0,
]);
});
resultDatasetTreatment[hive_id] = resultTreatment;
}

return {
datasetCheckup: resultDatasetCheckup,
datasetTreatment: resultDatasetTreatment,
stats: resultStats,
};
}
}
17 changes: 17 additions & 0 deletions src/api/routes/v1/statistic.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ZodTypeProvider } from 'fastify-type-provider-zod';
import { z } from 'zod';
import StatisticController from '../../controllers/statistic.controller.js';
import { Validator } from '../../hooks/validator.hook.js';
import { numberSchema } from '../../utils/zod.util.js';

export default function routes(
instance: FastifyInstance,
Expand Down Expand Up @@ -143,5 +144,21 @@ export default function routes(
StatisticController.getCheckupRatingHive,
);

server.get(
'/varroa',
{
preHandler: Guard.authorize([ROLES.read, ROLES.admin, ROLES.user]),
preValidation: Validator.isPremium,
schema: {
querystring: z.object({
start_date: z.string(),
end_date: z.string(),
hive_ids: z.array(numberSchema),
}),
},
},
StatisticController.getVarroa,
);

done();
}
38 changes: 18 additions & 20 deletions src/services/wizbee.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { encode } from 'gpt-3-encoder';
import OpenAI from 'openai';

import { env, openAI } from '../config/environment.config.js';
import { VectorServer } from '../servers/vector.server.js';
// import { Stream } from 'node_modules/openai/streaming.js';
Expand All @@ -27,10 +26,10 @@ export class WizBee {
private template: string;
private static indexName = 'link';
private static vectorField = 'content_vector';
private static completionModel = 'gpt-3.5-turbo' as const;
private static turboModel = 'gpt-3.5-turbo' as const;
private static completionModel = 'gpt-4-1106-preview' as const;
private static turboModel = 'gpt-4-1106-preview' as const;
private static embeddingModel = 'text-embedding-ada-002' as const;
private static maxToken = 3000;
private static maxToken = 5000;

constructor() {
this.openAI = new OpenAI({
Expand Down Expand Up @@ -224,22 +223,21 @@ export class WizBee {
*/
private async createAnswer(input: string, contextText: string, lang: string) {
try {
const messages: OpenAI.Chat.Completions.CreateChatCompletionRequestMessage[] =
[
{
role: 'system',
content:
'You are a friendly bot assistant, answering beekeeping related question or questions about the usage of the b.tree beekeeping software by using only given context. The context could be from multiple references or from the official b.tree documentation and each is separated by ###. If you cannot give a good answer with given context, please type "Sorry, we cannot give a good answer to that question."',
},
{
role: 'system',
content: `Context: ${contextText}`,
},
{
role: 'user',
content: `${input}`,
},
];
const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
{
role: 'system',
content:
'You are a friendly bot assistant, answering beekeeping related question or questions about the usage of the b.tree beekeeping software by using only given context. The context could be from multiple references or from the official b.tree documentation and each is separated by ###. If you cannot give a good answer with given context, please type "Sorry, we cannot give a good answer to that question."',
},
{
role: 'system',
content: `Context: ${contextText}`,
},
{
role: 'user',
content: `${input}`,
},
];

if (lang !== 'en') {
messages.push({
Expand Down

0 comments on commit 214aff0

Please sign in to comment.