Skip to content

Commit

Permalink
add Inconsistent field to AgencyMonthlyInfo and AnnualSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
JezzDiego committed Nov 25, 2024
1 parent d3b675a commit d940839
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions models/monthlyInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type AgencyMonthlyInfo struct {
Score *Score `json:"score,omitempty"`
Duration float64 `json:"duration,omitempty"` // Crawling duration (seconds)
ManualCollection bool `json:"coleta_manual,omitempty"` // If the data was collected manually
Inconsistent bool `json:"inconsistent"` // If the data is inconsistent
}

type Meta struct {
Expand Down Expand Up @@ -94,6 +95,7 @@ type AnnualSummary struct {
NumMonthsWithData int `json:"months_with_data,omitempty"`
Package *Backup `json:"package,omitempty"`
ItemSummary ItemSummary `json:"item_summary,omitempty"`
Inconsistent bool `json:"inconsistent,omitempty"` // If the data is inconsistent
}

type RemmunerationSummary struct {
Expand Down
3 changes: 3 additions & 0 deletions repo/database/dto/annuaISummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type AnnualSummaryDTO struct {
RemunerationsPerCapita float64 `gorm:"column:remuneracoes_membro"`
NumMonthsWithData int `gorm:"column:meses_com_dados"`
ItemSummary ItemSummary `gorm:"embedded"`
Inconsistent bool `gorm:"column:inconsistente"`
}

func NewAnnualSummaryDTO(ami models.AnnualSummary) *AnnualSummaryDTO {
Expand All @@ -44,6 +45,7 @@ func NewAnnualSummaryDTO(ami models.AnnualSummary) *AnnualSummaryDTO {
HealthAllowance: ami.ItemSummary.HealthAllowance,
Others: ami.ItemSummary.Others,
},
Inconsistent: ami.Inconsistent,
}
}

Expand Down Expand Up @@ -71,5 +73,6 @@ func (ami *AnnualSummaryDTO) ConvertToModel() *models.AnnualSummary {
HealthAllowance: ami.ItemSummary.HealthAllowance,
Others: ami.ItemSummary.Others,
},
Inconsistent: ami.Inconsistent,
}
}
3 changes: 3 additions & 0 deletions repo/database/dto/monthlyInfoDTO.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type AgencyMonthlyInfoDTO struct {
Meta
Score
ManualCollection bool `gorm:"column:manual"` // Tempo de execução da coleta em segundos
Inconsistent bool `gorm:"column:inconsistente"`
}

func (AgencyMonthlyInfoDTO) TableName() string {
Expand Down Expand Up @@ -133,6 +134,7 @@ func (a AgencyMonthlyInfoDTO) ConvertToModel() (*models.AgencyMonthlyInfo, error
Package: &pkg,
Duration: a.Duration,
ManualCollection: a.ManualCollection,
Inconsistent: a.Inconsistent,
}, nil
}

Expand Down Expand Up @@ -212,6 +214,7 @@ func NewAgencyMonthlyInfoDTO(agmi models.AgencyMonthlyInfo) (*AgencyMonthlyInfoD
Package: pkg,
Duration: agmi.Duration,
ManualCollection: agmi.ManualCollection,
Inconsistent: agmi.Inconsistent,
}, nil
}

Expand Down
28 changes: 25 additions & 3 deletions repo/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,14 @@ func (p *PostgresDB) GetMonthlyInfo(agencies []models.Agency, year int) (map[str
for _, agency := range agencies {
var dtoAgmis []dto.AgencyMonthlyInfoDTO
//Pegando as coletas do postgres, filtrando por órgão, ano e a coleta atual.
m := p.db.Model(&dto.AgencyMonthlyInfoDTO{})
m := p.db.Model(&dto.AgencyMonthlyInfoDTO{}).Select(`coletas.*, EXISTS (
SELECT 1
FROM remuneracoes r
WHERE inconsistente = TRUE
AND r.orgao = coletas.id_orgao
AND r.ano = coletas.ano
and r.mes = coletas.mes
) AS inconsistente`)
m = m.Where("id_orgao = ? AND ano = ? AND atual = TRUE AND (procinfo::text = 'null' OR procinfo IS NULL) ", agency.ID, year)
m = m.Order("mes ASC")
if err := m.Find(&dtoAgmis).Error; err != nil {
Expand Down Expand Up @@ -327,7 +334,14 @@ func (p *PostgresDB) GetAnnualSummary(agency string) ([]models.AnnualSummary, er
MAX(media_por_membro.salario) AS remuneracao_base_membro,
MAX(media_por_membro.beneficios) AS outras_remuneracoes_membro,
MAX(media_por_membro.descontos) AS descontos_membro,
MAX(media_por_membro.remuneracao) AS remuneracoes_membro`
MAX(media_por_membro.remuneracao) AS remuneracoes_membro,
EXISTS (
SELECT 1
FROM remuneracoes r
WHERE inconsistente = TRUE
AND r.orgao = coletas.id_orgao
AND r.ano = coletas.ano
) AS inconsistente`

join := `LEFT JOIN media_por_membro on coletas.ano = media_por_membro.ano and coletas.id_orgao = media_por_membro.orgao`
m := p.db.Model(&dtoAgmi).Select(query).Joins(join)
Expand All @@ -346,7 +360,15 @@ func (p *PostgresDB) GetAnnualSummary(agency string) ([]models.AnnualSummary, er
func (p *PostgresDB) GetOMA(month int, year int, agency string) (*models.AgencyMonthlyInfo, *models.Agency, error) {
var dtoAgmi dto.AgencyMonthlyInfoDTO
id := fmt.Sprintf("%s/%s/%d", strings.ToLower(agency), dto.AddZeroes(month), year)
m := p.db.Model(dto.AgencyMonthlyInfoDTO{}).Where("id = ? AND atual = true", id).First(&dtoAgmi)
m := p.db.Model(dto.AgencyMonthlyInfoDTO{}).Select(`coletas.*, EXISTS (
SELECT 1
FROM remuneracoes r
WHERE inconsistente = TRUE
AND r.orgao = coletas.id_orgao
AND r.ano = coletas.ano
and r.mes = coletas.mes
) AS inconsistente`)
m = m.Where("id = ? AND atual = true", id).First(&dtoAgmi)
if err := m.Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, nil, fmt.Errorf("there is no data with this parameters")
Expand Down
2 changes: 2 additions & 0 deletions repo/database/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,8 @@ func (g getAnnualSummary) testWhenMonthlyInfoExists(t *testing.T) {
assert.Equal(t, amis[1].ItemSummary.Vacation, returnedAmis[1].ItemSummary.Vacation)
assert.Equal(t, 1000.0, returnedAmis[2].BaseRemunerationPerCapita)
assert.Equal(t, 1200.0, returnedAmis[2].OtherRemunerationsPerCapita)
assert.Equal(t, amis[0].Inconsistent, returnedAmis[0].Inconsistent)
assert.Equal(t, false, returnedAmis[0].Inconsistent)
truncateTables()
}

Expand Down

0 comments on commit d940839

Please sign in to comment.