generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* PI-2145 Initial allocations report CSV endpoint * Added cron job to pull report and upload to Slack
- Loading branch information
1 parent
88db8c6
commit cad5d00
Showing
22 changed files
with
414 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
projects/workforce-allocations-to-delius/deploy/templates/initial-allocations-report.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{{- $config := index .Values "initial-allocations-report" | default dict -}} | ||
{{- if $config.enabled | default false -}} | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: initial-allocations-report-script | ||
data: | ||
script.sh: | | ||
#!/bin/bash | ||
set -euo pipefail | ||
date=$(date +%Y-%m-%d) | ||
filename=initial-allocations-$date.csv | ||
echo Getting HMPPS Auth token... | ||
hmpps_auth_token=$(curl -fsSL -XPOST -u "$CLIENT_ID:$CLIENT_SECRET" '{{ index .Values "generic-service" "env" "SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_HMPPS-AUTH_TOKEN-URI" }}?grant_type=client_credentials' | jq -r .access_token) | ||
echo Downloading report... | ||
curl -fsSL -H "Authorization: Bearer $hmpps_auth_token" https://{{ index .Values "generic-service" "ingress" "host" }}/initial-allocations.csv > "/tmp/$filename" | ||
echo Downloaded report with $(wc -l < "/tmp/$filename") rows | ||
echo Starting file upload... | ||
file_details=$(curl -fsSL -XPOST -F "token=$SLACK_TOKEN" -F "filename=$filename" -F 'snippet_type=csv' -F "length=$(wc -c < "/tmp/$filename")" https://slack.com/api/files.getUploadURLExternal) | ||
echo Got file upload details: "$file_details" | ||
file_id=$(echo "$file_details" | jq -r .file_id) | ||
upload_url=$(echo "$file_details" | jq -r .upload_url) | ||
echo Uploading file... | ||
curl -fsSL -F "file=@/tmp/$filename" "$upload_url" | ||
echo Sending message... | ||
curl -fsSL -F "token=$SLACK_TOKEN" -F "initial_comment=Initial Allocations Report ($(date '+%d/%m/%Y'))" -F "files=[{\"id\":\"$file_id\"}]" -F "channel_id={{ index .Values "initial-allocations-report" "channel_id" }}" https://slack.com/api/files.completeUploadExternal | ||
echo Uploaded report to Slack | ||
--- | ||
apiVersion: batch/v1 | ||
kind: CronJob | ||
metadata: | ||
name: initial-allocations-report | ||
spec: | ||
schedule: {{ index .Values "initial-allocations-report" "schedule" }} | ||
concurrencyPolicy: Forbid | ||
failedJobsHistoryLimit: 1 | ||
successfulJobsHistoryLimit: 1 | ||
jobTemplate: | ||
spec: | ||
template: | ||
spec: | ||
serviceAccountName: workforce-allocations-to-delius | ||
volumes: | ||
- name: script-volume | ||
configMap: | ||
name: initial-allocations-report-script | ||
containers: | ||
- name: generate-report | ||
image: "ghcr.io/ministryofjustice/hmpps-devops-tools:latest" | ||
command: [ "bash", "/script.sh" ] | ||
volumeMounts: | ||
- name: script-volume | ||
mountPath: /script.sh | ||
subPath: script.sh | ||
env: | ||
- name: CLIENT_ID | ||
valueFrom: | ||
secretKeyRef: | ||
name: workforce-allocations-to-delius-client-credentials | ||
key: CLIENT_ID | ||
optional: false | ||
- name: CLIENT_SECRET | ||
valueFrom: | ||
secretKeyRef: | ||
name: workforce-allocations-to-delius-client-credentials | ||
key: CLIENT_SECRET | ||
optional: false | ||
- name: SLACK_TOKEN | ||
valueFrom: | ||
secretKeyRef: | ||
name: slack-bot | ||
key: TOKEN | ||
optional: false | ||
securityContext: | ||
capabilities: | ||
drop: | ||
- ALL | ||
runAsNonRoot: true | ||
allowPrivilegeEscalation: false | ||
seccompProfile: | ||
type: RuntimeDefault | ||
restartPolicy: Never | ||
{{- end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/ExistingAllocationsDataLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package uk.gov.justice.digital.hmpps.data | ||
|
||
import jakarta.persistence.EntityManager | ||
import jakarta.transaction.Transactional | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
import org.springframework.stereotype.Component | ||
import uk.gov.justice.digital.hmpps.data.generator.* | ||
import uk.gov.justice.digital.hmpps.integrations.delius.event.OrderManagerRepository | ||
|
||
@Component | ||
@ConditionalOnProperty("seed.database") | ||
class ExistingAllocationsDataLoader( | ||
private val orderManagerRepository: OrderManagerRepository, | ||
private val existingAllocationsRefDataLoader: ExistingAllocationsRefDataLoader, | ||
) { | ||
fun loadData() { | ||
existingAllocationsRefDataLoader.loadData() | ||
orderManagerRepository.save(OrderManagerGenerator.UNALLOCATED) | ||
orderManagerRepository.save(OrderManagerGenerator.INITIAL_ALLOCATION) | ||
} | ||
} | ||
|
||
@Component | ||
@Transactional | ||
class ExistingAllocationsRefDataLoader(private val entityManager: EntityManager) { | ||
fun loadData() { | ||
entityManager.persist(ProviderGenerator.PDU) | ||
entityManager.persist(ProviderGenerator.LAU) | ||
entityManager.persist(TeamGenerator.TEAM_IN_LAU) | ||
entityManager.persist(StaffGenerator.ALLOCATED) | ||
entityManager.persist(EventGenerator.HAS_INITIAL_ALLOCATION) | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...o-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/entity/ProbationDeliveryUnit.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package uk.gov.justice.digital.hmpps.data.entity | ||
|
||
import jakarta.persistence.* | ||
import org.hibernate.annotations.Immutable | ||
import uk.gov.justice.digital.hmpps.integrations.delius.provider.StaffWithUser | ||
import uk.gov.justice.digital.hmpps.integrations.delius.provider.Team | ||
import java.time.ZonedDateTime | ||
|
||
@Entity | ||
@Immutable | ||
@Table(name = "borough") | ||
class ProbationDeliveryUnit( | ||
@Id | ||
@Column(name = "borough_id") | ||
val id: Long, | ||
val code: String, | ||
val description: String, | ||
) | ||
|
||
@Entity | ||
@Immutable | ||
@Table(name = "district") | ||
class LocalAdminUnit( | ||
@Id | ||
@Column(name = "district_id") | ||
val id: Long, | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "borough_id") | ||
val pdu: ProbationDeliveryUnit, | ||
) | ||
|
||
@Entity | ||
@Immutable | ||
@Table(name = "team") | ||
class TeamWithLocalAdminUnit( | ||
@Id | ||
@Column(name = "team_id") | ||
val id: Long, | ||
|
||
@Column(name = "code", columnDefinition = "char(6)") | ||
val code: String, | ||
|
||
@Column(name = "probation_area_id") | ||
val providerId: Long, | ||
|
||
val description: String, | ||
|
||
@Column(name = "end_date") | ||
val endDate: ZonedDateTime? = null, | ||
|
||
@ManyToMany(mappedBy = "teams") | ||
val staff: List<StaffWithUser> = listOf(), | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "district_id") | ||
val localAdminUnit: LocalAdminUnit? = null, | ||
) | ||
|
||
fun Team.withLocalAdminUnit(localAdminUnit: LocalAdminUnit) = | ||
TeamWithLocalAdminUnit(id, code, providerId, description, endDate, staff, localAdminUnit) | ||
|
||
fun TeamWithLocalAdminUnit.toTeam() = Team(id, code, providerId, description, endDate, staff) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.