Skip to content

Commit

Permalink
Implemented redis flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardolopezb committed Jul 7, 2023
1 parent fbecab7 commit 2658302
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 90 deletions.
18 changes: 10 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ repositories {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/austral-ingsis/class-redis-streams")
credentials {
username = System.getenv("USERNAME")
password = System.getenv("TOKEN")
username = System.getenv("USERNAME") ?: project.findProperty("gpr.user")
password = System.getenv("TOKEN") ?: project.findProperty("gpr.key")
}
}
maven {
Expand All @@ -35,8 +35,8 @@ repositories {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/ingsis-group-6/snippet-events")
credentials {
username = System.getenv("USERNAME")
password = System.getenv("TOKEN")
username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
}
}
}
Expand All @@ -54,15 +54,17 @@ dependencies {
implementation 'org.springframework.metrics:spring-metrics:0.5.1.RELEASE'
implementation 'com.netflix.spectator:spectator-api:0.55.0'
implementation 'org.springframework:spring-aspects'
implementation 'io.projectreactor.kotlin:reactor-kotlin-extensions'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'

implementation 'org.ingsis.group6:snippet-events:0.0.2-SNAPSHOT'
implementation 'org.ingsis.group6:snippet-events:0.0.4-SNAPSHOT'


implementation("spring-mvc-redis-streams:lib:0.0.1-SNAPSHOT")
// implementation("spring-mvc-redis-streams:lib:0.0.1-SNAPSHOT")
implementation("org.springframework.boot:spring-boot-starter-data-redis")

// implementation 'org.austral.ingsis:redis-streams:0.1.7'
// implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
implementation 'org.austral.ingsis:redis-streams:0.1.7'
implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'
}

Expand Down
72 changes: 36 additions & 36 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@ services:
POSTGRES_DB: db
command: -p 5433

datadog:
image: datadog/agent
container_name: "datadog-dev-env-manager"
environment:
- DD_API_KEY=${DD_API_KEY}
- DD_SITE=us5.datadoghq.com
- DD_LOGS_ENABLED=true
- DD_APM_ENABLED=true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /proc/:/host/proc/:ro
- /sys/fs/cgroup:/host/sys/fs/cgroup:ro
# datadog:
# image: datadog/agent
# container_name: "datadog-dev-env-manager"
# environment:
# - DD_API_KEY=${DD_API_KEY}
# - DD_SITE=us5.datadoghq.com
# - DD_LOGS_ENABLED=true
# - DD_APM_ENABLED=true
# volumes:
# - /var/run/docker.sock:/var/run/docker.sock
# - /proc/:/host/proc/:ro
# - /sys/fs/cgroup:/host/sys/fs/cgroup:ro

redis:
image: redis
ports:
- "6379:6379"
# redis:
# image: redis
# ports:
# - "6379:6379"

app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8081:8081"
environment:
DD_API_KEY: ${DD_API_KEY}
DB_HOST: db
DB_PORT: 5433
DB_NAME: db
DB_USER: postgres
DB_PASSWORD: postgres
SHARE_URI: "http://localhost:8080/share"
REDIS_STREAM_KEY: ingsis
REDIS_HOST: localhost
REDIS_PORT: 6379
REDIS_STREAM_GROUP_LINT: lint-manager
labels:
com.datadoghq.ad.logs: '[{"source": "java", "service": "manager-api-dev", "log_processing_rules": [{"type": "multi_line", "name": "log_start_with_date", "pattern" : "\\d{4}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])"}]}]'
# app:
# build:
# context: .
# dockerfile: Dockerfile
# ports:
# - "8081:8081"
# environment:
# DD_API_KEY: ${DD_API_KEY}
# DB_HOST: db
# DB_PORT: 5433
# DB_NAME: db
# DB_USER: postgres
# DB_PASSWORD: postgres
# SHARE_URI: "http://localhost:8080/share"
# REDIS_STREAM_KEY: ingsis
# REDIS_HOST: localhost
# REDIS_PORT: 6379
# REDIS_STREAM_GROUP_LINT: lint-manager
# labels:
# com.datadoghq.ad.logs: '[{"source": "java", "service": "manager-api-dev", "log_processing_rules": [{"type": "multi_line", "name": "log_start_with_date", "pattern" : "\\d{4}-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])"}]}]'

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
gpr.user=ricardolopezb
gpr.key=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
gpr.key=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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.springframework.metrics.export.datadog.EnableDatadogMetrics


