mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-08-26 09:19:40 +00:00
Compare commits
7 Commits
9254db08f9
...
feature/te
Author | SHA1 | Date | |
---|---|---|---|
77cf893121 | |||
b281ad766d | |||
bed2c526dd | |||
3ebca8d98f | |||
eac72dd2a4 | |||
2b1f5190ef | |||
ffa38de046 |
25
.github/workflows/pr-description.yml
vendored
25
.github/workflows/pr-description.yml
vendored
@ -1,4 +1,4 @@
|
||||
name: 🤖 AI PR Description Commenter (100% Safe with jq)
|
||||
name: 🤖 AI PR Description with HuggingFace Falcon
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
@ -27,26 +27,23 @@ jobs:
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GH_PERSONAL_TOKEN }}
|
||||
|
||||
- name: Generate PR Description with OpenAI (Safe JSON with jq)
|
||||
- name: Generate PR Description with HuggingFace Falcon
|
||||
run: |
|
||||
REQUEST_BODY=$(jq -n \
|
||||
--arg model "gpt-4o" \
|
||||
--arg content "Given the following commit messages:\n\n${commits}\n\nGenerate a clear and professional pull request description." \
|
||||
'{
|
||||
model: $model,
|
||||
messages: [{ role: "user", content: $content }]
|
||||
}'
|
||||
--arg inputs "Given the following commit messages:\n\n${commits}\n\nGenerate a clear and professional pull request description." \
|
||||
'{ inputs: $inputs }'
|
||||
)
|
||||
|
||||
RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \
|
||||
-H "Authorization: Bearer $OPENAI_API_KEY" \
|
||||
RESPONSE=$(curl -s https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct \
|
||||
-H "Authorization: Bearer $HUGGINGFACE_API_KEY" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$REQUEST_BODY")
|
||||
|
||||
DESCRIPTION=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
|
||||
|
||||
echo "---------- OpenAI Raw Response ----------"
|
||||
echo "---------- HuggingFace Raw Response ----------"
|
||||
echo "$RESPONSE"
|
||||
|
||||
DESCRIPTION=$(echo "$RESPONSE" | jq -r 'if type=="array" then .[0].generated_text else .error else "Error: Unexpected response" end')
|
||||
|
||||
echo "---------- Extracted Description ----------"
|
||||
echo "$DESCRIPTION"
|
||||
|
||||
@ -54,7 +51,7 @@ jobs:
|
||||
echo "$DESCRIPTION" >> $GITHUB_ENV
|
||||
echo "EOF" >> $GITHUB_ENV
|
||||
env:
|
||||
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||
HUGGINGFACE_API_KEY: ${{ secrets.HUGGINGFACE_API_KEY }}
|
||||
commits: ${{ env.commits }}
|
||||
|
||||
- name: Post AI Generated Description as Comment
|
||||
|
@ -1,13 +1,15 @@
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { firebaseDataBase } from '../../firebase.config';
|
||||
import {
|
||||
Database,
|
||||
DataSnapshot,
|
||||
@ -15,9 +17,7 @@ import {
|
||||
ref,
|
||||
runTransaction,
|
||||
} from 'firebase/database';
|
||||
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
||||
import { firebaseDataBase } from '../../firebase.config';
|
||||
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
@Injectable()
|
||||
export class DeviceStatusFirebaseService {
|
||||
private tuya: TuyaContext;
|
||||
@ -79,77 +79,64 @@ export class DeviceStatusFirebaseService {
|
||||
device: any;
|
||||
}[],
|
||||
): Promise<void> {
|
||||
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
||||
|
||||
const allLogs = [];
|
||||
|
||||
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
||||
|
||||
for (const item of batch) {
|
||||
const device = item.device;
|
||||
|
||||
if (!device?.uuid) {
|
||||
console.log(`⛔ Skipped unknown device: ${item.deviceTuyaUuid}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Determine properties based on environment
|
||||
const properties =
|
||||
this.isDevEnv && Array.isArray(item.log?.properties)
|
||||
? item.log.properties
|
||||
: Array.isArray(item.status)
|
||||
? item.status
|
||||
: null;
|
||||
|
||||
if (!properties) {
|
||||
console.log(
|
||||
`⛔ Skipped invalid status/properties for device: ${item.deviceTuyaUuid}`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
const logs = properties.map((property) =>
|
||||
const logs = item.log.properties.map((property) =>
|
||||
this.deviceStatusLogRepository.create({
|
||||
deviceId: device.uuid,
|
||||
deviceTuyaId: item.deviceTuyaUuid,
|
||||
productId: device.productDevice?.uuid,
|
||||
productId: item.log.productId,
|
||||
log: item.log,
|
||||
code: property.code,
|
||||
value: property.value,
|
||||
eventId: item.log?.dataId,
|
||||
eventTime: new Date(
|
||||
this.isDevEnv ? property.time : property.t,
|
||||
).toISOString(),
|
||||
eventId: item.log.dataId,
|
||||
eventTime: new Date(property.time).toISOString(),
|
||||
}),
|
||||
);
|
||||
|
||||
allLogs.push(...logs);
|
||||
}
|
||||
|
||||
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
||||
|
||||
const chunkSize = 300;
|
||||
let insertedCount = 0;
|
||||
const insertLogsPromise = (async () => {
|
||||
const chunkSize = 300;
|
||||
let insertedCount = 0;
|
||||
|
||||
for (let i = 0; i < allLogs.length; i += chunkSize) {
|
||||
const chunk = allLogs.slice(i, i + chunkSize);
|
||||
try {
|
||||
const result = await this.deviceStatusLogRepository
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into('device-status-log')
|
||||
.values(chunk)
|
||||
.orIgnore()
|
||||
.execute();
|
||||
for (let i = 0; i < allLogs.length; i += chunkSize) {
|
||||
const chunk = allLogs.slice(i, i + chunkSize);
|
||||
try {
|
||||
const result = await this.deviceStatusLogRepository
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into('device-status-log') // or use DeviceStatusLogEntity
|
||||
.values(chunk)
|
||||
.orIgnore() // skip duplicates
|
||||
.execute();
|
||||
|
||||
insertedCount += result.identifiers.length;
|
||||
console.log(
|
||||
`✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('❌ Insert error (skipped chunk):', error.message);
|
||||
insertedCount += result.identifiers.length;
|
||||
console.log(
|
||||
`✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('❌ Insert error (skipped chunk):', error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`);
|
||||
console.log(
|
||||
`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`,
|
||||
);
|
||||
})();
|
||||
|
||||
await insertLogsPromise;
|
||||
}
|
||||
|
||||
async addDeviceStatusToFirebase(
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as NodeCache from 'node-cache';
|
||||
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SosHandlerService } from './sos.handler.service';
|
||||
import * as NodeCache from 'node-cache';
|
||||
|
||||
@Injectable()
|
||||
export class TuyaWebSocketService implements OnModuleInit {
|
||||
@ -74,12 +74,7 @@ export class TuyaWebSocketService implements OnModuleInit {
|
||||
this.client.message(async (ws: WebSocket, message: any) => {
|
||||
try {
|
||||
const { devId, status, logData } = this.extractMessageData(message);
|
||||
// console.log(
|
||||
// `📬 Received message for device: ${devId}, status:`,
|
||||
// status,
|
||||
// logData,
|
||||
// );
|
||||
if (!Array.isArray(status)) {
|
||||
if (!Array.isArray(logData?.properties)) {
|
||||
this.client.ackMessage(message.messageId);
|
||||
return;
|
||||
}
|
||||
@ -167,8 +162,6 @@ export class TuyaWebSocketService implements OnModuleInit {
|
||||
status: any;
|
||||
logData: any;
|
||||
} {
|
||||
// console.log('Received message:', message);
|
||||
|
||||
const payloadData = message.payload.data;
|
||||
|
||||
if (this.isDevEnv) {
|
||||
|
@ -3,11 +3,11 @@ import { IsNotEmpty, IsOptional, IsUUID, Matches } from 'class-validator';
|
||||
|
||||
export class BookingRequestDto {
|
||||
@ApiProperty({
|
||||
description: 'Month in MM-YYYY format',
|
||||
example: '07-2025',
|
||||
description: 'Month in MM/YYYY format',
|
||||
example: '07/2025',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@Matches(/^(0[1-9]|1[0-2])\-\d{4}$/, {
|
||||
@Matches(/^(0[1-9]|1[0-2])\/\d{4}$/, {
|
||||
message: 'Date must be in MM/YYYY format',
|
||||
})
|
||||
month: string;
|
||||
|
@ -51,7 +51,7 @@ export class BookingService {
|
||||
}
|
||||
|
||||
async findAll({ month, space }: BookingRequestDto, project: string) {
|
||||
const [monthNumber, year] = month.split('-').map(Number);
|
||||
const [monthNumber, year] = month.split('/').map(Number);
|
||||
const fromDate = new Date(year, monthNumber - 1, 1);
|
||||
const toDate = new Date(year, monthNumber, 0, 23, 59, 59);
|
||||
return this.bookingEntityRepository.find({
|
||||
|
@ -1 +0,0 @@
|
||||
export * from './weather.controller';
|
@ -1,28 +0,0 @@
|
||||
import { Controller, Get, Query } from '@nestjs/common';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route'; // Assuming this is where the routes are defined
|
||||
import { WeatherService } from '../services';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { GetWeatherDetailsDto } from '../dto/get.weather.dto';
|
||||
|
||||
@ApiTags('Weather Module')
|
||||
@Controller({
|
||||
version: EnableDisableStatusEnum.ENABLED,
|
||||
path: ControllerRoute.WEATHER.ROUTE, // use the static route constant
|
||||
})
|
||||
export class WeatherController {
|
||||
constructor(private readonly weatherService: WeatherService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.WEATHER.ACTIONS.FETCH_WEATHER_DETAILS_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.WEATHER.ACTIONS.FETCH_WEATHER_DETAILS_DESCRIPTION,
|
||||
})
|
||||
async fetchWeatherDetails(
|
||||
@Query() query: GetWeatherDetailsDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.weatherService.fetchWeatherDetails(query);
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsNumber } from 'class-validator';
|
||||
|
||||
export class GetWeatherDetailsDto {
|
||||
@ApiProperty({
|
||||
description: 'Latitude coordinate',
|
||||
example: 35.6895,
|
||||
})
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
lat: number;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Longitude coordinate',
|
||||
example: 139.6917,
|
||||
})
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
lon: number;
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './weather.service';
|
@ -1,51 +0,0 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
import { GetWeatherDetailsDto } from '../dto/get.weather.dto';
|
||||
import { calculateAQI } from '@app/common/util/calculate.aqi';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
|
||||
@Injectable()
|
||||
export class WeatherService {
|
||||
private readonly weatherApiUrl: string;
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly httpService: HttpService,
|
||||
) {
|
||||
this.weatherApiUrl = this.configService.get<string>('WEATHER_API_URL');
|
||||
}
|
||||
|
||||
async fetchWeatherDetails(
|
||||
query: GetWeatherDetailsDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const { lat, lon } = query;
|
||||
const weatherApiKey = this.configService.get<string>(
|
||||
'OPEN_WEATHER_MAP_API_KEY',
|
||||
);
|
||||
const url = `${this.weatherApiUrl}/current.json?key=${weatherApiKey}&q=${lat},${lon}&aqi=yes`;
|
||||
|
||||
const response = await firstValueFrom(this.httpService.get(url));
|
||||
const pm2_5 = response.data.current.air_quality.pm2_5; // Raw PM2.5 (µg/m³)
|
||||
|
||||
return new SuccessResponseDto({
|
||||
message: `Weather details fetched successfully`,
|
||||
data: {
|
||||
aqi: calculateAQI(pm2_5), // Converted AQI (0-500)
|
||||
temperature: response.data.current.temp_c,
|
||||
humidity: response.data.current.humidity,
|
||||
},
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`Error fetching weather data: ${error}`);
|
||||
|
||||
throw new HttpException(
|
||||
`Api can't handle these lat and lon values`,
|
||||
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { HttpModule } from '@nestjs/axios'; // <-- Import this!
|
||||
import { WeatherController } from './controllers';
|
||||
import { WeatherService } from './services';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, HttpModule],
|
||||
controllers: [WeatherController],
|
||||
providers: [WeatherService],
|
||||
})
|
||||
export class WeatherModule {}
|
Reference in New Issue
Block a user