Skip to content

Commit

Permalink
Deployment on VPS (#17)
Browse files Browse the repository at this point in the history
Updated config files
Extended github actions file with extra jobs
  • Loading branch information
michmzr committed Jun 6, 2023
1 parent e49fe92 commit 40780f9
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 46 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: PocketStats CI

on: [push, pull_request]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
tests:
runs-on: ubuntu-latest
Expand All @@ -26,8 +30,8 @@ jobs:
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
build:
# needs:
# - tests
needs:
- tests
runs-on: ubuntu-latest
environment: master
steps:
Expand Down Expand Up @@ -60,7 +64,6 @@ jobs:
ssh-keyscan 38.242.142.123 >> ~/.ssh/known_hosts
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}

- name: Deploy app.jar to server
run: |
scp ./build/libs/app.jar pocketstats@38.242.142.123:/usr/lib/pocketstats/app.jar
Expand All @@ -74,3 +77,10 @@ jobs:
ssh pocketstats@38.242.142.123 'systemctl status pocketstats.service'
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Health check
run: |
echo "Waiting for 30 seconds before healthcheck"
sleep 30
echo "Performing healthcheck"
wget --retry-connrefused --waitretry=5 --read-timeout=20 --timeout=15 -t 10 http://38.242.142.123:8080/pocketstats/actuator/health -O /dev/null
shell: bash
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,9 @@ venv
./src/frontend/node_modules

