Skip to content

Commit

Permalink
APLU / Arbo
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoccino committed Mar 1, 2022
1 parent 6e9f407 commit 4efa48d
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 6 deletions.
11 changes: 11 additions & 0 deletions api/src/graphql/aplu.sdl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const schema = gql`
type Query {
arbo: ArboPath! @requireAuth
}
type ArboPath {
path: String!
count: Int!
children: [ArboPath!]!
}
`
5 changes: 5 additions & 0 deletions api/src/lib/files/S3Path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ describe('paths', () => {
expect(S3Path.getPath('a/b/', 'b.zip')).toEqual('a/b/b.zip')
expect(S3Path.getPath('/', 'b.zip')).toEqual('b.zip')
})
it('splitPath', () => {
expect(S3Path.splitPath('a.jpg')).toEqual(['a.jpg'])
expect(S3Path.splitPath('a/b.jpg')).toEqual(['a', 'b.jpg'])
expect(S3Path.splitPath('a/b/c.jpg')).toEqual(['a', 'b', 'c.jpg'])
})
})
17 changes: 11 additions & 6 deletions api/src/lib/files/S3Path.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
export default {
const S3Path = {
getBasePath(path: string) {
const basePath = path
.split('/')
.filter((p) => p !== '')
.slice(0, -1)
.join('/')
const splitted = S3Path.splitPath(path)
const basePath = splitted.slice(0, -1).join('/')

return basePath
},
Expand All @@ -29,4 +26,12 @@ export default {

return path
},

splitPath(path: string) {
const splitted = path.split('/').filter((p) => p !== '')

return splitted
},
}

export default S3Path
146 changes: 146 additions & 0 deletions api/src/services/aplu/aplu.scenarios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
export const standard = defineScenario({
tagGroup: {
one: {
data: {
name: 'TG1',
},
},
two: {
data: {
name: 'TG2',
},
},
},
tag: {
g1t1: (scenario) => ({
data: {
name: 'g1t1',
tagGroupId: scenario.tagGroup.one.id,
},
}),
g1t2: (scenario) => ({
data: {
name: 'g1t2',
tagGroupId: scenario.tagGroup.one.id,
},
}),
g1t3: (scenario) => ({
data: {
name: 'g1t3',
tagGroupId: scenario.tagGroup.one.id,
},
}),
g2t1: (scenario) => ({
data: {
name: 'g2t1',
tagGroupId: scenario.tagGroup.two.id,
},
}),
g2t2: (scenario) => ({
data: {
name: 'g2t2',
tagGroupId: scenario.tagGroup.two.id,
},
}),
g2t3: (scenario) => ({
data: {
name: 'g2t3',
tagGroupId: scenario.tagGroup.two.id,
},
}),
},

image: {
p1: (scenario) => ({
data: {
path: 'p1.jpg',
dateTaken: '2022-01-01T00:01:00Z',
tagsOnImages: {
create: [
{
tagId: scenario.tag.g1t1.id,
},
{
tagId: scenario.tag.g2t1.id,
},
],
},
},
}),
p2: (scenario) => ({
data: {
path: 'ath1/p2.jpg',
dateTaken: '2022-01-01T00:02:00Z',
tagsOnImages: {
create: [
{
tagId: scenario.tag.g1t2.id,
},
{
tagId: scenario.tag.g2t2.id,
},
],
},
},
}),
p3: (scenario) => ({
data: {
path: 'ath1/ath2/p3.jpg',
dateTaken: '2022-01-01T00:03:00Z',
tagsOnImages: {
create: [
{
tagId: scenario.tag.g1t1.id,
},
{
tagId: scenario.tag.g2t2.id,
},
],
},
},
}),
p4: (scenario) => ({
data: {
path: 'ath3/p4.jpg',
dateTaken: '2022-01-01T00:04:00Z',
tagsOnImages: {
create: [
{
tagId: scenario.tag.g1t2.id,
},
{
tagId: scenario.tag.g2t1.id,
},
],
},
},
}),
p5: (scenario) => ({
data: {
path: 'p5.jpg',
dateTaken: '2022-01-01T00:05:00Z',
tagsOnImages: {
create: [
{
tagId: scenario.tag.g1t1.id,
},
{
tagId: scenario.tag.g2t2.id,
},
{
tagId: scenario.tag.g2t3.id,
},
],
},
},
}),
notag: {
data: {
path: 'notag.jpg',
dateTaken: '2022-01-02T00:00:00Z',
},
},
},
})

export type StandardScenario = typeof standard
40 changes: 40 additions & 0 deletions api/src/services/aplu/aplu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { arbo } from './aplu'
import { ArboPath } from 'types/graphql'

type NewArbo = {
path: string
count: number
children: Record<string, NewArbo>
}

function arboToObject(arbo: ArboPath): NewArbo {
return {
path: arbo.path,
count: arbo.count,
children: arbo.children.reduce(
(acc, curr) => ({
...acc,
[curr.path]: arboToObject(curr),
}),
{}
),
}
}

describe('aplu', () => {
scenario('arbo', async (scenario) => {
const root = await arbo()
const rootO = arboToObject(root)
expect(rootO.path).toBe('/')
expect(rootO.count).toEqual(Object.keys(scenario.image).length)
expect(Object.keys(rootO.children).length).toEqual(2)

expect(rootO.children.ath1.path).toEqual('ath1')
expect(rootO.children.ath1.count).toEqual(2)
expect(Object.keys(rootO.children.ath1.children).length).toEqual(1)
expect(rootO.children.ath1.children.ath2.path).toEqual('ath2')
expect(rootO.children.ath1.children.ath2.count).toEqual(1)
expect(rootO.children.ath3.path).toEqual('ath3')
expect(rootO.children.ath3.count).toEqual(1)
})
})
42 changes: 42 additions & 0 deletions api/src/services/aplu/aplu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { ArboPath } from 'types/graphql'
import { db } from 'src/lib/db'
import _ from 'lodash'
import S3Path from 'src/lib/files/S3Path'

export const arbo = async (): Promise<ArboPath> => {
const root = {
path: '/',
count: 0,
children: [],
}

const allImages = await db.image.findMany({
select: {
path: true,
},
})
root.count = allImages.length

_.forEach(allImages, ({ path }) => {
const basePath = S3Path.getBasePath(path)
const splitted = S3Path.splitPath(basePath)

let currArbo = root
if (splitted.length === 0) return

_.forEach(splitted, (subPath, i) => {
let subArbo = currArbo.children.find((a) => a.path === subPath)
if (!subArbo) {
subArbo = {
path: subPath,
count: 0,
children: [],
}
currArbo.children.push(subArbo)
}
currArbo = subArbo
currArbo.count++
})
})
return root
}

0 comments on commit 4efa48d

Please sign in to comment.