Skip to content

Commit

Permalink
Merge pull request #62 from qwertyyb/feature/statistics-date-range
Browse files Browse the repository at this point in the history
feat: 数据统计支持调整时间范围
  • Loading branch information
qwertyyb authored Jun 12, 2022
2 parents 1685dd5 + 62359a8 commit 261dbb5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
28 changes: 25 additions & 3 deletions Fire/Preferences/StatisticsPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import SwiftUI
import Preferences
import Defaults
import Combine

func formatCount(_ count: Int64) -> String {
return NumberFormatter.localizedString(from: NSNumber(value: count), number: .decimal)
Expand Down Expand Up @@ -49,9 +50,12 @@ struct CountCircle: View {
}

class DateCountData: ObservableObject {
@Published var startDate = Date().addingTimeInterval(-5 * 24 * 60 * 60)
@Published var endDate = Date()
@Published var data: [DateCount] = []
@Published var total: Int64 = 0

var cancellables = Set<AnyCancellable>()
private var observer: Any?

init() {
Expand All @@ -62,6 +66,14 @@ class DateCountData: ObservableObject {
name: Statistics.updated,
object: nil
)
$startDate.sink { date in
self.refreshData(startDate: date, endDate: nil)
}
.store(in: &cancellables)
$endDate.sink { date in
self.refreshData(startDate: nil, endDate: date)
}
.store(in: &cancellables)
}

deinit {
Expand All @@ -73,9 +85,13 @@ class DateCountData: ObservableObject {
}

@objc func refresh() {
NSLog("[DateCountData] refresh start")
data = Statistics.shared.queryCountByDate()
NSLog("[DateCountData] refresh start: \(startDate)")
total = Statistics.shared.queryTotalCount()
self.refreshData(startDate: nil, endDate: nil)
}

func refreshData(startDate: Date?, endDate: Date?) {
data = Statistics.shared.queryCountByDate(startDate: startDate ?? self.startDate, endDate: endDate ?? self.endDate)
}

func clear() {
Expand Down Expand Up @@ -197,7 +213,6 @@ struct StatisticsPane: View {
}
}
Spacer(minLength: 20)
Text("\(NumberFormatter.localizedString(from: NSNumber(value: dateCountData.data.count), number: .spellOut))天内输入情况统计")
}
}

Expand Down Expand Up @@ -234,6 +249,13 @@ struct StatisticsPane: View {
}

GroupBox(label: Text("输入统计")) {
HStack {
DatePicker("开始日期", selection: $dateCountData.startDate, displayedComponents: [.date])
.datePickerStyle(.field)
DatePicker("结束日期", selection: $dateCountData.endDate, displayedComponents: [.date])
.datePickerStyle(.field)
}
Spacer(minLength: 20)
if dateCountData.data.count <= 0 {
HStack {
Spacer()
Expand Down
12 changes: 9 additions & 3 deletions Fire/Utils/Statistics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,21 @@ class Statistics {
NotificationCenter.default.post(name: Statistics.updated, object: nil)
}

func queryCountByDate() -> [DateCount] {
func queryCountByDate(startDate: Date, endDate: Date) -> [DateCount] {
var queryStatement: OpaquePointer?
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let start = formatter.string(from: startDate)
let end = formatter.string(from: endDate)
let sql = """
select date, count from
(select
date(createdAt, 'localtime') as date,
sum(length(text)) as count
from data group by date(createdAt))
order by date desc limit 0, 5;
from data
where date(createdAt, 'localtime') >= "\(start)" and date(createdAt, 'localtime') <= "\(end)"
group by date(createdAt, 'localtime'))
order by date desc;
PRAGMA key = 'testkey'
"""
if sqlite3_prepare_v2(database, sql, -1, &queryStatement, nil) == SQLITE_OK {
Expand Down

0 comments on commit 261dbb5

Please sign in to comment.