# Logs
loga
loga
src/main/java/eu/cybershu/pocketstats/pocket/api/.DS_Store
.DS_Store
src/.DS_Store
src/main/.DS_Store
src/main/raporting/.DS_Store
2 changes: 1 addition & 1 deletion src/frontend/.env.production
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VUE_APP_BACKEND_URL=http://38.242.142.123:8082
VUE_APP_BACKEND_URL=https://pocketstats.cybershu.eu
5 changes: 4 additions & 1 deletion src/frontend/vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ module.exports = defineConfig({
module.exports = {
devServer: {
port: 8080
}
},
publicPath: process.env.NODE_ENV === 'production'
? '/'
: '/'
};
14 changes: 14 additions & 0 deletions src/main/java/eu/cybershu/pocketstats/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package eu.cybershu.pocketstats.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Clock;

@Configuration
public class AppConfig {
@Bean
Clock clock() {
return Clock.systemUTC();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
@Service
public class PocketItemStatsService {
private final MongoTemplate mongoTemplate;

private final Clock clock;

public PocketItemStatsService(
Clock clock,
MongoTemplate mongoTemplate
) {
this.clock = Clock.systemUTC();
this.clock = clock;
this.mongoTemplate = mongoTemplate;
}

Expand Down Expand Up @@ -223,11 +225,14 @@ public PeriodItemsStats itemsStatsPeriod(TimePeriod timePeriod) {
.append("archived", 1L))));

Document docs = result.first();
Objects.requireNonNull(docs);

return new PeriodItemsStats(
docs.getLong("added"), docs.getLong("archived")
);
if (docs == null) {
return new PeriodItemsStats(0L, 0L);
} else {
return new PeriodItemsStats(
docs.getLong("added"), docs.getLong("archived")
);
}
}

public PeriodItemsStats itemsStatsTotal() {
Expand Down
7 changes: 7 additions & 0 deletions src/main/raporting/raporting/raporting.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ spring:
server:
port: ${PORT:8081}
servlet:
context-path: /pocketstats
context-path: /
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ auth:
access_token: https://getpocket.com/v3/oauth/authorize
get: https://getpocket.com/v3/get
consumer-key: ${POCKET_CONSUMER_KEY}
redirect_uri: ${BACKEND_URL:http://localhost:8081}/pocket/auth/token
redirect_uri: ${VUE_APP_BACKEND_URL:http://localhost:8081}/pocket/auth/token

management:
endpoints:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import eu.cybershu.pocketstats.utils.TimeUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.test.context.ContextConfiguration
import spock.lang.Shared

import java.time.*
Expand All @@ -20,7 +23,16 @@ import java.time.temporal.ChronoUnit
import static eu.cybershu.pocketstats.PocketItemBuilder.*
import static org.assertj.core.api.Assertions.assertThat

@TestConfiguration
class TestConfig {
@Bean
Clock clock() {
return Clock.fixed(Instant.parse("2023-04-26T10:00:00Z"), ZoneId.of("UTC"))
}
}

@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
@AutoConfigureDataMongo
class PocketItemStatsServiceIntTest extends BaseTest {
@Autowired
Expand Down Expand Up @@ -80,8 +92,8 @@ class PocketItemStatsServiceIntTest extends BaseTest {
assert repository.saveAll(items).size() == items.size()
def start = LocalDate.now().minusDays(7)
def end = LocalDate.now()
def start = LocalDate.now(clock).minusDays(7)
def end = LocalDate.now(clock)
when:
def result = statsService.getDayStatsRecords(
start, end, DayStatsType.ARCHIVED
Expand All @@ -92,15 +104,16 @@ class PocketItemStatsServiceIntTest extends BaseTest {
assertNumberInDay(result, start, 3)
assertNumberInDay(result, end.minusDays(5), 2)
assertNumberInDay(result, end.minusDays(2), 1)
assertNumberInDay(result, end, 1)
}
def "given items in time period expect valid number of valid TODO items"() {
given:
def items = []
def start = LocalDate.now(clock).minusDays(7)
def end = LocalDate.now(clock)
def items = []
//-7 days: 3 added
Instant minus7days = now.minus(7, ChronoUnit.DAYS)
Instant minus7days = TimeUtils.toStartDayInstant(start)
items.addAll([
todo(minus7days),
todo(minus7days),
Expand Down Expand Up @@ -129,17 +142,16 @@ class PocketItemStatsServiceIntTest extends BaseTest {
])
//today: 3 added
Instant endInstant = TimeUtils.toStartDayInstant(end)
Instant endMightnightInstant = TimeUtils.toEndOfDay(endInstant)
items.addAll([
todo(TimeUtils.instantTodayEnd()),
todo(now),
todo(TimeUtils.instantTodayEnd()),
archived(now, now),
todo(endMightnightInstant),
todo(endInstant),
todo(endMightnightInstant),
archived(endInstant, now),
])
assert repository.saveAll(items).size() == items.size()
def start = LocalDate.now().minusDays(7)
def end = LocalDate.now()
when:
def result = statsService.getDayStatsRecords(
start, end, DayStatsType.TODO
Expand Down Expand Up @@ -250,24 +262,18 @@ class PocketItemStatsServiceIntTest extends BaseTest {
assert repository.saveAll(doneItems).size() == doneItems.size()
assert repository.saveAll(todoItems).size() == todoItems.size()
when:
def result = statsService.itemsStatsAggregated()
then: "last year"
assetPeriodStats(result, "last-year", 1, 2)
and: "last month"
assetPeriodStats(result, "last-month", 1, 2)
and: "last week"
assetPeriodStats(result, "last-week", 1, 2)
then: "current year"
assetPeriodStats(result, "current-year", 5, 5 + 5)
and: "current month"
assetPeriodStats(result, "current-month", 3, 3 + 3)
and: "current week"
assetPeriodStats(result, "current-week", 2, 2 + 2)
and: "total"
assetPeriodStats(result, "total", 8, 7 + 8)
then:
verifyAll {
assetPeriodStats(result, "last-year", 1, 2)
assetPeriodStats(result, "last-month", 1, 2) //3,6
assetPeriodStats(result, "last-week", 1, 2)
assetPeriodStats(result, "current-year", 5, 5 + 5)
assetPeriodStats(result, "current-month", 3, 3 + 3)
assetPeriodStats(result, "current-week", 2, 2 + 2)
assetPeriodStats(result, "total", 8, 7 + 8)
}
}
private assetPeriodStats(ItemsStatsAggregated resul, String name, int expectedRead, int expectedAdded) {
Expand All @@ -286,10 +292,6 @@ class PocketItemStatsServiceIntTest extends BaseTest {
todo(timeAdded, title, url)
}

private PocketItem daysDiffArchived(int days) {
daysDiffArchived(days, instantDaysDiffers(-30))
}

private PocketItem daysDiffArchived(int days, Instant dayAdded) {
def timeAdded = instantDaysDiffers(days)
String title = "read " + timeAdded
Expand Down Expand Up @@ -320,7 +322,7 @@ class PocketItemStatsServiceIntTest extends BaseTest {

private void assertNumberInDay(DayStatsRecords dayStatsRecords, LocalDate date, Integer expected) {
def found = dayStatsRecords.stats().findAll { it -> it.day() == date }
assert found.size() == 1, "Found more than 1 record for day ${date}. It can be only one!"
assert found.size() == 1, "Not found record for day ${date}. Expected one!"
assert found.get(0).number() == expected, "Expected ${expected} got ${found.size()} items for day ${date}"
}
}

0 comments on commit 40780f9

Please sign in to comment.