Skip to content

Commit

Permalink
Filter by last draft timestamp in admin preset list
Browse files Browse the repository at this point in the history
  • Loading branch information
HSZemi committed Apr 15, 2023
1 parent a6f40a7 commit c6ebcc5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 58 deletions.
55 changes: 45 additions & 10 deletions create_draft_index.py → create_draft_and_preset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import json
from pathlib import Path
from typing import Dict

def main():
number_of_days = 1
Expand All @@ -13,10 +14,39 @@ def main():
pass

info = {'presets':[], 'drafts':{}, 'drafts_by_preset_id':{}, 'drafts_by_title':{}, 'timestamp': {'presets':'', 'drafts':''}}
output_json = Path(__file__).with_name('presets-and-drafts.json')
output_json = Path(__file__).with_name('xxx-presets-and-drafts.json')
if output_json.exists():
info = json.loads(output_json.read_text())

create_preset_index(number_of_days, info)
create_draft_index(number_of_days, info)

output_json.write_text(json.dumps(info, sort_keys=True))


def create_preset_index(number_of_days: int, info: Dict):
known_preset_ids = set(item['code'] for item in info['presets'])
presets_dir = Path(__file__).parent / 'presets'
now = datetime.now(tz=timezone.utc)
info['timestamp']['presets'] = str(now)
limit = now.timestamp() - (60*60*24*number_of_days)

for f in presets_dir.glob('*.json'):
mtime = f.stat().st_mtime
if mtime > limit:
try:
data = json.loads(f.read_text())
preset_id = f.stem
name = data['name']
if preset_id not in known_preset_ids:
info['presets'].append({'code':preset_id, 'name':name, 'created':mtime, 'last_draft': mtime})
known_preset_ids.add(preset_id)
except json.decoder.JSONDecodeError:
print(f'Not a valid json file: {f}')
info['presets'] = sorted(info['presets'], key=lambda x: (x['name'], x['last_draft']))


def create_draft_index(number_of_days: int, info: Dict):
known_draft_ids = set(info['drafts'].keys())
drafts_dir = Path(__file__).parent / 'data'
now = datetime.now(tz=timezone.utc)
Expand All @@ -31,34 +61,39 @@ def main():
draft_id = f.stem
if draft_id in known_draft_ids:
continue

data = json.loads(f.read_text())
preset_id = data['preset'].get('presetId', '<none>')
title = data['preset']['name']
host = data['nameHost']
guest = data['nameGuest']

info['drafts'][draft_id] = {'host':host, 'guest':guest, 'created':mtime}

if preset_id not in info['drafts_by_preset_id']:
info['drafts_by_preset_id'][preset_id] = []
info['drafts_by_preset_id'][preset_id].append(draft_id)

if title not in info['drafts_by_title']:
info['drafts_by_title'][title] = []
info['drafts_by_title'][title].append(draft_id)

known_draft_ids.add(draft_id)
except json.decoder.JSONDecodeError:
print(f'Not a valid json file: {f}')

for preset in info['presets']:
preset_id = preset['code']
if preset_id in info['drafts_by_preset_id']:
last_draft = max([info['drafts'][draft_id]['created'] for draft_id in info['drafts_by_preset_id'][preset_id]])

info['presets'] = sorted(info['presets'], key=lambda x: (x['name'], x['last_draft']))

for preset_id in info['drafts_by_preset_id']:
info['drafts_by_preset_id'][preset_id] = sorted(info['drafts_by_preset_id'][preset_id], reverse=True, key=lambda draft_id: info['drafts'][draft_id]['created'])

for title in info['drafts_by_title']:
info['drafts_by_title'][title] = sorted(info['drafts_by_title'][title], reverse=True, key=lambda draft_id: info['drafts'][draft_id]['created'])

output_json.write_text(json.dumps(info, sort_keys=True))


if __name__ == '__main__':
main()
42 changes: 0 additions & 42 deletions create_preset_index.py

This file was deleted.

14 changes: 8 additions & 6 deletions src/components/admin/PresetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PresetList extends React.Component<Props, State> {

constructor(props: Props) {
super(props);
this.state = {shortList: true, nameFilter: "", daysPast: 90};
this.state = {shortList: true, nameFilter: "", daysPast: 10};
}

private toggleFullList() {
Expand All @@ -35,16 +35,16 @@ class PresetList extends React.Component<Props, State> {
const displayLimitTimestamp = (Date.now() / 1000) - (this.state.daysPast * 24 * 60 * 60);
const presetRows = this.props.presetsAndDrafts.presets
.sort((a, b) => {
if (a.created < b.created) {
if (a.last_draft < b.last_draft) {
return 1;
}
if (a.created > b.created) {
if (a.last_draft > b.last_draft) {
return -1;
}
return 0;
})
.filter(value => this.state.shortList ? (this.props.presetsAndDrafts?.drafts_by_preset_id[value.code]?.length) : true)
.filter(value => this.state.shortList ? (value.created > displayLimitTimestamp) : true)
.filter(value => this.state.shortList ? (value.last_draft > displayLimitTimestamp) : true)
.filter(value => value.name.includes(this.state.nameFilter))
.map(preset => {
let listDraftsTitle = <span className="has-text-grey">{preset.name}</span>;
Expand Down Expand Up @@ -75,6 +75,7 @@ class PresetList extends React.Component<Props, State> {
</Link>
</td>
<td>{Util.formatTimestamp(preset.created)}</td>
<td>{Util.formatTimestamp(preset.last_draft)}</td>
</tr>)
});
return (
Expand All @@ -86,14 +87,14 @@ class PresetList extends React.Component<Props, State> {
this.toggleFullList()
}}/>
<label htmlFor="toggleFullList">
Show only presets created in the past <input type="text" value={this.state.daysPast}
Show only presets with drafts in the past <input type="text" value={this.state.daysPast}
style={{width: "2em"}}
onChange={(event) => {
const newValue = parseInt(event.target.value);
if (newValue) {
this.setState({daysPast: newValue});
}
}}/> days with drafts
}}/> days
</label>
</div>
<div className="field has-addons mt-2 mb-0">
Expand All @@ -116,6 +117,7 @@ class PresetList extends React.Component<Props, State> {
<th>Code</th>
<th>Name</th>
<th>Created</th>
<th>Last Draft</th>
</tr>
</thead>
<tbody>
Expand Down
1 change: 1 addition & 0 deletions src/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export interface IPresetMetaData {
code: string
name: string
created: number
last_draft: number
}

export interface IDraftMetaData {
Expand Down

0 comments on commit c6ebcc5

Please sign in to comment.