Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retornando a data da última coleta #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions models/agency.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package models

import "google.golang.org/protobuf/types/known/timestamppb"

// Agency A Struct containing the main descriptions of each Agency.
type Agency struct {
ID string `json:"aid,omitempty"` // 'trt13'
Name string `json:"name,omitempty"` // 'Tribunal Regional do Trabalho 13° Região'
Type string `json:"type,omitempty"` // "R" for Regional, "M" for Municipal, "F" for Federal, "E" for State.
Entity string `json:"entity,omitempty"` // "J" For Judiciário, "M" for Ministério Público, "P" for Procuradorias and "D" for Defensorias.
UF string `json:"uf,omitempty"` // Short code for federative unity.
URL string `json:"url,omitempty"` // Link for state url
Collecting []Collecting `json:"collecting,omitempty"`
TwitterHandle string `json:"twitter_handle,omitempty"` // Agency's twitter handle
OmbudsmanURL string `json:"ombudsman_url,omitempty"` //Agencys's ombudsman url
ID string `json:"aid,omitempty"` // 'trt13'
Name string `json:"name,omitempty"` // 'Tribunal Regional do Trabalho 13° Região'
Type string `json:"type,omitempty"` // "R" for Regional, "M" for Municipal, "F" for Federal, "E" for State.
Entity string `json:"entity,omitempty"` // "J" For Judiciário, "M" for Ministério Público, "P" for Procuradorias and "D" for Defensorias.
UF string `json:"uf,omitempty"` // Short code for federative unity.
URL string `json:"url,omitempty"` // Link for state url
Collecting []Collecting `json:"collecting,omitempty"`
TwitterHandle string `json:"twitter_handle,omitempty"` // Agency's twitter handle
OmbudsmanURL string `json:"ombudsman_url,omitempty"` //Agencys's ombudsman url
LastCollection *timestamppb.Timestamp `json:"last_collection,omitempty"`
}

// Collecting A Struct containing the day we checked the status of the data and the reasons why we didn't collected it.
Expand Down
59 changes: 35 additions & 24 deletions repo/database/dto/agencyDTO.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ package dto
import (
"encoding/json"
"fmt"
"time"

"github.com/dadosjusbr/storage/models"
"google.golang.org/protobuf/types/known/timestamppb"
"gorm.io/datatypes"
)

// Agency A Struct containing the main descriptions of each Agency.
type AgencyDTO struct {
ID string `gorm:"column:id"`
Name string `gorm:"column:nome"`
Type string `gorm:"column:jurisdicao"`
Entity string `gorm:"column:entidade"`
UF string `gorm:"column:uf"`
Collecting datatypes.JSON `gorm:"column:coletando"`
TwitterHandle string `gorm:"column:twitter_handle"`
OmbudsmanURL string `gorm:"column:ouvidoria"`
ID string `gorm:"column:id"`
Name string `gorm:"column:nome"`
Type string `gorm:"column:jurisdicao"`
Entity string `gorm:"column:entidade"`
UF string `gorm:"column:uf"`
Collecting datatypes.JSON `gorm:"column:coletando"`
TwitterHandle string `gorm:"column:twitter_handle"`
OmbudsmanURL string `gorm:"column:ouvidoria"`
LastCollection time.Time `gorm:"column:ultima_coleta"`
}

func (AgencyDTO) TableName() string {
Expand All @@ -35,14 +38,15 @@ func (a AgencyDTO) ConvertToModel() (*models.Agency, error) {
return nil, fmt.Errorf("error while unmarshaling collecting: %q", err)
}
return &models.Agency{
ID: a.ID,
Name: a.Name,
Type: a.Type,
Entity: a.Entity,
UF: a.UF,
Collecting: collecting,
TwitterHandle: a.TwitterHandle,
OmbudsmanURL: a.OmbudsmanURL,
ID: a.ID,
Name: a.Name,
Type: a.Type,
Entity: a.Entity,
UF: a.UF,
Collecting: collecting,
TwitterHandle: a.TwitterHandle,
OmbudsmanURL: a.OmbudsmanURL,
LastCollection: timestamppb.New(a.LastCollection),
}, nil
}

Expand All @@ -51,14 +55,21 @@ func NewAgencyDTO(agency models.Agency) (*AgencyDTO, error) {
if err != nil {
return nil, fmt.Errorf("error while marshaling collecting: %q", err)
}
var lastCollection time.Time
if agency.LastCollection != nil {
lastCollection = time.Unix(agency.LastCollection.Seconds, int64(agency.LastCollection.Nanos))
} else {
lastCollection = time.Now()
}
return &AgencyDTO{
ID: agency.ID,
Name: agency.Name,
Type: agency.Type,
Entity: agency.Entity,
UF: agency.UF,
Collecting: collecting,
TwitterHandle: agency.TwitterHandle,
OmbudsmanURL: agency.OmbudsmanURL,
ID: agency.ID,
Name: agency.Name,
Type: agency.Type,
Entity: agency.Entity,
UF: agency.UF,
Collecting: collecting,
TwitterHandle: agency.TwitterHandle,
OmbudsmanURL: agency.OmbudsmanURL,
LastCollection: lastCollection,
}, nil
}
4 changes: 3 additions & 1 deletion repo/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ func (p *PostgresDB) GetAgenciesByUF(uf string) ([]models.Agency, error) {
func (p *PostgresDB) GetAgency(aid string) (*models.Agency, error) {
var dtoOrgao dto.AgencyDTO
aid = strings.ToLower(aid)
if err := p.db.Model(&dto.AgencyDTO{}).Where("id = ?", aid).First(&dtoOrgao).Error; err != nil {
m := p.db.Model(&dto.AgencyDTO{}).Select("orgaos.*, max(coletas.timestamp) as ultima_coleta")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precisamos mesmo desse orgaos.* ?

m = m.Joins("INNER JOIN coletas ON atual = true AND coletas.id_orgao = orgaos.id AND orgaos.id = ?", aid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Não seria melhor colocar o actual = true o orgaos.id == aid para uma cláusula where? Penso que isso vai diminuir o impacto negativo do join.

if err := m.Group("orgaos.id").First(&dtoOrgao).Error; err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precisamos mesmo desse agrupamento?

return nil, fmt.Errorf("error getting agency '%s': %q", aid, err)
}
orgao, err := dtoOrgao.ConvertToModel()
Expand Down