Skip to content

Commit

Permalink
added: added fetch range level 2 7day updating feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rishithaminol committed Mar 16, 2018
1 parent bcbf8ee commit b3df779
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
15 changes: 11 additions & 4 deletions cmarketcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ char *col_names[] = {
"hr_2", "hr_3", "hr_4", "hr_5", "hr_6", "hr_7", "hr_8", "hr_9",
"hr_10", "hr_11", "hr_12", "hr_13", "hr_14", "hr_15", "hr_16",
"hr_17", "hr_18", "hr_19", "hr_20", "hr_21", "hr_22", "hr_23",
"hr_24", "day_1", "day_2", "day_3", "day_4", "day_5", "day_6", "day_7"
"hr_24", "day_1", "day_2", "day_3", "day_4", "day_5", "day_6", "day_7" //43rd column
};

char *prog_name = NULL; /**< Program name */
Expand All @@ -49,15 +49,22 @@ void *__cb_update_database(void *db_)
time_diff = time(NULL); /* === clock starts === */

LOCK_SHIFT_COLUMN_LOCKER;

/* detects 24 hour completed and shifts hour table */
if (hour_col == 24) {/* 36th column filled */
int j;
for (j = 42; j > 35; j--)
shift_columns(db, col_names[j], col_names[j - 1]);

hour_col--;
}

if (min_hour_trig == 12) { /* executes after filling 12 columns. */
int j;
for (j = min_hour_trig + hour_col; j > 11; j--)
shift_columns(db, col_names[j], col_names[j - 1]);

hour_col++; /**! filled an 1 hour column */

if (hour_col == 24) /* 36th column filled */
hour_col--;
}

if (min_hour_trig > 0) { /* Executes from the second step of the loop */
Expand Down
7 changes: 7 additions & 0 deletions httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ void send_json_response(int sockfd, struct myhttp_header *header, sqlite3 *db)
UNLOCK_SHIFT_COLUMN_LOCKER;
free_uri_base(tokens);

return;
} else if (strcmp(range, "2") == 0) {
LOCK_SHIFT_COLUMN_LOCKER;
fetch_range_level2(coin_id, db, sockfd);
UNLOCK_SHIFT_COLUMN_LOCKER;
free_uri_base(tokens);

return;
}
} else {
Expand Down
53 changes: 51 additions & 2 deletions sql_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ struct coin_status_base *fetch_duration(sqlite3 *db, const char *col1,

sql = sqlite3_mprintf("SELECT %s, %s, coin_key "
"FROM coin_history "
"WHERE %s < %s "
"ORDER BY %s;", col1, col2, col1, col2, col1);
"WHERE %s < %s AND %s > 0 "
"ORDER BY %s;", col1, col2, col1, col2, col1, col1);

if (sqlite3_prepare_v2(db, sql, -1, &stmt_1, 0) != SQLITE_OK)
CM_ERROR("%s\n", sqlite3_errmsg(db));
Expand Down Expand Up @@ -213,6 +213,55 @@ void fetch_range_level1(const char *coin_id, sqlite3 *db, int sockfd)
sqlite3_free(sql);
}

/* min_0, hr_24, day_2, day_7 */
void fetch_range_level2(const char *coin_id, sqlite3 *db, int sockfd)
{
char *sql;
sqlite3_stmt *stmt_1;
int rc;
char tempstr[255];

LOCK_DB_ACCESS;

sql = sqlite3_mprintf("SELECT min_0, hr_24, day_1, day_2, day_3, day_4, day_5, day_6, day_7 "
"FROM coin_history "
"WHERE coin_key = '%s';", coin_id);

if (sqlite3_prepare_v2(db, sql, -1, &stmt_1, 0) != SQLITE_OK)
CM_ERROR("%s\n", sqlite3_errmsg(db));

rc = sqlite3_step(stmt_1);
if(rc == SQLITE_ROW) { /* new row of data ready */
sprintf(tempstr, "{ "
"\t\"min_0\": %d, "
"\t\"hr_24\": %d, "
"\t\"day_1\": %d, "
"\t\"day_2\": %d, "
"\t\"day_3\": %d, "
"\t\"day_4\": %d, "
"\t\"day_5\": %d, "
"\t\"day_6\": %d, "
"\t\"day_7\": %d "
"}",
sqlite3_column_int(stmt_1, 0),
sqlite3_column_int(stmt_1, 1),
sqlite3_column_int(stmt_1, 2),
sqlite3_column_int(stmt_1, 3),
sqlite3_column_int(stmt_1, 4),
sqlite3_column_int(stmt_1, 5),
sqlite3_column_int(stmt_1, 6),
sqlite3_column_int(stmt_1, 7),
sqlite3_column_int(stmt_1, 8)
);
write(sockfd, tempstr, strlen(tempstr));
}

UNLOCK_DB_ACCESS;

sqlite3_finalize(stmt_1);
sqlite3_free(sql);
}

/**
* @brief Return two columns of data as a difference of two times.
*
Expand Down
1 change: 1 addition & 0 deletions sql_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ extern struct coin_status_base *fetch_duration(sqlite3 *db, const char *col1,
extern void print_coin_status_base(struct coin_status_base *sb);
extern void free_coin_status_base(struct coin_status_base *sb);
extern void fetch_range_level1(const char *coin_id, sqlite3 *db, int sockfd);
extern void fetch_range_level2(const char *coin_id, sqlite3 *db, int sockfd);

#endif

0 comments on commit b3df779

Please sign in to comment.