- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.5k
 
Ublog recommender to increase old blog visibility #17315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            22 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      12cb12f
              
                blog-recommender
              
              
                schlawg ea30bd2
              
                Merge pull request #17303 from schlawg/ublog-recommender
              
              
                schlawg 800be1d
              
                Merge remote-tracking branch 'upstream/master' into ublog-recommender
              
              
                schlawg a2e5f0b
              
                blog recommendation carousel
              
              
                schlawg 9f46750
              
                Merge remote-tracking branch 'upstream/ublog-recommender' into ublog-…
              
              
                schlawg ee76c8d
              
                fix lint
              
              
                schlawg 6f559f4
              
                Merge branch 'master' into ublog-recommender
              
              
                ornicar 988e488
              
                full and incremental computations of similar ublog posts
              
              
                ornicar eac442e
              
                document mongodb scripts
              
              
                ornicar c08b32a
              
                Merge branch 'master' into ublog-recommender
              
              
                ornicar 6fef73a
              
                pnpm format
              
              
                ornicar 74f6223
              
                rename scripts
              
              
                ornicar f91e95b
              
                remove references to ublog recommender external service - MIGRATION
              
              
                ornicar eba6a4b
              
                compute 6 similar posts
              
              
                ornicar 6bd10a6
              
                no need to fetch 4 posts here, also prevent recommendation duplicates
              
              
                ornicar 4ae40ae
              
                move code out of app/ controller
              
              
                ornicar 596068d
              
                fix other posts card grid UI
              
              
                ornicar f47edd1
              
                remove superfluous imports and dependencies
              
              
                ornicar 182ac3a
              
                remove unused translation key
              
              
                ornicar ef211e0
              
                remove unused css dep
              
              
                ornicar 76ce16a
              
                remove broken ublog page carousel
              
              
                ornicar 828fb0d
              
                better similar blog previews
              
              
                ornicar File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 hidden or 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,35 @@ | ||
| /* O(n) for ublog_post documents | ||
| * for 48k documents in the DB, it takes about 2 minutes to run | ||
| * and uses up to 600MB of memory. | ||
| * | ||
| * Should run only once, then be replaced with | ||
| * ublog-graph-incremental.js | ||
| */ | ||
| const nbSimilar = 6; | ||
| console.log('Full recompute'); | ||
| const all = db.ublog_post.find({ live: true, 'likers.1': { $exists: true } }, { likers: 1 }).toArray(); | ||
| console.log(`${all.length} posts to go.`); | ||
| 
     | 
||
| console.log(`Computing likers...`); | ||
| const likers = new Map(); | ||
| all.forEach(p => { | ||
| p.likers.forEach(l => { | ||
| if (!likers.has(l)) likers.set(l, []); | ||
| likers.get(l).push(p._id); | ||
| }); | ||
| }); | ||
| console.log(likers.size + ` likers found.`); | ||
| 
     | 
||
| console.log(`Updating posts...`); | ||
| all.forEach(p => { | ||
| const similar = new Map(); | ||
| p.likers.forEach(liker => { | ||
| (likers.get(liker) || []).forEach(id => { | ||
| if (id != p._id) similar.set(id, (similar.get(id) || 0) + 1); | ||
| }); | ||
| }); | ||
| const top3 = Array.from(similar) | ||
| .sort((a, b) => b[1] - a[1]) | ||
| .slice(0, nbSimilar); | ||
| db.ublog_post.updateOne({ _id: p._id }, { $set: { similar: top3.map(([id, _]) => id) } }); | ||
| }); | 
  
    
      This file contains hidden or 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,37 @@ | ||
| /* O(1) assuming constant number of recently updated posts | ||
| * At the time of writing, with 48k ublog_post in the DB, | ||
| * it takes about 4 seconds to run and uses up to 300MB of memory. | ||
| * | ||
| * Should run periodically, e.g. every 1 hour. | ||
| */ | ||
| const nbSimilar = 6; | ||
| const since = new Date(Date.now() - 1000 * 60 * 60 * 24 * 15); | ||
| const updatable = db.ublog_post.find({ live: true, 'updated.at': { $gt: since } }, { likers: 1 }).toArray(); | ||
| console.log(`${updatable.length} posts were updated since ${since}`); | ||
| 
     | 
||
| const updatableLikers = new Set(); | ||
| updatable.forEach(p => p.likers.forEach(l => updatableLikers.add(l))); | ||
| console.log(`They have ${updatableLikers.size} likers.`); | ||
| 
     | 