@SpringBootApplication
@EnableDatadogMetrics
//@EnableDatadogMetrics
class SnippetManagerApplication

fun main(args: Array<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class LinterRulesController {
}

@GetMapping("/rule/linter")
fun getFormatterRulesByUserId(principal: Principal): ResponseEntity<LinterRulesDTO> {
suspend fun getFormatterRulesByUserId(principal: Principal): ResponseEntity<LinterRulesDTO> {
return ResponseEntity.ok(ruleService.getLinterRulesByUserId(principal.name))
}

@PutMapping("/rule/linter")
fun updateFormatterRules(@RequestBody linterRules: LinterRulesDTO, principal: Principal): ResponseEntity<LinterRulesDTO> {
suspend fun updateFormatterRules(@RequestBody linterRules: LinterRulesDTO, principal: Principal): ResponseEntity<LinterRulesDTO> {
return ResponseEntity.ok(ruleService.updateLinterRules(linterRules, principal.name))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ingsis.snippetmanager.domains.rule.dto

import ingsis.snippetmanager.domains.rule.model.CaseConvention
import ingsis.snippetmanager.domains.rule.model.LinterRules
import ingsis.snippetmanager.domains.rule.model.LinterRulesEntity

class LinterRulesDTO {

Expand All @@ -19,7 +19,7 @@ class LinterRulesDTO {
this.userId = userId
}

constructor(rule: LinterRules){
constructor(rule: LinterRulesEntity){
this.caseConvention = rule.caseConvention
this.printExpressionsEnabled = rule.printExpressionsEnabled
this.userId = rule.userId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import javax.persistence.*

@Entity
@Table(name = "linter_rules")
class LinterRules {
class LinterRulesEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ingsis.snippetmanager.domains.rule.repository

import ingsis.snippetmanager.domains.rule.model.LinterRules
import ingsis.snippetmanager.domains.rule.model.LinterRulesEntity
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.util.UUID

interface LinterRulesRepository: JpaRepository<LinterRules, UUID> {
interface LinterRulesRepository: JpaRepository<LinterRulesEntity, UUID> {

@Query("SELECT l FROM LinterRules l WHERE l.userId = :userId")
fun findByUserId(userId: String): LinterRules
@Query("SELECT l FROM LinterRulesEntity l WHERE l.userId = :userId")
fun findByUserId(userId: String): LinterRulesEntity
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import ingsis.snippetmanager.domains.rule.dto.LinterRulesDTO

interface LinterRulesService {

fun getLinterRulesByUserId(userId: String): LinterRulesDTO
fun updateLinterRules(linterRules: LinterRulesDTO, userId: String): LinterRulesDTO
suspend fun getLinterRulesByUserId(userId: String): LinterRulesDTO
suspend fun updateLinterRules(linterRules: LinterRulesDTO, userId: String): LinterRulesDTO
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@ package ingsis.snippetmanager.domains.rule.service

import ingsis.snippetmanager.domains.rule.dto.LinterRulesDTO
import ingsis.snippetmanager.domains.rule.model.CaseConvention
import ingsis.snippetmanager.domains.rule.model.LinterRules
import ingsis.snippetmanager.domains.rule.model.LinterRulesEntity

import ingsis.snippetmanager.domains.rule.repository.LinterRulesRepository
import ingsis.snippetmanager.domains.snippet.service.SnippetService
import ingsis.snippetmanager.redis.producer.LintRequestProducer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import snippet.events.lint.LintRequestEvent
import snippet.events.lint.rules.LinterRules

@Service
class LinterRulesServiceImpl: LinterRulesService {

@Autowired
private var linterRulesRepository: LinterRulesRepository

constructor(linterRulesRepository: LinterRulesRepository){
@Autowired
private var snippetService: SnippetService

@Autowired
private var producer: LintRequestProducer

constructor(linterRulesRepository: LinterRulesRepository, snippetService: SnippetService, producer: LintRequestProducer){
this.linterRulesRepository = linterRulesRepository
this.snippetService = snippetService
this.producer = producer
}

override fun getLinterRulesByUserId(userId: String): LinterRulesDTO {
override suspend fun getLinterRulesByUserId(userId: String): LinterRulesDTO {
try {
val rules = this.linterRulesRepository.findByUserId(userId)
return LinterRulesDTO(rules)
Expand All @@ -26,14 +39,32 @@ class LinterRulesServiceImpl: LinterRulesService {
}
}

override fun updateLinterRules(linterRules: LinterRulesDTO, userId: String): LinterRulesDTO {
override suspend fun updateLinterRules(linterRules: LinterRulesDTO, userId: String): LinterRulesDTO {
try {
val rules = linterRulesRepository.findByUserId(userId)
rules.caseConvention = linterRules.caseConvention as CaseConvention
rules.printExpressionsEnabled = linterRules.printExpressionsEnabled
sendSnippetsToLinterService(userId, linterRules)
return LinterRulesDTO(linterRulesRepository.save(rules))
}catch (e: Exception){
return LinterRulesDTO(linterRulesRepository.save(LinterRules(linterRules.caseConvention as CaseConvention,linterRules.printExpressionsEnabled,userId)))
val createdLinterRules = linterRulesRepository.save(LinterRulesEntity(linterRules.caseConvention as CaseConvention,linterRules.printExpressionsEnabled,userId))
sendSnippetsToLinterService(userId, linterRules)
return LinterRulesDTO(createdLinterRules)
}
}

private suspend fun sendSnippetsToLinterService(userId: String, linterRulesDto: LinterRulesDTO) {

val userSnippets = this.snippetService.getSnippetsByUserId(userId)
for (snippet in userSnippets) {
val rules = LinterRules(enumValueOf<snippet.events.lint.rules.CaseConvention>(linterRulesDto.caseConvention.toString()), linterRulesDto.printExpressionsEnabled!!)
val event = LintRequestEvent(snippet.id.toString(), snippet.content!!, rules)
this.producer.publishEvent(event)

}




}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package ingsis.snippetmanager.redis.consumer

import org.austral.ingsis.`class`.redis.RedisStreamConsumer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.redis.connection.stream.ObjectRecord
import org.springframework.data.redis.core.ReactiveRedisTemplate
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.stream.StreamReceiver
import org.springframework.stereotype.Component
import snippet.events.LintResultEvent
import spring.mvc.redis.streams.RedisStreamConsumer
import snippet.events.lint.LintResultEvent
import java.time.Duration

@Component
class SampleConsumer @Autowired constructor(
redis: RedisTemplate<String, String>,
@Value("\${redis.stream.key}") streamKey: String,
redis: ReactiveRedisTemplate<String, String>,
@Value("\${redis.stream.result_key}") streamKey: String,
@Value("\${redis.groups.lint}") groupId: String
) : RedisStreamConsumer<LintResultEvent>(streamKey, groupId, redis) {

init {
subscription()
}
override fun onMessage(record: ObjectRecord<String, LintResultEvent>) {
val a = 42
println("Received event of snippet ${record.value.lintedSnippetId} with status ${record.value.status}")
println("Received event of snippet ${record.value.snippetId} with status ${record.value.status}")
}

override fun options(): StreamReceiver.StreamReceiverOptions<String, ObjectRecord<String, LintResultEvent>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ingsis.snippetmanager.redis.producer

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import kotlinx.coroutines.reactor.awaitSingle
import org.austral.ingsis.`class`.redis.RedisStreamProducer
import org.springframework.data.redis.core.ReactiveRedisTemplate
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.stereotype.Component
import snippet.events.lint.LintRequestEvent
import snippet.events.lint.LintResultEvent
import snippet.events.lint.LintResultStatus

import java.util.*

@Component
class LintRequestProducer @Autowired constructor(
@Value("\${redis.stream.request_key}") streamKey: String,
redis: ReactiveRedisTemplate<String, String>
) : RedisStreamProducer(streamKey, redis) {

suspend fun publishEvent(event: LintRequestEvent){
println("Publishing on stream: $streamKey")
emit(event).awaitSingle()
}

}
14 changes: 7 additions & 7 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}
#spring.datasource.url=jdbc:postgresql://localhost:5433/db
#spring.datasource.username=postgres
#spring.datasource.password=pablo
#spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
#spring.datasource.username=${DB_USER}
#spring.datasource.password=${DB_PASSWORD}
spring.datasource.url=jdbc:postgresql://localhost:5433/db
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto= update
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=false
server.port=8080
server.port=8081
datadog.apiKey= ${DD_API_KEY}
datadog.enabled= true
datadog.step= PT10S
Expand Down
Loading

0 comments on commit 2658302

Please sign in to comment.