Skip to content

Commit

Permalink
Merge pull request #29 from ProjectBARO/feat/analysis-report
Browse files Browse the repository at this point in the history
์ž์„ธ ๋ถ„์„ ๋ณด๊ณ ์„œ ๊ธฐ๋Šฅ ๊ตฌํ˜„ (์•ฑ ๊ด€๋ จ ๋Œ€์‘)
  • Loading branch information
YehyeokBang authored Jan 23, 2024
2 parents ee82db9 + 0af5d01 commit f2614e3
Show file tree
Hide file tree
Showing 10 changed files with 585 additions and 159 deletions.
90 changes: 79 additions & 11 deletions app/report/controllers/report_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"gdsc/baro/app/report/services"
"gdsc/baro/app/report/types"
"gdsc/baro/global"
"strconv"

"github.com/gin-gonic/gin"
)

type ReportController struct {
ReportService *services.ReportService
ReportService services.ReportServiceInterface
}

func NewReportController(reportService *services.ReportService) *ReportController {
func NewReportController(reportService services.ReportServiceInterface) *ReportController {
return &ReportController{
ReportService: reportService,
}
Expand All @@ -23,13 +24,13 @@ func NewReportController(reportService *services.ReportService) *ReportControlle
// @Description ์ž์„ธ๋ฅผ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค. (๋™์˜์ƒ URL์„ ์ž…๋ ฅ๋ฐ›์•„ ์ž์„ธ๋ฅผ ์ถ”์ •ํ•ฉ๋‹ˆ๋‹ค.)
// @Accept json
// @Produce json
// @Param video_url body types.RequestPredict true "๋™์˜์ƒ URL"
// @Param video_url body types.RequestAnalysis true "URL, ์•Œ๋ฆผ ํšŸ์ˆ˜ ๋“ฑ"
// @Success 200 {object} global.Response
// @Failure 400 {object} global.Response
// @Security Bearer
// @Router /predict [post]
func (controller *ReportController) Predict(c *gin.Context) {
var input types.RequestPredict
// @Router /analysis [post]
func (controller *ReportController) Analysis(c *gin.Context) {
var input types.RequestAnalysis
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(400, global.Response{
Status: 400,
Expand All @@ -46,7 +47,7 @@ func (controller *ReportController) Predict(c *gin.Context) {
return
}

response, err := controller.ReportService.Predict(c, input)
response, err := controller.ReportService.Analysis(c, input)
if err != nil {
c.JSON(400, global.Response{
Status: 400,
Expand All @@ -70,8 +71,8 @@ func (controller *ReportController) Predict(c *gin.Context) {
// @Success 200 {object} global.Response
// @Failure 400 {object} global.Response
// @Security Bearer
// @Router /predict [get]
func (controller *ReportController) GetPredict(c *gin.Context) {
// @Router /analysis [get]
func (controller *ReportController) GetAnalysis(c *gin.Context) {
response, err := controller.ReportService.FindReportByCurrentUser(c)
if err != nil {
c.JSON(400, global.Response{
Expand All @@ -88,6 +89,73 @@ func (controller *ReportController) GetPredict(c *gin.Context) {
})
}

// @Tags Reports
// @Summary ์ž์„ธ ์ถ”์ • ๊ฒฐ๊ณผ id๋กœ ์กฐํšŒ
// @Description ๋ณด๊ณ ์„œ id๋กœ ์ž์„ธ ์ถ”์ • ๊ฒฐ๊ณผ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. (์š”์•ฝ์œผ๋กœ ๋จผ์ € ๋ณด๊ณ ์„œ id ์กฐํšŒํ•˜๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ๊ทธ๊ฑธ ๋ˆ„๋ฅด๋ฉด ์ด๊ฑธ ์‚ฌ์šฉํ•˜๊ธฐ)
// @Accept json
// @Produce json
// @Param id path int true "์ž์„ธ ์ถ”์ • ๊ฒฐ๊ณผ id"
// @Success 200 {object} global.Response
// @Failure 400 {object} global.Response
// @Security Bearer
// @Router /analysis/{id} [get]
func (controller *ReportController) GetAnalysisById(c *gin.Context) {
idStr := c.Param("id")

id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.JSON(400, global.Response{
Status: 400,
Message: "Invalid ID",
})
return
}

response, err := controller.ReportService.FindById(c, uint(id))
if err != nil {
c.JSON(400, global.Response{
Status: 400,
Message: err.Error(),
})
return
}

c.JSON(200, global.Response{
Status: 200,
Message: "success",
Data: response,
})
}

// @Tags Reports
// @Summary ์ž์„ธ ์ถ”์ • ๊ฒฐ๊ณผ ์›”๋ณ„ ์š”์•ฝ ์กฐํšŒ
// @Description ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž์˜ ์ž์„ธ ์ถ”์ • ๊ฒฐ๊ณผ๋ฅผ ์›”๋ณ„๋กœ ์š”์•ฝํ•˜์—ฌ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. (์บ˜๋ฆฐ๋” ์  ์ฐ๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ)
// @Accept json
// @Produce json
// @Param ym query string true "์กฐํšŒํ•  ๋…„์›” (YYYYMM) ์˜ˆ์‹œ: 202401 (2024๋…„ 1์›”)"
// @Success 200 {object} global.Response
// @Failure 400 {object} global.Response
// @Security Bearer
// @Router /analysis/summary [get]
func (controller *ReportController) GetAnalysisSummary(c *gin.Context) {
yearAndMonth := c.Query("ym")

response, err := controller.ReportService.FindReportSummaryByMonth(c, yearAndMonth)
if err != nil {
c.JSON(400, global.Response{
Status: 400,
Message: err.Error(),
})
return
}

c.JSON(200, global.Response{
Status: 200,
Message: "success",
Data: response,
})
}

// @Tags Reports
// @Summary ์ž์„ธ ์ถ”์ • ๊ฒฐ๊ณผ ์ „์ฒด ์กฐํšŒ (ํ…Œ์ŠคํŠธ์šฉ)
// @Description ์ž์„ธ ์ถ”์ • ๊ฒฐ๊ณผ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
Expand All @@ -96,8 +164,8 @@ func (controller *ReportController) GetPredict(c *gin.Context) {
// @Success 200 {object} global.Response
// @Failure 400 {object} global.Response
// @Security Bearer
// @Router /predict/all [get]
func (controller *ReportController) GetPredicts(c *gin.Context) {
// @Router /analysis/all [get]
func (controller *ReportController) GetAnalyzes(c *gin.Context) {
response, err := controller.ReportService.FindAll()
if err != nil {
c.JSON(400, global.Response{
Expand Down
11 changes: 7 additions & 4 deletions app/report/models/Report.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package models
import "time"

type Report struct {
ID uint `gorm:"primaryKey"`
UserID uint
Predict string
CreatedAt time.Time `gorm:"autoCreateTime"`
ID uint `gorm:"primaryKey"`
UserID uint
AlertCount uint
AnalysisTime uint
Type string
Predict string
CreatedAt time.Time `gorm:"autoCreateTime"`
}
20 changes: 20 additions & 0 deletions app/report/repositories/report_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"gorm.io/gorm"
)

type ReportRepositoryInterface interface {
Save(report *models.Report) error
FindByUserID(userID uint) ([]models.Report, error)
FindById(id uint) (models.Report, error)
FindByYearAndMonth(userID uint, month string) ([]models.Report, error)
FindAll() ([]models.Report, error)
}

type ReportRepository struct {
DB *gorm.DB
}
Expand All @@ -29,6 +37,18 @@ func (repo *ReportRepository) FindByUserID(userID uint) ([]models.Report, error)
return reports, result.Error
}

func (repo *ReportRepository) FindById(id uint) (models.Report, error) {
var report models.Report
result := repo.DB.Where("id = ?", id).First(&report)
return report, result.Error
}

func (repo *ReportRepository) FindByYearAndMonth(userID uint, yearAndMonth string) ([]models.Report, error) {
var reports []models.Report
result := repo.DB.Where("user_id = ? AND DATE_FORMAT(created_at, '%Y%m') = ?", userID, yearAndMonth).Find(&reports)
return reports, result.Error
}

func (repo *ReportRepository) FindAll() ([]models.Report, error) {
var reports []models.Report
result := repo.DB.Find(&reports)
Expand Down
97 changes: 78 additions & 19 deletions app/report/services/report_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,37 @@ import (
"golang.org/x/net/html"
)

type ReportServiceInterface interface {
Analysis(c *gin.Context, input types.RequestAnalysis) (string, error)
FindReportByCurrentUser(c *gin.Context) ([]types.ResponseReport, error)
FindById(c *gin.Context, id uint) (types.ResponseReport, error)
FindReportSummaryByMonth(c *gin.Context, yearAndMonth string) ([]types.ResponseReportSummary, error)
FindAll() ([]types.ResponseReport, error)
}

type ReportService struct {
ReportRepository *repositories.ReportRepository
UserUtil *utils.UserUtil
ReportRepository repositories.ReportRepositoryInterface
UserUtil utils.UserUtilInterface
}

func NewReportService(reportRepository *repositories.ReportRepository, userUtil *utils.UserUtil) *ReportService {
func NewReportService(reportRepository repositories.ReportRepositoryInterface, userUtil utils.UserUtilInterface) *ReportService {
return &ReportService{
ReportRepository: reportRepository,
UserUtil: userUtil,
}
}

func (service *ReportService) Predict(c *gin.Context, input types.RequestPredict) (string, error) {
func (service *ReportService) Analysis(c *gin.Context, input types.RequestAnalysis) (string, error) {
user, err := service.UserUtil.FindCurrentUser(c)
if err != nil {
return "", err
return "Not Found User", err
}

REQUEST_URL := os.Getenv("AI_SERVER_API_URL")

u, err := url.Parse(REQUEST_URL)
if err != nil {
return "", err
return "Not Found AI Server", err
}

q := u.Query()
Expand Down Expand Up @@ -70,7 +78,10 @@ func (service *ReportService) Predict(c *gin.Context, input types.RequestPredict
}

report := models.Report{
UserID: user.ID,
UserID: user.ID,
AlertCount: input.AlertCount,
AnalysisTime: input.AnalysisTime,
Type: input.Type,
}

// TODO: refactor, AI Server Change to return JSON
Expand Down Expand Up @@ -99,7 +110,7 @@ func (service *ReportService) Predict(c *gin.Context, input types.RequestPredict
return message, nil
}

func (service *ReportService) FindReportByCurrentUser(c *gin.Context) ([]types.ResponsePredict, error) {
func (service *ReportService) FindReportByCurrentUser(c *gin.Context) ([]types.ResponseReport, error) {
user, err := service.UserUtil.FindCurrentUser(c)
if err != nil {
return nil, err
Expand All @@ -110,12 +121,57 @@ func (service *ReportService) FindReportByCurrentUser(c *gin.Context) ([]types.R
return nil, err
}

var responseReports []types.ResponsePredict
var responseReports []types.ResponseReport
for _, report := range reports {
responseReport := types.ResponseReport{
ID: report.ID,
UserID: report.UserID,
AlertCount: report.AlertCount,
AnalysisTime: report.AnalysisTime,
Predict: report.Predict,
Type: report.Type,
CreatedAt: report.CreatedAt,
}
responseReports = append(responseReports, responseReport)
}

return responseReports, nil
}

func (service *ReportService) FindById(c *gin.Context, id uint) (types.ResponseReport, error) {
report, err := service.ReportRepository.FindById(id)
if err != nil {
return types.ResponseReport{}, err
}

responseReport := types.ResponseReport{
ID: report.ID,
UserID: report.UserID,
AlertCount: report.AlertCount,
AnalysisTime: report.AnalysisTime,
Predict: report.Predict,
Type: report.Type,
CreatedAt: report.CreatedAt,
}

return responseReport, nil
}

func (service *ReportService) FindReportSummaryByMonth(c *gin.Context, yearAndMonth string) ([]types.ResponseReportSummary, error) {
user, err := service.UserUtil.FindCurrentUser(c)
if err != nil {
return nil, err
}

reports, err := service.ReportRepository.FindByYearAndMonth(user.ID, yearAndMonth)
if err != nil {
return nil, err
}

var responseReports []types.ResponseReportSummary
for _, report := range reports {
responseReport := types.ResponsePredict{
responseReport := types.ResponseReportSummary{
ID: report.ID,
UserID: report.UserID,
Predict: report.Predict,
CreatedAt: report.CreatedAt,
}
responseReports = append(responseReports, responseReport)
Expand All @@ -124,19 +180,22 @@ func (service *ReportService) FindReportByCurrentUser(c *gin.Context) ([]types.R
return responseReports, nil
}

func (service *ReportService) FindAll() ([]types.ResponsePredict, error) {
func (service *ReportService) FindAll() ([]types.ResponseReport, error) {
reports, err := service.ReportRepository.FindAll()
if err != nil {
return nil, err
}

var responseReports []types.ResponsePredict
var responseReports []types.ResponseReport
for _, report := range reports {
responseReport := types.ResponsePredict{
ID: report.ID,
UserID: report.UserID,
Predict: report.Predict,
CreatedAt: report.CreatedAt,
responseReport := types.ResponseReport{
ID: report.ID,
UserID: report.UserID,
AlertCount: report.AlertCount,
AnalysisTime: report.AnalysisTime,
Predict: report.Predict,
Type: report.Type,
CreatedAt: report.CreatedAt,
}
responseReports = append(responseReports, responseReport)
}
Expand Down
13 changes: 10 additions & 3 deletions app/report/types/request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ func init() {
validate = validator.New()
}

type RequestPredict struct {
VideoURL string `json:"video_url" validate:"required"`
type RequestAnalysis struct {
VideoURL string `json:"video_url" validate:"required"`
AlertCount uint `json:"alert_count" validate:"required"`
AnalysisTime uint `json:"analysis_time" validate:"required"`
Type string `json:"type" validate:"required"`
}

func (r *RequestPredict) Validate() error {
type RequestReportSummary struct {
YearAndMonth string `json:"year_and_month" validate:"required"`
}

func (r *RequestAnalysis) Validate() error {
return validate.Struct(r)
}
14 changes: 11 additions & 3 deletions app/report/types/response_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ package types

import "time"

type ResponsePredict struct {
type ResponseReportSummary struct {
ID uint `json:"id"`
UserID uint `json:"user_id"`
Predict string `json:"predict"`
CreatedAt time.Time `json:"created_at"`
}

type ResponseReport struct {
ID uint `json:"id"`
UserID uint `json:"user_id"`
AlertCount uint `json:"alert_count"`
AnalysisTime uint `json:"analysis_time"`
Predict string `json:"predict"`
Type string `json:"type"`
CreatedAt time.Time `json:"created_at"`
}
Loading

0 comments on commit f2614e3

Please sign in to comment.