||
| console.log(`Computing liker->ids...`); | ||
| const likerToIds = new Map(); | ||
| db.ublog_post.find({ live: true, likers: { $in: Array.from(updatableLikers) } }, { likers: 1 }).forEach(p => { | ||
| updatableLikers.intersection(new Set(p.likers)).forEach(l => { | ||
| if (!likerToIds.has(l)) likerToIds.set(l, []); | ||
| likerToIds.get(l).push(p._id); | ||
| }); | ||
| }); | ||
| console.log(`Updating ${updatable.length} posts...`); | ||
| 
     | 
||
| updatable.forEach(p => { | ||
| const similar = new Map(); | ||
| p.likers.forEach(liker => { | ||
| (likerToIds.get(liker) || []).forEach(id => { | ||
| if (id != p._id) similar.set(id, (similar.get(id) || 0) + 1); | ||
| }); | ||
| }); | ||
| const top3 = Array.from(similar) | ||
| .sort((a, b) => b[1] - a[1]) | ||
| .slice(0, nbSimilar); | ||
| db.ublog_post.updateOne({ _id: p._id }, { $set: { similar: top3.map(([id, _]) => id) } }); | ||
| }); | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | 
|---|---|---|
| 
          
            
          
           | 
    @@ -158,6 +158,9 @@ | |
| display: block; | ||
| } | ||
| } | ||
| .ublog-post-card--link:hover { | ||
| box-shadow: none; | ||
| } | ||
| } | ||
| 
     | 
||
| .ublog-post__mod-tools { | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or 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 hidden or 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,67 @@ | ||
| import { frag } from './common'; | ||
| 
     | 
||
| // it is an abomination to have more than one of these per page, so it's not supported | ||
| 
     | 
||
| type CarouselOpts = { | ||
| selector: string; | ||
| itemWidth: number; // this is a suggestion | ||
| pauseFor: Seconds; | ||
| slideFor?: Seconds; | ||
| }; | ||
| 
     | 
||
| export function makeCarousel({ selector, itemWidth, pauseFor, slideFor = 0.6 }: CarouselOpts): void { | ||
| let timer: number | undefined = undefined; | ||
| 
     | 
||
| requestIdleCallback(() => { | ||
| const el = document.querySelector<HTMLElement>(selector)!; | ||
| if (!el) return; | ||
| 
     | 
||
| const track = frag<HTMLElement>('<div class="track"></div>'); | ||
| track.append(...el.children); | ||
| el.innerHTML = ''; | ||
| el.append(track); | ||
| el.style.visibility = 'visible'; | ||
| 
     | 
||
| layoutChanged(); | ||
| window.addEventListener('resize', layoutChanged); | ||
| 
     | 
||
| function layoutChanged() { | ||
| const kids = [...track.children].filter((k): k is HTMLElement => k instanceof HTMLElement); | ||
| const styleGap = toPx('gap', el); | ||
| const gap = Number.isNaN(styleGap) ? 0 : styleGap; | ||
| const visible = Math.floor((el.clientWidth + gap) / (itemWidth + gap)); | ||
| const itemW = Math.floor((el.clientWidth - gap * (visible - 1)) / visible); | ||
| 
     | 
||
| kids.forEach(k => (k.style.width = `${itemW}px`)); | ||
| kids.forEach(k => (k.style.marginRight = `${gap}px`)); | ||
| 
     | 
||
| const rotateInner = () => { | ||
| kids.forEach(k => (k.style.transition = `transform ${slideFor}s ease`)); | ||
| kids.forEach(k => (k.style.transform = `translateX(-${itemW + gap}px)`)); | ||
| setTimeout(() => { | ||
| track.append(track.firstChild!); | ||
| fix(); | ||
| }, slideFor * 1000); | ||
| }; | ||
| 
     | 
||
| const fix = () => { | ||
| kids.forEach(k => (k.style.transition = '')); | ||
| kids.forEach(k => (k.style.transform = '')); | ||
| }; | ||
| requestAnimationFrame(fix); | ||
| clearInterval(timer); | ||
| if (kids.length <= visible) return; | ||
| timer = setInterval(rotateInner, pauseFor * 1000); | ||
| } | ||
| }); | ||
| } | ||
| 
     | 
||
| function toPx(key: keyof CSSStyleDeclaration, contextEl: HTMLElement = document.body): number { | ||
| // must be simple units like vw, em, and %. things like 'auto' will return NaN | ||
| const style = window.getComputedStyle(contextEl); | ||
| const el = frag<HTMLElement>(`<div style="position:absolute;visibility:hidden;width:${style[key]}"/>`); | ||
| contextEl.append(el); | ||
| const pixels = parseFloat(window.getComputedStyle(el).width); | ||
| el.remove(); | ||
| return pixels; | ||
| } | 
This file was deleted.
      
      Oops, something went wrong.
      
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